public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
HObject ho_Image, ho_GrayImage, ho_Regions;
HObject ho_RegionFillUp, ho_ConnectedRegions, ho_SelectedRegions;
// Local control variables
HTuple hv_WindowID = null, hv_Width = null;
HTuple hv_Height = null, hv_Area = null, hv_Row = null;
HTuple hv_Column = null;
HTuple hv_AcqHandle = null;
int i = 0;
private void button1_Click(object sender, EventArgs e)
{
if (HDevWindowStack.IsOpen())
{
HOperatorSet.CloseWindow(HDevWindowStack.Pop());
}
HOperatorSet.GenEmptyObj(out ho_Image);
string ImagePath;
openFileDialog1.Filter = "图片文件|*.jpg;*.bmp;*.gif;*.png;*.jpeg";
openFileDialog1.RestoreDirectory = true;//选择路径
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
ImagePath = openFileDialog1.FileName;
ho_Image.Dispose();
HOperatorSet.ReadImage(out ho_Image, ImagePath);
HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
HOperatorSet.OpenWindow(0, 0, hWindowControl1.Width, hWindowControl1.Height, hWindowControl1.HalconWindow, "visible", "", out hv_WindowID);
HDevWindowStack.Push(hv_WindowID);
}
if (HDevWindowStack.IsOpen())
{
HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_Height, hv_Width);
}
if (HDevWindowStack.IsOpen())
{
HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
}
}
private void button2_Click(object sender, EventArgs e)
{
HOperatorSet.GenEmptyObj(out ho_GrayImage);
HOperatorSet.GenEmptyObj(out ho_Regions);
HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
//ho_GrayImage.Dispose();
//HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage);
//ho_Regions.Dispose();
//HOperatorSet.Threshold(ho_GrayImage, out ho_Regions, 38, 167);
//ho_RegionFillUp.Dispose();
//HOperatorSet.FillUp(ho_Regions, out ho_RegionFillUp);
//ho_ConnectedRegions.Dispose();
//HOperatorSet.Connection(ho_RegionFillUp, out ho_ConnectedRegions);
//ho_SelectedRegions.Dispose();
//HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area",
// "and", 32194, 43322.7);
//HOperatorSet.AreaCenter(ho_SelectedRegions, out hv_Area, out hv_Row, out hv_Column);
ho_GrayImage.Dispose();
HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage);
ho_Regions.Dispose();
HOperatorSet.Threshold(ho_GrayImage, out ho_Regions, 0, 77);
HOperatorSet.AreaCenter(ho_Regions, out hv_Area, out hv_Row, out hv_Column);
ho_Image.Dispose();
ho_GrayImage.Dispose();
ho_Regions.Dispose();
ho_RegionFillUp.Dispose();
ho_ConnectedRegions.Dispose();
ho_SelectedRegions.Dispose();
textBox1.Text = "面积:" + hv_Area + "," + "坐标:(" + hv_Row + "," + hv_Column + ")";
}
private void button3_Click(object sender, EventArgs e)
{
HOperatorSet.GenEmptyObj(out ho_Image);
HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb", -1, "false", "default", "[0] AX USB2.0", 0, -1, out hv_AcqHandle);
HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
//HOperatorSet.SetFramegrabberParam(hv_AcqHandle,"grab_timeout" ,2000);
timer1.Enabled = true;
if (HDevWindowStack.IsOpen())
{
HOperatorSet.CloseWindow(HDevWindowStack.Pop());
}
HOperatorSet.OpenWindow(0, 0, hWindowControl1.Width, hWindowControl1.Height, hWindowControl1.HalconWindow, "visible", "", out hv_WindowID);
HDevWindowStack.Push(hv_WindowID);
}
private void timer1_Tick(object sender, EventArgs e)
{
//HOperatorSet.OpenWindow(0, 0, hWindowControl1.Width, hWindowControl1.Height, hWindowControl1.HalconWindow, "visible", "", out hv_WindowID);
//HDevWindowStack.Push(hv_WindowID);
ho_Image.Dispose();
HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
if (HDevWindowStack.IsOpen())
{
HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_Height, hv_Width);
}
if (HDevWindowStack.IsOpen())
{
HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
}
}
private void timer2_Tick(object sender, EventArgs e)
{
button2.PerformClick();
}
private void button4_Click(object sender, EventArgs e)
{
timer2.Enabled = true;
}
}
}
09-20
1894