Decim123 2019-05-21 13:37 采纳率: 0%
浏览 2220
已采纳

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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 如果update 一个列名为参数的value
  • ¥15 基于51单片机的水位检测系统设计中LCD1602一直不显示
  • ¥15 OCS2安装出现问题,请大家给点意见
  • ¥15 有没有大能能帮我出个适应度函数图,T_T
  • ¥15 ros小车启动launch文件报错
  • ¥15 vs2015到期想登陆但是登陆不上
  • ¥15 IPQ5018制作烧录固件,boot运行失败(操作系统-linux)(相关搜索:操作系统)(相关搜索:操作系统)
  • ¥20 icefall在librispeech基础上加入个人数据集
  • ¥30 keepalive高可用故障运维配置询问
  • ¥15 求帮助!国家电网内网u盘突然识别不出来了。