OpenCV中IplImage与Qt中的QImage转化

OpenCV中IplImage与Qt中的QImage转化

在工程中由于要涉及到OpenCV中IplImage与Qt中的QImage转化,所以写了两个类:

#ifndef IMAGETRANSFORM_H

#define IMAGETRANSFORM_H

 

#include "highgui.h"

#include "cv.h"

#include <QImage>

 

#define RGB_TYPE 3              //R,G,B三通道

#define GRAY_TYPE 1             // 灰度图,单通道

 

class ImageCVtoQT

{

public:

       ImageCVtoQT(IplImage *_srcImage);

       ~ImageCVtoQT();

       const QImage getQtImage(void);

 

private:

       IplImage *srcImage;          //原图

 

       //QImage desImage;             //转换图

       int width;

       int height;

       int channel;                 //图像通道数

};

 

class ImageQTtoCV

{

public:

       ImageQTtoCV(QImage _srcImage);

       ~ImageQTtoCV();

       IplImage *getCvImage(void);

 

private:

       QImage srcImage;

       int width;

       int height;

       //int channel;

};

 

#endif

 

 

#include"ImageTransform.h"

 

/*************************************************

*OpenCV中的图像结构IplImage转换成QT中的QImage

*************************************************/

ImageCVtoQT::ImageCVtoQT(IplImage *_srcImage)

:srcImage(_srcImage)

{

       assert(srcImage!=NULL);  

       width=srcImage->width;

       height=srcImage->height;

       channel=srcImage->nChannels;

}

 

ImageCVtoQT::~ImageCVtoQT()

{

       cvReleaseImage(&srcImage);

}

 

const QImage ImageCVtoQT::getQtImage()

{

       QImage desImage=QImage(width,height,QImage::Format_RGB32);

       for(int i=0;i<height;i++)

       {

              for(int j=0;j<width;j++)

              {

                     int r,g,b;

                     if(RGB_TYPE==channel)

                     {

                            b=(int)CV_IMAGE_ELEM(srcImage,uchar,i,j*3+0);

                            g=(int)CV_IMAGE_ELEM(srcImage,uchar,i,j*3+1);

                            r=(int)CV_IMAGE_ELEM(srcImage,uchar,i,j*3+2);

                     }

                     else if(GRAY_TYPE==channel)

                     {

                            b=(int)CV_IMAGE_ELEM(srcImage,uchar,i,j);

                            g=b;

                            r=b;

                     }

                     desImage.setPixel(j,i,qRgb(r,g,b));

              }

       }

       return desImage;

}

 

 

//////////////////////////////////////////////////////////////////////

 

/*********************************************

*QT中的QImage图像结构转换成OpenCV中的IplImage

**********************************************/

ImageQTtoCV::ImageQTtoCV(QImage _srcImage)

:srcImage(_srcImage)

{

       assert(!srcImage.isNull());

       width=srcImage.width();

       height=srcImage.height();

 

}

 

ImageQTtoCV::~ImageQTtoCV()

{

}

 

IplImage *ImageQTtoCV::getCvImage()

{

       IplImage *desImage=cvCreateImage(cvSize(width,height),8,3);

       for(int i=0;i<height;i++)

       {

              for(int j=0;j<width;j++)

              {

                     QRgb rgb=srcImage.pixel(j,i);

                     CV_IMAGE_ELEM(desImage,uchar,i,j*3+0)=qBlue(rgb);

                     CV_IMAGE_ELEM(desImage,uchar,i,j*3+1)=qGreen(rgb);

                     CV_IMAGE_ELEM(desImage,uchar,i,j*3+2)=qRed(rgb);

              }

       }

       return desImage;

}

上一篇:Python / OpenCV:转换从捕获中获取的图像


下一篇:vue 图片懒加载