[Halcon学习笔记]Halcon窗口进行等比例显示图像

需求分析

在使用Halcon加载图像时,点击Halcon的适应窗口,图像都会按照窗口大小对图像进行拉伸后显示,实际项目中,需要等比例显示图像,体现图像原本的尺寸细节和效果,特别是在Qt或VS中,需要方便调节,这里记录一下等比例显示图像的原理和实现方法。

Halcon显示原理

Halcon显示图像的原理就是去调节视场大小,相当于图像的尺寸大小是固定的,通过调节相机高度和视野去适配图像的大小,这样图像的实际尺寸就不会被缩放。
Halcon中一般使用dev_set_part这个算子,算子四个参数,主要对应的是Halcon视场的左上和右下对应的行列尺寸,比如说图像宽度是500,你想显示的时候左右各留100空余背景,这时列的设置就是(-100,500+100),相当于从-100开始,将图像移动到中间。

显示实现

实际显示的时候,不仅要考虑图像的纵横比,还要考虑控件窗口的纵横比,如果图像的宽高比大于窗口的宽高比,这个时候就把图像的宽度方向靠近边缘,高度方向上下预留空白。

picWHRatio := 1.0 * Width / Height
winWHRatio := 1.0 * winWidth / winHeight
    if (picWHRatio >= winWHRatio)
        * 如果图片宽高比 大于 窗口宽高比
        * 则宽度方向顶格
        dispWidth := Width
        dispHeight := Width / winWHRatio
        offset:=(dispHeight-Height)/2
        dev_set_part (-offset, 0, dispHeight-offset, dispWidth)

宽图像
如果图像的宽高比小于窗口的宽高比,这个时候就把图像的高度方向靠近边缘,宽度方向左右预留空白。

picWHRatio := 1.0 * Width / Height
winWHRatio := 1.0 * winWidth / winHeight
if (Width > winWidth or Height > winHeight)
    if (picWHRatio < winWHRatio)
        * 如果图片宽高比 小于 窗口宽高比
        * 则高度方向顶格
        dispWidth := Height * winWHRatio
        dispHeight := Height
        offset:=(dispWidth-Width)/2
        dev_set_part (0, -offset, dispHeight, dispWidth-offset)

窄图像
如果图像的尺寸均小于控件的尺寸,此时将图像的尺寸乘以控件的纵横比之后再像上边进行比较。

    * 如果图片的长和宽都小于窗口
    if(picWHRatio >= winWHRatio)
        dispWidth := Width
        dispHeight := Width / winWHRatio
        offset:=(dispHeight-Height)/2
        dev_set_part (-offset, 0, dispHeight-offset, Width)
    else
        dispWidth := Height * winWHRatio
        dispHeight := Height
        offset:=(dispWidth-Width)/2
        dev_set_part (0, -offset, dispHeight, dispWidth-offset)
    endif  

小尺寸图像

具体实现Halcon代码

dev_close_window ()
dev_open_window (0, 0, 400, 400, 'gray', WindowHandle)
dev_clear_window ()
dev_update_window ('off')

read_image (Image, 'C:/Users/Administrator/Desktop/lena小尺寸.png')
get_window_extents (WindowHandle, Row, Column, winWidth, winHeight)

set_system ('int_zooming', 'false')
get_image_size (Image, Width, Height)
picWHRatio := 1.0 * Width / Height
winWHRatio := 1.0 * winWidth / winHeight
if (Width > winWidth or Height > winHeight)
    if (picWHRatio >= winWHRatio)
        * 如果图片宽高比 大于 窗口宽高比
        * 则宽度方向顶格
        dispWidth := Width
        dispHeight := Width / winWHRatio
        offset:=(dispHeight-Height)/2
        dev_set_part (-offset, 0, dispHeight-offset, dispWidth)
    else
        * 如果图片宽高比 小于 窗口宽高比
        * 则高度方向顶格
        dispWidth := Height * winWHRatio
        dispHeight := Height
        offset:=(dispWidth-Width)/2
        dev_set_part (0, -offset, dispHeight, dispWidth-offset)
    endif
else
    * 如果图片的长和宽都小于窗口
    if(picWHRatio >= winWHRatio)
        dispWidth := Width
        dispHeight := Width / winWHRatio
        offset:=(dispHeight-Height)/2
        dev_set_part (-offset, 0, dispHeight-offset, Width)
    else
        dispWidth := Height * winWHRatio
        dispHeight := Height
        offset:=(dispWidth-Width)/2
        dev_set_part (0, -offset, dispHeight, dispWidth-offset)
    endif  
endif
dev_display (Image)

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值