utility.cpp
1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <QImage>
#include <limits>
#include <vector>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
/**** STATIC ****/
QImage toQImage(const Mat &mat)
{
// Convert to 8U depth
Mat mat8u;
if (mat.depth() != CV_8U) {
double globalMin = std::numeric_limits<double>::max();
double globalMax = -std::numeric_limits<double>::max();
std::vector<Mat> mv;
split(mat, mv);
for (size_t i=0; i<mv.size(); i++) {
double min, max;
minMaxLoc(mv[i], &min, &max);
globalMin = std::min(globalMin, min);
globalMax = std::max(globalMax, max);
}
assert(globalMax >= globalMin);
double range = globalMax - globalMin;
if (range != 0) {
double scale = 255 / range;
convertScaleAbs(mat, mat8u, scale, -(globalMin * scale));
} else {
// Monochromatic
mat8u = Mat(mat.size(), CV_8UC1, Scalar((globalMin+globalMax)/2));
}
} else {
mat8u = mat;
}
// Convert to 3 channels
Mat mat8uc3;
if (mat8u.channels() == 4) cvtColor(mat8u, mat8uc3, CV_BGRA2RGB);
else if (mat8u.channels() == 3) cvtColor(mat8u, mat8uc3, CV_BGR2RGB);
else if (mat8u.channels() == 1) cvtColor(mat8u, mat8uc3, CV_GRAY2RGB);
return QImage(mat8uc3.data, mat8uc3.cols, mat8uc3.rows, 3*mat8uc3.cols, QImage::Format_RGB888).copy();
}