Decim123
2019-05-21 13:37
采纳率: 33.3%
浏览 2.1k

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,但是好像需要涉及到图片数据类型的转换?求大神解答

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 白色一大坨 2019-05-21 13:47
    已采纳

    是涉及转换问题,主要是把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;
    }
    
    点赞 评论
  • dabocaiqq 2019-05-21 13:51
    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

    点赞 评论

相关推荐 更多相似问题