Halcon与Opencv图像数据格式转换

// halcon_opencv.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include <HalconCpp.h>
#include <HalconCDefs.h>
#include <HProto.h>

Hobject IplImageToHImage(cv::Mat& pImage);
cv::Mat HImageToIplImage(Hobject &Hobj);

int _tmain(int argc, _TCHAR* argv[])
{
	Hobject Image, GrayImage;
	read_image(&Image, "1.jpg");;
	rgb1_to_gray(Image, &GrayImage);
	cv::Mat opencvImg = HImageToIplImage(GrayImage);
	return 0;
}

cv::Mat HImageToIplImage(Hobject &Hobj)
{
	cv::Mat pImage;
	Hlong htChannels;
	char cType[MAX_STRING];
	Hlong     width, height;
	width = height = 0;
	//转换图像格式
	convert_image_type(Hobj, &Hobj, "byte");
	count_channels(Hobj, &htChannels);
	if (htChannels == 1)
	{
		unsigned char *ptr;
		get_image_pointer1(Hobj, (Hlong *)&ptr, cType, &width, &height);
		pImage = cv::Mat(height, width, CV_8UC1);
		memcpy(pImage.data, ptr, width * height);

	}
	else if (htChannels == 3)
	{

		unsigned char *ptrRed, *ptrGreen, *ptrBlue;
		ptrRed = ptrGreen = ptrBlue = NULL;
		get_image_pointer3(Hobj, (Hlong *)&ptrRed, (Hlong *)&ptrGreen, (Hlong *)&ptrBlue, cType, &width, &height);
		pImage = cv::Mat(height, width, CV_8UC3);
		for (int row = 0; row < height; row++)
		{
			uchar* ptr = pImage.ptr<uchar>(row);
			for (int col = 0; col < width; col++)
			{
				ptr[3 * col] = ptrBlue[row * width + col];
				ptr[3 * col + 1] = ptrGreen[row * width + col];
				ptr[3 * col + 2] = ptrRed[row * width + col];
			}
		}

	}

	return pImage;
}

Hobject IplImageToHImage(cv::Mat& pImage)
{
	Hobject Hobj = NULL;
	if (3 == pImage.channels())
	{
		cv::Mat pImageRed, pImageGreen, pImageBlue;
		std::vector<cv::Mat> sbgr(3);
		cv::split(pImage, sbgr);

		int length = pImage.rows * pImage.cols;
		uchar *dataBlue = new uchar[length];
		uchar *dataGreen = new uchar[length];
		uchar *dataRed = new uchar[length];

		int height = pImage.rows;
		int width = pImage.cols;
		for (int row = 0; row <height; row++)
		{
			uchar* ptr = pImage.ptr<uchar>(row);
			for (int col = 0; col < width; col++)
			{
				dataBlue[row * width + col] = ptr[3 * col];
				dataGreen[row * width + col] = ptr[3 * col + 1];
				dataRed[row * width + col] = ptr[3 * col + 2];
			}
		}

		gen_image3(&Hobj, "byte", width, height, (Hlong)(dataRed), (Hlong)(dataGreen), (Hlong)(dataBlue));
		delete[] dataRed;
		delete[] dataGreen;
		delete[] dataBlue;
	}
	else if (1 == pImage.channels())
	{
		int height = pImage.rows;
		int width = pImage.cols;
		uchar *dataGray = new uchar[width * height];
		memcpy(dataGray, pImage.data, width * height);
		gen_image1(&Hobj, "byte", width, height, (Hlong)(dataGray));
		delete[] dataGray;
	}

	return Hobj;
}

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值