使用Halcon HSmartWindowControl展示图片
概述:使用Halcon在VS中的控件显示一张图片。
要点:使用了图像缩放和图像显示函数,以及鼠标滚轮响应函数。
1、创建WinForm项目
首先在VS中添加Halcon所需的控件HSmartWindowControl
然后创建新的winForm工程,并向窗体中拖入一个HSmartWindowControl控件和两个按钮控件。
拖入后不添加代码,直接运行,可能会出现如下错误:
HalconDotNet.HHandleBase 的类型初始化值设定项引发异常
Halcon error #2381:License is for wrong edition of Halcon in Operator set_system
原因是halcon运行库是64位的,vs的运行模式也需要设置为64位,默认的debug模式可能是any CPU,需要把这里修改成x64。
或者从解决方案资源管理器(solution Explorer)中双击打开属性(Property)页面,把Build栏的Platform target 改为x64即可。
2、创建Halcon实时显示代码并导出
打开halcon,写一句简单的代码
接着,选择文件->导出,将HDevelop语言导出为C#语言。
3、halcon 导出的C#代码分析
1 using System;
2 using HalconDotNet;
3
4 public partial class HDevelopExport
5 {
6 public HTuple hv_ExpDefaultWinHandle;
7
8 // Main procedure
9 private void action()
10 {
11 // Local iconic variables
12 HObject ho_Image;
13 // Local control variables
14 HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
15 // Initialize local and output iconic variables
16 HOperatorSet.GenEmptyObj(out ho_Image);
17 ho_Image.Dispose();
18 HOperatorSet.ReadImage(out ho_Image, "printer_chip/printer_chip_01");
19 hv_Width.Dispose();hv_Height.Dispose();
20 HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
21 ho_Image.Dispose();
22
23 hv_Width.Dispose();
24 hv_Height.Dispose();
25 }
26
27 public void InitHalcon()
28 {
29 // Default settings used in HDevelop
30 HOperatorSet.SetSystem("width", 512);
31 HOperatorSet.SetSystem("height", 512);
32 }
33
34 public void RunHalcon(HTuple Window)
35 {
36 hv_ExpDefaultWinHandle = Window;
37 action();
38 }
39 }
实际有用的代码就是action()内的代码,首先声明了图像变量:HObject ho_Image;
需要注意的是,图像类型需要先初始化再使用:HOperatorSet.GenEmptyObj(out ho_Image);
然后调用ReadImage函数读入图像文件,最后调用Dispose函数清空对象。但是没有显示图像的代码,需要自己添加。
4、向VS中插入代码
打开Winform工程窗体关联的cs文件Form1.cs,首先需要在文件头部添加命名空间引用:
using HalconDotNet;
需要在类中定义全局的窗口变量,便于操作窗体:
1 private static HWindow hwindow; //全局窗口变量
2 public HTuple hv_ExpDefaultWinHandle; //导出的代码定义的,其实没用到
窗口的初始化函数中添加全局变量的初始化函数:
1 public Form1()
2 {
3 InitializeComponent();
4 hwindow = hSmartWindowControl1.HalconWindow;//初始化窗口变量
5 this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.my_MouseWheel);
6 }
然后在按钮的响应函数中添加载入图像的及显示的代码:
1 private void button2_Click(object sender, EventArgs e)
2 {
3 #region 初始化变量
4 // Local iconic variables
5 HObject ho_Image;
6 // Local control variables
7 HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
8 // Initialize local and output iconic variables
9 HOperatorSet.GenEmptyObj(out ho_Image);
10 ho_Image.Dispose();
11 HOperatorSet.ReadImage(out ho_Image, "clip.png");
12 hv_Width.Dispose(); hv_Height.Dispose();
13 #endregion
14
15 #region 缩放图像,适应窗口
16 bool needResizeImage = true;
17 //获取图像大小及纵横比
18 HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
19 int im_width = int.Parse(hv_Width.ToString());
20 int im_height = int.Parse(hv_Height.ToString());
21 double im_AspectRatio = (double)(im_width)/(double)(im_height);
22 //获取窗口大小及纵横比
23 int w_width = hSmartWindowControl1.Size.Width;
24 int w_height = hSmartWindowControl1.Size.Height;
25 double w_AspectRatio = (double)(w_width)/(double)(w_height);
26
27 HOperatorSet.SetSystem("int_zooming", "false");//图像缩放之前最好将此参数设置为false.
28 HTuple para = new HTuple("constant");
29 HObject ho_zoomImage;
30 HOperatorSet.GenEmptyObj(out ho_zoomImage);
31
32 ho_zoomImage.Dispose();
33 if(w_width<im_width && im_AspectRatio>w_AspectRatio)
34 {
35 //超宽图像
36 HOperatorSet.ZoomImageSize(ho_Image, out ho_zoomImage, w_width, w_width / im_AspectRatio, para);
37 }
38 else if (w_height < im_height && im_AspectRatio < w_AspectRatio)
39 {
40 //超高图像
41 HOperatorSet.ZoomImageSize(ho_Image, out ho_zoomImage, w_height * im_AspectRatio, w_height, para);
42 }
43 else
44 {
45 needResizeImage = false;
46 }
47 #endregion
48
49 #region 显示图像
50 hwindow.SetPart(0, 0, -2, -2);
51 if (needResizeImage)
52 {
53 hwindow.DispObj(ho_zoomImage);
54 }
55 else
56 {
57 hwindow.DispObj(ho_Image);
58 }
59
60
61 //HOperatorSet.GetImageSize(ho_zoomImage, out hv_Width, out hv_Height);
62 //im_width = int.Parse(hv_Width.ToString());
63 //im_height = int.Parse(hv_Height.ToString());
64 //MessageBox.Show(hv_Width.ToString() + " " + hv_Height.ToString());
65
66 #endregion
67
68 ho_Image.Dispose();
69 ho_zoomImage.Dispose();
70 hv_Width.Dispose();
71 hv_Height.Dispose();
72 }
5、图像缩放和显示
这里我对导出的代码做了修改,主要是增加了图像缩放和显示功能。
图像缩放使用了ZoomImageSize函数,在帮助文档中,这个函数有两种用法:
1 static void HOperatorSet.ZoomImageSize(HObject image, out HObject imageZoom, HTuple width,HTuple height, HTuple interpolation)
2 HImage HImage.ZoomImageSize(int width, int height, string interpolation)
上面的代码中使用了第一种用法,所有的参数必须是Halcon的类型,但是实际使用时width 和height 可以用int类型,interpolation即差值算法参数也可以直接传入string类型,如“constant”。
第二种方法需要将使用HImage类型的变量来操作,所以需要将 HObject 类型定义的图像变量转换一下:
1 HImage img = new HImage(ho_Image);
2 ho_zoomImage = img.ZoomImageSize(w_width, w_width, "constant");
图像显示功能:先调用SetPart函数确定要显示图像的区域,然后调用DispObj函数显示图像,DispObj也有三种用法:
1 static void HOperatorSet.DispObj(HObject objectVal, HTuple windowHandle)
2 void HObject.DispObj(HWindow windowHandle)
3 void HWindow.DispObj(HObject objectVal)
此处我用的第三种:通过Hwindow调用,参数为要显示的图像变量。
1 hwindow.SetPart(0, 0, -2, -2);
2 hwindow.DispObj(ho_zoomImage);
6、SmartWindowControl窗口交互
前面实现了图像显示,SmartWindowControl最大的亮点其实是方便的交互功能。
可以用鼠标拖动图像水平移动,利用鼠标滚轮实现图像放大缩小。
另外,在winForm项目中,为了使用SmartWindowControl控件中图像的缩放,还需要添加鼠标滚轮响应的回调函数。
详见:C:/Program Files/MVTec/HALCON-18.05-Progress/doc/html/manuals/programmers_guide/programmers_guide_0051.html
实际测试发现帮助文档中的用法有一些问题,消息相应函数的注册应该在整个窗体的初始化函数中,而不是SmartWindowControl的初始化函数中。
1 private void hSmartWindowControl1_Load(object sender, EventArgs e)
2 {
3 //这句话不起作用
4 this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.my_MouseWheel);
5 }
1 public Form1()
2 {
3 InitializeComponent();
4 hwindow = hSmartWindowControl1.HalconWindow;//初始化窗口变量
5 // 鼠标滚轮的响应函数注册
6 this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.my_MouseWheel);
7 }
这样修改后,直接使用帮助文档中提供的my_MouseWheel也有一个小bug,就是无论在窗体的任何位置滑动滚轮,图片都会缩放,所以需要对鼠标的位置做一下限制:
1 private void my_MouseWheel(object sender, MouseEventArgs e)
2 {
3 System.Drawing.Point pt = this.Location;
4 int leftBorder = hSmartWindowControl1.Location.X;
5 int rightBorder = hSmartWindowControl1.Location.X + hSmartWindowControl1.Size.Width;
6 int topBorder = hSmartWindowControl1.Location.Y;
7 int bottomBorder = hSmartWindowControl1.Location.Y + hSmartWindowControl1.Size.Height;
8 if(e.X > leftBorder && e.X < rightBorder && e.Y > topBorder && e.Y < bottomBorder)
9 {
10 MouseEventArgs newe = new MouseEventArgs(e.Button, e.Clicks,
11 e.X - pt.X, e.Y - pt.Y, e.Delta);
12 hSmartWindowControl1.HSmartWindowControl_MouseWheel(sender, newe);
13 }
14 }
原文地址:https://www.cnblogs.com/ybqjymy/p/12720985.html
推荐阅读
-
微信 "扫一扫 "物联网,全面揭秘 "扫一扫 "背后的扫盲技术!-1.1 扫一扫感知物体是做什么的? 1.1 微信扫一扫是做什么的? 扫一扫识物是指以图片或视频(商品图片:鞋/包/美妆/服饰/家电/玩具/图书/食品/珠宝/家具/其他商品)为输入媒介,挖掘微信内容生态中的有价值信息(电商+百科+资讯,如图1所示),并展示给用户。这里的电商基本涵盖了微信小程序覆盖上亿SKU的全量优质电商,可以支持用户货比N家并直接下单购买,百科和资讯则聚合了微信内的头部自媒体如搜狗、搜搜、百度等,向用户展示和分享拍摄商品相关的内容资讯。 图 1 扫一扫识别功能示意图 欢迎大家更新iOS新版微信→扫一扫→识货,亲自体验,也欢迎大家通过识货界面的反馈按钮向我们提交反馈意见。 扫一扫识物实景图展示 1.2 扫一扫识物有哪些使用场景? 扫一扫识物的目的是为用户访问微信内部生态内容开辟一个新窗口,以用户扫图片为输入形式,为用户提供微信生态内容中的百科、资讯、电商等作为展示页面。除了用户熟悉的扫一扫操作外,我们还将进一步拓展长按操作,让用户更方便地进行扫一扫操作。"扫一扫知事 "的落地场景主要涵盖三大部分: a. 科普知识: a.科普知识。用户通过扫一扫,可以在微信生态圈中获取该对象的百科、资讯等常识或趣闻,帮助用户更好地了解该对象; b.购物场景。同样的搜索功能支持用户看到喜欢的商品立即检索到微信小程序电商中的同款商品,支持用户即扫即购; c.广告场景。扫一扫识别物体可以辅助公众号文章、视频更好地理解其中蕴含的图片信息,从而更好地投放匹配广告,提高点击率。 1.3 Sweep Sense 为 Sweep 家族带来了哪些新技术? 对于扫一扫来说,大家耳熟能详的应该就是扫一扫二维码、扫一扫小程序码、扫一扫条形码、扫一扫翻译了。无论是各种形式的编码还是文字字符,都可以看作是图片的一种特定编码形式,而物的识别则是对自然场景图片的识别,这对于扫一扫家族来说是一个质的飞跃,我们希望从物的识别入手,进一步拓展扫一扫对自然场景图片的理解能力,比如扫酒、扫车、扫植物、扫人脸等服务,如下图3所示。 图 3 Sweep 家族
-
实现在类似陌陌的界面中使用RecyclerView流畅地展示并滑动浏览图片
-
使用Konva库在Canvas上绘图并保存为图片,实时展示在网页上
-
在Android上使用ContentProvider搜索手机照片,模拟微信展示本地图片功能
-
使用Halcon HSmartWindowControl展示图片
-
码农必备的8大工具神器:在线图片压缩超好用!-网址:https://worldvectorlogo.com/ 网络异常,图片无法展示 | 在写文档、做行业调研、制作ppt时,经常遇到需要下载某个公司的logo文件问题,有的可以直接官网找到,有的却很难找到高清版本的logo图片。 这个网址收集了全球知名企业logo源文件,svg格式,可以免费下载不限次数,一直被我安利给身边的朋友们,网站界面操作也非常简单。 文档翻译神器-DeepL 网址:https://www.deepl.com/zh/translator 网络异常,图片无法展示 | 比某歌某道好用几倍的翻译软件,词汇和语法相对来说都用的更地道,机翻痕迹较少;而且支持整篇文档上传翻译,临时看英文文献的效率翻倍;最重要的是免费! 它支持26种语言之间的互译,而且译文基本都可以直接拿来使用。打开界面,可以看到它会自动检测语言,并且默认译文是中文,我们也可以根据自己需求来设置语言。然后,手动输入需要翻译的内容或者直接粘贴到文本框。最后,它会快速翻译,翻译结果支持一键复制到剪切板,方便后续使用。 在线小工具合集-蛙蛙工具 网址:https://www.iamwawa.cn/ 网络异常,图片无法展示 | “蛙蛙在线工具“是一个包含大量在线工具网站,包含语言工具,便民查询,转换工具,生理健康,站长工具等等的网站。 类似于个人所得税计算这样的工具,我只是偶尔用到 ,并不想单独存一个网站,这个工具就很好的帮我解决了这个问题,无需登录,在线使用,用完即走。
-
C语言中使用异或运算交换两个数的值:图2展示了异或方法和加减法汇编代码的对比,点击图片可查看详细信息。
-
go(goav) 中使用 ffmpeg 获取摄像头视频流,并转换成图片,发送给前端界面实时展示
-
使用U盘重新烧写龙芯自带系统loongnix- 网络异常,图片无法展示 |