Using OpenCV with Qt

系统 1643 0

The IplImage and Qt

In order to take advantage of the 500 algorithms offered by OpenCV, Qt applications have to manage   iplImages . Therefore, converting   QImage to/from   iplImage   is very important.

Converting QImage to iplImage

This snippet will convert   QImages   into   iplImage   with depth 8 and 3 channels.   QImage   can store data in several formats. This code accepts just 24-bit   QImage::Format_RGB888   and the QImage::Format_RGB32 . Apha values in the 32-bit format will be removed during the conversion.

    static IplImage* qImage2IplImage(const QImage& qImage)

  {

    int width = qImage.width();

    int height = qImage.height();

 

    // Creates a iplImage with 3 channels

    IplImage *img = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);

    char * imgBuffer = img->imageData;

 

    //Remove alpha channel

    int jump = (qImage.hasAlphaChannel()) ? 4 : 3;

 

    for (int y=0;y<img->height;y++){

      QByteArray a((const char*)qImage.scanLine(y), qImage.bytesPerLine());

      for (int i=0; i<a.size(); i+=jump){

          //Swap from RGB to BGR

          imgBuffer[2] = a[i];

          imgBuffer[1] = a[i+1];

          imgBuffer[0] = a[i+2];

          imgBuffer+=3;

      }

  }

 

  return img;

  }
  

Converting iplImage to QImage

This snippet will convert a   iplImage   with depth 8 and 1 or 3 channels into a 8/24-bit   QImage .

Note:  This code won't work for images with different depth and number of channels.
    static QImage IplImage2QImage(const IplImage *iplImage)

{

    int height = iplImage->height;

    int width = iplImage->width;

 

    if  (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 3)

    {

      const uchar *qImageBuffer = (const uchar*)iplImage->imageData;

      QImage img(qImageBuffer, width, height, QImage::Format_RGB888);

      return img.rgbSwapped();

    } else if  (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 1){

	const uchar *qImageBuffer = (const uchar*)iplImage->imageData;

	QImage img(qImageBuffer, width, height, QImage::Format_Indexed8);

 

	QVector<QRgb> colorTable;

	for (int i = 0; i < 256; i++){

	    colorTable.push_back(qRgb(i, i, i));

	}

	img.setColorTable(colorTable);

	return img;

    }else{

      qWarning() << "Image cannot be converted.";

      return QImage();

    }

}
  

 

http://www.developer.nokia.com/Community/Wiki/Using_OpenCV_with_Qt

 

Using OpenCV with Qt


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论