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 ...
随机推荐
- bootbox显示中文的按钮
$("selector").on('click',function(){ bootbox.confirm({ title : "请确认", buttons: { ...
- excel复制+粘贴,怎样让公式里的参数不自动变化?
例如,某一单元格内容为:=A1+A2 我把它复制+粘贴到其他地方,就自动变成了:=B1+B2 怎样让它不变化,仍保持=A1+A2 ?? 答: Excel一般使用相对地址来引用单元格的位置,当把一个含有 ...
- (原创)Python文件与文件系统系列(5)——stat模块
stat模块中定义了许多的常量和函数,可以帮助解释 os.stat().os.fstat().os.lstat()等函数返回的 st_result 类型的对象. 通常使用 os.path.is*() ...
- [MAC] SVN lock的使用
转载 : http://www.eefocus.com/czzheng/blog/12-03/245532_4ca94.html 如果压根没有锁lock,那么每个人都拥有一个本地copy,每个人都能自 ...
- Java 全半角转换
* 全角转半角的 转换函数* @return String*/public static final String full2HalfChange(String QJstr){StringBuffer ...
- Human Gene Functions(poj 1080)
题目大意是:给定两组DNA序列,要你求出它们的最大相似度 每个字母与其他字母或自身和空格对应都有一个打分,求在这两个字符串中插入空格,让这两个字符串的匹配分数最大 /* 思路是很好想的,设f[i][j ...
- Android UI组件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- js “+” 连接字符串&数字相加 数字相加出现多位小数 函数调用单引号双引号嵌套和转义字符的使用
一.机制 JavaScript中,加号不仅表示相加还表示字符串连接 当加号两边存在字符串时,加号代表连接,实际上是将两侧都转为了字符串,如 "1" + 1 = "11&q ...
- 【Chrome】手动下载和安装Adblock Plus的方法
由于强大的GFW,导致很多Google的站点没法访问,也就没法直接安装chrome的插件了,网页都打不卡, 那么几乎是必备的Adblock Plus如何下载安装呢? 1.访问官方网站: https:/ ...
- 10年程序员谈.Net程序员的职业规划(图/文) (转载)
转载地址:http://www.cnblogs.com/donghongtao/p/3611623.html