OpenCV Mat与UIImage之间的转换

2019-05-25 15:57:58 浏览数 (1)

  UIImage 转 OpenCV cvMat:

代码语言:javascript复制
 1 - (cv::Mat)cvMatWithImage:(UIImage *)image
 2 {
 3     CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
 4     CGFloat cols = image.size.width;
 5     CGFloat rows = image.size.height;
 6     
 7     cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels
 8     
 9     CGContextRef contextRef = CGBitmapContextCreate(cvMat.data,                 // Pointer to backing data
10                                                     cols,                       // Width of bitmap
11                                                     rows,                       // Height of bitmap
12                                                     8,                          // Bits per component
13                                                     cvMat.step[0],              // Bytes per row
14                                                     colorSpace,                 // Colorspace
15                                                     kCGImageAlphaNoneSkipLast |
16                                                     kCGBitmapByteOrderDefault); // Bitmap info flags
17     
18     CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
19     CGContextRelease(contextRef);
20     
21     return cvMat;
22 }

  OpenCV cvMat 转 UIImage : 

代码语言:javascript复制
24 - (UIImage *)UIImageFromCVMat:(cv::Mat)cvMat {
25     NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()];
26     
27     CGColorSpaceRef colorSpace;
28     CGBitmapInfo bitmapInfo;
29     
30     if (cvMat.elemSize() == 1) {
31         colorSpace = CGColorSpaceCreateDeviceGray();
32         bitmapInfo = kCGImageAlphaNone | kCGBitmapByteOrderDefault;
33     } else {
34         colorSpace = CGColorSpaceCreateDeviceRGB();
35         bitmapInfo = kCGBitmapByteOrder32Little | (
36                                                    cvMat.elemSize() == 3? kCGImageAlphaNone : kCGImageAlphaNoneSkipFirst
37                                                    );
38     }
39     
40     CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
41     
42     // Creating CGImage from cv::Mat
43     CGImageRef imageRef = CGImageCreate(
44                                         cvMat.cols,                 //width
45                                         cvMat.rows,                 //height
46                                         8,                          //bits per component
47                                         8 * cvMat.elemSize(),       //bits per pixel
48                                         cvMat.step[0],              //bytesPerRow
49                                         colorSpace,                 //colorspace
50                                         bitmapInfo,                 // bitmap info
51                                         provider,                   //CGDataProviderRef
52                                         NULL,                       //decode
53                                         false,                      //should interpolate
54                                         kCGRenderingIntentDefault   //intent
55                                         );
56     
57     // Getting UIImage from CGImage
58     UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
59     CGImageRelease(imageRef);
60     CGDataProviderRelease(provider);
61     CGColorSpaceRelease(colorSpace);
62     
63     return finalImage;
64 }

0 人点赞