Add next and previous navigation links to all tutorials
[platform/upstream/opencv.git] / doc / tutorials / ios / image_manipulation / image_manipulation.markdown
1 OpenCV iOS - Image Processing {#tutorial_image_manipulation}
2 =============================
3
4 @prev_tutorial{tutorial_hello}
5 @next_tutorial{tutorial_video_processing}
6
7 Goal
8 ----
9
10 In this tutorial we will learn how to do basic image processing using OpenCV in iOS.
11
12 Introduction
13 ------------
14
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.
19 @code{.m}
20 - (cv::Mat)cvMatFromUIImage:(UIImage *)image
21 {
22   CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
23   CGFloat cols = image.size.width;
24   CGFloat rows = image.size.height;
25
26   cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels (color channels + alpha)
27
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
36
37   CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
38   CGContextRelease(contextRef);
39
40   return cvMat;
41 }
42 @endcode
43 @code{.m}
44 - (cv::Mat)cvMatGrayFromUIImage:(UIImage *)image
45 {
46   CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
47   CGFloat cols = image.size.width;
48   CGFloat rows = image.size.height;
49
50   cv::Mat cvMat(rows, cols, CV_8UC1); // 8 bits per component, 1 channels
51
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
60
61   CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
62   CGContextRelease(contextRef);
63
64   return cvMat;
65  }
66 @endcode
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).
69 @code{.m}
70 cv::Mat greyMat;
71 cv::cvtColor(inputMat, greyMat, COLOR_BGR2GRAY);
72 @endcode
73 After the processing we need to convert it back to UIImage.
74 @code{.m}
75 -(UIImage *)UIImageFromCVMat:(cv::Mat)cvMat
76 {
77   NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()];
78   CGColorSpaceRef colorSpace;
79
80   if (cvMat.elemSize() == 1) {
81       colorSpace = CGColorSpaceCreateDeviceGray();
82   } else {
83       colorSpace = CGColorSpaceCreateDeviceRGB();
84   }
85
86   CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
87
88   // Creating CGImage from cv::Mat
89   CGImageRef imageRef = CGImageCreate(cvMat.cols,                                 //width
90                                      cvMat.rows,                                 //height
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
97                                      NULL,                                       //decode
98                                      false,                                      //should interpolate
99                                      kCGRenderingIntentDefault                   //intent
100                                      );
101
102
103   // Getting UIImage from CGImage
104   UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
105   CGImageRelease(imageRef);
106   CGDataProviderRelease(provider);
107   CGColorSpaceRelease(colorSpace);
108
109   return finalImage;
110  }
111 @endcode
112
113 Output
114 --------
115
116 ![](images/output.jpg)
117
118 Check out an instance of running code with more Image Effects on
119 [YouTube](http://www.youtube.com/watch?v=Ko3K_xdhJ1I) .
120
121 @youtube{Ko3K_xdhJ1I}