您的位置:首页 > 移动开发 > Objective-C

【Halcon】获取Hobject的参数[pointer,type,width,height]

2016-06-15 08:48 405 查看
在将halcon程序转换到C++代码后有时我们会经常涉及到Halcon里面的Hobject类型的Image与Opencv里面的IplImage类型的Image之间的相互转换。相互转换分装成了两个函数,具体实现如下:
(1)、从Hobject到IplImage
IplImage* HImageToIplImage(Hobject &Hobj)
{
IplImage* pImage;
HTuple htChannels;
char cType[MAX_STRING];
Hlong width,height;
width=height=0;
//转换图像格式
convert_image_type(Hobj,&Hobj,"byte");
count_channels(Hobj,&htChannels);
if(htChannel[0].I()==1)
{
unsinged char* ptr;
get_image_pointer1(Hobj,(Hlong*)&ptr,cType,&width,&height);
pImage=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
for(int i=0;i<height;i++)
{
memcpy(pImage->imageData+pImage->widthStep*i,ptr+width*i,width);
}
}
if(htChannels[0].I()==3)
{
unsinged char *ptrRed , *ptrGreen , *ptrBlue;
ptrRed=ptrGreen=ptrBlue=NULL;
get_image_pointer3(Hobj,(Hlong*)&ptrRed,(Hlong*)&ptrGreen,(Hlong*)&ptrBlue,cType,&width,&height)
IplImage *pImageRed , *pImageGreen , *pImageBlue ;
pImage=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,3);
pImageRed=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
pImageGreen=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
pImageBlue=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
for(int i=0;i<height;i++)
{
memcpy(pImageRed->imageData+pImageRed->widthStep*i , ptrRed+width*i , width);
memcpy(pImageGreen->imageData+pImageGreen->widthStep*i , ptrGreen+width*i , width);
memcpy(pImageBlue->imageData+pImageBlue->widthStep*i , ptrBlue+width*i , width);
}
cvMerge(pImageBlue,pImageGreen,pImageRed,NULL,pImage);
cvReleaseImage(&pImageRed);
cvReleaseImage(&pImageGreen);
cvReleaseImage(&pImageBlue);
}
return pImage;
}

(2)、从IplImage到Hobject
Hobject IplImageToHImage(IplImage* pImage)
{
Hobject Hobj;
if(pImage->nChannels==1)
{
int height=pImage->height;
int width=pImage->width;
uchar *dataGray=new uchar[width*height];
for(int i=0; i<height; i++)
{
memcpy(dataGray+width*i, pImage->imageData+pImage->widthStep*i,width);
}
gen_image1(&Hobj,"byte",pImage->width,pImage->height,(Hlong)(dataGray));
delete[ ] dataGray;
}
if(pImage->nChannels==3)
{
IplImage *IpImageRed, *IplImageGreen, *IplImageBlue;
IplImageRed=cvCreateImage(cvSize(pImage),IPL_DEPTH_8U,1);
IplImageGreen=cvCreateImage(cvSize(pImage),IPL_DEPTH_8U,1);
IplImageBlue=cvCreateImage(cvSize(pImage),IPL_DEPTH_8U,1);
cvSplit(pImage, pImageBlue, pImageGreen, pImageRed,NULL);
uchar* dataRed=new uchar[pImage->width*pImage->height];
uchar* dataGreen=new uchar[pImage->width*pImage->height];
uchar* dataBlue=new uchar[pImage->width*pImage->height];
int height=pImage->height;
int width=pImage->width;
for(int i=0; i<height; i++)
{
memcpy(dataRed+width*i, pImageRed->imageData+pImageRed->widthStep*i,width);
memcpy(dataGreen+width*i, pImageGreen->imageData+pImageGreen->widthStep*i,width);
memcpy(dataBlue+width*i, pImageBlue->imageData+pImageBlue->widthStep*i,width);
}
gen_image3(&Hobj,"byte",pImage->width,pImage->height,(Hlong)(dataRed),(Hlong)(dataGreen),(Hlong)(dataBlue));
cvReleaseImage(&pImageRed);
cvReleaseImage(&pImageGreen);
cvReleaseImage(&pImageBlue);
delete[ ] dataRed;
delete[ ] dataGreen;
delete[ ] dataBlue;
}
return Hobj;
}

(3)、封装一个画图函数
void DrawPicToHDC(IplImage* img , UINT ID)
{
CDC* pdc=GetDlgItem(ID)->GetDC();
HDC hdc=pdc->GetSafeHdc();
CRect rect;
GetDlgItem(ID)->GetClientRect(&rect);
CvvImage vvimg;
vvimg.CopyOf(img);
vvimg.DrawToHDC(hdc , &rect);
ReleaseDC(pdc);
}

(4)、测试
新建一个MFC对话框项目,添加一个pic控件,ID为IDC_IMG,添加一个图片显示按钮ShowImg,双击按钮添加消息处理函数,如下
建立一个全局变量
IplImage* opencv_image;
void OnBnClickedShowimg()
{
Hobject Image;
read_image(&Image,"E:/.../1.jpg");
opencv_image=HImageToIplImage(Image);
DrawPicToHDC(opencv_image,IDC_IMG);
cvReleaseImage(&opencv_image);
}
如果没有语法输入错误,应该就没问题了,可以将Halcon的图片转换为Opencv的图片,然后画到pic控件上。需要的环境就是要附加上opencv库和halcon库,还要加上CvvImage.h和CvvImage.cpp两个文件。这样就OK啦!

接口函数实现:
open
{
float fVCamUID = config[0];
float fVFrame  = config[1];
float fVShut   = config[2];
float fVGain   = config[3];
float fVResol  = config[4];
float fVBright = config[5];

//Code generated by Image Acquisition 01
if( !bOpenFlag )
{
// 		open_framegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "progressive", -1, "default",
// 			-1, "false", "default", "dca501bdf7cd_MindVision_GE500C", 0, -1, &AcqHandle);
open_framegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "progressive", 8, "gray", -1,
"false", "default", "dca501bdf7cd_MindVision_GE500C", 0, -1, &AcqHandle);
set_framegrabber_param(AcqHandle, "Width", 800);
set_framegrabber_param(AcqHandle, "Height", 600);
grab_image_start(AcqHandle, -1);
bOpenFlag = true;
}

return 0;
}

snap
{
float uid3 = uid;

if( bOpenFlag )
{
grab_image(&camImage, AcqHandle);
HTuple Type, htChannels;
char cType[MAX_STRING];
Hlong width,height;

//转换图像格式
convert_image_type(camImage,&camImage,"byte");
count_channels(camImage,&htChannels);
if(htChannels[0].I()==1)
{
imgType = 0;
unsigned char* ptr;
ptr = NULL;
get_image_pointer1(camImage,(Hlong*)&ptr, cType, &width, &height);
}
if(htChannels[0].I()==3)
{
imgType = 1;
unsigned char *ptrRed , *ptrGreen , *ptrBlue;
ptrRed = ptrGreen = ptrBlue = NULL;
get_image_pointer3(camImage,(Hlong*)&ptrRed,(Hlong*)&ptrGreen,(Hlong*)&ptrBlue,cType,&width,&height);
}
imgWidth  = width;
imgHeight = height;
}

return 0;
}

close
{
float uid3 = uid;
uid3 = uid;
if( bOpenFlag )
{
close_framegrabber(AcqHandle);
bOpenFlag = false;
}
return 0;
}

info
{
float uid3 = uid;

if( bOpenFlag )
{
info[0] = imgType;
info[1] = imgWidth;
info[2] = imgHeight;
}
info[0] = 0;
info[1] = 800;
info[2] = 600;
return 0;
}

data
{
float uid3 = uid;
char cType[MAX_STRING];
Hlong width,height;

if ( 0 == imgType )
{
UINT8* pDst = new UINT8[imgWidth*imgHeight];
unsigned char* ptr;
ptr = NULL;
get_image_pointer1(camImage,(Hlong*)&ptr, cType, &width, &height);
memcpy(pDst, ptr, width*height*sizeof(UINT8));
return pDst;
}

if ( 1 == imgType )
{
UINT8* pDst = new UINT8[imgWidth*imgHeight*3];
unsigned char *ptrRed , *ptrGreen , *ptrBlue;
ptrRed = ptrGreen = ptrBlue = NULL;
get_image_pointer3(camImage,(Hlong*)&ptrRed,(Hlong*)&ptrGreen,(Hlong*)&ptrBlue,cType,&width,&height);

for(int i=0; i < height; i++)
{
for ( int j=0;j < width; j++ )
{
memcpy( pDst + width*i + j,     ptrRed +   width*i + j , sizeof(UINT8));
memcpy( pDst + width*i + j + 1, ptrGreen + width*i + j , sizeof(UINT8));
memcpy( pDst + width*i + j + 2, ptrBlue +  width*i + j , sizeof(UINT8));
}
}
return pDst;
}
return 0;
}

num
{
float uid3 = 1;
unsigned int usNum;
usNum = 1;
return usNum;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: