OpenCV iOS - Image Processing
来源:互联网 发布:linux重启服务器命令 编辑:程序博客网 时间:2024/05/01 15:16
Goal
In this tutorial we will learn how to do basic image processing using OpenCV in iOS.
Introduction
In OpenCV all the image processing operations are usually carried out on theMat structure. In iOS however, to render an image on screen it have to be an instance of theUIImage class. To convert an OpenCV Mat to an UIImage we use theCore Graphics framework available in iOS. Below is the code needed to covert back and forth between Mat’s and UIImage’s.
- (cv::Mat)cvMatFromUIImage:(UIImage *)image{ CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage); CGFloat cols = image.size.width; CGFloat rows = image.size.height; cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels (color channels + alpha) CGContextRef contextRef = CGBitmapContextCreate(cvMat.data, // Pointer to data cols, // Width of bitmap rows, // Height of bitmap 8, // Bits per component cvMat.step[0], // Bytes per row colorSpace, // Colorspace kCGImageAlphaNoneSkipLast | kCGBitmapByteOrderDefault); // Bitmap info flags CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage); CGContextRelease(contextRef); return cvMat;}
- (cv::Mat)cvMatGrayFromUIImage:(UIImage *)image{ CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage); CGFloat cols = image.size.width; CGFloat rows = image.size.height; cv::Mat cvMat(rows, cols, CV_8UC1); // 8 bits per component, 1 channels CGContextRef contextRef = CGBitmapContextCreate(cvMat.data, // Pointer to data cols, // Width of bitmap rows, // Height of bitmap 8, // Bits per component cvMat.step[0], // Bytes per row colorSpace, // Colorspace kCGImageAlphaNoneSkipLast | kCGBitmapByteOrderDefault); // Bitmap info flags CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage); CGContextRelease(contextRef); return cvMat; }
After the processing we need to convert it back to UIImage. The code below can handle both gray-scale and color image conversions (determined by the number of channels in theif statement).
cv::Mat greyMat;cv::cvtColor(inputMat, greyMat, CV_BGR2GRAY);
After the processing we need to convert it back to UIImage.
-(UIImage *)UIImageFromCVMat:(cv::Mat)cvMat{ NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()]; CGColorSpaceRef colorSpace; if (cvMat.elemSize() == 1) { colorSpace = CGColorSpaceCreateDeviceGray(); } else { colorSpace = CGColorSpaceCreateDeviceRGB(); } CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data); // Creating CGImage from cv::Mat CGImageRef imageRef = CGImageCreate(cvMat.cols, //width cvMat.rows, //height 8, //bits per component 8 * cvMat.elemSize(), //bits per pixel cvMat.step[0], //bytesPerRow colorSpace, //colorspace kCGImageAlphaNone|kCGBitmapByteOrderDefault,// bitmap info provider, //CGDataProviderRef NULL, //decode false, //should interpolate kCGRenderingIntentDefault //intent ); // Getting UIImage from CGImage UIImage *finalImage = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); CGDataProviderRelease(provider); CGColorSpaceRelease(colorSpace); return finalImage; }
Output
Check out an instance of running code with more Image Effects on YouTube .
- OpenCV iOS - Image Processing
- OpenCV iOS - Image Processing¶
- OpenCV iOS - Video Processing
- 三、openCV学习笔记(Image Processing)
- OpenCV iOS - Video Processing¶
- A Basic Introduction to OpenCV for Image Processing
- openCV学习笔记(三)--图像处理Image Processing
- OpenCV学习笔记8 OpenCV图像处理模块ImgProc Module. Image Processing(一)
- OpenCV学习笔记9 OpenCV图像处理模块ImgProc Module. Image Processing(二)
- OpenCV学习笔记10 OpenCV图像处理模块ImgProc Module. Image Processing(三)
- OpenCV学习笔记11 OpenCV图像处理模块ImgProc Module. Image Processing(四)
- OpenCV学习笔记12 OpenCV图像处理模块ImgProc Module. Image Processing(五)
- OpenCV学习笔记13 OpenCV图像处理模块ImgProc Module. Image Processing(六)
- OpenCV学习笔记14 OpenCV图像处理模块ImgProc Module. Image Processing(七)
- OpenCV学习笔记15 OpenCV图像处理模块ImgProc Module. Image Processing(八)
- OpenCV学习笔记16 OpenCV图像处理模块ImgProc Module. Image Processing(九)
- Image Processing Fundamentals
- Image Processing Tricks
- ARC使用心得(二)
- 黑马程序员-交通灯管理系统分析
- DOM对象和JQuery对象的区别
- ibatis中#与$的区别
- 处理类似这样的字符串(2.05,-90)(12.1,120)提取出数据
- OpenCV iOS - Image Processing
- 对象序列化ObjectOutputStream,ObjectInputStream
- 这个不错---C语言清空输入缓冲区的N种方法对比
- 我参加了51CTO博客大赛,求投票!
- Pearson correlation coefficient
- 如何判断ie浏览器的几个版本
- 学会指引访客方能让站点可持续发展
- iOS学习系列 - 扩展机制category与associative
- OpenCV iOS - Video Processing