Qt中顯示OpenCV的IplImage
轉自:http://blog.csdn.net/tianchaotian/article/details/8984429
opencv中的影象型別是IplImage,Qt中是QImage
必須要實現兩者的轉換!。
QImage MainWindow::IplImage2QImage(const IplImage *iplImage, double mini, double maxi) {
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 == 1)
{
/* OpenCV image 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 = 0; y < height; y++)
{
// Copy line by line
memcpy(QImagePtr, iplImagePtr, width);
QImagePtr += width;
iplImagePtr += widthStep;
}
}
else if(iplImage->nChannels == 3)
{
/* OpenCV image is stored with 3 byte color pixels (3 channels).
We convert it to a 32 bit depth QImage.
*/
qImageBuffer = (uchar *) malloc(width*height*4*sizeof(uchar));
uchar *QImagePtr = qImageBuffer;
const uchar *iplImagePtr = (const uchar *) iplImage->imageData;
for(int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
// We cannot help but copy manually.
QImagePtr[0] = iplImagePtr[0];
QImagePtr[1] = iplImagePtr[1];
QImagePtr[2] = iplImagePtr[2];
QImagePtr[3] = 0;
QImagePtr += 4;
iplImagePtr += 3;
}
iplImagePtr += widthStep-3*width;
}
}
else
{
qDebug("IplImageToQImage: image format is not supported : depth=8U and %d channels ", iplImage->nChannels);
}
break;
case IPL_DEPTH_16U:
if(iplImage->nChannels == 1)
{
/* OpenCV image 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 *);
const unsigned int *iplImagePtr = (const unsigned int *)iplImage->imageData;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
// We take only the highest part of the 16 bit value. It is
//similar to dividing by 256.
*QImagePtr++ = ((*iplImagePtr++) >> 8);
}
iplImagePtr += widthStep/sizeof(unsigned int)-width;
}
}
else
{
qDebug("IplImageToQImage: image format is not supported : depth=16U and %d channels ", iplImage->nChannels);
}
break;
case IPL_DEPTH_32F:
if(iplImage->nChannels == 1)
{
/* OpenCV image 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 = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
uchar p;
float pf = 255 * ((*iplImagePtr++) - mini) / (maxi - mini);
if(pf < 0) p = 0;
else if(pf > 255) p = 255;
else p = (uchar) pf;
*QImagePtr++ = p;
}
iplImagePtr += widthStep/sizeof(float)-width;
}
}
else
{
qDebug("IplImageToQImage: image format is not supported : depth=32F and %d channels ", iplImage->nChannels);
}
break;
case IPL_DEPTH_64F:
if(iplImage->nChannels == 1)
{
/* 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 = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
uchar p;
double pf = 255 * ((*iplImagePtr++) - mini) / (maxi - mini);
if(pf < 0) p = 0;
else if(pf > 255) p = 255;
else p = (uchar) pf;
*QImagePtr++ = p;
}
iplImagePtr += widthStep/sizeof(double)-width;
}
}
else
{
qDebug("IplImageToQImage: image format is not supported : depth=64F and %d channels ", iplImage->nChannels);
}
break;
default:
qDebug("IplImageToQImage: image format is not supported : depth=%d and %d channels ", iplImage->depth, iplImage->nChannels);
}
QImage qImage;
QVector<QRgb> vcolorTable;
if(iplImage->nChannels == 1)
{
// We should check who is going to destroy this allocation.
QRgb *colorTable = new QRgb[256];
for(int i = 0; i < 256; i++)
{
colorTable[i] = qRgb(i, i, i);
vcolorTable[i] = colorTable[i];
}
qImage = QImage(qImageBuffer, width, height, QImage::Format_Indexed8).copy();
qImage.setColorTable(vcolorTable);
}
else
{
qImage = QImage(qImageBuffer, width, height, QImage::Format_RGB32).copy();
}
free(qImageBuffer);
return qImage;
}
相關文章
- opencv mat轉IplImage*OpenCV
- opencv顯示中文OpenCV
- Qt中在按鈕上顯示字元'&'QT字元
- 用Qt5和OpenCV讀取顯示中文路徑的圖片QTOpenCV
- Qt中設定視窗居中顯示QT
- 【Qt】UI顯示中文QTUI
- Qt 時間顯示QT
- Pycharm中,pyqt5.11.1的Qt assistant顯示PyCharmQT
- QT居中螢幕顯示QT
- qt視窗居中顯示QT
- OpenCV_python全屏顯示影像OpenCVPython
- 將MYSQL資料顯示在QT的tablewidget中/將QT中的資料儲存到MYSQL資料庫中MySqlQT資料庫
- IplImage
- Qt 讓彈出的視窗居中顯示QT
- Qt中關於QLabel等其顯示內容的縮放與對齊,動態圖的顯示QT
- Qt之彈出介面顯示在父視窗中間QT
- qt中debug顯示64位不能除錯32位QT除錯
- qt中實現實時的顯示當前時刻的時間QT
- QT顯示當前日期時間QT
- Qt 設定視窗居中顯示QT
- QT時鐘控制元件顯示QT控制元件
- QT設定標籤顯示位置QT
- QT tableWidget 內容居中顯示QT
- QT介面顯示實時時間QT
- QT版用QLCDnumber顯示時間QT
- Qt5:視窗居中顯示QT
- Qt視窗螢幕居中顯示QT
- QT中文顯示亂碼解決QT
- Qt 把窗體顯示在螢幕的中心QT
- Qt如何讓彈出的視窗居中顯示QT
- Qt 實現 Logger 日誌的顯示QT
- QT學習 實時顯示時間QT
- Qt實時顯示系統時間QT
- Qt之設定窗體居中顯示QT
- QT之控制元件疊加顯示QT控制元件
- OpenCV成長之路(1):影象的讀寫與顯示OpenCV
- 配置QT Mingw & opencvQTOpenCV
- Qt設計:時間顯示(QTimer)QT