c# & Halcon 实现通过相机图像采集及处理

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 showpicture
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
HObject ho_image, ho_GrayImage, ho_regions;
HTuple hv_WindowHandle = null, hv_Height, hv_Width;
HTuple hv_AcqHandle = null;
private void button1_Click(object sender, EventArgs e)
{
string imagePath;
openFileDialog1.Filter = "JEPG文件|*.jpg*|BMP文件|*.bmp*";
openFileDialog1.RestoreDirectory = true;
HOperatorSet.GenEmptyObj(out ho_image);
HOperatorSet.GenEmptyObj(out ho_GrayImage);
HOperatorSet.GenEmptyObj(out ho_regions);
ho_image.Dispose();
if (openFileDialog1.ShowDialog() == DialogResult.OK) ;
imagePath = openFileDialog1.FileName;
HOperatorSet.ReadImage(out ho_image, imagePath);
}
private void button2_Click(object sender, EventArgs e)
{
HOperatorSet.OpenWindow(0, 0, hWindowControl1.Width, hWindowControl1.Height, hWindowControl1.HalconWindow, "", "", out hv_WindowHandle);
HDevWindowStack.Push(hv_WindowHandle);
ho_GrayImage.Dispose();
HOperatorSet.Rgb1ToGray(ho_image, out ho_GrayImage);
ho_regions.Dispose();
HOperatorSet.Threshold(ho_GrayImage, out ho_regions, 103, 145);
if (HDevWindowStack.IsOpen())
{
HOperatorSet.DispObj(ho_regions, HDevWindowStack.GetActive());
}
}
private void button3_Click(object sender, EventArgs e)
{
HOperatorSet.GenEmptyObj(out ho_image);
//Image Acquisition 01: Code generated by Image Acquisition 01
HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
-1, "false", "default", "[0] USB 2.0 Camera", 0, -1, out hv_AcqHandle);
HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
}
private void button4_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
HOperatorSet.OpenWindow(0, 0, hWindowControl1.Width, hWindowControl1.Height, hWindowControl1.HalconWindow, "", "", out hv_WindowHandle);
HDevWindowStack.Push(hv_WindowHandle);
}
private void button6_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
HOperatorSet.CloseFramegrabber(hv_AcqHandle);
ho_image.Dispose();
}
private void button7_Click(object sender, EventArgs e)
{
HOperatorSet.CloseWindow(HDevWindowStack.Pop());
}
private void timer1_Tick(object sender, EventArgs e)
{
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());
}
}
}
}
资源文件列表
showpicture/.vs/showpicture/v15/.suo , 42496
showpicture/showpicture/bin/Debug/halcondotnet.dll , 1507848
showpicture/showpicture/bin/Debug/halcondotnet.xml , 4582423
showpicture/showpicture/bin/Debug/showpicture.exe , 11776
showpicture/showpicture/bin/Debug/showpicture.pdb , 30208
showpicture/showpicture/Form1.cs , 3624
showpicture/showpicture/Form1.Designer.cs , 7169
showpicture/showpicture/Form1.resx , 6420
showpicture/showpicture/obj/Debug/DesignTimeResolveAssemblyReferences.cache , 9686
showpicture/showpicture/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache , 6971
showpicture/showpicture/obj/Debug/showpicture.csproj.CopyComplete , 0
showpicture/showpicture/obj/Debug/showpicture.csproj.CoreCompileInputs.cache , 42
showpicture/showpicture/obj/Debug/showpicture.csproj.FileListAbsolute.txt , 1084
showpicture/showpicture/obj/Debug/showpicture.csproj.GenerateResource.cache , 847
showpicture/showpicture/obj/Debug/showpicture.csprojAssemblyReference.cache , 10209
showpicture/showpicture/obj/Debug/showpicture.exe , 11776
showpicture/showpicture/obj/Debug/showpicture.Form1.resources , 180
showpicture/showpicture/obj/Debug/showpicture.pdb , 30208
showpicture/showpicture/obj/Debug/showpicture.Properties.Resources.resources , 180
showpicture/showpicture/Program.cs , 492
showpicture/showpicture/Properties/AssemblyInfo.cs , 1320
showpicture/showpicture/Properties/Resources.Designer.cs , 2835
showpicture/showpicture/Properties/Resources.resx , 5612
showpicture/showpicture/Properties/Settings.Designer.cs , 1098
showpicture/showpicture/Properties/Settings.settings , 249
showpicture/showpicture/showpicture.csproj , 3541
showpicture/showpicture.sln , 1132
showpicture/.vs/showpicture/v15/Server/sqlite3/db.lock , 0
showpicture/.vs/showpicture/v15/Server/sqlite3/storage.ide , 528384
showpicture/.vs/showpicture/v15/Server/sqlite3/storage.ide-shm , 32768
showpicture/.vs/showpicture/v15/Server/sqlite3/storage.ide-wal , 4445512
