Halcon与C++图像数据结构转换

参考地址:http://tsindahui.blog.sohu.com/151668003.html

1.   从Halcon到VC++

      read_image(&Image,"文件名");//读入的为灰度图像

      //获取图像指针,注意输出变量的类型

char lpcsType[MAX_STRING];

Hlong Pointer,Width, Height;

get_image_pointer1(Image, &Pointer, lpcsType, &Width, &Height);

//Halcon与VC++中的图像之间,存在着上下翻转

BYTE * lpByte;

BYTE * ImageG;

int bytewidth;

bytewidth = ((long) Width * 3 + 3 ) / 4 * 4 ;

ImageG = NULL ;

ImageG = new BYTE[ bytewidth * (long) Height ];

lpByte = (BYTE *) Pointer;       //注意结合图像像素存储的类型进行定义

int i,j;

for( j = (long)Height-1; j>=0; j--)

{            //(注意tuple中图像数据的存放和VC中的差别)

              for( i = 0; i < (long)WidthGray; i++)

              {

                     * (ImageG + j * bytewidth + i * 3 + 0 ) = * lpByte ;

                     * (ImageG + j * bytewidth + i * 3 + 1 ) = * lpByte ;

                     * (ImageG + j * bytewidth + i * 3 + 2 ) = * lpByte ;

                     lpByte++;

              }

}

BITMAPINFO * RotateBmpInfo;

BYTE * bitBuffer;

bitBuffer = NULL;

bitBuffer = new BYTE[sizeof(BITMAPINFO)];

RotateBmpInfo = (BITMAPINFO *)bitBuffer;

RotateBmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);

RotateBmpInfo->bmiHeader.biHeight      = Height;

RotateBmpInfo->bmiHeader.biWidth      = Width;

RotateBmpInfo->bmiHeader.biPlanes      = 1;

RotateBmpInfo->bmiHeader.biBitCount = 24;

RotateBmpInfo->bmiHeader.biCompression   = BI_RGB;

RotateBmpInfo->bmiHeader.biSizeImage       = Height * bytewidth;

RotateBmpInfo->bmiHeader.biXPelsPerMeter= 0;

RotateBmpInfo->bmiHeader.biYPelsPerMeter= 0;

RotateBmpInfo->bmiHeader.biClrUsed          = 0;

RotateBmpInfo->bmiHeader.biClrImportant   = 0;

CWnd * m_pWnd ;

m_pWnd = AfxGetApp()->GetMainWnd();

CDC *pDC = m_pWnd->GetDC();

::StretchDIBits(

                            pDC->GetSafeHdc(),

                            Width + 10,

                            Height + 10,

                            Width,                        //显示窗口宽度

                            Height,                        //显示窗口高度

                            0,

                            0,

                            Width,                        //图像宽度

                            Height,                        //图像高度

                            ImageG,               

                            RotateBmpInfo,                

                            DIB_RGB_COLORS,

                            SRCCOPY);

m_pWnd->ReleaseDC(pDC);

delete [] ImageG ;

delete [] bitBuffer ;

2.   从VC++到Halcon

unsigned char *Pointer;        int width, height;        Pointer = new unsigned char[width * height];       int i, j;        for (i=0; i<height; i++)        {          for (j=0; j<width; j++)         {           Pointer[i*width+j] = j % 255;        }       }       Hobject Image;       gen_image1_extern(&Image, "byte", (HTuple)width, (HTuple)height, (long)Pointer, NULL);

注:

a)   gen_image1_extern函数中的变量width,height必须为HTuple类型,Pointer指针为unsigned char类型,输入时转换为long型。

b)   width, height必须与Pointer指向的图像数据的长宽一致。

c)   Pointer指针在gen_image1_extern函数调用之前分配了内存,之后不要马上释放,否则会出错。应该在确保不再使用Image变量之后再释放。halcon内部会自动释放Image,感觉没有释放Pointer(还需要进一步验证)。

d)   显示图像时,可能存在着图像的上下翻转,可以按照1中的方法,将图像数据翻转后再调用gen_image1_extern,或者使用halcon中的函数mirror_image()进行翻转。

3.   在VC界面中建立图像窗口

Hlong lWWindowID;

HTuple WindowHandle;

lWWindowID = (Hlong)m_hWnd; //要显示图片的控件的句柄 set_window_attr("border_width",0); //设置窗口属性 set_window_attr("background_color","light gray"); //设置窗口背景颜色 set_check("~father"); open_window(0,0,m_Width,m_Height,lWWindowID,"visible","",&WindowHandle); //创建窗口 set_check("father"); set_part(WindowHandle,0,0,m_Width-1,m_Height-1); //对窗口上显示图像和区域的一些设置 set_draw(WindowHandle,"margin"); set_colored(WindowHandle,12);

disp_obj(Image,WindowHandle); //显示图像Image(Hobject类型)

4.   从HTuple类型读取数据

//HTuple有一个元素

HTuple aa = 120; double dd = aa[0].D(); // dd=120.000 int ii = aa[0].I(); //ii=120 long ll = aa[0].L(); //ll=120 Hlong hh = aa[0].L();//hh=120 long num = aa.Num(); //num =1; aa = "120"; //HTuple为字符串时,如果赋值不是字符串,不能调用S()函数 const char *cc; cc = aa[0].S(); //cc[0]='1',cc[1]='2',cc[2]='0'

//当aa为多元素的数组时

aa[1] = 230; num = aa.Num(); //num =2; ii = aa[1].I(); //ii=230   //其他获取数据的方法与上面类似

posted @ 2012-03-16 17:19  Jane_bai  阅读(4563)  评论(1编辑  收藏  举报