UIImage与Iplimage相互转换

转载 2012年03月23日 14:19:52

UIImage与Iplimage相互转换

作者:知识天地  来源:博客园  发布时间:2012-02-27 15:43  阅读:48 次  原文链接   [收藏]  

原创文章,转载请注明出处:http://www.cnblogs.com/mfryf/archive/2012/02/27/2369952.html 

Opencv中所有的图像处理都是Iplimage类型的格式,iso的sdk显示一张图像的对象类型是UIImage。因此,我们如果想在ios环境中使用opencv,

就必须把UIImage转换成Iplimage,处理之后再把Iplimage转换成UIImage。非常幸运的是,IOS的sdk为我们提供了转换API。

从UIImage创建一个Iplimage结构,代码如下:

// NOTE you SHOULD cvReleaseImage() for the return value when end of the code.
- (IplImage *)CreateIplImageFromUIImage:(UIImage *)image {
// Getting CGImage from UIImage
CGImageRef imageRef = image.CGImage;

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// Creating temporal IplImage for drawing
IplImage *iplimage = cvCreateImage(
cvSize(image.size.width,image.size.height), IPL_DEPTH_8U, 4
);
// Creating CGContext for temporal IplImage
CGContextRef contextRef = CGBitmapContextCreate(
iplimage->imageData, iplimage->width, iplimage->height,
iplimage->depth, iplimage->widthStep,
colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault
);
// Drawing CGImage to CGContext
CGContextDrawImage(
contextRef,
CGRectMake(0, 0, image.size.width, image.size.height),
imageRef
);
CGContextRelease(contextRef);
CGColorSpaceRelease(colorSpace);

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

return ret;
}

使用玩iplimage之后,别忘记释放内存。cvreleaseImage().

从iplimage创建一张UIImage

代码如下:

// NOTE You should convert color mode as RGB before passing to this function
- (UIImage *)UIImageFromIplImage:(IplImage *)image {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// 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;
}

尽情的在ios中使用opencv吧~

相关文章推荐

ios–OpenCV–IplImage与UIImage类型相互转换

/// UIImage类型转换为IPlImage类型 -(IplImage*)convertToIplImage:(UIImage*)image { CGImageRef imageRef =...

opencv UIimage,cvmat 和IplImage的相互转换

1.opencv UIimage转换cvmat+ (cv::Mat)cvMatFromUIIMage:(UIImage *)image { @synchronized (image) { ...

UIImage和IplImage之间的相互转换

Converting images between UIImage and IplImage This content is copied from Yoshimasa's ...
  • Koupoo
  • Koupoo
  • 2012年05月28日 16:04
  • 682

NSdata与NSString,Byte数组,UIImage的相互转换

原址:http://w11h22j33.iteye.com/blog/1333521 1. NSData 与 NSString NSData-> NSString NSStrin...

【OpenCV】IplImage与Mat的相互转换

1、IplImage --> Mat 转换函数: //! converts old-style IplImage to the new matrix; the data is not copied b...

IplImage, CvMat, Mat 的关系和相互转换

opencv中常见的与图像操作有关的数据容器有Mat,cvMat和IplImage,这三种类型都可以代表和显示图像,但是,Mat类型侧重于计算,数学性较高,openCV对Mat类型的计算也进行了优化。...

IplImage与位图的相互转换,Windows系统中一行像素所占的字节数为4的倍数

1、位图----->IplImage   BYTE *pt;  pt=classify->m_pData;  int m_Width = classify->GetWidth();  int m_He...

Mat与IplImage相互转换

原文地址:http://blog.csdn.net/lichengyu/article/details/24272349 Mat 类有一个IplImage()运算符成员函数: [cp...

DIB与IplImage图像格式的相互转换

DIB转换为IplImage:   Void DIB2IplImage(CDIB m_dib) { //////////////////////////////////////////////...

【转】IplImage, CvMat, Mat 的关系和相互转换

【转】IplImage, CvMat, Mat 的关系和相互转换   (2012-05-24 09:43:13) 转载▼ 标签:  it 分类: 智能算...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:UIImage与Iplimage相互转换
举报原因:
原因补充:

(最多只允许输入30个字)