dev_close_window()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
read_image (Image, 'D:/picture/IMG_20190427_200621.jpg')
*转换成灰度图(R,G,B)
decompose3 (Image, r, g, b)
*转换成HSV图
trans_from_rgb (r, g, b, h, s,v,'hsv')
*灰度提取 提取64以下
threshold (g, Regions, 0, 64)
*填充空隙
fill_up (Regions, RegionFillUp1)
*去噪
connection (RegionFillUp1, ConnectedRegions1)
*形态学开运算,对腐蚀结果进行膨胀,使区域更明显
opening_circle (ConnectedRegions1, RegionOpening, 30)
*选择区域 面积在10000以上
select_shape (RegionOpening, SelectedRegions, 'area', 'and', 10000, 1000000000)
*画轮廓
boundary (SelectedRegions, RegionBorder, 'inner')
*数数
count_obj (SelectedRegions, Number)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HalconDotNet;
namespace tuxduqu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//label2.Text = DateTime.Now.ToLongTimeString().ToString();
string []daysOfWeek =
new[]{ "10mm", "11mm", "13mm",
"14mm", "15mm", "16mm",
"17mm" };
comboBox1.DataSource = daysOfWeek;
}
HObject ho_Img20190425182757;
string str = "";
int number1 = 0;
private void b1_Click_1(object sender, EventArgs e)
{
HOperatorSet.GenEmptyObj(out ho_Img20190425182757);
OpenFileDialog openfile = new OpenFileDialog();
if (openfile.ShowDialog() == DialogResult.OK && openfile.FileName != "")
{
str = openfile.FileName;
}
HOperatorSet.ReadImage(out ho_Img20190425182757, str);
HTuple hv_Width, hv_Height;
HOperatorSet.GetImageSize(ho_Img20190425182757, out hv_Width, out hv_Height);
HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, hv_Height - 1, hv_Width - 1);
hWindowControl1.HalconWindow.DispObj(ho_Img20190425182757);
// while (true)
//{
// label2.Text = DateTime.Now.ToLongTimeString().ToString();
// Application.DoEvents();
// System.Threading.Thread.Sleep(100);
// }
}
private void button1_Click(object sender, EventArgs e)
{
HObject ho_Image, ho_r, ho_g, ho_b, ho_h, ho_s;
HObject ho_v, ho_Regions, ho_RegionFillUp1, ho_ConnectedRegions1;
HObject ho_RegionOpening, ho_SelectedRegions, ho_RegionBorder;
HObject ho_Image1;
HTuple hv_Number = null;
HOperatorSet.GenEmptyObj(out ho_Image);
HOperatorSet.GenEmptyObj(out ho_r);
HOperatorSet.GenEmptyObj(out ho_g);
HOperatorSet.GenEmptyObj(out ho_b);
HOperatorSet.GenEmptyObj(out ho_h);
HOperatorSet.GenEmptyObj(out ho_s);
HOperatorSet.GenEmptyObj(out ho_v);
HOperatorSet.GenEmptyObj(out ho_Regions);
HOperatorSet.GenEmptyObj(out ho_RegionFillUp1);
HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
HOperatorSet.GenEmptyObj(out ho_RegionOpening);
HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
HOperatorSet.GenEmptyObj(out ho_RegionBorder);
HOperatorSet.GenEmptyObj(out ho_Image1);
try
{
HOperatorSet.ReadImage(out ho_Image, str);
//ho_r.Dispose(); ho_g.Dispose(); ho_b.Dispose();
HOperatorSet.Decompose3(ho_Image, out ho_r, out ho_g, out ho_b);
//ho_h.Dispose(); ho_s.Dispose(); ho_v.Dispose();
HOperatorSet.TransFromRgb(ho_r, ho_g, ho_b, out ho_h, out ho_s, out ho_v, "hsv");
//ho_Regions.Dispose();
HOperatorSet.Threshold(ho_g, out ho_Regions, 0, 64);
//填充
HOperatorSet.FillUp(ho_Regions, out ho_RegionFillUp1);
//去小点儿
//ho_ConnectedRegions1.Dispose();
HOperatorSet.Connection(ho_RegionFillUp1, out ho_ConnectedRegions1);
//选择区域
// ho_RegionOpening.Dispose();
HOperatorSet.OpeningCircle(ho_ConnectedRegions1, out ho_RegionOpening, 30);
// ho_SelectedRegions.Dispose();
HOperatorSet.SelectShape(ho_RegionOpening, out ho_SelectedRegions, "area",
"and", 10000, 1000000000);
//画轮廓
ho_RegionBorder.Dispose();
HOperatorSet.Boundary(ho_SelectedRegions, out ho_RegionBorder, "inner");
HOperatorSet.CountObj(ho_SelectedRegions, out hv_Number);
textBox1.Text = hv_Number.ToString();
}
catch (HalconException HDevExpDefaultException)
{
ho_Image.Dispose();
ho_r.Dispose();
ho_g.Dispose();
ho_b.Dispose();
ho_h.Dispose();
ho_s.Dispose();
ho_v.Dispose();
ho_Regions.Dispose();
ho_RegionFillUp1.Dispose();
ho_ConnectedRegions1.Dispose();
ho_RegionOpening.Dispose();
ho_SelectedRegions.Dispose();
ho_RegionBorder.Dispose();
ho_Image1.Dispose();
throw HDevExpDefaultException;
}
ho_Image.Dispose();
ho_r.Dispose();
ho_g.Dispose();
ho_b.Dispose();
ho_h.Dispose();
ho_s.Dispose();
ho_v.Dispose();
ho_Regions.Dispose();
ho_RegionFillUp1.Dispose();
ho_ConnectedRegions1.Dispose();
ho_RegionOpening.Dispose();
ho_SelectedRegions.Dispose();
ho_RegionBorder.Dispose();
ho_Image1.Dispose();
}
private void 当前时间_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void hWindowControl1_HMouseMove(object sender, HMouseEventArgs e)
{
}
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void label2_Click_1(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
// Application.Exit();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
System.Environment.Exit(0);
}
private void label5_Click(object sender, EventArgs e)
{
}
private void label7_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
if (number1 == Convert.ToInt16(textBox2.Text))
{
label7.Text = "合格";
}
else
{
label7.Text = "不合格";
}
}
}
}
推荐学习网站 :https://www.51zxw.net/list.aspx?page=3&cid=710