Iphone 将 IplImage 转换为 UIImage 图像变蓝
Posted
技术标签:
【中文标题】Iphone 将 IplImage 转换为 UIImage 图像变蓝【英文标题】:Iphone Converting IplImage to UIImage images become bluish 【发布时间】:2013-06-08 05:00:59 【问题描述】:在人脸检测中,我首先使用代码将 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;
但是当它显示在图像视图中时,它显示为蓝色。如果我使用色彩空间
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
然后整个图像变灰。两种方式都完全失去了原来的颜色。
提前致谢。
【问题讨论】:
【参考方案1】:更改 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;
【讨论】:
以上是关于Iphone 将 IplImage 转换为 UIImage 图像变蓝的主要内容,如果未能解决你的问题,请参考以下文章
在 OpenCV 中不使用 CvSaveImage 将 IplImage 转换为 JPEG
将 c++ opencv IplImage imageData 和 widthStep 转换为 android opencv Mat