MFC窗口怎么显示halcon采集到的图片

在halcon上连接相机采集了图像,之后把这个部分的cpp代码导出了,如下:
void action()
{

// Local iconic variables
HObject ho_Image;

// Local control variables
HTuple hv_AcqHandle;

//Image Acquisition 01: Code generated by Image Acquisition 01
OpenFramegrabber("USB3Vision", 0, 0, 0, 0, 0, 0, "progressive", -1, "default", -1, "false", "default", "2BA200001454_DahengImaging_MER050560U3C", 0, -1, &hv_AcqHandle);
GrabImageStart(hv_AcqHandle, -1);
while (0 != 1)
{
GrabImageAsync(&ho_Image, hv_AcqHandle, -1);
}
CloseFramegrabber(hv_AcqHandle);
}

想新建一个MFC文件,之后在串口设一个按钮,把上述代码放到按钮代码里,但是问题是怎么才能让采集到的图像显示在窗口的PictureControl控件上呢?
查阅了一些资料有的说用OpenWindow和dispobj,但是好像需要涉及到图片数据类型的转换?求大神解答

c++

2个回答

是涉及转换问题,主要是把halcon类型转换为数组,然后根据宽高设置到控件,8位图用get_image_pointer1,24位图用get_image_pointer3,以下是我写的转换函数,你可以参考一下:

#ifdef HALCON11
int ConvertHalcon11ToKSJ(HObject Image, unsigned char ** ppImageData)//ppImageData在接口调用后需要释放
{
#else
int ConvertHalcon10ToKSJ(Hobject Image, unsigned char ** ppImageData)
{
#endif

    mirror_image(Image, &Image, "row");//上下反转图片
    HTuple     Hchannels;
    count_channels(Image, &Hchannels);
    int nChannels = Hchannels[0].I();
    HTuple     Htype;
    HTuple     Hwidth, Hheight;
    unsigned char * pTempData = NULL;
    if (nChannels == 1)//根据位图深度调用相应接口
    {
        HTuple hpoint;
        get_image_pointer1(Image, &hpoint, &Htype, &Hwidth, &Hheight);
        int nLineByte = MAKEWIDTHBYTES(Hwidth[0].I(), 8);
        pTempData = new unsigned char[nLineByte * Hheight[0].I()];
        unsigned char *  ptr = (unsigned char *)hpoint[0].L();
        for (int i = 0; i < Hheight[0].I(); i++)//深拷贝
        {
            memcpy(pTempData + i * nLineByte, ptr + i * Hwidth[0].I(), Hwidth[0].I());
        }
    }
    else if (nChannels == 3 || nChannels == 4)
    {
        HTuple Hred;
        HTuple Hgreen;
        HTuple Hblue;
        get_image_pointer3(Image, &Hred, &Hgreen, &Hblue, &Htype, &Hwidth, &Hheight);
        int nLineByte = MAKEWIDTHBYTES(Hwidth[0].I(), nChannels * 8);
        pTempData = new unsigned char[nLineByte * Hheight[0].I()];
        unsigned char *  ptr_red   = (unsigned char *)Hred[0].L();
        unsigned char *  ptr_green = (unsigned char *)Hgreen[0].L();
        unsigned char *  ptr_blue  = (unsigned char *)Hblue[0].L();
        for (int i = 0; i < Hheight[0].I(); i++)//将红、绿、蓝保存到像素中
        {
            int nOffset     = i * Hwidth[0].I();
            int nRealOffset = i * nLineByte;
            for (int j = 0; j < Hwidth[0].I(); j++)//深拷贝
            {
                int nPixelOffset = j * nChannels;
                pTempData[nRealOffset + nPixelOffset + 2] = ptr_red[nOffset + j];
                pTempData[nRealOffset + nPixelOffset + 1] = ptr_green[nOffset + j];
                pTempData[nRealOffset + nPixelOffset]     = ptr_blue[nOffset + j];
            }
        }
    }
    else
    {
        return -1;
    }

    *ppImageData = pTempData;
    return 0;
}
phenix2009
白色一大坨 回复Decim123: HObject 知道是什么吧,转换为一个图像的byte数组,你看一下get_image_pointer1和get_image_pointer3这两个函数,这些都是可以查的,写出来是方便你理解
大约一年之前 回复
Decim123
Decim123 感觉这个好复杂,看不太懂怎么应用到我这个简单程序里
大约一年之前 回复
dabocaiqq
穷在人世中少你左右我想我连什么价值也没有 也不知道你从哪里抄来的代码,还不给出出处。已经举报。
大约一年之前 回复
dabocaiqq
穷在人世中少你左右我想我连什么价值也没有 回答错误,误导人家,还出言不逊
大约一年之前 回复
1.建立对话框工程(字符集:多字节字符集) 
2.配置属性: 
(1)配置属性》c++》常规》附加库目录,加入: 
D:\Program Files\MVTec\HALCON-12.0\include\halconcpp; 

D:\Program Files\MVTec\HALCON-12.0\include; 

(2)配置属性》链接器》常规》附加库目录,加入: 
D:\Program Files\MVTec\HALCON-12.0\lib\x64-win64; 
(3)配置属性》链接器-》输入》附加依赖项,加入: 
halconcpp.lib 
3.Halcon调用,需在Dlg.h或调用文件中加入
include”HalconCpp.h”
using namespace HalconCpp; 

4. 
(1)在主窗口中显示 

/********在对话框中添加按钮,打开图像的路径*******/

CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"(*.jpg;*.png;*.bmp;*.tif)|*.jpg;*.png;*.bmp;*.tif||",NULL);
if(dlg.DoModal() == IDCANCEL)
{
GetDlgItem(IDC_BUTTON1_Readimage)->EnableWindow(TRUE);
return;
}

/*******返回带文件名的路径,并传递给编辑控件变量********/

CString strPathname = dlg.GetPathName();
char *str=(char*)LPCTSTR(strPathname );

/******读取图像******/

HTuple m_ImageWidth, m_ImageHeight, WindowHandle;
HObject Image; 
ReadImage(&Image,str); 
//GetImagePointer1(Image, &pointer, &Type, &m_ImageWidth, &m_ImageHeight);
GetImageSize(Image, &m_ImageWidth, &m_ImageHeight); 
SetWindowAttr("background_color","black"); 
OpenWindow(0,0,m_ImageWidth,m_ImageHeight,0,"","",&WindowHandle); 
//OpenWindow(0,0,m_ImageWidth,m_ImageHeight,0,"","",this); 
SetPart(WindowHandle,0, 0, m_ImageWidth, m_ImageHeight);
DispObj(Image, WindowHandle); ;
}



具体你可以看
https://blog.csdn.net/xueyuanling/article/details/52483954

Decim123
Decim123 回复见了你女王大人喵姐还不跪下: 我按照上面的方法试了,但是发现并不管用,每次打开调试器后点击按钮开关都没反应,设了断点发现程序再跑但是不显示图像
大约一年之前 回复
devmiao
见了你女王大人喵姐还不跪下 很详细,lz可以采纳了。
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐