QT中图像格式转换 Mat,QImage,Image

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;
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沐雨清风 จุ๊บ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值