HSmartWindowControl 之 显示图像

匿名 (未验证) 提交于 2019-12-02 22:06:11

概述:使用Halcon在VS中的控件显示一张图片。

要点:使用了图像缩放和图像显示函数,以及鼠标滚轮响应函数。

1、创建WinForm项目

参考上一篇:HSmartWindowControl之安装篇 (Visual Studio 2013 & Halcon 18)https://www.cnblogs.com/oucsheep/p/9563876.html

在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#代码分析

using System; using HalconDotNet;  public partial class HDevelopExport {   public HTuple hv_ExpDefaultWinHandle;    // Main procedure    private void action()   {     // Local iconic variables      HObject ho_Image;     // Local control variables      HTuple hv_Width = new HTuple(), hv_Height = new HTuple();     // Initialize local and output iconic variables      HOperatorSet.GenEmptyObj(out ho_Image);     ho_Image.Dispose();     HOperatorSet.ReadImage(out ho_Image, "printer_chip/printer_chip_01");     hv_Width.Dispose();hv_Height.Dispose();     HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);     ho_Image.Dispose();      hv_Width.Dispose();     hv_Height.Dispose();   }    public void InitHalcon()   {     // Default settings used in HDevelop      HOperatorSet.SetSystem("width", 512);     HOperatorSet.SetSystem("height", 512);   }    public void RunHalcon(HTuple Window)   {     hv_ExpDefaultWinHandle = Window;     action();   } }

实际有用的代码就是action()内的代码,首先声明了图像变量:HObject ho_Image;

需要注意的是,图像类型需要先初始化再使用:HOperatorSet.GenEmptyObj(out ho_Image);

然后调用ReadImage函数读入图像文件,最后调用Dispose函数清空对象。但是没有显示图像的代码,需要自己添加。

4、向VS中插入代码

打开Winform工程窗体关联的cs文件Form1.cs,首先需要在文件头部添加命名空间引用:

using HalconDotNet;

需要在类中定义全局的窗口变量,便于操作窗体:

private static HWindow hwindow; //全局窗口变量 public HTuple hv_ExpDefaultWinHandle; //导出的代码定义的,其实没用到

窗口的初始化函数中添加全局变量的初始化函数:

public Form1()         {             InitializeComponent();             hwindow = hSmartWindowControl1.HalconWindow;//初始化窗口变量             this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.my_MouseWheel);         }

然后在按钮的响应函数中添加载入图像的及显示的代码:

private void button2_Click(object sender, EventArgs e)         {             #region 初始化变量             // Local iconic variables              HObject ho_Image;             // Local control variables              HTuple hv_Width = new HTuple(), hv_Height = new HTuple();             // Initialize local and output iconic variables              HOperatorSet.GenEmptyObj(out ho_Image);             ho_Image.Dispose();             HOperatorSet.ReadImage(out ho_Image, "clip.png");             hv_Width.Dispose(); hv_Height.Dispose();             #endregion              #region 缩放图像,适应窗口             //获取图像大小及纵横比             HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);             int im_width = int.Parse(hv_Width.ToString());             int im_height = int.Parse(hv_Height.ToString());             double im_AspectRatio = (double)(im_width)/(double)(im_height);             //获取窗口大小及纵横比             int w_width = hSmartWindowControl1.Size.Width;             int w_height = hSmartWindowControl1.Size.Height;             double w_AspectRatio = (double)(w_width)/(double)(w_height);              HOperatorSet.SetSystem("int_zooming", "false");//图像缩放之前最好将此参数设置为false.             HTuple para = new HTuple("constant");             HObject ho_zoomImage;             HOperatorSet.GenEmptyObj(out ho_zoomImage);              ho_zoomImage.Dispose();             if(w_width<im_width && im_AspectRatio>w_AspectRatio)             {                 //超宽图像                                HOperatorSet.ZoomImageSize(ho_Image, out ho_zoomImage, w_width, w_width / im_AspectRatio, para);                         }             else if (w_height < im_height && im_AspectRatio < w_AspectRatio)             {                 //超高图像                                 HOperatorSet.ZoomImageSize(ho_Image, out ho_zoomImage, w_height * im_AspectRatio, w_height, para);             }             #endregion              #region 显示图像             hwindow.SetPart(0, 0, -2, -2);             hwindow.DispObj(ho_zoomImage);              //HOperatorSet.GetImageSize(ho_zoomImage, out hv_Width, out hv_Height);             //im_width = int.Parse(hv_Width.ToString());             //im_height = int.Parse(hv_Height.ToString());             //MessageBox.Show(hv_Width.ToString() + "  " + hv_Height.ToString());                          #endregion              ho_Image.Dispose();             ho_zoomImage.Dispose();             hv_Width.Dispose();             hv_Height.Dispose();         }

5、图像缩放和显示

这里我对导出的代码做了修改,主要是增加了图像缩放和显示功能。

图像缩放使用了ZoomImageSize函数,在帮助文档中,这个函数有两种用法:

static void HOperatorSet.ZoomImageSize(HObject image, out HObject imageZoom, HTuple width,HTuple height, HTuple interpolation)  HImage HImage.ZoomImageSize(int width, int height, string interpolation)

上面的代码中使用了第一种用法,所有的参数必须是Halcon的类型,但是实际使用时width 和height 可以用int类型,interpolation即差值算法参数也可以直接传入string类型,如“constant”。

第二种方法需要将使用HImage类型的变量来操作,所以需要将 HObject 类型定义的图像变量转换一下:

HImage img = new HImage(ho_Image); ho_zoomImage = img.ZoomImageSize(w_width, w_width, "constant");


图像显示功能:先调用SetPart函数确定要显示图像的区域,然后调用DispObj函数显示图像,DispObj也有三种用法:

static void HOperatorSet.DispObj(HObject objectVal, HTuple windowHandle)  void HObject.DispObj(HWindow windowHandle)  void HWindow.DispObj(HObject objectVal)

此处我用的第三种:通过Hwindow调用,参数为要显示的图像变量。

hwindow.SetPart(0, 0, -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的初始化函数中。

private void hSmartWindowControl1_Load(object sender, EventArgs e)         {             //这句话不起作用             this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.my_MouseWheel);         }
public Form1()         {             InitializeComponent();             hwindow = hSmartWindowControl1.HalconWindow;//初始化窗口变量             // 鼠标滚轮的响应函数注册             this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.my_MouseWheel);         }

这样修改后,直接使用帮助文档中提供的my_MouseWheel也有一个小bug,就是无论在窗体的任何位置滑动滚轮,图片都会缩放,所以需要对鼠标的位置做一下限制:

private void my_MouseWheel(object sender, MouseEventArgs e)         {             System.Drawing.Point pt = this.Location;             int leftBorder = hSmartWindowControl1.Location.X;             int rightBorder = hSmartWindowControl1.Location.X + hSmartWindowControl1.Size.Width;             int topBorder = hSmartWindowControl1.Location.Y;             int bottomBorder = hSmartWindowControl1.Location.Y + hSmartWindowControl1.Size.Height;             if(e.X > leftBorder && e.X < rightBorder && e.Y > topBorder && e.Y < bottomBorder)             {                 MouseEventArgs newe = new MouseEventArgs(e.Button, e.Clicks,                                                      e.X - pt.X, e.Y - pt.Y, e.Delta);                 hSmartWindowControl1.HSmartWindowControl_MouseWheel(sender, newe);             }                    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!