|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
|
|
#include <opencv2\opencv.hpp>
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
#include "GSurface.h"
|
|
|
|
|
|
|
|
|
|
|
|
class ImageUtils
|
|
|
|
|
|
{
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>mat <20>ڰ<EFBFBD>ͼ
|
|
|
|
|
|
static void convertSurfaceToMat(GSurface* pSrcSurf, float zlower, float zupper, cv::Mat& dstMat)
|
|
|
|
|
|
{
|
|
|
|
|
|
int nx = pSrcSurf->XNum() - 1;
|
|
|
|
|
|
int ny = pSrcSurf->YNum() - 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Mat<61><74><EFBFBD>죺<EFBFBD><ECA3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>洢<EFBFBD>ṹ<EFBFBD><E1B9B9><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>stepÿ<70>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>ֽ<EFBFBD>
|
|
|
|
|
|
dstMat = cv::Mat::zeros(ny, nx, CV_8UC1); //ȱʡ<C8B1><CAA1>ɫ
|
|
|
|
|
|
|
|
|
|
|
|
float v;
|
|
|
|
|
|
zlower -= 1e-4;
|
|
|
|
|
|
zupper += 1e-4;
|
|
|
|
|
|
for (int j = 0; j < ny; j++)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (int i = 0; i < nx; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
v = pSrcSurf->Z(i, j);
|
|
|
|
|
|
if (v > zlower && v < zupper)
|
|
|
|
|
|
{
|
|
|
|
|
|
dstMat.at<uchar>(j, i) = 255; // <20><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD>ֵΪ<D6B5><CEAA>ɫ
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//mat ת<><D7AA>Ϊ surface
|
|
|
|
|
|
static bool convertMatToSurface(cv::Mat& mat, float x0, float y0, float deltX, float deltY, GSurface* pDstSurf)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
int nx = mat.cols;
|
|
|
|
|
|
int ny = mat.rows;
|
|
|
|
|
|
if (nx < 2 || ny < 2)
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
pDstSurf->Create(nx, ny, x0, y0, deltX, deltY);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (mat.type())
|
|
|
|
|
|
{
|
|
|
|
|
|
case CV_8UC1:
|
|
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < ny; j++)
|
|
|
|
|
|
{
|
|
|
|
|
|
uchar* p = mat.ptr<uchar>(j);
|
|
|
|
|
|
for (int i = 0; i < nx; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
pDstSurf->setZ(i, j, (unsigned int)p[i]);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
case CV_32FC1: //float
|
|
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < ny; j++)
|
|
|
|
|
|
{
|
|
|
|
|
|
float* p = mat.ptr<float>(j);
|
|
|
|
|
|
for (int i = 0; i < nx; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
pDstSurf->setZ(i, j, (float)p[i]);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pDstSurf->CalcRange();
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Mat ת<><D7AA>ΪQImage
|
|
|
|
|
|
static bool ConvertMatToQImage(cv::Mat& mat, QImage& image, QImage::Format fmt = QImage::Format_ARGB32_Premultiplied)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (mat.empty()) {
|
|
|
|
|
|
qDebug() << "load image fail!";
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch (mat.type())
|
|
|
|
|
|
{
|
|
|
|
|
|
case CV_8UC1:
|
|
|
|
|
|
{
|
|
|
|
|
|
image = QImage(mat.cols, mat.rows, QImage::Format_Indexed8);
|
|
|
|
|
|
//set the color table
|
|
|
|
|
|
image.setColorCount(256);
|
|
|
|
|
|
for (int i = 0; i < 256; i++)
|
|
|
|
|
|
image.setColor(i, qRgb(i, i, i));
|
|
|
|
|
|
|
|
|
|
|
|
//copy input mat
|
|
|
|
|
|
uchar* pSrc = mat.data;
|
|
|
|
|
|
for (int row = 0; row < mat.rows; row++)
|
|
|
|
|
|
{
|
|
|
|
|
|
uchar* pDest = image.scanLine(row);
|
|
|
|
|
|
memcpy(pDest, pSrc, mat.cols);
|
|
|
|
|
|
pSrc += mat.step;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
case CV_8UC3:
|
|
|
|
|
|
image = QImage((const unsigned char*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
|
|
|
|
|
|
image = image.rgbSwapped(); // BRGתΪRGB
|
|
|
|
|
|
// Qt5.14<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Format_BGR888
|
|
|
|
|
|
// image = QImage((const unsigned char*)mat.data, mat.cols, mat.rows, mat.cols * 3, QImage::Format_BGR888);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case CV_8UC4:
|
|
|
|
|
|
image = QImage((const unsigned char*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case CV_16UC4:
|
|
|
|
|
|
image = QImage((const unsigned char*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGBA8888);// Format_RGBA64);
|
|
|
|
|
|
image = image.rgbSwapped(); // BRGתΪRGB
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!image.isNull())
|
|
|
|
|
|
image = image.convertToFormat(fmt);
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//QImageת<65><D7AA>ΪMat
|
|
|
|
|
|
static bool ConvertQImageToMat(const QImage& image, cv::Mat& mat)
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (image.format())
|
|
|
|
|
|
{
|
|
|
|
|
|
case QImage::Format_Grayscale8: // <20>Ҷ<EFBFBD>ͼ<EFBFBD><CDBC>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>1<EFBFBD><31><EFBFBD>ֽڣ<D6BD>8λ<38><CEBB>
|
|
|
|
|
|
// Mat<61><74><EFBFBD>죺<EFBFBD><ECA3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>洢<EFBFBD>ṹ<EFBFBD><E1B9B9><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>stepÿ<70>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>ֽ<EFBFBD>
|
|
|
|
|
|
mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
|
|
|
|
|
|
break;
|
|
|
|
|
|
case QImage::Format_ARGB32: // uint32<33>洢0xAARRGGBB<42><42>pcһ<63><D2BB>С<EFBFBD>˴洢<CBB4><E6B4A2>λ<EFBFBD><CEBB>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>˳<EFBFBD><CBB3><EFBFBD>ͳ<EFBFBD><CDB3><EFBFBD>BGRA
|
|
|
|
|
|
case QImage::Format_RGB32: // AlphaΪFF
|
|
|
|
|
|
case QImage::Format_ARGB32_Premultiplied:
|
|
|
|
|
|
mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
|
|
|
|
|
|
break;
|
|
|
|
|
|
case QImage::Format_RGB888: // RR,GG,BB<42>ֽ<EFBFBD>˳<EFBFBD><CBB3><EFBFBD>洢
|
|
|
|
|
|
mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
|
|
|
|
|
|
// opencv<63><76>ҪתΪBGR<47><52><EFBFBD>ֽ<EFBFBD>˳<EFBFBD><CBB3>
|
|
|
|
|
|
cv::cvtColor(mat, mat, cv::COLOR_RGB2BGR);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case QImage::Format_Indexed8:
|
|
|
|
|
|
mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.bits(), image.bytesPerLine());
|
|
|
|
|
|
break;
|
|
|
|
|
|
//case QImage::Format_RGBA64: // uint64<36>洢<EFBFBD><E6B4A2>˳<EFBFBD><CBB3><EFBFBD><EFBFBD>Format_ARGB32<33>෴<EFBFBD><E0B7B4>RGBA
|
|
|
|
|
|
// mat = cv::Mat(image.height(), image.width(), CV_16UC4, (void*)image.constBits(), image.bytesPerLine());
|
|
|
|
|
|
// // opencv<63><76>ҪתΪBGRA<52><41><EFBFBD>ֽ<EFBFBD>˳<EFBFBD><CBB3>
|
|
|
|
|
|
// cv::cvtColor(mat, mat, cv::COLOR_RGBA2BGRA);
|
|
|
|
|
|
// break;
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|