【Halcon】图像BYTE数据生成 Mat 格式和 Hobject 格式

32 篇文章 2 订阅
24 篇文章 9 订阅

BYTE转换成Mat格式

int nImgMatType;
int nTmgMatType;
if ( nImgType == 0 )
{
nImgMatType = CV_8UC1;
}
else if ( nImgType == 1 )
{
nImgMatType = CV_8UC3;
}
else
{
return 1;
}


if ( nTmgType == 0 )
{
nTmgMatType = CV_8UC1;
}
else if ( nTmgType == 1 )
{
nTmgMatType = CV_8UC3;
}
else
{
return 1;
}


CvSize size;
size.width  = (int)((fImgWidth*1000+1)/1000);
size.height = (int)((fImgHeight*1000+1)/1000);


CvSize sizeTemp;
sizeTemp.width  = (int)((fTmgWidth*1000+1)/1000);
sizeTemp.height = (int)((fTmgHeight*1000+1)/1000);


Mat imgMat(size.height, size.width, nImgMatType, image_data);
Mat tmgMat(sizeTemp.height, sizeTemp.width, nTmgMatType, temp_data); 


namedWindow("Source");
imshow("Source", imgMat);
namedWindow("Template");  
imshow("Template", tmgMat);
cvWaitKey();
cvDestroyAllWindows();




BYTE转换成Hobject格式

//--------------------------------//

if ( nImgType == 0 )
{
nImgMatType = 1;

unsigned char *dataGray = new unsigned char [nImgWidth * nImgHeight];


for(int i = 0; i<nImgHeight; i++)

for ( int j=0;j<nImgWidth;j++)
{
dataGray[nImgWidth*i + j] = image_data[(nImgWidth*nImgMatType)*i + j*nImgMatType];
}
}
gen_image1(&Image,"byte",nImgWidth,nImgHeight,(Hlong)(dataGray));


}
else if ( nImgType == 1 )
{
nImgMatType = 3;


unsigned char *dataRed = new unsigned char [nImgWidth * nImgHeight];
unsigned char *dataGreen = new unsigned char [nImgWidth * nImgHeight];
unsigned char *dataBlue = new unsigned char [nImgWidth * nImgHeight];


for(int i = 0; i<nImgHeight; i++)
{
for(int j = 0; j<nImgWidth; j++)
{
dataBlue[nImgWidth*i + j] = image_data[(nImgWidth*nImgMatType)*i + j*nImgMatType + 0];
dataGreen[nImgWidth*i + j] = image_data[(nImgWidth*nImgMatType)*i + j*nImgMatType + 1];
dataRed[nImgWidth*i + j] = image_data[(nImgWidth*nImgMatType)*i + j*nImgMatType + 2];
}
}
gen_image3(&Image,"byte",nImgWidth,nImgHeight,(Hlong)(dataRed),(Hlong)(dataGreen),(Hlong)(dataBlue));


HTuple  Width, Height, WindowHandle;
  if (HDevWindowStack::IsOpen())
  close_window(HDevWindowStack::Pop());
  get_image_size(Image, &Width, &Height);
  set_window_attr("background_color","black");
  open_window(0,0,Width,Height,0,"","",&WindowHandle);
  HDevWindowStack::Push(WindowHandle);
  if (HDevWindowStack::IsOpen())
  disp_obj(Image, HDevWindowStack::GetActive());
write_image(Image, "bmp", 0, "e:/1.bmp");
}
else
{
return 1;
}


//--------------------------------//
if ( nTmgType == 0 )
{
nTmgMatType = 1;


unsigned char *dataGray = new unsigned char [nTmgWidth * nTmgHeight];
for(int i = 0; i<nTmgHeight; i++)

for ( int j=0;j<nTmgWidth;j++)
{
dataGray[nTmgWidth*i + j] = temp_data[(nTmgWidth*nImgMatType)*i + j*nImgMatType];
}
}
gen_image1(&TmpImage,"byte",nTmgWidth,nTmgHeight,(Hlong)(dataGray));
}
else if ( nTmgType == 1 )
{
nTmgMatType = 3;
unsigned char *dataRed = new unsigned char [nTmgWidth * nTmgHeight];
unsigned char *dataGreen = new unsigned char [nTmgWidth * nTmgHeight];
unsigned char *dataBlue = new unsigned char [nTmgWidth * nTmgHeight];


for(int i = 0; i<nTmgHeight; i++)
{
for(int j = 0; j<nTmgWidth; j++)
{
dataBlue[nTmgWidth*i + j] = temp_data[(nTmgWidth*nTmgMatType)*i + j*nTmgMatType + 0];
dataGreen[nTmgWidth*i + j] = temp_data[(nTmgWidth*nTmgMatType)*i + j*nTmgMatType + 1];
dataRed[nTmgWidth*i + j] = temp_data[(nTmgWidth*nTmgMatType)*i + j*nTmgMatType + 2];
}
}
gen_image3(&TmpImage,"byte",nTmgWidth,nTmgHeight,(Hlong)(dataRed),(Hlong)(dataGreen),(Hlong)(dataBlue));


HTuple  Width, Height, WindowHandle;
// if (HDevWindowStack::IsOpen())
// close_window(HDevWindowStack::Pop());
get_image_size(TmpImage, &Width, &Height);
set_window_attr("background_color","black");
open_window(0,0,Width,Height,0,"","",&WindowHandle);
HDevWindowStack::Push(WindowHandle);
if (HDevWindowStack::IsOpen())
disp_obj(TmpImage, HDevWindowStack::GetActive());
write_image(TmpImage, "bmp", 0, "e:/2.bmp");
}
else
{
return 1;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值