找出图片有几个点,合适大小的点
read_image (SsSingle10, 'C:/ss_single_10.tiff')
gen_rectangle1 (ROI_1, 137.948, 99.8922, 1300.91, 1568.13)//画区域
reduce_domain (SsSingle10, ROI_1, ImageReduced)//删除不必要的区域
threshold(ImageReduced, ROI_1, 4000, 20000)//灰度值分割
connection (ROI_1, ConnectedRegions)//把区域连接
select_shape (ConnectedRegions, selectedRegions, 'area', 'and', 1150,5000)//选择形状或者区域,这里按大小区分
count_obj(selectedRegions,NumBalls)//计算合适大小个数
area_center(selectedRegions, Area, Row, Column)//这个我也不知道干嘛的
效果如图:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.button3.Enabled = true;
}
HDevelopExport HD = new HDevelopExport();
string ImagePath;
public static int aa;
public void button3_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "选择文件";
openFileDialog.Filter = "JPEG文件|*.jpg*|BMP文件|*.bmp*|TIFF文件|*.tiff*";
openFileDialog.FileName = string.Empty;
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;
openFileDialog.DefaultExt = "tiff";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
ImagePath = openFileDialog.FileName;
HObject image;
HTuple width, height;
HOperatorSet.ReadImage(out image, ImagePath);
HOperatorSet.GetImageSize(image, out width, out height);
HOperatorSet.SetPart(hWindowControl2.HalconWindow, 0, 0, height-1, width-1);
HOperatorSet.DispObj(image, hWindowControl2.HalconWindow);
this.button4.Enabled = true;
}
}
public void button4_Click_1(object sender, EventArgs e)
{
// Local iconic variables
HObject image, ho_ROI_1, ho_ConnectedRegions;
HObject ho_selectedRegions;
// Local control variables
HTuple hv_NumBalls = null, hv_Area = null;
HTuple hv_Row = null, hv_Column = null;
// Initialize local and output iconic variables
HOperatorSet.GenEmptyObj(out image);
HOperatorSet.GenEmptyObj(out ho_ROI_1);
HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
HOperatorSet.GenEmptyObj(out ho_selectedRegions);
image.Dispose();
HOperatorSet.ReadImage(out image, ImagePath);
ho_ROI_1.Dispose();
HOperatorSet.GenRectangle1(out ho_ROI_1, 137.948, 99.8922, 1300.91, 1568.13);
ho_ROI_1.Dispose();
HOperatorSet.Threshold(image, out ho_ROI_1, 4000, 20000);
ho_ConnectedRegions.Dispose();
HOperatorSet.Connection(ho_ROI_1, out ho_ConnectedRegions);
ho_selectedRegions.Dispose();
HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_selectedRegions, "area",
"and", 1150, 5000);
HOperatorSet.CountObj(ho_selectedRegions, out hv_NumBalls);
HOperatorSet.AreaCenter(ho_selectedRegions, out hv_Area, out hv_Row, out hv_Column);
HOperatorSet.SetPart(hWindowControl2.HalconWindow, 0, 0, 1460, 1920);
HOperatorSet.DispObj(ho_selectedRegions, hWindowControl2.HalconWindow);
//aa = hv_Area;
int num = hv_NumBalls;
textBox2.Text = num.ToString();
richTextBox2.Text = hv_Area.ToString();
CsvHelper w = new CsvHelper();
w.WriteCsv("C:\\hhhhh.csv", hv_Area);
//StreamWriter fileWriter = new StreamWriter("C:\\test1.csv", true, Encoding.Default);
//fileWriter.WriteLine(String.Join(",", hv_Area));
//fileWriter.Flush();
//fileWriter.Close();
image.Dispose();
ho_ROI_1.Dispose();
ho_ConnectedRegions.Dispose();
ho_selectedRegions.Dispose();
}
}