QT中图像格式转换 Mat,QImage,Image
#pragma once
#include <opencv2/opencv.hpp>
#include <halconcpp/HalconCpp.h>
#include<qimage.h>
#include <iostream>
using namespace std;
using namespace HalconCpp;
void HImageToMat(HObject &Hobj, cv::Mat &img);
void HLObjectToMat(HObject ho_object, cv::Mat &image);
HObject MatToHImage(cv::Mat& pImage);
QImage MatToQImage(cv::Mat &mat);
cv::Mat QImage2cvMat(QImage image);
#include "TransImageType.h"
void HImageToMat(HObject &Hobj, cv::Mat &img)
{
HTuple htchaannels = HTuple();
HTuple cType;
HTuple width;
HTuple height;
int H, W;
//转换图像格式
//ConvertImageType(Hobj, &Hobj, "byte");
HTuple hisnull;
HObject hnullobj;
GenEmptyObj(&hnullobj);
if (Hobj.IsInitialized())
{
TestEqualObj(Hobj, hnullobj, &hisnull);
if (hisnull == 1)
{
return;
}
}
else
{
return;
}
CountChannels(Hobj, &htchaannels);
//单通道图像
if (htchaannels[0].I() == 1){
HTuple ptr;
GetImagePointer1(Hobj, &ptr, &cType, &width, &height);
H = (Hlong)height;
W = (Hlong)width;
if (cType == "byte")
{
img.create(H, W, CV_8UC1);
}
else if (cType == "uint2")
{
img.create(H, W, CV_16UC1);
}
uchar* pdata = (uchar*)ptr[0].L();
memcpy(img.data, pdata, W*H*sizeof(uchar));
}
//三通道图像
if (htchaannels[0].I() == 3)
{
HTuple ptrR,ptrG,ptrB;
GetImagePointer3(Hobj, &ptrR, &ptrG,&ptrB, &cType, &width,&height);
H = (Hlong)height;
W = (Hlong)width;
img.create(H, W, CV_8UC3);
cv::Mat imgR(H, W, CV_8UC1);
cv::Mat imgG(H, W, CV_8UC1);
cv::Mat imgB(H, W, CV_8UC1);
uchar* ptrRed = (uchar*)ptrR[0].L();
uchar* ptrGreen = (uchar*)ptrG[0].L();
uchar* ptrBlue = (uchar*)ptrB[0].L();
memcpy(imgR.data, ptrRed, (int)(H *W)*sizeof(uchar));
memcpy(imgG.data, ptrGreen, (int)(H *W) * sizeof(uchar));
memcpy(imgB.data, ptrBlue, (int)(H *W) * sizeof(uchar));
std::vector<cv::Mat>mgImage;
mgImage.push_back(imgB);
mgImage.push_back(imgG);
mgImage.push_back(imgR);
merge(mgImage, img);
}
}
void HLObjectToMat(HObject ho_object, cv::Mat &image)
{
HTuple htCh = HTuple();
HTuple cType;
CountChannels(ho_object, &htCh);
HTuple wid;
HTuple hgt;
int W, H;
if (htCh.I() == 1)
{
HTuple ptr;
GetImagePointer1(ho_object, &ptr, &cType, &wid, &hgt);
W = (Hlong)wid;
H = (Hlong)hgt;
image.create(H, W, CV_32FC1);
//image.create(H, W, CV_16SC1);
float *pdata;
pdata = (float*)(ptr[0].L());
memcpy(image.data, pdata, W*H * sizeof(float));
}
else if (htCh.I() == 3)
{
HObject X, Y, Z;
Decompose3(ho_object, &X, &Y, &Z);
HTuple ptr_x, ptr_y, ptr_z;
GetImagePointer1(X, &ptr_x, &cType, &wid, &hgt);
GetImagePointer1(Y, &ptr_y, &cType, &wid, &hgt);
GetImagePointer1(Z, &ptr_z, &cType, &wid, &hgt);
W = (Hlong)wid;
H = (Hlong)hgt;
float *pdata_x, *pdata_y, *pdata_z;
pdata_x = (float*)(ptr_x[0].L());
pdata_y = (float*)(ptr_y[0].L());
pdata_z = (float *)(ptr_z[0].L());
image.create(H, W, CV_32FC3);
std::vector<cv::Mat> vecM(3);
vecM[2].create(H, W, CV_32FC1);
vecM[1].create(H, W, CV_32FC1);
vecM[0].create(H, W, CV_32FC1);
memcpy(vecM[2].data, pdata_x, W*H * sizeof(float));
memcpy(vecM[1].data, pdata_y, W*H * sizeof(float));
memcpy(vecM[0].data, pdata_z, W*H * sizeof(float));
merge(vecM, image);
}
}
HObject MatToHImage(cv::Mat& pImage)
{
if (pImage.empty()) {
HObject ho_Img;
GenEmptyObj(&ho_Img);
return ho_Img;
}
HObject Hobj;
GenEmptyObj(&Hobj);
if (pImage.channels() == 1)
{
int height = pImage.rows;
int width = pImage.cols;
if (pImage.depth() == CV_8U)
{
uchar* dataGray = new uchar[width * height];
for (int i = 0; i < height; i++)
{
memcpy(dataGray + width * i, pImage.data + pImage.step * i, width);
}
GenImage1(&Hobj, "byte", pImage.cols, pImage.rows, (Hlong)(dataGray));
delete[] dataGray;
dataGray = nullptr;
}
else if (pImage.depth() == CV_16U)
{
uchar* dataGray = new uchar[width * height * 2];
for (int i = 0; i < height; i++)
{
memcpy(dataGray + width * i * 2, pImage.data + pImage.step * i, width * 2);
}
GenImage1(&Hobj, "uint2", pImage.cols, pImage.rows, (Hlong)(dataGray));
delete[] dataGray;
dataGray = nullptr;
}
}
if (pImage.channels() == 3)
{
int height = pImage.rows;
int width = pImage.cols;
cv::Mat ImageRed, ImageGreen, ImageBlue;
ImageRed = cv::Mat(height, width, CV_8UC1);
ImageGreen = cv::Mat(height, width, CV_8UC1);
ImageBlue = cv::Mat(height, width, CV_8UC1);
cv::Mat ImageChannels[3];
split(pImage, ImageChannels);
ImageBlue = ImageChannels[0];
ImageGreen = ImageChannels[1];
ImageRed = ImageChannels[2];
uchar* dataRed = new uchar[pImage.cols*pImage.rows];
uchar* dataGreen = new uchar[pImage.cols*pImage.rows];
uchar* dataBlue = new uchar[pImage.cols*pImage.rows];
for (int i = 0; i < height; i++){
memcpy(dataRed + width * i, ImageRed.data + ImageRed.step*i, width);
memcpy(dataGreen + width * i, ImageGreen.data + ImageGreen.step*i, width);
memcpy(dataBlue + width * i, ImageBlue.data + ImageBlue.step*i, width);
}
GenImage3(&Hobj, "byte", pImage.cols, pImage.rows, (Hlong)(dataRed), (Hlong)(dataGreen), (Hlong)(dataBlue));
delete[] dataRed;
delete[] dataGreen;
delete[] dataBlue;
dataRed = nullptr;
dataGreen = nullptr;
dataBlue = nullptr;
}
return Hobj;
}
//======================================================================
// FUNCTION: Mat to QImage
//======================================================================
QImage MatToQImage(cv::Mat &mat)
{
QImage img;
int nChannel = mat.channels();
if (nChannel == 3)
{
//cv::cvtColor(mat, mat, CV_BGR2RGB);
img = QImage((const unsigned char*)mat.data, mat.cols, mat.rows, mat.cols*mat.channels(), QImage::Format_RGB888);
}
else if (nChannel == 4 || nChannel == 1)
{
img = QImage((const unsigned char*)mat.data, mat.cols, mat.rows, mat.cols*mat.channels(), QImage::Format_Indexed8);//Format_ARGB32
}
return img;
}
//======================================================================
// FUNCTION: QImage to Mat
//======================================================================
cv::Mat QImage2cvMat(QImage image)
{
cv::Mat mat;
//qDebug() << image.format();
switch (image.format())
{
case QImage::Format_ARGB32:
case QImage::Format_RGB32:
case QImage::Format_ARGB32_Premultiplied:
mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
break;
case QImage::Format_RGB888:
mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
//cv::cvtColor(mat, mat, CV_BGR2RGB);
break;
case QImage::Format_Indexed8:
mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
break;
}
return mat;
}