QtOpenCV.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2011-2014, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #include "find_object/QtOpenCV.h"
29 #include <opencv2/core/core_c.h>
30 #include <stdio.h>
31 
32 namespace find_object {
33 
34 QImage cvtCvMat2QImage(const cv::Mat & image, bool isBgr)
35 {
36  QImage qtemp;
37  if(!image.empty() && image.depth() == CV_8U)
38  {
39  if(image.channels()==3)
40  {
41  const unsigned char * data = image.data;
42  if(image.channels() == 3)
43  {
44  qtemp = QImage(image.cols, image.rows, QImage::Format_RGB32);
45  for(int y = 0; y < image.rows; ++y, data += image.cols*image.elemSize())
46  {
47  for(int x = 0; x < image.cols; ++x)
48  {
49  QRgb * p = ((QRgb*)qtemp.scanLine (y)) + x;
50  if(isBgr)
51  {
52  *p = qRgb(data[x * image.channels()+2], data[x * image.channels()+1], data[x * image.channels()]);
53  }
54  else
55  {
56  *p = qRgb(data[x * image.channels()], data[x * image.channels()+1], data[x * image.channels()+2]);
57  }
58  }
59  }
60  }
61  }
62  else if(image.channels() == 1)
63  {
64  // mono grayscale
65  qtemp = QImage(image.data, image.cols, image.rows, image.cols, QImage::Format_Indexed8).copy();
66  QVector<QRgb> my_table;
67  for(int i = 0; i < 256; i++) my_table.push_back(qRgb(i,i,i));
68  qtemp.setColorTable(my_table);
69  }
70  else
71  {
72  printf("Wrong image format, must have 1 or 3 channels\n");
73  }
74  }
75  return qtemp;
76 }
77 
78 cv::Mat cvtQImage2CvMat(const QImage & image)
79 {
80  cv::Mat cvImage;
81  if(!image.isNull() && image.depth() == 32 && image.format() == QImage::Format_RGB32)
82  {
83  // assume RGB (3 channels)
84  int channels = 3;
85  cvImage = cv::Mat(image.height(), image.width(), CV_8UC3);
86  unsigned char * data = cvImage.data;
87  for(int y = 0; y < image.height(); ++y, data+=cvImage.cols*cvImage.elemSize())
88  {
89  for(int x = 0; x < image.width(); ++x)
90  {
91  QRgb rgb = image.pixel(x, y);
92  data[x * channels+2] = qRed(rgb); //r
93  data[x * channels+1] = qGreen(rgb); //g
94  data[x * channels] = qBlue(rgb); //b
95  }
96  }
97  }
98  else
99  {
100  printf("Failed to convert image : depth=%d(!=32) format=%d(!=%d)\n", image.depth(), image.format(), QImage::Format_RGB32);
101  }
102  return cvImage;
103 }
104 
105 #if CV_MAJOR_VERSION < 3
106 QImage cvtIplImage2QImage(const IplImage * image)
107 {
108  QImage qtemp;
109  if (image && image->depth == IPL_DEPTH_8U && cvGetSize(image).width>0)
110  {
111  const char * data = image->imageData;
112  qtemp= QImage(image->width, image->height,QImage::Format_RGB32);
113 
114  for(int y = 0; y < image->height; ++y, data +=image->widthStep )
115  {
116  for(int x = 0; x < image->width; ++x)
117  {
118  uint *p = (uint*)qtemp.scanLine (y) + x;
119  *p = qRgb(data[x * image->nChannels+2], data[x * image->nChannels+1],data[x * image->nChannels]);
120  }
121  }
122  }
123  else if(image && image->depth != IPL_DEPTH_8U)
124  {
125  printf("Wrong iplImage format, must be 8_bits\n");
126  }
127  return qtemp;
128 }
129 
130 // Returned image must be released explicitly (using cvReleaseImage()).
131 IplImage * cvtQImage2IplImage(const QImage & image)
132 {
133  IplImage * iplTmp = 0;
134  if(!image.isNull() && image.depth() == 32 && image.format() == QImage::Format_RGB32)
135  {
136  // assume RGB (3 channels)
137  int channels = 3;
138  iplTmp = cvCreateImage(cvSize(image.width(), image.height()), IPL_DEPTH_8U, channels);
139  char * data = iplTmp->imageData;
140  for(int y = 0; y < image.height(); ++y, data+=iplTmp->widthStep)
141  {
142  for(int x = 0; x < image.width(); ++x)
143  {
144  QRgb rgb = image.pixel(x, y);
145  data[x * channels+2] = qRed(rgb); //r
146  data[x * channels+1] = qGreen(rgb); //g
147  data[x * channels] = qBlue(rgb); //b
148  }
149  }
150  }
151  else
152  {
153  printf("Failed to convert image : depth=%d(!=32) format=%d(!=%d)\n", image.depth(), image.format(), QImage::Format_RGB32);
154  }
155  return iplTmp;
156 }
157 #endif
158 
159 } // namespace find_object
FINDOBJECT_EXP cv::Mat cvtQImage2CvMat(const QImage &image)
Definition: QtOpenCV.cpp:78
FINDOBJECT_EXP QImage cvtIplImage2QImage(const IplImage *image)
Definition: QtOpenCV.cpp:106
TFSIMD_FORCE_INLINE const tfScalar & y() const
TFSIMD_FORCE_INLINE const tfScalar & x() const
FINDOBJECT_EXP IplImage * cvtQImage2IplImage(const QImage &image)
Definition: QtOpenCV.cpp:131
unsigned int uint
Definition: Settings.h:47
FINDOBJECT_EXP QImage cvtCvMat2QImage(const cv::Mat &image, bool isBgr=true)
Definition: QtOpenCV.cpp:34


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Mon Jun 10 2019 13:21:31