#include #include #include #include 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::max(); double globalMax = -std::numeric_limits::max(); std::vector mv; split(mat, mv); for (size_t i=0; i= 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(); }