简介
本Demo采用Halcon联合C#编程实现加载图片显示第一个显示窗口,并进行处理,将处理结果显示在第二个窗口上。
窗体界面设计
界面摆放两个HWindowControl显示控件和两个按钮控件,第一个按钮负责加载图片将图片显示在第一个显示控件上,第二个按钮负责进行图像处理并且将结果显示在第二个显示控件上。
程序源码
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace ShowProcessing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
HObject OriginImage= new HObject();
private void LoadImage_Click(object sender, EventArgs e)
{
HOperatorSet.GenEmptyObj(out OriginImage);
using(OpenFileDialog ofg = new OpenFileDialog())
{
ofg.Title = "图片";
ofg.Multiselect = false;
ofg.Filter = "图像|*.png;*.bmp;*.jpg";
if(ofg.ShowDialog() == DialogResult.OK)
{
HOperatorSet.ReadImage(out OriginImage, ofg.FileName);
HOperatorSet.GetImageSize(OriginImage,out HTuple OriginImageWith, out HTuple OriginImageHeight);
hWindowControl1.HalconWindow.SetPart(0, 0, (int)(OriginImageHeight - 1), (int)(OriginImageWith - 1));
hWindowControl1.HalconWindow.DispObj(OriginImage);
}
}
}
private void ThresholdSegmentation_Click(object sender, EventArgs e)
{
HOperatorSet.Rgb1ToGray(OriginImage, out HObject GrayImage);
HOperatorSet.Threshold(OriginImage, out HObject Region, 50, 138);
HOperatorSet.PaintRegion(Region, OriginImage,out HObject ImageResult, ((new HTuple(0)).TupleConcat(
0)).TupleConcat(255), "margin");
hWindowControl2.HalconWindow.DispObj(ImageResult);
}
}
}