开发者

Gray UIImage - Gray IPlimage Conversion

I wanna transform a grayscaled iplimage into uiimage and vicever开发者_C百科sa. I have two methods like below that are successful in RGB colorspace conversion, But no success for Gray images.

- (UIImage *)UIImageFromIplImage:(IplImage *)image {

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
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;

}

- (IplImage *)CreateIplImageFromUIImage:(UIImage *)image {

CGImageRef imageRef = image.CGImage;

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
IplImage *iplimage = cvCreateImage(cvSize(image.size.width, image.size.height), IPL_DEPTH_8U, 4);
CGContextRef contextRef = CGBitmapContextCreate(iplimage->imageData, iplimage->width, iplimage->height,
                                                iplimage->depth, iplimage->widthStep,
                                                colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault);
CGContextDrawImage(contextRef, CGRectMake(0, 0, image.size.width, image.size.height), imageRef);

CGContextRelease(contextRef);
CGColorSpaceRelease(colorSpace);

IplImage *ret = cvCreateImage(cvGetSize(iplimage), IPL_DEPTH_8U, 3);
cvCvtColor(iplimage, ret, CV_RGBA2BGR);
cvReleaseImage(&iplimage);

return ret;

}

I need help at this point to figure out the problem I have.

Thanks in advance..


What about convert the color channel before passing in the argument?

    IplImage *image = cvCreateImage(cvGetSize(imgGray), IPL_DEPTH_32F, 3);
    cvCvtColor(imgGray, image, CV_GRAY2BGR);
    imageView.image = [self UIImageFromIplImage:image];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜