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

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条)

报告相同问题?

悬赏问题

  • ¥100 求书法图像文字切割代码
  • ¥20 【初学者】comsol周期性电流如何设置?例如通电三天断电三天
  • ¥15 Proteus仿真程序只能执行一次
  • ¥15 语音识别websocket报错
  • ¥15 激光器,引脚问题,无法处理
  • ¥20 求写一份!只提交Mapper映射文件 如:UsersMapper.xml
  • ¥15 CH340C串口模块无法被识别
  • ¥15 为什么site-packages库不能被调用
  • ¥15 docker环境配置
  • ¥20 绿盟安全扫描--检测到目标站点存在javascript框架库漏洞