我是 OpenCV 的新手。我在图像转换方面遇到了一些问题。我创建了一个新项目。它还有 2 个文件 Wrapper.h/mm 和 UIImage+OpenCV.h/.mm。以下代码在这里:
//UIImage+OpenCV.h
#import <UIKit/UIKit.h>
@interface UIImage (OpenCV)
//cv::Mat to UIImage
+ (UIImage *)imageWithCVMat:(const cv::Mat&)cvMat;
+ (UIImage *)imageWithCVMat:(const cv::Mat&)cvMat
orientation:(UIImageOrientation)orientation;
- (id)initWithCVMat:(const cv::Mat&)cvMat
orientation:(UIImageOrientation)orientation;
//UIImage to cv::Mat
- (cv::Mat)CVMat;
- (cv::Mat)CVMat3; // no alpha channel
- (cv::Mat)CVGrayscaleMat;
@end
//UIImage+OpenCV.mm
#import "UIImage+OpenCV.h"
@implementation UIImage (OpenCV)
-(cv::Mat)CVMat
{
CGColorSpaceRef colorSpace = CGImageGetColorSpace(self.CGImage);
CGFloat cols = self.size.width;
CGFloat rows = self.size.height;
cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 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), self.CGImage);
CGContextRelease(contextRef);
return cvMat;
}
- (cv::Mat)CVMat3
{
cv::Mat result = [self CVMat];
cv::cvtColor(result , result , CV_RGBA2RGB);
return result;
}
-(cv::Mat)CVGrayscaleMat
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGFloat cols = self.size.width;
CGFloat rows = self.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
kCGImageAlphaNone |
kCGBitmapByteOrderDefault); // Bitmap info flags
CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), self.CGImage);
CGContextRelease(contextRef);
CGColorSpaceRelease(colorSpace);
return cvMat;
}
+ (UIImage *)imageWithCVMat:(const cv::Mat&)cvMat
{
return [[UIImage alloc] initWithCVMat:cvMat];
}
- (id)initWithCVMat:(const 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
self = [self initWithCGImage:imageRef];
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpace);
return self;
}
@end
上面的代码我在这里。
//Wrapper.h
#import <Foundation/Foundation.h>
@interface CVWrapper : NSObject
+(UIImage*) returnPic: (UIImage*)image;
@end
//Wrapper.mm
#import "CVWrapper.h"
#import "UIImage+OpenCV.h"
@implementation CVWrapper
+(UIImage*)returnPic:(UIImage *)image
{
UIImage *result = nil;
if (image)
{
cv::Mat matImage = [image CVMat];
cv::Mat greyMat; //It doesn't' work
cv::cvtColor(matImage, greyMat, 7); //It doesn't work. In enum of imgproc.hpp COLOR_RGB2GRAY=7
result = [UIImage imageWithCVMat:greyMat];
}
return result;
}
@end
在 ViewController 中,我调用显示图像的函数。我试图理解一个基本概念。如何将图片转换为灰色?我必须在哪里做?