下午打算把这个类封装下,封装成dll形式。
#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; }