halcon & opencv-python & C# 自顺应差别巨细图像并保持纵横比

[复制链接]
发表于 2025-11-7 23:39:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
初学halcon的同砚都知道:
  1. read_image (Image, '1')
  2. get_image_size (Image, imgWidth, imgHeight)
  3. dev_open_window (0, 0,win_height, win_width,  'black', WindowHandle)
  4. *或者
  5. dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle1)
  6. *最后
  7. dev_set_part (0, 0, 511, 511)
  8. dev_display (Image)
复制代码
初学opencv的同砚也会知道:
  1. import cv2
  2. image_path = 'image.jpg'  # 图片路径
  3. image = cv2.imread(image_path)
  4. cv2.imshow('Image Window', image)
  5. cv2.waitKey(0)
  6. cv2.destroyAllWindows()
复制代码
起首窗口尺寸是固定的(600*800)但是图像尺寸是厘革的,小到32*32大到5472*3648,以致更大。
这时halcon内里的dev_set_part有点用但是不会保持纵横比。
而opencv内里图像体现cv2.imshow是不能缩放的又乐意用matplotlib之类。
opencv体现大图就会特殊丢脸,信托实验过的同砚深有了解。
这时间halcon偶然候体现反而友爱一点。但是也不是很友爱。
偶然发现以下这么一行步调:
他可以支持恣意巨细的窗口打开恣意尺寸的图像并保持纵横比 固然 图像和图像框的比例不要相差过大。步调如下所示:作者已经不知道是谁了, 在此致敬先辈。 
  1.              HOperatorSet.GetImageSize(ho_image, out ImageWidth, out ImageHeight);
  2.              HOperatorSet.SetPart(WindowID, 0, 0, ImageHeight, ImageWidth);
  3.              HOperatorSet.DispObj(ho_image, WindowID);*/
  4.             //获取图像及显示窗口长宽
  5.             HOperatorSet.GetImageSize(image, out  imgWidth, out  imgHeight);
  6.             int wndWidth = hSmartWindowControl1.ClientRectangle.Width;
  7.             int wndHeight = hSmartWindowControl1.ClientRectangle.Height;
  8.             //计算比例
  9.             double scale = Math.Max(1.0 * imgWidth.I / wndWidth, 1.0 * imgHeight / wndHeight);
  10.             double w = wndWidth * scale;
  11.             double h = wndHeight * scale;
  12.             //居中时,Part的区域
  13.             hSmartWindowControl1.HalconWindow.SetPart(-(h - imgHeight) / 2, -(w - imgWidth) / 2, imgHeight + (h - imgHeight.D) / 2, imgWidth + (w - imgWidth) / 2);
  14.             //背景色
  15.             hSmartWindowControl1.HalconWindow.SetWindowParam("background_color", "black");
  16.             hSmartWindowControl1.HalconWindow.ClearWindow();
  17.             hSmartWindowControl1.HalconWindow.DispObj(image);
复制代码
由此我照葫芦画瓢做了halcon版本的:贴上去。
  1. win_width:=500
  2. win_height:=500
  3. read_image (Image, '1')
  4. get_image_size (Image, imgWidth, imgHeight)
  5. scale:=max2(1.0 * imgWidth / win_width, 1.0 * imgHeight / win_height)
  6. w:=win_width*scale
  7. h:=win_height*scale
  8. dev_open_window (0, 0,win_height, win_width,  'black', WindowHandle)
  9. dev_set_part (-(h - imgHeight) / 2, -(w - imgWidth) / 2, imgHeight + (h - imgHeight) / 2, imgWidth + (w - imgWidth) / 2)
  10. dev_display (Image)
复制代码
公式没变语法不一样,只是。
接下来放大招:
python版本的halcon,经常逛CSDN的人都知道我是最早玩python版halcon的那一批人,python支持是早晚的事。
上步调:开始是如许画风。
  1. Imagename="1rgb.bmp"
  2. #自适应窗口显示超大图和超小图
  3. win_width=500
  4. win_height=500
  5. imgWidth=5472
  6. imgHeight=3648
  7. #Image = ha.read_image(Imagename);  Width, Height = ha.get_image_size(Image);  imgWidth=Width[0];  imgHeight=Height[0] ;echo(f"{imgWidth}*{imgHeight}")
  8. scale=ha.tuple_max2(1.0 * imgWidth / win_width, 1.0 * imgHeight / win_height)
  9. w=win_width*scale[0]
  10. h=win_height*scale[0]
  11. WindowHandle =open_window(win_height, win_width,0,0,father_window=0)
  12. ha.set_part (WindowHandle,(-(h-imgHeight))/2, (-(w-imgWidth))/2, imgHeight+((h-imgHeight)/2), imgWidth+((w-imgWidth)/2))
  13. #ha.disp_obj(Image, WindowHandle);
  14. #img = cv2.imread(Imagename)#('1.bmp');
  15. #hacvimshow(img, WindowHandle)
复制代码
厥后:
  1. def open_window(width, height,row=0,column=0,father_window=0):
  2.     if os.name == 'nt':
  3.         ha.set_system('use_window_thread', 'true')
  4.     return ha.open_window(
  5.         row=row,
  6.         column=column,
  7.         width=width,
  8.         height=height,
  9.         father_window=father_window,
  10.         mode='visible',
  11.         machine=''
  12.     )
  13. def HaAutoSetPart(Image,WindowHandle,
  14.                     win_width=500,
  15.                     win_height=500,
  16.                     imgWidth=5472,
  17.                     imgHeight=3648
  18.                     ):
  19.     #自适应窗口显示超大图和超小图
  20.     scale=ha.tuple_max2(1.0 * imgWidth / win_width, 1.0 * imgHeight / win_height)
  21.     w=win_width*scale[0]
  22.     h=win_height*scale[0]
  23.     ha.set_part (WindowHandle,
  24.                 (-(h-imgHeight))/2, (-(w-imgWidth))/2,
  25.                 imgHeight+((h-imgHeight)/2), imgWidth+((w-imgWidth)/2))
  26.     ha.disp_obj(Image, WindowHandle);
  27. def hacvimshow(img, WindowHandle,ts=0.5):
  28.     #img = img[:, :, ::-1]
  29.     ha.disp_obj(ha.himage_from_numpy_array(img), WindowHandle);
  30. WindowHandle =open_window(win_height, win_width,0,0,father_window=0)
  31. HaAutoSetPart(img,WindowHandle1,
  32.                           win_width=Width,win_height=Height,
  33.                           #win_width=800,win_height=800,
  34.                           imgWidth=nWidth,imgHeight=nHeight)
  35. ha.disp_obj(img, WindowHandle1);
  36. hacvimshow(dilation, WindowHandle)#cv2.imshow("dilation",dilation)       
复制代码
发现大招了不,在halcon内里可以
ha.disp_obj(img, WindowHandle1);
在opencv内里也可以用
hacvimshow(dilation, WindowHandle)#cv2.imshow("dilation",dilation)    
以后调opencv多惬意。用hacvimshow(dilation, WindowHandle)
可以更换opencv 原来的 cv2.imshow("dilation",dilation)   窗口稳定的环境下自顺应很大大概很小的图    。 cv2.imshow 也不消resize了哈哈哈。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表