IplImage to QImage + QImage to IplImage


/ Published in: C++
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /*---------------------------------------------------------------*
  2.  * Interface between Qt images and OpenCV images *
  3.  * Filename : qtipl.h *
  4.  * Creation : 23 April 2003 *
  5.  * Authors : Rémi Ronfard, David Knossow and Matthieu Guilbert *
  6.  *---------------------------------------------------------------*/
  7.  
  8. #ifndef QTIPL_H
  9. #define QTIPL_H
  10.  
  11. #include <qimage.h>
  12. #include <iostream>
  13. #include <cstring>
  14. #include "opencv/cv.h"
  15.  
  16. typedef unsigned short uint16_t;
  17.  
  18. using std::string;
  19. using std::iostream;
  20.  
  21. QImage *IplImageToQImage(
  22. const IplImage* iplImage,
  23. uchar** data,
  24. double mini=0.0,
  25. double maxi=0.0);
  26. IplImage *QImageToIplImage(const QImage * qImage);
  27. #endif
  28.  
  29.  
  30. /*---------------------------------------------------------------*
  31.  * Interface between Qt images and OpenCV images *
  32.  * Filename : qtipl.cpp *
  33.  * Creation : 23 April 2003 *
  34.  * Authors : Rémi Ronfard, David Knossow and Matthieu Guilbert *
  35.  *---------------------------------------------------------------*/
  36.  
  37. #include "qtipl.h"
  38.  
  39. inline int align(int size, int align)
  40. {
  41. return (size + align - 1) & -align;
  42. }
  43.  
  44. IplImage *QImageToIplImage(const QImage * qImage)
  45. {
  46. int width = qImage->width();
  47. int height = qImage->height();
  48. CvSize Size;
  49. Size.height = height;
  50. Size.width = width;
  51.  
  52. IplImage *charIplImageBuffer = cvCreateImage(Size, IPL_DEPTH_8U, 1);
  53. char *charTemp = (char *) charIplImageBuffer->imageData;
  54.  
  55. for (int y = 0; y < height; y++)
  56. {
  57. for (int x = 0; x < width; x++)
  58. {
  59. int index = y * width + x;
  60. charTemp[index] = (char) qGray(qImage->pixel(x, y));
  61. }
  62. }
  63. return charIplImageBuffer;
  64. }
  65.  
  66. /* Usage:
  67.   uchar *data;
  68.   QImage *qImg = IplImageToQImage(iplImg, &data);
  69.   QPixmap pix = QPixmap::fromImage(*qImg);
  70.   pixmapLabel->setPixmap(pix);
  71.   delete qImg;
  72.   cvReleaseImage(&iplImg);
  73. */
  74. QImage *IplImageToQImage(const IplImage * iplImage, uchar **data,
  75. double mini, double maxi)
  76. {
  77. uchar *qImageBuffer = NULL;
  78. int width = iplImage->width;
  79.  
  80. // Note here that OpenCV image is stored so that each lined is
  81. // 32-bits aligned thus * explaining the necessity to "skip"
  82. // the few last bytes of each line of OpenCV image buffer.
  83. int widthStep = iplImage->widthStep;
  84. int height = iplImage->height;
  85.  
  86. switch (iplImage->depth)
  87. {
  88. case IPL_DEPTH_8U:
  89. if (iplImage->nChannels == 1)
  90. {
  91. // IplImage is stored with one byte grey pixel.
  92. // We convert it to an 8 bit depth QImage.
  93. qImageBuffer = (uchar *) malloc(width*height*sizeof(uchar));
  94. uchar *QImagePtr = qImageBuffer;
  95. const uchar *iplImagePtr = (const uchar *)iplImage->imageData;
  96. for (int y = 0; y < height; y++)
  97. {
  98. // Copy line by line
  99. memcpy(QImagePtr, iplImagePtr, width);
  100. QImagePtr += width;
  101. iplImagePtr += widthStep;
  102. }
  103. }
  104. else if (iplImage->nChannels == 3)
  105. {
  106. // IplImage is stored with 3 byte color pixels (3 channels).
  107. // We convert it to a 32 bit depth QImage.
  108. qImageBuffer = (uchar *) malloc(width*height*4*sizeof(uchar));
  109. uchar *QImagePtr = qImageBuffer;
  110. const uchar *iplImagePtr = (const uchar *) iplImage->imageData;
  111.  
  112. for (int y = 0; y < height; y++)
  113. {
  114. for (int x = 0; x < width; x++)
  115. {
  116. // We cannot help but copy manually.
  117. QImagePtr[0] = iplImagePtr[0];
  118. QImagePtr[1] = iplImagePtr[1];
  119. QImagePtr[2] = iplImagePtr[2];
  120. QImagePtr[3] = 0;
  121.  
  122. QImagePtr += 4;
  123. iplImagePtr += 3;
  124. }
  125. iplImagePtr += widthStep-3*width;
  126. }
  127. }
  128. else
  129. {
  130. qDebug("IplImageToQImage: image format is not supported:\
  131. depth=8U and %d channels\n", iplImage->nChannels);
  132. }
  133. break;
  134.  
  135. case IPL_DEPTH_16U:
  136. if (iplImage->nChannels == 1)
  137. {
  138. // IplImage is stored with 2 bytes grey pixel.
  139. // We convert it to an 8 bit depth QImage.
  140. qImageBuffer = (uchar *) malloc(width*height*sizeof(uchar));
  141. uchar *QImagePtr = qImageBuffer;
  142. const uint16_t *iplImagePtr = (const uint16_t *)iplImage->imageData;
  143.  
  144. for (int y = 0; y < height; y++)
  145. {
  146. for (int x = 0; x < width; x++)
  147. {
  148. // We take only the highest part of the 16 bit value.
  149. // It is similar to dividing by 256.
  150. *QImagePtr++ = ((*iplImagePtr++) >> 8);
  151. }
  152. iplImagePtr += widthStep/sizeof(uint16_t)-width;
  153. }
  154. }
  155. else
  156. {
  157. qDebug("IplImageToQImage: image format is not supported:\
  158. depth=16U and %d channels\n", iplImage->nChannels);
  159. }
  160. break;
  161.  
  162. case IPL_DEPTH_32F:
  163. if (iplImage->nChannels == 1)
  164. {
  165. // IplImage is stored with float (4 bytes) grey pixel.
  166. // We convert it to an 8 bit depth QImage.
  167. qImageBuffer = (uchar *) malloc(width*height*sizeof(uchar));
  168. uchar *QImagePtr = qImageBuffer;
  169. const float *iplImagePtr = (const float *) iplImage->imageData;
  170.  
  171. for (int y = 0; y < height; y++)
  172. {
  173. for (int x = 0; x < width; x++)
  174. {
  175. uchar p;
  176. float pf = 255 * ((*iplImagePtr++) - mini) / (maxi - mini);
  177.  
  178. if (pf < 0) p = 0;
  179. else if (pf > 255) p = 255;
  180. else p = (uchar) pf;
  181.  
  182. *QImagePtr++ = p;
  183. }
  184. iplImagePtr += widthStep/sizeof(float)-width;
  185. }
  186. }
  187. else
  188. {
  189. qDebug("IplImageToQImage: image format is not supported:\
  190. depth=32F and %d channels\n", iplImage->nChannels);
  191. }
  192. break;
  193.  
  194. case IPL_DEPTH_64F:
  195. if (iplImage->nChannels == 1)
  196. {
  197. // OpenCV image is stored with double (8 bytes) grey pixel.
  198. // We convert it to an 8 bit depth QImage.
  199. qImageBuffer = (uchar *) malloc(width*height*sizeof(uchar));
  200. uchar *QImagePtr = qImageBuffer;
  201. const double *iplImagePtr = (const double *) iplImage->imageData;
  202.  
  203. for (int y = 0; y < height; y++)
  204. {
  205. for (int x = 0; x < width; x++)
  206. {
  207. uchar p;
  208. double pf = 255 * ((*iplImagePtr++) - mini) / (maxi - mini);
  209.  
  210. if (pf < 0) p = 0;
  211. else if (pf > 255) p = 255;
  212. else p = (uchar) pf;
  213.  
  214. *QImagePtr++ = p;
  215. }
  216. iplImagePtr += widthStep/sizeof(double)-width;
  217. }
  218. }
  219. else
  220. {
  221. qDebug("IplImageToQImage: image format is not supported:\
  222. depth=64F and %d channels\n", iplImage->nChannels);
  223. }
  224. break;
  225.  
  226. default:
  227. qDebug("IplImageToQImage: image format is not supported: depth=%d\
  228. and %d channels\n", iplImage->depth, iplImage->nChannels);
  229. }
  230.  
  231. QImage *qImage;
  232. if (iplImage->nChannels == 1)
  233. {
  234. QVector<QRgb> colorTable;
  235. for (int i = 0; i < 256; i++)
  236. {
  237. colorTable.push_back(qRgb(i, i, i));
  238. }
  239. qImage = new QImage(qImageBuffer, width, height, QImage::Format_Indexed8);
  240. qImage->setColorTable(colorTable);
  241. }
  242. else
  243. {
  244. qImage = new QImage(qImageBuffer, width, height, QImage::Format_RGB32);
  245. }
  246. *data = qImageBuffer;
  247.  
  248. return qImage;
  249. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.