Results 1 to 20 of 48

Thread: OpenCV integration

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: OpenCV integration

    I put this QImage IplImageToQImage here on this thread if people like to search...
    I tested on QT4.4 and run ok....

    from QPainter p(&gd); forward is only a date print...
    I write & copy this piece to a label :
    Animated Portable Network Graphics APNG , which run on Firefox 3 or opera...
    http://www.qt-apps.org/content/show....?content=82221


    Qt Code:
    1. #ifdef OPCAMENABLE
    2.  
    3. #include "cv.h"
    4. #include "highgui.h"
    5. #include <stdio.h>
    6. #include <ctype.h>
    7.  
    8. static QImage IplImageToQImage(const IplImage *iplImage, uchar **data , bool mirroRimage = true )
    9. {
    10. uchar *qImageBuffer = NULL;
    11. int width = iplImage->width;
    12.  
    13. /*
    14.   * Note here that OpenCV image is stored so that each lined is 32-bits aligned thus
    15.   * explaining the necessity to "skip" the few last bytes of each line of OpenCV image buffer.
    16.   */
    17. int widthStep = iplImage->widthStep;
    18. int height = iplImage->height;
    19.  
    20. switch (iplImage->depth)
    21. {
    22. case IPL_DEPTH_8U:
    23. if (iplImage->nChannels == 1)
    24. {
    25. /* OpenCV image is stored with one byte grey pixel. We convert it to an 8 bit depth QImage. */
    26. qImageBuffer = (uchar *)malloc(width * height * sizeof(uchar));
    27. uchar *QImagePtr = qImageBuffer;
    28. const uchar *iplImagePtr = (const uchar *)iplImage->imageData;
    29. for (int y = 0; y < height; ++y)
    30. {
    31. // Copy line by line
    32. memcpy(QImagePtr, iplImagePtr, width);
    33. QImagePtr += width;
    34. iplImagePtr += widthStep;
    35. }
    36. }
    37. else if (iplImage->nChannels == 3)
    38. {
    39. /* OpenCV image is stored with 3 byte color pixels (3 channels). We convert it to a 32 bit depth QImage. */
    40. qImageBuffer = (uchar *)malloc(width * height * 4 * sizeof(uchar));
    41. uchar *QImagePtr = qImageBuffer;
    42. const uchar *iplImagePtr = (const uchar *)iplImage->imageData;
    43.  
    44. for (int y = 0; y < height; ++y)
    45. {
    46. for (int x = 0; x < width; ++x)
    47. {
    48. // We cannot help but copy manually.
    49. QImagePtr[0] = iplImagePtr[0];
    50. QImagePtr[1] = iplImagePtr[1];
    51. QImagePtr[2] = iplImagePtr[2];
    52. QImagePtr[3] = 0;
    53.  
    54. QImagePtr += 4;
    55. iplImagePtr += 3;
    56. }
    57. iplImagePtr += widthStep - 3 * width;
    58. }
    59. }
    60. else
    61. qDebug("IplImageToQImage: image format is not supported : depth=8U and %d channels\n", iplImage->nChannels);
    62.  
    63. break;
    64.  
    65. case IPL_DEPTH_16U:
    66. if (iplImage->nChannels == 1)
    67. {
    68. /* OpenCV image is stored with 2 bytes grey pixel. We convert it to an 8 bit depth QImage. */
    69. qImageBuffer = (uchar *)malloc(width * height * sizeof(uchar));
    70. uchar *QImagePtr = qImageBuffer;
    71. const uint16_t *iplImagePtr = (const uint16_t *)iplImage->imageData;
    72.  
    73. for (int y = 0; y < height; ++y)
    74. {
    75. for (int x = 0; x < width; ++x)
    76. *QImagePtr++ = ((*iplImagePtr++) >> 8); // We take only the highest part of the 16 bit value. It is similar to dividing by 256.
    77. iplImagePtr += widthStep / sizeof(uint16_t) - width;
    78. }
    79. }
    80. else
    81. qDebug("IplImageToQImage: image format is not supported : depth=16U and %d channels\n", iplImage->nChannels);
    82.  
    83. break;
    84.  
    85. case IPL_DEPTH_32F:
    86. if (iplImage->nChannels == 1)
    87. {
    88. /* OpenCV image is stored with float (4 bytes) grey pixel. We convert it to an 8 bit depth QImage. */
    89. qImageBuffer = (uchar *)malloc(width * height * sizeof(uchar));
    90. uchar *QImagePtr = qImageBuffer;
    91. const float *iplImagePtr = (const float *)iplImage->imageData;
    92.  
    93. for (int y = 0; y < height; ++y)
    94. {
    95. for (int x = 0; x < width; ++x)
    96. *QImagePtr++ = (uchar)(255 * ((*iplImagePtr++)));
    97. iplImagePtr += widthStep / sizeof(float) - width;
    98. }
    99. }
    100. else
    101. qDebug("IplImageToQImage: image format is not supported : depth=32F and %d channels\n", iplImage->nChannels);
    102.  
    103. break;
    104.  
    105. case IPL_DEPTH_64F:
    106. if (iplImage->nChannels == 1)
    107. {
    108. /* OpenCV image is stored with double (8 bytes) grey pixel. We convert it to an 8 bit depth QImage. */
    109. qImageBuffer = (uchar *) malloc(width * height * sizeof(uchar));
    110. uchar *QImagePtr = qImageBuffer;
    111. const double *iplImagePtr = (const double *) iplImage->imageData;
    112.  
    113. for (int y = 0; y < height; ++y)
    114. {
    115. for (int x = 0; x < width; ++x)
    116. *QImagePtr++ = (uchar)(255 * ((*iplImagePtr++)));
    117. iplImagePtr += widthStep / sizeof(double) - width;
    118. }
    119. }
    120. else
    121. qDebug("IplImageToQImage: image format is not supported : depth=64F and %d channels\n", iplImage->nChannels);
    122.  
    123. break;
    124.  
    125. default:
    126. qDebug("IplImageToQImage: image format is not supported : depth=%d and %d channels\n", iplImage->depth, iplImage->nChannels);
    127. }
    128.  
    129. QImage *qImage;
    130. if (iplImage->nChannels == 1)
    131. {
    132. QVector<QRgb> colorTable;
    133. for (int i = 0; i < 256; i++)
    134. colorTable.push_back(qRgb(i, i, i));
    135.  
    136. qImage = new QImage(qImageBuffer, width, height, QImage::Format_Indexed8);
    137. qImage->setColorTable(colorTable);
    138. }
    139. else
    140. qImage = new QImage(qImageBuffer, width, height, QImage::Format_RGB32);
    141. QImage gd0 = qImage->mirrored(false,mirroRimage);
    142. *data = qImageBuffer;
    143. QColor textColor = Qt::black;
    144. QColor fillrectcolor = Qt::red;
    145. QColor shapepicture = Qt::white;
    146. QImage gd = gd0.scaledToWidth(350);
    147. QDateTime now = QDateTime::currentDateTime ();
    148. QString selectionText = now.toString("dd.MM.yyyy hh:mm:ss");
    149. QPainter p(&gd);
    150. p.setRenderHint(QPainter::Antialiasing, true);
    151.  
    152. QFontMetrics fm( qApp->font() );
    153. int stringWidth = fm.width(selectionText);
    154. int stringHeight = fm.ascent();
    155. const int sx = gd.width() - stringWidth - 5;
    156. QPen pen;
    157. pen.setStyle( Qt::SolidLine );
    158. pen.setWidth( 2 );
    159. pen.setColor( textColor );
    160. p.setPen( pen);
    161. p.drawText(QPointF(sx - 1 ,gd.height() - 2 - stringHeight - 1),selectionText);
    162. pen.setColor( fillrectcolor );
    163. p.setPen( pen);
    164. p.drawText(QPointF(sx,gd.height() - 2 - stringHeight),selectionText);
    165.  
    166. return gd;
    167. }
    168.  
    169.  
    170.  
    171. #endif
    To copy to clipboard, switch view to plain text mode 

  2. The following 2 users say thank you to patrik08 for this useful post:

    carllooper (18th August 2009), droetker (18th May 2012)

Similar Threads

  1. Replies: 8
    Last Post: 18th March 2011, 11:27
  2. Replies: 7
    Last Post: 22nd December 2010, 08:13
  3. Qt4 integration with VS2008 Express how-to interest?
    By thomaspu in forum Qt Programming
    Replies: 11
    Last Post: 21st May 2008, 12:53
  4. How to open two cameras with opencv?
    By alphaboy in forum General Programming
    Replies: 2
    Last Post: 21st December 2007, 10:58
  5. VS Integration plugins not visible
    By kemp in forum Qt Tools
    Replies: 1
    Last Post: 11th August 2006, 22:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.