IplImage 与 QImage 相互转换
在使用Qt和OpenCV编程时,对于它们各自的图像类QImage和IplImage难以避免的需要互相之间的转换,下面我们就来看它们的相互转换。
1. QImage 转换为 IplImage
IplImage *QImageToIplImage(const QImage * qImage)
{
int width = qImage->width();
int height = qImage->height();
CvSize Size;
Size.height = height;
Size.width = width; IplImage *charIplImageBuffer = cvCreateImage(Size, IPL_DEPTH_8U, );
char *charTemp = (char *) charIplImageBuffer->imageData; for (int y = ; y < height; y++)
{
for (int x = ; x < width; x++)
{
int index = y * width + x;
charTemp[index] = (char) qGray(qImage->pixel(x, y));
}
}
return charIplImageBuffer;
}
2. IplImage 转换为 QImage
QImage *IplImageToQImage(const IplImage * iplImage)
{
uchar *qImageBuffer = NULL;
int width = iplImage->width; // Note here that OpenCV image is stored so that each lined is
// 32-bits aligned thus * explaining the necessity to "skip"
// the few last bytes of each line of OpenCV image buffer.
int widthStep = iplImage->widthStep;
int height = iplImage->height; switch (iplImage->depth)
{
case IPL_DEPTH_8U:
if (iplImage->nChannels == )
{
// IplImage is stored with one byte grey pixel.
// We convert it to an 8 bit depth QImage.
qImageBuffer = (uchar *) malloc(width*height*sizeof(uchar));
uchar *QImagePtr = qImageBuffer;
const uchar *iplImagePtr = (const uchar *)iplImage->imageData;
for (int y = ; y < height; y++)
{
// Copy line by line
memcpy(QImagePtr, iplImagePtr, width);
QImagePtr += width;
iplImagePtr += widthStep;
}
}
else if (iplImage->nChannels == )
{
// IplImage is stored with 3 byte color pixels (3 channels).
// We convert it to a 32 bit depth QImage.
qImageBuffer = (uchar *) malloc(width*height**sizeof(uchar));
uchar *QImagePtr = qImageBuffer;
const uchar *iplImagePtr = (const uchar *) iplImage->imageData; for (int y = ; y < height; y++)
{
for (int x = ; x < width; x++)
{
// We cannot help but copy manually.
QImagePtr[] = iplImagePtr[];
QImagePtr[] = iplImagePtr[];
QImagePtr[] = iplImagePtr[];
QImagePtr[] = ; QImagePtr += ;
iplImagePtr += ;
}
iplImagePtr += widthStep-*width;
}
}
else
{
qDebug("IplImageToQImage: image format is not supported:\
depth=8U and %d channels\n", iplImage->nChannels);
}
break; case IPL_DEPTH_16U:
if (iplImage->nChannels == )
{
// IplImage is stored with 2 bytes grey pixel.
// We convert it to an 8 bit depth QImage.
qImageBuffer = (uchar *) malloc(width*height*sizeof(uchar));
uchar *QImagePtr = qImageBuffer;
const uint16_t *iplImagePtr = (const uint16_t *)iplImage->imageData; for (int y = ; y < height; y++)
{
for (int x = ; x < width; x++)
{
// We take only the highest part of the 16 bit value.
// It is similar to dividing by 256.
*QImagePtr++ = ((*iplImagePtr++) >> );
}
iplImagePtr += widthStep/sizeof(uint16_t)-width;
}
}
else
{
qDebug("IplImageToQImage: image format is not supported:\
depth=16U and %d channels\n", iplImage->nChannels);
}
break; case IPL_DEPTH_32F:
if (iplImage->nChannels == )
{
// IplImage is stored with float (4 bytes) grey pixel.
// We convert it to an 8 bit depth QImage.
qImageBuffer = (uchar *) malloc(width*height*sizeof(uchar));
uchar *QImagePtr = qImageBuffer;
const float *iplImagePtr = (const float *) iplImage->imageData; for (int y = ; y < height; y++)
{
for (int x = ; x < width; x++)
{
uchar p;
float pf = (*iplImagePtr++); if (pf < ) p = ;
else if (pf > ) p = ;
else p = (uchar) pf; *QImagePtr++ = p;
}
iplImagePtr += widthStep/sizeof(float)-width;
}
}
else
{
qDebug("IplImageToQImage: image format is not supported:\
depth=32F and %d channels\n", iplImage->nChannels);
}
break; case IPL_DEPTH_64F:
if (iplImage->nChannels == )
{
// OpenCV image is stored with double (8 bytes) grey pixel.
// We convert it to an 8 bit depth QImage.
qImageBuffer = (uchar *) malloc(width*height*sizeof(uchar));
uchar *QImagePtr = qImageBuffer;
const double *iplImagePtr = (const double *) iplImage->imageData; for (int y = ; y < height; y++)
{
for (int x = ; x < width; x++)
{
uchar p;
double pf = * ((*iplImagePtr++) - mini) / (maxi - mini); if (pf < ) p = ;
else if (pf > ) p = ;
else p = (uchar) pf; *QImagePtr++ = p;
}
iplImagePtr += widthStep/sizeof(double)-width;
}
}
else
{
qDebug("IplImageToQImage: image format is not supported:\
depth=64F and %d channels\n", iplImage->nChannels);
}
break; default:
qDebug("IplImageToQImage: image format is not supported: depth=%d\
and %d channels\n", iplImage->depth, iplImage->nChannels);
} QImage *qImage;
if (iplImage->nChannels == )
{
QVector<QRgb> colorTable;
for (int i = ; i < ; i++)
{
colorTable.push_back(qRgb(i, i, i));
}
qImage = new QImage(qImageBuffer, width, height, QImage::Format_Indexed8);
qImage->setColorTable(colorTable);
}
else
{
qImage = new QImage(qImageBuffer, width, height, QImage::Format_RGB32);
} return qImage;
}
精简版:
QImage *IplImageToQImage(IplImage *image){
QImage *result;
if (image){
uchar * qImageBuffer = NULL;
int width = image->width;
int widthStep = image->widthStep;
int height = image->height;
QImage::Format format = QImage::Format_Invalid;
if (IPL_DEPTH_8U == image->depth && == image->nChannels){
qImageBuffer = (uchar *) malloc(width * height * * sizeof(uchar));
uchar *QImagePtr = qImageBuffer;
const uchar *iplImagePtr = (const uchar *) image->imageData;
format = QImage::Format_RGB32;
if (!qImageBuffer){
qDebug() << "Insufficient memory for image buffer!" << endl;
return result;
}
for (int y = ; y < height; y++)
{
for (int x = ; x < width; x++)
{
QImagePtr[] = iplImagePtr[];
QImagePtr[] = iplImagePtr[];
QImagePtr[] = iplImagePtr[];
QImagePtr[] = ; QImagePtr += ;
iplImagePtr += ;
}
iplImagePtr += widthStep-*width;
}
} else {
qDebug("Image format is not supported: depth=%d and %d channels\n", image->depth, image->nChannels);
return result;
}
if (qImageBuffer){
QImage *result = new QImage(qImageBuffer, image->width, image->height, format);
}
} else {
qDebug() << "Image pointer is NULL" << endl;
}
return result;
}
IplImage 与 QImage 相互转换的更多相关文章
- Qt OpenCV::Mat与Qt::QImage相互转换
Mat转QImage QImage mat2qim(Mat & mat) { cvtColor(mat, mat, COLOR_BGR2RGB); QImage qim((const unsi ...
- qt中使用opencv处理图片 QImage 和 IplImage 相互之间转换问题
在用opencv处理图片显示在qt label上的时候遇到不是问题 1. qt上要用qimage形式才干显示 IplImage转成 Qimage 彩色图像转换 IplImage *fram; QIm ...
- 【转】OpenCV与CxImage转换(IplImage)、IplImage QImage Mat 格式互转
最近由于在项目中用到了Opencv库,但是为了更好的显示图像还是使用了Cximage库,它可以快捷地存取.显示.转换各种图像.Opencv库用于高级图像处理与识别.为了使Cximage图像与Openc ...
- 更快地从IplImage转换成QImage
转:http://blog.sina.com.cn/s/blog_5c70dfc80100qzif.html 在Qt平台上使用OpenCV肯定会遇到从IplImage到QImage的转换问题,找了很多 ...
- QImage和IplImage转换总结
在arm中做图像处理,因为不支持GTK,一般都会用到QT来实现显示功能,所以不可避免的要涉及到QImage和IplImage两种图像格式之间的转换,下面总结一下转换的方法. (下面格式转换的代码都是网 ...
- 关于QImage和IplImage之间转换的实现
在嵌入式系统中实现qt和opencv的处理,最基础的就是QImage和IplImage之间的转换.这样两者就可以进行一起使用图像数据,从而达到利用qt显示和利用opencv处理的功能. 下面我将贴出代 ...
- 知乎上有一个问题“在mfc框架中,有上面方法能直接将opencv2.0库中的Mat格式图片传递到Picture Control”中显示?
一直以来,我使用的方法都是shiqiyu在opencvchina上面提供的引入directshow,并且采用cvvimage和cameraDs的方法.这个方法虽然在xp/win7/win8下面都能够成 ...
- 简易视频播放器2 (基于Qt、opencv)
因项目需要,需要实现一个对以保存的监测视频快速查看功能. 查询网上一些资料,初步简易的实现了一下. 实际效果图: 该程序基于Qt5.4,opencv248,开发环境为win8.1 结构为: video ...
- openCV(二)---iOS中使用openCV的图片格式转换
可以实现将UIImage和IplImage类型实现相互转换 //由于OpenCV主要针对的是计算机视觉方面的处理,因此在函数库中,最重要的结构体是IplImage结构. - (IplImage *)C ...
随机推荐
- 《转》.NET开源核心运行时,且行且珍惜
转载自infoQ 背景 InfoQ中文站此前报道过,2014年11月12日,ASP.NET之父.微软云计算与企业级产品工程部执行副总裁Scott Guthrie,在Connect全球开发者在线会议上宣 ...
- Excel Sheet Column Title & Excel Sheet Column Number
Excel Sheet Column Title Given a positive integer, return its corresponding column title as appear i ...
- apache AllowEncodedSlashes 允许URL中对路径分隔符进行编码
2013年11月29日 10:35:32 情景: 你想通过在当前的URL中记录来源页面的URL,以便处理完请求后再跳转回来源页: http://www.example1.com/refer/http: ...
- Adobe Flash Player 因过期而遭到阻止 更新插件 运行一次 解决方法
老机器运行 10.3.183.90 比较流畅 可是 Chrome 浏览器提示 Adobe Flash Player 因过期而遭到阻止 更新插件 运行一次 每次单击 运行一次 才运行,这样每次提醒很烦人 ...
- css3学习总结1--CSS3选择器
CSS3的属性选择器主要包括以下几种: 1. E[attr^="value"]:指定了属性名,并且有属性值,属性值是以value开头的: 2. E[attr$="valu ...
- Android之数据库操作
安卓数据库帮助类 /** * 数据库帮助类,用于管理数据库 * @author Administrator * */ public class PersonSQLiteOpenHelper exten ...
- select into from 提示 Undeclared variable.....错误的解决办法 && select into from 和 insert into select 的用法和区别
然而今天在使用 SELECT INTO FROM 备份mysql数据表的时候,运行相关 sql 语句的时候却一直返回 [Err] 1327 - Undeclared variable: ...... ...
- Android_adb shell am/pm使用
转自:http://blog.sina.com.cn/s/blog_51335a0001017ux5.html adb shell am instrument [options] <COMP ...
- ytu 2463:给小鼠补充代码(DFS 深度优先搜索)
2463: 给小鼠补充代码 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 5 Solved: 2[Submit][Status][Web Board] ...
- [原]AppPoolService-IIS应用程序池辅助类(C#控制应用程序池操作)
using System.Collections.Generic; using System.DirectoryServices; using System.Linq; using Microsoft ...