I'm a new in OpenCV. I have some issues with image converting. I create a new project. It has 2 more files Wrapper.h/mm and UIImage+OpenCV.h/.mm. The following code is here:
//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
Above code I took here
//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;
cv::cvtColor(matImage, greyMat, CV_RGBA2GRAY); //It doesn't work
result = [UIImage imageWithCVMat:greyMat];
}
return result;
}
@end
In ViewController I call the function that displays an image. I try to understand a basic concepts. How can i convert a picture in a grey color? Where i have to do it?