是否存在像iOS的cvShowImage?

时间:2012-04-25 15:53:21

标签: iphone objective-c ios opencv cmsamplebufferref

我正在处理图片(在iOS上使用AVFoundation和OpenCV),我想简单地显示CMSampleBufferRef(或IplImage)的内容进行筛选。

简单:我只想显示(就像使用OpenCV的cvShowImage())未转换的图片,看看我是不是在处理损坏或以某种方式变形的图像。

1 个答案:

答案 0 :(得分:0)

可悲的是没有。不同的位图表示。

也许你想要一个类别?我使用了类似的东西:

 // NSImage+OpenCV.h
 @interface NSImage (OpenCV)
 + (NSImage*)imageWithCVMat:(const cv::Mat&)cvMat;
 - (id)initWithCVMat:(const cv::Mat&)cvMat;
 - (cv::Ptr<cv::Mat>)cvMat;
 @end

 // NSImage+OpenCV.m
 using namespace cv;
 @implementation NSImage (OpenCV)

 - (id)initWithCVMat:(const cv::Mat&)cvMat {
      NSData *data = [NSData dataWithBytes:cvMat.data
                                 length:cvMat.total()*cvMat.elemSize()];    
      CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
      CGColorSpaceRef colourSpace =  CGColorSpaceCreateDeviceRGB();

      CGImageRef imageRef = CGImageCreate(cvMat.cols,
                                         cvMat.rows,
                                         8,
                                         8 * cvMat.elemSize(),
                                         cvMat.step[0],
                                         colourSpace,                                           
                                         kCGImageAlphaNone |
                                         kCGBitmapByteOrderDefault,
                                         provider,
                                         NULL,
                                         false,
                                         kCGRenderingIntentDefault);       

      NSImage *image = [[NSImage alloc] initWithCGImage:imageRef size:CGSizeMake(cvMat.cols,cvMat.rows)];

      CGColorSpaceRelease(colourSpace);
      CGDataProviderRelease(provider);
      CGImageRelease(imageRef);

      return image;
 }

 +(NSImage*)imageWithCVMat:(const cv::Mat&)cvMat {
    return [[NSImage alloc] initWithCVMat:cvMat];
 }

 - (cv::Ptr<cv::Mat>)cvMat {
      CGImageSourceRef source =  CGImageSourceCreateWithData((__bridge CFDataRef)[self TIFFRepresentation],  NULL);     
      CGImageRef imageRef =  CGImageSourceCreateImageAtIndex(source, 0, NULL);
      cv::Ptr<cv::Mat> cvMat = new cv::Mat(self.size.height, self.size.width, CV_8UC4);          

      CGContextRef contextRef = CGBitmapContextCreate(cvMat->data,
                                                   cvMat->cols,
                                                   cvMat->rows,
                                                   8,
                                                   cvMat->step[0],                                                    
                                                   CGImageGetColorSpace(imageRef),
                                                   kCGImageAlphaNoneSkipLast |
                                                   kCGBitmapByteOrderDefault);   

      CGContextDrawImage(contextRef,
                        CGRectMake(0, 0, cvMat->cols, cvMat->rows),
                        imageRef);

      CGContextRelease(contextRef);
      CGImageRelease(imageRef);

      return cvMat;   
 }

 @end