Halcon 联合C# 编程实验

2022/1/4 20:04:11

本文主要是介绍Halcon 联合C# 编程实验,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace CsharpHaldemon2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        #region HObject 转换为 HImage
        private void HobjectToHimage(HObject hobject, ref HImage image)
        {
            HTuple pointer, type, width, height;
            HOperatorSet.GetImagePointer1(hobject, out pointer, out type, out width, out height);
            image.GenImage1(type, width, height, pointer);
        }
        #endregion

        #region 设置图片与窗口等比例
        private void WindowAdaptation(int width,int height, out HTuple row1, out HTuple column1, out HTuple row2, out HTuple column2)
        {
            double ratioWidth = (1.0) * width / hWindowControl1.Width;
            double ratioHeight = (1.0) * height / hWindowControl1.Height;
            if (ratioWidth > ratioHeight)
            {
                row1 = -(1.0) * ((hWindowControl1.Height * ratioWidth) - height) / 2;
                column1 = 0;
                row2 = row1 + hWindowControl1.Height * ratioWidth;
                column2 = column1 + hWindowControl1.Width * ratioWidth;
            }
            else
            {
                row1 = 0;
                column1 = -(1.0) * ((hWindowControl1.Width * ratioHeight) - width) / 2;

                row2 = row1 + hWindowControl1.Height * ratioHeight;
                column2 = column1 + hWindowControl1.Width * ratioHeight;
            }
        }
        #endregion

        private CancellationTokenSource cts = new CancellationTokenSource(); 
        private void btnOpenPic_Click(object sender, EventArgs e)
        {

            //HObject ho_Image, ho_ROI_0, ho_Cross;
            //HXLD
            //HXLDCont
            //HXLDPoly //HXLDCont轮廓和HXLDPoly多边形

            //HImage ho_Image = new HImage();
            //HRegion ho_ROI_0 = new HRegion(); //区域
            //HXLDCont ho_Cross = new HXLDCont(); //轮廓
            //ho_Image.ReadImage(@"C:\Users\ZG\Pictures\claudia.png");
            //ho_ROI_0.GenRectangle1(30.0, 20, 100, 200);
            //hWindowControl1.HalconWindow.SetColor("blue");
            //ho_ROI_0.DispRegion(hWindowControl1.HalconWindow);

            //hWindowControl1.HalconWindow.SetColor("green");
            //ho_Cross.GenCrossContourXld(180.0, 180, 61, 0.785398);
            //ho_Cross.DispXld(hWindowControl1.HalconWindow);
            //this.hSmartWindowControl1.SetFullImagePart(ho_Image);
            //this.hSmartWindowControl1.HalconWindow.DispObj(ho_Image);

            //this.hWindowControl1.SetFullImagePart(ho_Image);
            //this.hWindowControl1.HalconWindow.DispObj(ho_Image);



            HObject ho_Image;
            HImage ho_Image1 = new HImage();
            HXLDCont ho_Cross = new HXLDCont(); //轮廓
            HTuple hv_AcqHandle = new HTuple();
            HTuple row1, column1, row2, column2;
            int width, height;
            Task.Run(() =>
            {
                HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
                -1, "false", "default", "[0] Chicony USB2.0 Camera", 0, -1, out hv_AcqHandle);
                HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
                //调用摄像头异步采集图像
                HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
                //关闭句柄
                HOperatorSet.CloseFramegrabber(hv_AcqHandle);
                //把obj变为Himage 
                HobjectToHimage(ho_Image, ref ho_Image1);
                //获取图像的长宽
                ho_Image1.GetImageSize(out width, out height);

                //XLD边缘提取

                ho_Cross = ho_Image1.Rgb1ToGray().EdgesSubPix("canny", 2, 12, 22);

                WindowAdaptation(width, height, out row1, out column1, out row2, out column2);

                HOperatorSet.SetPart(hWindowControl1.HalconWindow, row1, column1, row2, column2);

                //ho_Image1.DispImage(hWindowControl1.HalconWindow);
                ho_Cross.DispXld(hWindowControl1.HalconWindow);
            }, cts.Token);
        }

        private void btnThreshold_Click(object sender, EventArgs e)
        {
            int width, height;
            HTuple row1, column1, row2, column2;
            HImage ho_Image = new HImage();
            HRegion ho_ROI_0 = new HRegion();
            ho_Image.ReadImage(@"C:/Users/ZG/Pictures/clip.png");
            ho_Image.GetImageSize(out width, out height);
            ho_ROI_0 = ho_Image.Rgb1ToGray().Threshold(0.0, 106).Connection().SelectShape("area", "and", 150, 99999);
            WindowAdaptation(width, height, out row1, out column1, out row2, out column2);

            hWindowControl1.HalconWindow.SetColor("blue");
            HOperatorSet.SetPart(hWindowControl1.HalconWindow, row1, column1, row2, column2);
            ho_ROI_0.DispRegion(hWindowControl1.HalconWindow);
        }

        private void picConvertXLD_Click(object sender, EventArgs e)
        {
            int width, height;
            HTuple row1, column1, row2, column2;
            HImage ho_Image = new HImage();
            HRegion ho_ROI_0 = new HRegion();
            HXLDCont ho_Cross = new HXLDCont(); //轮廓
            ho_Image.ReadImage(@"C:/Users/ZG/Pictures/fabrik.png");
            ho_Cross = ho_Image.Rgb1ToGray().EdgesSubPix("canny", 2, 12, 22);

            //适配窗口
            ho_Image.GetImageSize(out width, out height);
            WindowAdaptation(width, height, out row1, out column1, out row2, out column2);
            HOperatorSet.SetPart(hWindowControl1.HalconWindow, row1, column1, row2, column2);
            ho_Cross.DispXld(hWindowControl1.HalconWindow);
        }
    }
    #region 设置图片与窗口等比例
    //ho_Image.GetImageSize(out width, out height);
    //double ratioWidth = (1.0) * width / hWindowControl1.Width;
    //double ratioHeight = (1.0) * height / hWindowControl1.Height;

    //if (ratioWidth > ratioHeight)
    //{
    //    row1 = -(1.0) * ((hWindowControl1.Height * ratioWidth) - height) / 2;
    //    column1 = 0;
    //    row2 = row1 + hWindowControl1.Height * ratioWidth;
    //    column2 = column1 + hWindowControl1.Width * ratioWidth;
    //}
    //else
    //{
    //    row1 = 0;
    //    column1 = -(1.0) * ((hWindowControl1.Width * ratioHeight) - width) / 2;

    //    row2 = row1 + hWindowControl1.Height * ratioHeight;
    //    column2 = column1 + hWindowControl1.Width * ratioHeight;
    //}
    #endregion
}


边缘亚像素提取

在这里插入图片描述



这篇关于Halcon 联合C# 编程实验的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程