目录
二、cv::Mat、HalconCpp::HObject格式互转
一、cv::Mat、QImage格式互转
cv::Mat qImage2cvMat(QImage& image)
{
cv::Mat mat;
switch (image.format())
{
case QImage::Format_Grayscale8:
{
mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
return mat.clone();
}
case QImage::Format_RGB888:
{
mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
cv::Mat copyMat = mat.clone();
cv::cvtColor(copyMat, copyMat, CV_BGR2RGB);
return copyMat;
}
case QImage::Format_ARGB32:
{
mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
return mat.clone();
}
default:
{
image = image.convertToFormat(QImage::Format_RGB888);
mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
cv::Mat copyMat = mat.clone();
cv::cvtColor(copyMat, copyMat, CV_BGR2RGB);
return copyMat;
}
}
}
QImage cvMat2qImage(cv::Mat& mat)
{
QImage image;
switch (mat.type())
{
case CV_8UC1:
{
const uchar* pSrc = (const uchar*)mat.data;
image = QImage(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_Grayscale8);
return image;
}
case CV_8UC3:
{
const uchar* pSrc = (const uchar*)mat.data;
image = QImage(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return image.rgbSwapped();
}
case CV_8UC4:
{
const uchar* pSrc = (const uchar*)mat.data;
image = QImage(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
return image.copy();
}
default:
{
mat.convertTo(mat, CV_8UC3);
const uchar* pSrc = (const uchar*)mat.data;
image = QImage(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return image.rgbSwapped();
}
}
}
二、cv::Mat、HalconCpp::HObject格式互转
using namespace HalconCpp;
HObject cvMat2hObject(const cv::Mat& srcMat)
{
HObject hImg;
std::vector<cv::Mat> vecSplitMat;
if (srcMat.type() == CV_8UC3 || srcMat.type() == CV_8UC4)
{
GenImage3(&hImg, "byte", srcMat.cols, srcMat.rows,
(Hlong)(vecSplitMat[2].data),
(Hlong)(vecSplitMat[1].data),
(Hlong)(vecSplitMat[0].data));
}
else if (srcMat.type() == CV_8UC1)
{
GenImage1(&hImg, "byte", srcMat.cols, srcMat.rows,
(Hlong)(vecSplitMat[0].data));
}
return hImg;
}
cv::Mat hObject2cvMat(const HObject& hImg)
{
HString hType;
Hlong hWidth, hHeight;
HTuple hCh;
CountChannels(hImg, &hCh);
cv::Mat rtnMat;
if (hCh.I() == 1)
{
void* hPtr = (HImage(hImg)).GetImagePointer1(&hType, &hWidth, &hHeight);
rtnMat.create(hHeight, hWidth, CV_8UC1);
memcpy(rtnMat.data, hPtr, hWidth * hHeight * sizeof(uchar));
}
else if (hCh.I() == 3)
{
void* rPtr;
void* gPtr;
void* bPtr;
(HImage(hImg)).GetImagePointer3(&rPtr, &gPtr, &bPtr, &hType, &hWidth, &hHeight);
std::vector<cv::Mat> vecMat(3);
vecMat[0].create(hHeight, hWidth, CV_8UC1);
vecMat[1].create(hHeight, hWidth, CV_8UC1);
vecMat[2].create(hHeight, hWidth, CV_8UC1);
int dataSize = hHeight * hWidth * sizeof(uchar);
memcpy(vecMat[2].data, rPtr, dataSize);
memcpy(vecMat[1].data, gPtr, dataSize);
memcpy(vecMat[0].data, bPtr, dataSize);
cv::merge(vecMat, rtnMat);
}
return rtnMat;
}
三、QImage的镜像与旋转
void qImageMirror(QImage* image, bool horizontal)
{
if (horizontal)
*image = image->mirrored(true, false);//水平镜像
else
*image = image->mirrored(false, true);//垂直镜像
}
void qImageRotate(QImage* image, double angle)
{
QMatrix matrix;
matrix.rotate(angle);
*image = image->transformed(matrix, Qt::FastTransformation);
}