请留意:
opencv为3.0.0版本,Qt为4.8.4版本
1 #include"image1.h" 2 #include<QApplication> 3 #include<QImage> 4 #include<QDebug> 5 #include<opencv2/opencv.hpp> 6 7 int main(int argv , char* argc[] ){ 8 9 QApplication a(argv,argc); 10 QImage img; 11 cv::Mat mat = cv::imread("D:\image\4.png",true); 12 if(mat.empty()){ 13 14 qDebug()<<"load image fail!"; 15 return a.exec(); 16 } 17 int chana = mat.channels(); 18 //依据通道数不同,改变不同的装换方式 19 if(3 == chana ){ 20 //调整通道次序 21 cv::cvtColor(mat,mat,CV_BGR2RGB); 22 img = QImage(static_cast<uchar *>(mat.data),mat.cols,mat.rows,QImage::Format_RGB888); 23 }else if(4 == chana ){ 24 //argb 25 img = QImage(static_cast<uchar *>(mat.data),mat.cols,mat.rows,QImage::Format_ARGB32); 26 }else { 27 //单通道,灰度图 28 img = QImage( mat.cols, mat.rows , QImage::Format_Indexed8); 29 uchar * matdata = mat.data; 30 for(int row = 0 ; row <mat.rows ; ++row ){ 31 uchar* rowdata = img.scanLine( row ); 32 memcpy(rowdata,matdata ,mat.cols); 33 matdata+=mat.cols; 34 } 35 } 36 Image1 w ; 37 w.load(img); 38 w.show(); 39 return a.exec(); 40 }
效果图:
用Qimage进行缩放:
当然也可以用opencv自带的imshow进行显示
![](file:///D:Program FilesTencentQQ1005267096ImageC2C90)@@)B$$YI93PQB_8FL4K6.jpg)