using HalconDotNet;
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// 设置刻度的位置为底部
trackBar1.TickStyle = TickStyle.Both;
trackBar2.TickStyle = TickStyle.Both;
trackBar3.TickStyle = TickStyle.Both;
// 设置刻度之间的间隔
trackBar1.TickFrequency = 1000;
trackBar1.TickFrequency = 1;
trackBar1.TickFrequency = 1;
// 设置刻度的范围
trackBar1.Scroll += trackBar1_Scroll;
trackBar2.Scroll += trackBar2_Scroll;
trackBar3.Scroll += trackBar3_Scroll;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void ImageAdaptive(double width, double height)
{
double ratioWidth = (1.0) * width / hWindowControl1.Width;
double ratioHeight = (1.0) * height / hWindowControl1.Height;
HTuple row1, column1, row2, column2;
if (ratioWidth >= ratioHeight)
{
row1 = -(1.0) * ((hWindowControl1.Height * ratioWidth) - height) / 2;
column1 = 0;
row2 = row1 + hWindowControl1.Height * ratioWidth;
column2 = column1 + hWindowControl1.Width * ratioWidth;
}
else
{
row1 = 0;
column1 = -(1.0) * ((hWindowControl1.Width * ratioHeight) - width) / 2;
row2 = row1 + hWindowControl1.Height * ratioHeight;
column2 = column1 + hWindowControl1.Width * ratioHeight;
}
HOperatorSet.SetPart(hWindowControl1.HalconWindow, row1, column1, row2, column2);
}
bool b = false;
HTuple MeanImage_num = new HTuple();
HTuple DilationCircle_num = new HTuple();
HTuple SelectShape_num = new HTuple();
HObject ho_Image; HObject ho_ImageMean, ho_DarkPixels;
HObject ho_ConnectedRegions, ho_SelectedRegions, ho_RegionUnion;
HObject ho_RegionDilation, ho_Skeleton, ho_Errors, ho_Scratches;
HObject ho_Dots;
HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
HTuple hv_WindowID = new HTuple(), hv_Message = new HTuple();
private void button3_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
HOperatorSet.ReadImage(out ho_Image, "C:\\Users\\yang\\Desktop\\halcon案例\\不均匀表面划伤检测/image.png");
HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
ImageAdaptive(hv_Width, hv_Height);
//均值滤波,滤波核大小为7*7
if (b)
{
HOperatorSet.MeanImage(ho_Image, out ho_ImageMean, MeanImage_num, MeanImage_num);
}
else
{
HOperatorSet.MeanImage(ho_Image, out ho_ImageMean, 7, 7);
}
//用局部阈值进行分割
HOperatorSet.DynThreshold(ho_Image, ho_ImageMean, out ho_DarkPixels, 5, "dark");
//联通处理
HOperatorSet.Connection(ho_DarkPixels, out ho_ConnectedRegions);
//输出对象的显示颜色的数目
HOperatorSet.SetColored(hWindowControl1.HalconWindow, 12);
HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
HOperatorSet.DispObj(ho_ConnectedRegions, hWindowControl1.HalconWindow);
//过滤出缺陷区域
if (b)
{
HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area",
"and", 10, SelectShape_num);
}
else
{
HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area",
"and", 10, 1000);
}
HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
HOperatorSet.DispObj(ho_SelectedRegions, hWindowControl1.HalconWindow);
//将SelectedRegions中的所有区域合并
HOperatorSet.Union1(ho_SelectedRegions, out ho_RegionUnion);
//对合并区域进行膨胀处理,其结构元素为圆形,半径为3.5
if (b)
{
HOperatorSet.DilationCircle(ho_RegionUnion, out ho_RegionDilation, DilationCircle_num);
}
else
{
HOperatorSet.DilationCircle(ho_RegionUnion, out ho_RegionDilation, 3.5);
}
HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
HOperatorSet.DispObj(ho_RegionDilation, hWindowControl1.HalconWindow);
//对膨胀后的区域进行求骨架
HOperatorSet.Skeleton(ho_RegionDilation, out ho_Skeleton);
//对获取的骨架区域进行连通处理
HOperatorSet.Connection(ho_Skeleton, out ho_Errors);
HOperatorSet.SetColored(hWindowControl1.HalconWindow, 12);
HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
HOperatorSet.DispObj(ho_Errors, hWindowControl1.HalconWindow);
//过滤出大的缺陷
HOperatorSet.SelectShape(ho_Errors, out ho_Scratches, "area", "and", 50, 10000);
//过滤出小的缺陷
HOperatorSet.SelectShape(ho_Errors, out ho_Dots, "area", "and", 1, 50);
HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
HOperatorSet.SetColor(hWindowControl1.HalconWindow, "red");
HOperatorSet.DispObj(ho_Scratches, hWindowControl1.HalconWindow);
HOperatorSet.SetColor(hWindowControl1.HalconWindow, "blue");
HOperatorSet.DispObj(ho_Dots, hWindowControl1.HalconWindow);
//b = false;
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
label1.Text = trackBar1.Value.ToString();
}
private void trackBar2_Scroll(object sender, EventArgs e)
{
label4.Text = trackBar2.Value.ToString();
}
private void trackBar3_Scroll(object sender, EventArgs e)
{
label6.Text = trackBar3.Value.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
b = true;
this.button2.Click += new System.EventHandler(button1_Click);
SelectShape_num = trackBar1.Value;
MeanImage_num = trackBar2.Value;
DilationCircle_num = trackBar3.Value;
}
}
}