halcon opencv-python C# 自适应不同大小图像并保持纵横比
初学halcon的同学都知道:
read_image (Image, '1')
get_image_size (Image, imgWidth, imgHeight)
dev_open_window (0, 0,win_height, win_width, 'black', WindowHandle)
*或者
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle1)
*最后
dev_set_part (0, 0, 511, 511)
dev_display (Image)
初学opencv的同学也会知道:
import cv2
image_path = 'image.jpg' # 图片路径
image = cv2.imread(image_path)
cv2.imshow('Image Window', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
首先窗口尺寸是固定的(600*800)但是图像尺寸是变化的,小到32*32大到5472*3648,甚至更大。
这时halcon里面的dev_set_part有点用但是不会保持纵横比。
而opencv里面图像显示cv2.imshow是不能缩放的又愿意用matplotlib之类。
opencv显示大图就会特别难看,相信尝试过的同学深有体会。
这时候halcon有时候显示反而友好一点。但是也不是很友好。
偶然发现以下这么一行程序:
他可以支持任意大小的窗口打开任意尺寸的图像并保持纵横比 当然 图像和图像框的比例不要相差过大。程序如下所示:作者已经不知道是谁了, 在此致敬前辈。
HOperatorSet.GetImageSize(ho_image, out ImageWidth, out ImageHeight);
HOperatorSet.SetPart(WindowID, 0, 0, ImageHeight, ImageWidth);
HOperatorSet.DispObj(ho_image, WindowID);*/
//获取图像及显示窗口长宽
HOperatorSet.GetImageSize(image, out imgWidth, out imgHeight);
int wndWidth = hSmartWindowControl1.ClientRectangle.Width;
int wndHeight = hSmartWindowControl1.ClientRectangle.Height;
//计算比例
double scale = Math.Max(1.0 * imgWidth.I / wndWidth, 1.0 * imgHeight / wndHeight);
double w = wndWidth * scale;
double h = wndHeight * scale;
//居中时,Part的区域
hSmartWindowControl1.HalconWindow.SetPart(-(h - imgHeight) / 2, -(w - imgWidth) / 2, imgHeight + (h - imgHeight.D) / 2, imgWidth + (w - imgWidth) / 2);
//背景色
hSmartWindowControl1.HalconWindow.SetWindowParam("background_color", "black");
hSmartWindowControl1.HalconWindow.ClearWindow();
hSmartWindowControl1.HalconWindow.DispObj(image);
由此我照葫芦画瓢做了halcon版本的:贴上去。
win_width:=500
win_height:=500
read_image (Image, '1')
get_image_size (Image, imgWidth, imgHeight)
scale:=max2(1.0 * imgWidth / win_width, 1.0 * imgHeight / win_height)
w:=win_width*scale
h:=win_height*scale
dev_open_window (0, 0,win_height, win_width, 'black', WindowHandle)
dev_set_part (-(h - imgHeight) / 2, -(w - imgWidth) / 2, imgHeight + (h - imgHeight) / 2, imgWidth + (w - imgWidth) / 2)
dev_display (Image)
公式没变语法不一样,只是。
接下来放大招:
python版本的halcon,经常逛CSDN的人都知道我是最早玩python版halcon的那一批人,python支持是早晚的事。
上程序:开始是这样画风。
Imagename="1rgb.bmp"
#自适应窗口显示超大图和超小图
win_width=500
win_height=500
imgWidth=5472
imgHeight=3648
#Image = ha.read_image(Imagename); Width, Height = ha.get_image_size(Image); imgWidth=Width[0]; imgHeight=Height[0] ;echo(f"{imgWidth}*{imgHeight}")
scale=ha.tuple_max2(1.0 * imgWidth / win_width, 1.0 * imgHeight / win_height)
w=win_width*scale[0]
h=win_height*scale[0]
WindowHandle =open_window(win_height, win_width,0,0,father_window=0)
ha.set_part (WindowHandle,(-(h-imgHeight))/2, (-(w-imgWidth))/2, imgHeight+((h-imgHeight)/2), imgWidth+((w-imgWidth)/2))
#ha.disp_obj(Image, WindowHandle);
#img = cv2.imread(Imagename)#('1.bmp');
#hacvimshow(img, WindowHandle)
后来:
def open_window(width, height,row=0,column=0,father_window=0):
if os.name == 'nt':
ha.set_system('use_window_thread', 'true')
return ha.open_window(
row=row,
column=column,
width=width,
height=height,
father_window=father_window,
mode='visible',
machine=''
)
def HaAutoSetPart(Image,WindowHandle,
win_width=500,
win_height=500,
imgWidth=5472,
imgHeight=3648
):
#自适应窗口显示超大图和超小图
scale=ha.tuple_max2(1.0 * imgWidth / win_width, 1.0 * imgHeight / win_height)
w=win_width*scale[0]
h=win_height*scale[0]
ha.set_part (WindowHandle,
(-(h-imgHeight))/2, (-(w-imgWidth))/2,
imgHeight+((h-imgHeight)/2), imgWidth+((w-imgWidth)/2))
ha.disp_obj(Image, WindowHandle);
def hacvimshow(img, WindowHandle,ts=0.5):
#img = img[:, :, ::-1]
ha.disp_obj(ha.himage_from_numpy_array(img), WindowHandle);
WindowHandle =open_window(win_height, win_width,0,0,father_window=0)
HaAutoSetPart(img,WindowHandle1,
win_width=Width,win_height=Height,
#win_width=800,win_height=800,
imgWidth=nWidth,imgHeight=nHeight)
ha.disp_obj(img, WindowHandle1);
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了哈哈哈。