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()); } } }}
评论