Iphone将IplImage转换为UIImage图像变得偏蓝

时间:2013-06-12 07:00:30

标签: iphone face-detection

在人脸检测中,我首先使用代码

将UIImage转换为Iplimage
- (IplImage *)CreateIplImageFromUIImage:(UIImage *)image {
CGImageRef      imageRef;
CGColorSpaceRef colorSpaceRef;
CGContextRef    context;
IplImage      * iplImage;
IplImage      * returnImage;

imageRef      = image.CGImage;
colorSpaceRef = CGColorSpaceCreateDeviceRGB();
iplImage      = cvCreateImage( cvSize( image.size.width, image.size.height ), IPL_DEPTH_8U, 4 );
context       = CGBitmapContextCreate
(
 iplImage->imageData,
 iplImage->width,
 iplImage->height,
 iplImage->depth,
 iplImage->widthStep,
 colorSpaceRef,
 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault
 );

CGContextDrawImage( context, CGRectMake( 0, 0, image.size.width, image.size.height ), imageRef );
CGContextRelease( context );
CGColorSpaceRelease( colorSpaceRef );

returnImage = cvCreateImage( cvGetSize( iplImage ), IPL_DEPTH_8U, 3 );

cvCvtColor( iplImage, returnImage, CV_RGBA2BGR);
cvReleaseImage( &iplImage );

return returnImage;

}

然后在检测到面部特征后,使用代码将iplimage转换为uiimage:

- (UIImage *)UIImageFromIplImage:(IplImage *)image {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();//CGColorSpaceCreateDeviceGray();
// Allocating the buffer for CGImage
NSData *data = [NSData dataWithBytes:image->imageData length:image->imageSize];
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
// Creating CGImage from chunk of IplImage
CGImageRef imageRef = CGImageCreate(image->width, image->height, image->depth, image->depth * image->nChannels, image->widthStep,
                                    colorSpace, kCGImageAlphaNone|kCGBitmapByteOrderDefault, provider, NULL, false, kCGRenderingIntentDefault);
// Getting UIImage from CGImage
UIImage *ret = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpace);
return ret;

}

但是当它在imageview中显示时显示为蓝色。如果我使用色彩空间

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

然后整个图像变灰。原始颜色在两个方面都完全失去了。

提前致谢。

1 个答案:

答案 0 :(得分:7)

在更改功能UIImageFromIplImage时,蓝色或灰色问题得以解决

- (UIImage *)UIImageFromIplImage:(IplImage *)image {
CGColorSpaceRef colorSpace;
if (image->nChannels == 1)
{
    colorSpace = CGColorSpaceCreateDeviceGray();
} else {
    colorSpace = CGColorSpaceCreateDeviceRGB();
    cvCvtColor(image, image, CV_BGR2RGB);
}
NSData *data = [NSData dataWithBytes:image->imageData length:image->imageSize];
CGDataProviderRef provider = CGDataProviderCreateWithCFData(( CFDataRef)data);
CGImageRef imageRef = CGImageCreate(image->width,
                                    image->height,
                                    image->depth,
                                    image->depth * image->nChannels,
                                    image->widthStep,
                                    colorSpace,
                                    kCGImageAlphaNone|kCGBitmapByteOrderDefault,
                                    provider,
                                    NULL,
                                    false,
                                    kCGRenderingIntentDefault
                                    );
UIImage *ret = [UIImage imageWithCGImage:imageRef];

CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpace);
return ret;
}