15 #ifndef FRAMEQIMAGECONVERTER_H_
16 #define FRAMEQIMAGECONVERTER_H_
18 #include <QtGui/QImage>
23 #include <opencv2/core/core.hpp>
24 #include <opencv2/imgproc/imgproc.hpp>
25 #include <opencv2/highgui/highgui.hpp>
29 #if CV_MAJOR_VERSION < 3
30 #include <opencv2/contrib/contrib.hpp>
33 namespace frame_helper
37 static const float MAX_H = 10.0f;
56 void hsvToRgb(
float h,
float s,
float v,
int &r,
int &g,
int &b)
60 float p = v * (1 - s);
61 float q = v * (1 - f * s);
62 float t = v * (1 - (1 - f) * s);
64 case 0: r = v*255; g = t*255; b = p*255;
break;
65 case 1: r = q*255; g = v*255; b = p*255;
break;
66 case 2: r = p*255; g = v*255; b = t*255;
break;
67 case 3: r = p*255; g = q*255; b = v*255;
break;
68 case 4: r = t*255; g = p*255; b = v*255;
break;
69 case 5: r = v*255; g = p*255; b = q*255;
break;
76 int copyFrameToQImageRGB888(QImage &dst,base::samples::frame::frame_mode_t mode,
int pixel_size,
unsigned int width,
unsigned int height,
const char* pbuffer,
const size_t buffer_size=0)
79 if(width == 0 || height == 0 || buffer_size == 0)
88 if((
unsigned int) dst.width() != width || (
unsigned int) dst.height()!= height || dst.format() != QImage::Format_RGB888)
90 dst = QImage(width,height,QImage::Format_RGB888);
95 case base::samples::frame::MODE_UNDEFINED:
96 throw std::runtime_error(
" FrameQImageConverter::copyFrameToQImageRGB888: Unknown frame mode!");
99 case base::samples::frame::MODE_BAYER_RGGB:
100 case base::samples::frame::MODE_BAYER_GRBG:
101 case base::samples::frame::MODE_BAYER_BGGR:
102 case base::samples::frame::MODE_BAYER_GBRG:
106 case base::samples::frame::MODE_BGR:
110 dst = QImage((
const uchar*)pbuffer, width, height, width*pixel_size, QImage::Format_RGB888);
111 dst = dst.rgbSwapped();
114 case base::samples::frame::MODE_RGB:
118 dst = QImage((
const uchar*)pbuffer, width, height, width*pixel_size, QImage::Format_RGB888).copy();
120 case base::samples::frame::MODE_PJPG:
125 case base::samples::frame::MODE_JPEG:
130 case base::samples::frame::MODE_UYVY:
140 uint8_t u,v,y1,y2,cb,cr,r1,r2,b1,b2,g1,g2;
141 for(i = 0 ;i < height ;++i){
142 uint8_t* output_line = dst.scanLine(i);
143 for(j = 0 ;j < width ;j+=2){
144 u = pbuffer[(i*width*2)+j*2+0];
145 y1 = pbuffer[(i*width*2)+j*2+1];
146 v = pbuffer[(i*width*2)+j*2+2];
147 y2 = pbuffer[(i*width*2)+j*2+3];
153 r1 = (255/219)*(y1-16) + (255/112)*0.701*(cr-128);
154 g1 = (255/219)*(y1-16) + (255/112)*0.886*(0.114/0.587)*(cb-128)-(255/112)*0.701*(0.299/0.587)*(cr-128);
155 b1 = (255/219)*(y1-16) + (255/112)*0.886*(cb-128);
157 r2 = (255/219)*(y2-16) + (255/112)*0.701*(cr-128);
158 g2 = (255/219)*(y2-16) + (255/112)*0.886*(0.114/0.587)*(cb-128)-(255/112)*0.701*(0.299/0.587)*(cr-128);
159 b2 = (255/219)*(y2-16) + (255/112)*0.886*(cb-128);
161 output_line[j*3+0] = r1;
162 output_line[j*3+1] = g1;
163 output_line[j*3+2] = b1;
164 output_line[j*3+3] = r2;
165 output_line[j*3+4] = g2;
166 output_line[j*3+5] = b2;
171 case base::samples::frame::MODE_GRAYSCALE:
176 dst = QImage((uchar*)pbuffer, width, height, width, QImage::Format_Indexed8).convertToFormat(QImage::Format_RGB888);
180 dst = QImage(width, height, QImage::Format_RGB888);
184 const cv::Mat mat_src(height,width,CV_16U,(
void*)pbuffer);
185 cv::Mat src_new(height,width,CV_8U);
186 cv::MatConstIterator_<uint16_t> it_src = mat_src.begin<uint16_t>();
187 cv::MatIterator_<uint8_t> it_dst = src_new.begin<uint8_t>();
190 for (; it_src != mat_src.end<uint16_t>(); ++it_src, ++it_dst)
192 uint16_t v = *it_src;
193 static double base = 2.2;
194 double v_log = log((
double)v+1) / log(base);
195 (*it_dst) = v_log*(255.0/ log((
double)65536) / log(base));
198 cv::Mat mat_dst(height,width,CV_8UC3);
199 cv::applyColorMap(src_new,mat_dst,cv::COLORMAP_RAINBOW);
202 cv::Vec3b black = cv::Vec3b(0, 0, 255);
203 for (cv::Mat3b::iterator it = mat_dst.begin<cv::Vec3b>(); it != mat_dst.end<cv::Vec3b>(); ++it)
207 *it = cv::Vec3b(0, 0, 0);
210 dst = QImage((
const uchar*)mat_dst.data, width, height, width*3, QImage::Format_RGB888).copy();
216 static const float SCALE_H = 1/MAX_H;
217 static const double SCALE_S = 1/floor(std::numeric_limits<double>::max()*SCALE_H);
219 int pixels = width*height;
220 double *data = (
double*)pbuffer;
221 unsigned char* data2 = dst.bits();
228 for(
int i=0;i < pixels;++i,++data)
231 if(fabs(*data) == std::numeric_limits<double>::infinity())
242 h = ::fmod(*data,MAX_H)*SCALE_H;
243 s = 1-floor(*data*SCALE_H)*SCALE_S;
247 h = ::fmod(-*data,MAX_H)*SCALE_H;
248 s = 1-floor(-*data*SCALE_H)*SCALE_S;
253 *data2 = (
unsigned char) r;
255 *data2 = (
unsigned char) g;
257 *data2 = (
unsigned char) b;
263 throw std::runtime_error(
"Pixel size is not supported by FrameQImageConverter!");
267 case base::samples::frame::MODE_RGB32:
272 dst = QImage((uchar*)pbuffer, width, height, width, QImage::Format_RGB32).convertToFormat(QImage::Format_RGB888);
277 case base::samples::frame::MODE_PNG:
279 char *buffer =
const_cast<char*
>(pbuffer);
280 const cv::Mat input(1,buffer_size,CV_8UC1,buffer);
281 cv::Mat img = cv::imdecode(input,CV_LOAD_IMAGE_COLOR);
282 dst = QImage((
const uchar*)img.data,img.cols,img.rows,QImage::Format_RGB888);
283 dst = dst.rgbSwapped();
288 throw std::runtime_error(
" FrameQImageConverter::copyFrameToQImageRGB888: Can not convert frame to RGB888!");
300 (
const char*)frame.getImageConstPtr(),
304 int copyFrameToQImageRGB888(QImage &dst,
const QString &mode,
int pixel_size,
int width,
int height,
const char* pbuffer,
const size_t size=0)
306 base::samples::frame::frame_mode_t _mode = base::samples::frame::Frame::toFrameMode(mode.toStdString());
int copyFrameToQImageRGB888(QImage &dst, const base::samples::frame::Frame &frame)
Definition: FrameQImageConverter.h:293
virtual ~FrameQImageConverter()
Definition: FrameQImageConverter.h:43
Definition: FrameQImageConverter.h:38
void hsvToRgb(float h, float s, float v, int &r, int &g, int &b)
Definition: FrameQImageConverter.h:56
static void convertPJPGToRGB24(const base::samples::frame::Frame &src, base::samples::frame::Frame &dst)
Definition: FrameHelper.cpp:1058
int copyFrameToQImageRGB888(QImage &dst, base::samples::frame::frame_mode_t mode, int pixel_size, unsigned int width, unsigned int height, const char *pbuffer, const size_t buffer_size=0)
Definition: FrameQImageConverter.h:76
int copyFrameToQImageRGB888(QImage &dst, const QString &mode, int pixel_size, int width, int height, const char *pbuffer, const size_t size=0)
Definition: FrameQImageConverter.h:304
static void convertBayerToRGB24(const base::samples::frame::Frame &src, base::samples::frame::Frame &dst)
Definition: FrameHelper.cpp:865
FrameQImageConverter()
Definition: FrameQImageConverter.h:41
static void convertJPEGToRGB24(uint8_t const *src, uint8_t *dst, size_t const src_size, int const width, int const height)
Definition: FrameHelper.cpp:1102