C# combined with Halcon template matching + measurement (1)

Table of Contents

1. Halcon implementation

2. vs implementation

2.1 Interface settings

2.2 Preparation panel control object

2.3 Implement the function of loading images


1. Halcon implementation

Feel free to use the relevant template matching + measurement cases in the halcon case. The following code is for reference only.

************1..Create a positioning template************
dev_set_draw ('margin')
dev_set_line_width (2)
list_files ('C:/PTT/Image', 'files', Files)
tuple_regexp_select (Files, ['\.(png|jpg|bmp)'], ImageFiles)
read_image (Image, ImageFiles[0])
get_image_size (Image, Width, Height)
gen_rectangle1 (ROI_0, 185.922, 187.828, 303.797, 410.766)
line_orientation (185.922, 187.828, 303.797, 410.766, Phi)
area_center (ROI_0, Area, Row, Column)
reduce_domain (Image, ROI_0, ImageReduced)
create_shape_model (ImageReduced, 4, rad(0), rad(360), 'auto', 'auto', 'use_polarity', [20,40], 10, ModelID)
*Create template ModelContours
get_shape_model_contours (ModelContours, ModelID, 1)
gen_rectangle2 (ROI_check, 160, 302, 0, 162.776, 7)
* Generate measurement ROI
gen_rectangle2_contour_xld (Rectangle_XLD, 140, 302, 0, 162.776, 7)
*************2. Start template positioning************
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
hom_mat2d_identity (HomMat2DIdentity)
for Index := 0 to |ImageFiles|-1 by 1
    read_image (Images, ImageFiles[Index])
    find_shape_model (Images, ModelID, rad(0), rad(360), 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row1, Column1, Angle, Score)
    *Template affine
    hom_mat2d_translate (HomMat2DIdentity, Row1, Column1, HomMat2DTranslate)
    hom_mat2d_rotate (HomMat2DTranslate, Angle, Row1, Column1, HomMat2DRotate)
    affine_trans_contour_xld (ModelContours, ContoursAffineTrans, HomMat2DRotate)
    *Measure ROI affine
    vector_angle_to_rigid (Row, Column,0, Row1, Column1, Angle, HomMat2D)
    affine_trans_contour_xld (Rectangle_XLD, ContoursAffineTrans, HomMat2D)
    area_center_xld (ContoursAffineTrans, Area1, Row2, Column2, PointOrder)
    *3.Measurement
    scale_image (Images, ImageScaled, 2.5, 0)
    gen_measure_rectangle2 (Row2, Column2, Angle, 160, 7, Width, Height, 'nearest_neighbor', MeasureHandle)
    measure_pairs (ImageScaled, MeasureHandle, 1, 50, 'all', 'all', RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, IntraDistance1)
    gen_cross_contour_xld (Cross, RowEdgeFirst, ColumnEdgeFirst, 10, rad(45))
    
    *Visual observation of test results
    disp_line (WindowHandle, RowEdgeFirst-8*cos(Angle), ColumnEdgeFirst-8*sin(Angle), RowEdgeFirst + 8*cos(Angle), ColumnEdgeFirst + 8*sin(Angle))
    disp_line (WindowHandle, RowEdgeSecond-8*cos(Angle), ColumnEdgeSecond-8*sin(Angle), RowEdgeSecond + 8*cos(Angle), ColumnEdgeSecond + 8*sin(Angle))
    *calculate
    distance1:=mean([IntraDistance,IntraDistance1])
    distanceMin:=min([IntraDistance,IntraDistance1])
    *The results show
    dev_disp_text ('The average pin spacing is:' + distance1$'0.4' + 'px', 'window', 12, 12, 'black', [], [])
    dev_disp_text ('The minimum value of pin spacing is:' + distanceMin$'0.4' + 'px', 'window', 32, 12, 'black', [], [])
    dev_disp_text ('The total number of pins is:' + |[IntraDistance,IntraDistance1]|, 'window', 52, 12, 'black', [], [])
    close_measure(MeasureHandle)
    dev_close_window ()
endfor

2. vs implementation

2.1 Interface Settings

Add corresponding controls according to requirements. In this case, panel is used to implement image display.

2.2 Preparation work-setting panel control object

It can be understood as: the pointer/interface that controls the image window

Note the statement that generates the image control in the periphery: private HSmartWindowControl hsWControl;

private void Form1_Load(object sender, EventArgs e)
        {
            //Create an image control object and add it to the panel container by filling it.
            hsWControl = new HSmartWindowControl();
            hsWControl.Dock = DockStyle.Fill;
            panel1.Controls.Add(hsWControl);
        }

2.3 Implement the image loading function

Note the declaration of the peripheral generated image variable private HImage ho_imge;

 private void button1_Click_1(object sender, EventArgs e)
        {
            //Use the file dialog class OpenFileDialog to specify filter files
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Select template image|*.png";
            if(ofd.ShowDialog()==DialogResult.OK )
            {
                //Get the path, create an image through the path, display it, and set the image to adapt to the container size (proportional scaling without distortion)
                string filePath = ofd.FileName;
                ho_imge = new HImage(filePath);
                hsWControl.HalconWindow.DispObj(ho_imge);
                hsWControl.SetFullImagePart();
            }
        }

Extra +

OpenfileDialog: function class, which can pop up a file dialog box.

  • Title: Set the title text of the dialog box.
  • Multiselect:Set whether to allow multiple file selections. The default value is false. If set to true, you can hold down the Ctrl or Shift key while selecting multiple files.
  • Filter:Set the file types that can be selected. The format is “Description text | File type”. Multiple types are separated by vertical bars |. If not set, all files can be selected by default.
  • ShowDialog(): Display the dialog box for opening the file and return the dialog box results. The dialog result is true if the user selects the file and false if the user deselects it.
  • FileNames: Gets the file name selected by the user. If the Multiselect property is true, a string array is returned; otherwise, a string array of length 1 is returned.
  • FileName: Gets the first file name selected by the user, equivalent to FileNames[0].

Implement multiple file traversal and use container storage.

List<string> imageListPath· = GetOpenFilesPath("Select image file", true, "PNG file(*.png)|*.png|JPG file(*.jpg)|*.jpg)|BMP file(* .bmp");