要实现c#创建窗体调用图片进行特征提取、特征筛选、计数的方法
第一步:创建窗体(具体请参考C#调用Haclon)可以自己根据喜好来排版
实现的最终效果:
第二步:创建RectangleDef类(前三个窗体的具体实现请参考C#与halcon联合函数封装)
第四步:在RectangleDef类中创建这次需要的类(代码及注释如下):
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithm
{
/// <summary>
/// 矩形的参数类
/// </summary>
public class RectangleDef
{
/// <summary>
/// 矩形的中行坐标
/// </summary>
public HTuple _CenterRow;
/// <summary>
/// 矩形的中心列坐标
/// </summary>
public HTuple _CenterCol;
/// <summary>
/// 矩形的旋转角度
/// </summary>
public HTuple Angle;
public HTuple len1;
public HTuple len2;
}
}
第五步:基于(C#与halcon联合函数封装中ImageHelp类)进行修改,添加最后计数和特征筛选的相关代码
ImageHelp类代码及注释如下:
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Algorithm
{
/// <summary>
/// 这个类就是我图像处理的算法类
/// </summary>
public class ImageHelp
{
public static string[] Color = new string[12] { "red", "green", "blue", "cyan", "magenta", "yellow", "coral", "spring green", "orange", "orange red", "pink", "navy" };
/// <summary>
/// 这是一个读取图片 并且在指定窗体上显示的方法
/// </summary>
/// <param name="hImage"></param>
/// <param name="filename"></param>
/// <returns></returns>
public static bool ReadImage(out HObject hImage, string filename, HWindow window)
{
try
{
HOperatorSet.ReadImage(out hImage, filename);
HTuple width, height;
//获得图片的宽高
HOperatorSet.GetImageSize(hImage, out width, out height);
//设置显示范围
//HWindowControl.HalconWindow -->控件的句柄 设置显示范围
HOperatorSet.SetPart(window, 0, 0, (height - 1), (width - 1));
//显示
HOperatorSet.DispObj(hImage, window);
return true;
}
catch
{
hImage = null;
return false;
}
}
public static bool ReadImage(out HObject hImage, OpenFileDialog openFileDialog)
{
//给halcon变量置空
hImage = null;
//给halcon变量置空
//HOperatorSet.GenEmptyObj(out hImage);
// openFileDialog.ShowDialog() -->返回值 OK 和Cancel
//返回ok就应该能读取到指定文件的路径
DialogResult result = openFileDialog.ShowDialog();
if (result == DialogResult.OK)
{
HOperatorSet.ReadImage(out hImage, openFileDialog.FileName);
return true;
}
else
{
return false;
}
}
/// <summary>
/// 只单单显示图片
/// </summary>
/// <param name="hImage"></param>
/// <param name="window"></param>
/// <returns></returns>
public static bool HalconWindowShowImage(HObject hImage, HWindow window)
{
try
{
HTuple width;
HTuple height;
HOperatorSet.GetImageSize(hImage, out width, out height);
//设置显示区域 将图片左上角 和右下角形成的正矩形区域 作为窗体的显示区域
HTuple LeftTopRow = 0;
HTuple LeftTopCol = 0;
HTuple RightTopRow = height;
HTuple RightTopCol = width;
HOperatorSet.SetPart(window, LeftTopRow, LeftTopCol, RightTopRow - 1, RightTopCol - 1);
//显示
HOperatorSet.DispObj(hImage, window);
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 图片和对象一起显示
/// </summary>
/// <param name="hImage"></param>
/// <param name="Region"></param>
/// <param name="window"></param>
/// <param name="color"></param>
/// <returns></returns>
public static bool HalconWindowShowImage(HObject hImage, HObject Region, HWindow window, string[] color)
{
try
{
HTuple width;
HTuple height;
HOperatorSet.GetImageSize(hImage, out width, out height);
//设置显示区域 将图片左上角 和右下角形成的正矩形区域 作为窗体的显示区域
HTuple LeftTopRow = 0;
HTuple LeftTopCol = 0;
HTuple RightTopRow = height;
HTuple RightTopCol = width;
HOperatorSet.SetPart(window, LeftTopRow, LeftTopCol, RightTopRow - 1, RightTopCol - 1);
//显示
HOperatorSet.DispObj(hImage, window);
HTuple NUMS;
HOperatorSet.CountObj(Region, out NUMS);//计算Region的个数
int index = 0;
for (int i = 0; i < NUMS; i++, index++)
{
HObject selectRrgion;
HOperatorSet.SetColor(window, color[index]);
HOperatorSet.SelectObj(Region, out selectRrgion, i + 1);
HOperatorSet.DispObj(selectRrgion, window);
if (index >= color.Length - 1)
{
index = 0;
}
}
return true;
}
catch (Exception)
{
return false;
}
}
#region 【阈值分割】
public static bool Threshold(HObject hImage, out HObject Region, int Min, int Max)
{
HOperatorSet.Threshold(hImage, out Region, Min, Max);
return true;
}
public static bool BinaryThreshold(HObject hImage, out HObject Region, 自动阈值分割方法 method, 亮或暗 lightAndDark)
{
HTuple use;
HOperatorSet.BinaryThreshold(hImage, out Region, method.ToString(), lightAndDark.ToString(), out use);
return true;
}
public static bool ThresholdALL(HObject hImage, out HObject Region, ThreSholdDef threSholdDef)
{
Region = null;
switch (threSholdDef._Segment)
{
case 阈值分割方式.Threshold:
Console.WriteLine("我调用了普通阈值分割 Threshold");
return Threshold(hImage, out Region, threSholdDef._MinGray, threSholdDef._MaxGray);
case 阈值分割方式.BinaryThreshold:
Console.WriteLine("我调用了自动阈值分割 BinaryThreshold");
return BinaryThreshold(hImage, out Region, threSholdDef._SegmentMothd, threSholdDef._LightOrDark);
default:
return false;
}
}
#endregion
/// <summary>
/// 根据输入区域 生成外接矩形 以及外接矩形的参数
/// </summary>
/// <param name="Region"></param>
/// <param name="ExternalRegion"></param>
/// <param name="rectangleDef"></param>
/// <param name="isMargin"></param>
/// <param name="hWindow"></param>
public static void GenRectangle(HObject Region, out HObject ExternalRegion, out RectangleDef rectangleDef, bool isMargin, HWindow hWindow)
{
if (isMargin == true)
{
HOperatorSet.SetDraw(hWindow, "margin");
}
else
{
HOperatorSet.SetDraw(hWindow, "fill");
}
rectangleDef = new RectangleDef();
HOperatorSet.SmallestRectangle2(Region, out rectangleDef._CenterRow, out rectangleDef._CenterCol, out rectangleDef.Angle, out rectangleDef.len1, out rectangleDef.len2);
HOperatorSet.GenRectangle2(out ExternalRegion, rectangleDef._CenterRow, rectangleDef._CenterCol, rectangleDef.Angle, rectangleDef.len1, rectangleDef.len2);
}
public static void ShowText(string Content, HWindow hWindow, HTuple ROW, HTuple COL)
{
HOperatorSet.DispText(hWindow, Content, "window", ROW, COL, "red", new HTuple(), new HTuple());
}
}
}
第六步:基于(C#与halcon联合函数封装中Form1类)进行修改,添加最后计数和特征筛选的相关代码
Form1代码及相关注释如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Algorithm;
using HalconDotNet;
namespace 第一个联合项目练习
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// comboBoxs--->调用ThreSholdel方法
comboBox1.Items.Add("area");
comboBox1.Items.Add("circularity");
comboBox1.Items.Add("rectangularity");
comboBox2.Items.Add("max_separability");
comboBox2.Items.Add("smooth_histo");
comboBox3.Items.Add("dark");
comboBox3.Items.Add("light");
//选择首个选项
comboBox1.SelectedIndex = 0;
comboBox2.SelectedIndex = 0;
comboBox3.SelectedIndex = 0;
}
HObject ho_image;
HTuple MinGray;
HTuple MaxGray;
HObject Region;
HTuple width, height;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK )
{
string filename = openFileDialog.FileName;
bool result = ImageHelp.ReadImage(out ho_image, filename, hWindowControl1.HalconWindow);
if (result != true)
{
MessageBox.Show(" Algorithm.ImageHelper.ReadImage 失败");
}
}
}
private void button2_Click(object sender, EventArgs e)
{
hWindowControl2.HalconWindow.ClearWindow();
if (ho_image == null)
{
button1_Click(null, null);
}
//设置颜色
HOperatorSet.SetColor(hWindowControl2.HalconWindow, "white");
//int.Parse(string)字符串类型转换成int类型
MaxGray = int.Parse(textBox1.Text);
MinGray = int.Parse(textBox2.Text);
ThreSholdDef threSholdDef = new ThreSholdDef(MinGray, MaxGray);
ImageHelp.ThresholdALL(ho_image, out Region, threSholdDef);
HOperatorSet.GetImageSize (ho_image, out width,out height);
//HWindowControl.HalconWindow --->窗体句柄 设置显示范围
HOperatorSet.SetPart(hWindowControl2.HalconWindow, 0, 0, (height - 1), (width - 1));
//显示
HOperatorSet.DispObj(Region, hWindowControl2.HalconWindow);
}
HObject ConnectionRegion;
HObject SelectRegion;
private void button3_Click(object sender, EventArgs e)
{
HOperatorSet.SetColor(hWindowControl3.HalconWindow, "green");
HTuple width, height;
//获得图片的宽高
HOperatorSet.GetImageSize(ho_image, out width, out height);
//设置显示范围
//HWindowControl.HalconWindow -->控件的句柄 设置显示范围
HOperatorSet.SetPart(hWindowControl3.HalconWindow, 0, 0, (height - 1), (width - 1));
//打散
HOperatorSet.Connection(Region, out ConnectionRegion);
//特征筛选
//HOperatorSet.SelectShape(ConnectionRegion,out SelectRegion, "area","and", 3227.85, 7897.33);
HOperatorSet.SelectShape(ConnectionRegion, out SelectRegion, comboBox1.SelectedItem.ToString(), "and", Double.Parse(textBox4.Text), Double.Parse(textBox3.Text));
//显示
HOperatorSet.DispObj(ho_image, hWindowControl3.HalconWindow);
HOperatorSet.DispObj(SelectRegion, hWindowControl3.HalconWindow);
}
private void button4_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
HOperatorSet.ReadImage(out ho_image, openFileDialog.FileName);
}
}
private void button6_Click(object sender, EventArgs e)
{
hWindowControl2.HalconWindow.ClearWindow();
if (ho_image == null)
{
button1_Click(null, null);
}
//设置颜色
HOperatorSet.SetColor(hWindowControl2.HalconWindow, "white");
ThreSholdDef threSholdDef = new ThreSholdDef(自动阈值分割方法.max_separability, 亮或暗.dark);
ImageHelp.ThresholdALL(ho_image, out Region, threSholdDef);
HOperatorSet.GetImageSize(ho_image, out width, out height);
//HWindowControl.HalconWindow --->窗体句柄 设置显示范围
HOperatorSet.SetPart(hWindowControl2.HalconWindow, 0, 0, (height - 1), (width - 1));
HOperatorSet.DispObj(Region, hWindowControl2.HalconWindow);
}
private void button5_Click(object sender, EventArgs e)
{
MessageBox.Show(comboBox1.SelectedItem.ToString());
}
private void button7_Click(object sender, EventArgs e)
{
string[] Color = new string[12] { "red", "green", "blue", "cyan", "magenta", "yellow", "coral", "spring green", "orange", "orange red", "pink", "navy" };
Console.WriteLine(files[i]);
//HObject hImage;
//Algorithm.ImageHelp.ReadImage(out hImage, files[i].FullName, hWindowControl4.HalconWindow);
//HObject Region;
//Algorithm.ThreSholdDef threSholdDef = new Algorithm.ThreSholdDef(0, 60);
//Algorithm.ImageHelp.ThresholdALL(hImage, out Region, threSholdDef);
//HObject ConnectionRegion;
//HOperatorSet.Connection(Region, out ConnectionRegion);
HObject RRegion;
Algorithm.RectangleDef rectangleDef;
Algorithm.ImageHelp.GenRectangle(SelectRegion, out RRegion, out rectangleDef, true, hWindowControl4.HalconWindow);
HTuple nums;
HOperatorSet.CountObj(RRegion, out nums);
Algorithm.ImageHelp.HalconWindowShowImage(ho_image, RRegion, hWindowControl4.HalconWindow, Color);
string str = "回形针数量" + nums.ToString();
Algorithm.ImageHelp.ShowText(str, hWindowControl4.HalconWindow, 12, 12);
System.Windows.Forms.Application.DoEvents();
Thread.Sleep(1000);
}
}
}
相关问题:1.调用相关类的时候注意相应的命名。2.按钮创建之后注意命名,一些情况下按钮名字会出现重复的状态。3.调用文件时注意命名空间,也可在开头用using+命名空间进行调用。4.创建函数名时最好创建在开头位置,避免调用出现错误。