QtOpenCV.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2011-2014, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007     * Redistributions of source code must retain the above copyright
00008       notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright
00010       notice, this list of conditions and the following disclaimer in the
00011       documentation and/or other materials provided with the distribution.
00012     * Neither the name of the Universite de Sherbrooke nor the
00013       names of its contributors may be used to endorse or promote products
00014       derived from this software without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00017 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
00020 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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                         // mono grayscale
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                 // assume RGB (3 channels)
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); //r
00093                                 data[x * channels+1] = qGreen(rgb); //g
00094                                 data[x * channels] = qBlue(rgb); //b
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 // Returned image must be released explicitly (using cvReleaseImage()).
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                 // assume RGB (3 channels)
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); //r
00146                                 data[x * channels+1] = qGreen(rgb); //g
00147                                 data[x * channels] = qBlue(rgb); //b
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 } // namespace find_object


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Thu Aug 27 2015 13:00:33