Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "find_object/QtOpenCV.h"
00029 #include <opencv2/core/core_c.h>
00030 #include <stdio.h>
00031
00032 namespace find_object {
00033
00034 QImage cvtCvMat2QImage(const cv::Mat & image, bool isBgr)
00035 {
00036 QImage qtemp;
00037 if(!image.empty() && image.depth() == CV_8U)
00038 {
00039 if(image.channels()==3)
00040 {
00041 const unsigned char * data = image.data;
00042 if(image.channels() == 3)
00043 {
00044 qtemp = QImage(image.cols, image.rows, QImage::Format_RGB32);
00045 for(int y = 0; y < image.rows; ++y, data += image.cols*image.elemSize())
00046 {
00047 for(int x = 0; x < image.cols; ++x)
00048 {
00049 QRgb * p = ((QRgb*)qtemp.scanLine (y)) + x;
00050 if(isBgr)
00051 {
00052 *p = qRgb(data[x * image.channels()+2], data[x * image.channels()+1], data[x * image.channels()]);
00053 }
00054 else
00055 {
00056 *p = qRgb(data[x * image.channels()], data[x * image.channels()+1], data[x * image.channels()+2]);
00057 }
00058 }
00059 }
00060 }
00061 }
00062 else if(image.channels() == 1)
00063 {
00064
00065 qtemp = QImage(image.data, image.cols, image.rows, image.cols, QImage::Format_Indexed8).copy();
00066 QVector<QRgb> my_table;
00067 for(int i = 0; i < 256; i++) my_table.push_back(qRgb(i,i,i));
00068 qtemp.setColorTable(my_table);
00069 }
00070 else
00071 {
00072 printf("Wrong image format, must have 1 or 3 channels\n");
00073 }
00074 }
00075 return qtemp;
00076 }
00077
00078 cv::Mat cvtQImage2CvMat(const QImage & image)
00079 {
00080 cv::Mat cvImage;
00081 if(!image.isNull() && image.depth() == 32 && image.format() == QImage::Format_RGB32)
00082 {
00083
00084 int channels = 3;
00085 cvImage = cv::Mat(image.height(), image.width(), CV_8UC3);
00086 unsigned char * data = cvImage.data;
00087 for(int y = 0; y < image.height(); ++y, data+=cvImage.cols*cvImage.elemSize())
00088 {
00089 for(int x = 0; x < image.width(); ++x)
00090 {
00091 QRgb rgb = image.pixel(x, y);
00092 data[x * channels+2] = qRed(rgb);
00093 data[x * channels+1] = qGreen(rgb);
00094 data[x * channels] = qBlue(rgb);
00095 }
00096 }
00097 }
00098 else
00099 {
00100 printf("Failed to convert image : depth=%d(!=32) format=%d(!=%d)\n", image.depth(), image.format(), QImage::Format_RGB32);
00101 }
00102 return cvImage;
00103 }
00104
00105
00106 QImage cvtIplImage2QImage(const IplImage * image)
00107 {
00108 QImage qtemp;
00109 if (image && image->depth == IPL_DEPTH_8U && cvGetSize(image).width>0)
00110 {
00111 const char * data = image->imageData;
00112 qtemp= QImage(image->width, image->height,QImage::Format_RGB32);
00113
00114 for(int y = 0; y < image->height; ++y, data +=image->widthStep )
00115 {
00116 for(int x = 0; x < image->width; ++x)
00117 {
00118 uint *p = (uint*)qtemp.scanLine (y) + x;
00119 *p = qRgb(data[x * image->nChannels+2], data[x * image->nChannels+1],data[x * image->nChannels]);
00120 }
00121 }
00122 }
00123 else if(image && image->depth != IPL_DEPTH_8U)
00124 {
00125 printf("Wrong iplImage format, must be 8_bits\n");
00126 }
00127 return qtemp;
00128 }
00129
00130
00131 IplImage * cvtQImage2IplImage(const QImage & image)
00132 {
00133 IplImage * iplTmp = 0;
00134 if(!image.isNull() && image.depth() == 32 && image.format() == QImage::Format_RGB32)
00135 {
00136
00137 int channels = 3;
00138 iplTmp = cvCreateImage(cvSize(image.width(), image.height()), IPL_DEPTH_8U, channels);
00139 char * data = iplTmp->imageData;
00140 for(int y = 0; y < image.height(); ++y, data+=iplTmp->widthStep)
00141 {
00142 for(int x = 0; x < image.width(); ++x)
00143 {
00144 QRgb rgb = image.pixel(x, y);
00145 data[x * channels+2] = qRed(rgb);
00146 data[x * channels+1] = qGreen(rgb);
00147 data[x * channels] = qBlue(rgb);
00148 }
00149 }
00150 }
00151 else
00152 {
00153 printf("Failed to convert image : depth=%d(!=32) format=%d(!=%d)\n", image.depth(), image.format(), QImage::Format_RGB32);
00154 }
00155 return iplTmp;
00156 }
00157
00158 }