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 ...
随机推荐
- POJ 2386
http://poj.org/problem?id=2386 这个题目与那个POJ 1562几乎是差不多的,只不过那个比这个输入要复杂一些 #include <stdio.h> #incl ...
- FileOutputStream与FileInputStream互相转换
List<InstorageNoticeDto> noticeList = null; FileOutputStream fos = null; FileInputStream is = ...
- php 301
2013年7月1日 13:35:45 PHP在301跳转的时候,如果是跨域跳转,记着把要跳转到的URL添上"http://"
- IOS多线程(GCD)
简介 Grand Central Dispatch 简称(GCD)是苹果公司开发的技术,以优化的应用程序支持多核心处理器和其他的对称多处理系统的系统.这建立在任务并行执行的线程池模式的基础上的.它首次 ...
- BestCoder14 1002.Harry And Dig Machine(hdu 5067) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5067 题目意思:给出一个 n * m 的方格,每一个小方格(大小为1*1)的值要么为 0 要么为一个正 ...
- Ubuntu下用命令行快速打开各类型文件
在Ubuntu下,通常用命令行打开文本文件,比如用命令gedit.more.cat.vim.less.但当需要打开其他格式文件时,比如pdf. jpg.mp3格式文件,咱们通常做法是进入到文件所在的目 ...
- [原]AppPoolService-IIS应用程序池辅助类(C#控制应用程序池操作)
using System.Collections.Generic; using System.DirectoryServices; using System.Linq; using Microsoft ...
- hdu 3183 贪心
题意:给一个数字,删掉其中的若干位,使得最后的数字最小 就是每次删除数的时候都是删掉第一个比右边数大的数 利用双向链表模拟 #include<cstdio> #include<ios ...
- Android_就像小朋友“搭积木”一样。
就像小朋友“搭积木”一样.感觉这句话很有意思.完整的话是这样的: Android提供了大量功能丰富的UI组件,开发者只要按一定规律把这些UI组件组合起来 --就像小朋友“搭积木”一样,把这些UI组件搭 ...
- HDU 5682/BestCoder Round #83 1003 zxa and leaf 二分+树
zxa and leaf Problem Description zxa have an unrooted tree with n nodes, including (n−1) undirected ...