在使用OpenCV的时候因为cv::Mat对像素点RGB操作比较麻烦,所以转而采用C语言的IplImage来操作像素点,就涉及一些转换问题,所以mark一下:
直接上代码
#pragma mark -- iplimage与uiimage互换
-(IplImage*)convertToIplImage:(UIImage*)image
{
CGImageRefimageRef = image.CGImage;
CGColorSpaceRefcolorSpace =CGColorSpaceCreateDeviceRGB();
IplImage*iplImage =cvCreateImage(cvSize(image.size.width, image.size.height),IPL_DEPTH_8U,4);
CGContextRefcontextRef =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_RGB2BGR);
cvReleaseImage(&iplImage);
returnret;
}
-(UIImage*)convertToUIImage:(IplImage*)image
{
cvCvtColor(image, image,CV_BGR2RGB);
CGColorSpaceRefcolorSpace =CGColorSpaceCreateDeviceRGB();
NSData*data = [NSDatadataWithBytes:image->imageDatalength:image->imageSize];
CGDataProviderRefprovider =CGDataProviderCreateWithCFData((CFDataRef)data);
CGImageRefimageRef =CGImageCreate(image->width, image->height, image->depth, image->depth* image->nChannels, image->widthStep, colorSpace,kCGImageAlphaNone|kCGBitmapByteOrderDefault, provider,NULL,false,kCGRenderingIntentDefault);
UIImage*ret = [UIImageimageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpace);
returnret;
}