1 OpenCV iOS - Image Processing {#tutorial_image_manipulation}
2 =============================
4 @prev_tutorial{tutorial_hello}
5 @next_tutorial{tutorial_video_processing}
10 In this tutorial we will learn how to do basic image processing using OpenCV in iOS.
15 In *OpenCV* all the image processing operations are usually carried out on the *Mat* structure. In
16 iOS however, to render an image on screen it have to be an instance of the *UIImage* class. To
17 convert an *OpenCV Mat* to an *UIImage* we use the *Core Graphics* framework available in iOS. Below
18 is the code needed to covert back and forth between Mat's and UIImage's.
20 - (cv::Mat)cvMatFromUIImage:(UIImage *)image
22 CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
23 CGFloat cols = image.size.width;
24 CGFloat rows = image.size.height;
26 cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels (color channels + alpha)
28 CGContextRef contextRef = CGBitmapContextCreate(cvMat.data, // Pointer to data
29 cols, // Width of bitmap
30 rows, // Height of bitmap
31 8, // Bits per component
32 cvMat.step[0], // Bytes per row
33 colorSpace, // Colorspace
34 kCGImageAlphaNoneSkipLast |
35 kCGBitmapByteOrderDefault); // Bitmap info flags
37 CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
38 CGContextRelease(contextRef);
44 - (cv::Mat)cvMatGrayFromUIImage:(UIImage *)image
46 CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
47 CGFloat cols = image.size.width;
48 CGFloat rows = image.size.height;
50 cv::Mat cvMat(rows, cols, CV_8UC1); // 8 bits per component, 1 channels
52 CGContextRef contextRef = CGBitmapContextCreate(cvMat.data, // Pointer to data
53 cols, // Width of bitmap
54 rows, // Height of bitmap
55 8, // Bits per component
56 cvMat.step[0], // Bytes per row
57 colorSpace, // Colorspace
58 kCGImageAlphaNoneSkipLast |
59 kCGBitmapByteOrderDefault); // Bitmap info flags
61 CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
62 CGContextRelease(contextRef);
67 After the processing we need to convert it back to UIImage. The code below can handle both
68 gray-scale and color image conversions (determined by the number of channels in the *if* statement).
71 cv::cvtColor(inputMat, greyMat, COLOR_BGR2GRAY);
73 After the processing we need to convert it back to UIImage.
75 -(UIImage *)UIImageFromCVMat:(cv::Mat)cvMat
77 NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()];
78 CGColorSpaceRef colorSpace;
80 if (cvMat.elemSize() == 1) {
81 colorSpace = CGColorSpaceCreateDeviceGray();
83 colorSpace = CGColorSpaceCreateDeviceRGB();
86 CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
88 // Creating CGImage from cv::Mat
89 CGImageRef imageRef = CGImageCreate(cvMat.cols, //width
91 8, //bits per component
92 8 * cvMat.elemSize(), //bits per pixel
93 cvMat.step[0], //bytesPerRow
94 colorSpace, //colorspace
95 kCGImageAlphaNone|kCGBitmapByteOrderDefault,// bitmap info
96 provider, //CGDataProviderRef
98 false, //should interpolate
99 kCGRenderingIntentDefault //intent
103 // Getting UIImage from CGImage
104 UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
105 CGImageRelease(imageRef);
106 CGDataProviderRelease(provider);
107 CGColorSpaceRelease(colorSpace);
116 ![](images/output.jpg)
118 Check out an instance of running code with more Image Effects on
119 [YouTube](http://www.youtube.com/watch?v=Ko3K_xdhJ1I) .
121 @youtube{Ko3K_xdhJ1I}