当前位置:网站首页>Halcon joint C # Programming Experiment

Halcon joint C # Programming Experiment

2022-01-15 02:11:40 La lave de Rodinia

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 Convertir en 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  Définir l'image à l'échelle de la fenêtre 
        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 CancellationTokenSource cts1 = new CancellationTokenSource();
        private void btnOpenPic_Click(object sender, EventArgs e)
        {
    

            //HObject ho_Image, ho_ROI_0, ho_Cross;
            //HXLD
            //HXLDCont
            //HXLDPoly //HXLDContContour etHXLDPolyPolygone

            //HImage ho_Image = new HImage();
            //HRegion ho_ROI_0 = new HRegion(); //Région
            //HXLDCont ho_Cross = new HXLDCont(); //Profil
            //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(); //Profil
            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);
                // Appelez la caméra pour obtenir des images asynchrones 
                HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
                //Fermer la poignée
                HOperatorSet.CloseFramegrabber(hv_AcqHandle);
                //Prends ça.objDevientHimage 
                HobjectToHimage(ho_Image, ref ho_Image1);
                // Obtenir la longueur et la largeur de l'image 
                ho_Image1.GetImageSize(out width, out height);

                //XLDExtraction de bord

                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);
        }
        //threshouldSegmentation des seuils
        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(); //Profil
            ho_Image.ReadImage(@"C:/Users/ZG/Pictures/fabrik.png");
            ho_Cross = ho_Image.Rgb1ToGray().EdgesSubPix("canny", 2, 12, 22);

            // Adapter la fenêtre 
            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);
        }

        private void btnThresholdMe_Click(object sender, EventArgs e)
        {
    
            HObject ho_Image;
            HImage ho_Image1 = new HImage();
            HRegion ho_ROI_0 = new HRegion();
            HXLDCont ho_Cross = new HXLDCont(); //Profil
            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);
                // Appelez la caméra pour obtenir des images asynchrones 
                HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
                //Fermer la poignée
                HOperatorSet.CloseFramegrabber(hv_AcqHandle);
                //Prends ça.objDevientHimage 
                HobjectToHimage(ho_Image, ref ho_Image1);
                // Obtenir la longueur et la largeur de l'image 
                ho_Image1.GetImageSize(out width, out height);

                //XLDExtraction de bord

                ho_ROI_0 = ho_Image1.Rgb1ToGray().Threshold(75.0, 122).Connection().SelectShape("area", "and", 150, 46614.6);

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

                HOperatorSet.SetPart(hWindowControl1.HalconWindow, row1, column1, row2, column2);
                hWindowControl1.HalconWindow.SetColor("blue");
                //ho_Image1.DispImage(hWindowControl1.HalconWindow);
                ho_ROI_0.DispRegion(hWindowControl1.HalconWindow);
                //ho_Cross.DispXld(hWindowControl1.HalconWindow);
            }, cts1.Token);
        }
    }
    #region  Définir l'image à l'échelle de la fenêtre 
    //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
}


Extraction de sous - pixels de bord

Insérer la description de l'image ici

版权声明
本文为[La lave de Rodinia]所创,转载请带上原文链接,感谢
https://chowdera.com/2022/01/202201080558306948.html

随机推荐