如何用C++调用halcon函数?

如题所述

    下面是一个用C++调用halcon函数的实例,说明://后面的部分为程序的说明,在程序运行中是不起作用的。

    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()进行翻转。

    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,

    从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);


温馨提示:答案为网友推荐,仅供参考
相似回答