1. 程式人生 > >QImage和IplImage轉換總結

QImage和IplImage轉換總結

title tpi ews vsize ann channel pre tro pan

在arm中做圖像處理,因為不支持GTK,一般都會用到QT來實現顯示功能,所以不可避免的要涉及到QImage和IplImage兩種圖像格式之間的轉換,下面總結一下轉換的方法。

(下面格式轉換的代碼都是網友寫的,我這裏只是歸納修改了一下)

IplImage 轉換為 QImage

方法1

[plain] view plain copy
  1. QImage *IplImageToQImage(const IplImage *img)
  2. {
  3. QImage *qmg;
  4. cvCvtColor(img,img,CV_BGR2RGB);
  5. qmg = new QImage((unsigned char*)img->imageData,img->width,img->height,img->widthStep,QImage::Format_RGB888);
  6. return qmg;
  7. }

這裏要註意的是,IplImage每個像素點存儲的格式為BGR,而QImage每個像素點存儲的格式為RGB,所以在轉換之前需要先使用opencv提供的cvCvtColor函數將BGR圖像變為RGB圖像。還有就是QImage最後一個參數QImage::Format_RGB888,這是指色彩空間R,G,B三個通道分別用8位來表示,如果是灰度圖,則參數為QImage::Format_Indexed8。

例如:

[plain] view plain copy
  1. QImage *IplImageToQImage(const IplImage *img)
  2. {
  3. QImage *qmg;
  4. IplImage *img_gray = cvCreateImage(cvGetSize(img),8,1);
  5. cvCvtColor(img,img_gray,CV_BGR2GRAY);
  6. qmg = new QImage((unsigned char*)img->imageData,img->width,img->height,img->widthStep,QImage::Format_Indexed8);
  7. return qmg;
  8. }

方法2

[plain] view plain copy
  1. QImage *IplImageToQImage(*img)
  2. {
  3. QImage *qmg = new QImage (img->width,img-height,img->widthStep,QImage::Format_RGB32);
  4. for (int i=0; i<height; ++i)
  5. {
  6. for (int j=0; j<width; ++j)
  7. {
  8. int r,g,b;
  9. if( 3 == img->nChannels )
  10. {
  11. b = (int)CV_IMAGE_ELEM( img, uchar, i, j*3+0 );
  12. g = (int)CV_IMAGE_ELEM( img, uchar, i, j*3+1 );
  13. r = (int)CV_IMAGE_ELEM( img, uchar, i, j*3+2 );
  14. }
  15. else if( 1 == img->nChannels )
  16. {
  17. b = (int)CV_IMAGE_ELEM( img, uchar, i, j*3+0 );
  18. g = b;
  19. r = b;
  20. }
  21. qmg->setPixel( j, i, qRgb(r,g,b) );
  22. }
  23. }
  24. return qmg;
  25. }


這裏要註意的是qmg->setPixel( j, i, qRgb(r,g,b) );括號裏面是(j,i);而不是(i,j)。我還沒弄明白為什麽這裏要調換一下i,j的順序,希望知道的朋友指點一下。

QImage轉換為IplImage

方法1

[plain] view plain copy
  1. IplImage *QImageToIplImage(const QImage * qImage)
  2. {
  3. int width = qImage->width();
  4. int height = qImage->height();
  5. CvSize Size;
  6. Size.height = height;
  7. Size.width = width;
  8. IplImage *IplImageBuffer = cvCreateImage(Size, IPL_DEPTH_8U, 3);
  9. for (int y = 0; y < height; ++y)
  10. {
  11. for (int x = 0; x < width; ++x)
  12. {
  13. QRgb rgb = qImage->pixel(x, y);
  14. cvSet2D(IplImageBuffer, y, x, CV_RGB(qRed(rgb), qGreen(rgb), qBlue(rgb)));
  15. }
  16. }
  17. return IplImageBuffer;
  18. }


方法2

[plain] view plain copy
  1. IplImage *QImageToIplImage(const QImage * qImage)
  2. {
  3. int width = qImage->width();
  4. int height = qImage->height();
  5. CvSize Size;
  6. Size.height = height;
  7. Size.width = width;
  8. IplImage *IplImageBuffer = cvCreateImage(Size, IPL_DEPTH_8U, 3);
  9. for (int y = 0; y < height; ++y)
  10. {
  11. for (int x = 0; x < width; ++x)
  12. {
  13. QRgb rgb = qImage->pixel(x, y);
  14. CV_IMAGE_ELEM( IplImageBuffer, uchar, y, x*3+0 ) = qBlue(rgb);
  15. CV_IMAGE_ELEM( IplImageBuffer, uchar, y, x*3+1 ) = qGreen(rgb);
  16. CV_IMAGE_ELEM( IplImageBuffer, uchar, y, x*3+2 ) = qRed(rgb);
  17. }
  18. }
  19. return IplImageBuffer;
  20. }


這兩種方法其實都是一樣的,只是表達形式有點不同,還有就是一個是生成RGB格式圖像,一個是生成BGR格式的圖像。

QImage和IplImage轉換總結