当前位置:网站首页>Halcon principle: correlation matching

Halcon principle: correlation matching

2022-06-23 11:59:00 Mr anhydrous

One 、 The basic principle

        NCC It is an algorithm based on statistics to calculate the correlation between two groups of samples , Its value range is 【-1,1】 Between , And for images , Each pixel is viewed as RGB Vector , The whole image is a sample set . If it has a subset , Match with another sample data , Then it's NCC The value is 1; It means that the correlation is very high , If it is -1, The table is completely irrelevant ; Based on this principle, template matching and recognition are realized , The first step is to normalize the data , The mathematical formula :

                ​​​​​​​        ​​​​​​​        \widetilde{f}=\frac{f-\mu}{\sigma}

         among ,f Express p Point gray value ,\mu Represents the average value of pixels in the window ,\sigma Represents the standard deviation .

         If t Represents the template pixel value , be :

        ​​​​​​​        ​​​​​​​        ​​​​​​​        NCC=\frac{1}{n-1}\sum_{x,y}^{}\frac{(f(x,y)-\mu_f)(t(x,y)-\mu_t)}{\sigma_f\sigma_t}

         among n Is the total number of template pixels ,n-1 It's the degree of freedom .

Two 、 Implementation method

1) Get the average value of template pixels 、 Standard deviation 、 Pixel and mean fiff Data samples .

2) According to the template size , On the target image , From top to bottom 、 Pan the template from left to right , Calculate every pixel moved , Template and image area NCC value , And record it at the center point . After completion , Of each point NCC Compare with the threshold , Beyond the threshold is the matching result .

3) Mark the matching position with a rectangular box .

4) Use UI Output .

3、 ... and 、Halcon Of NCC matching

        Halcon Of ncc Templates use , This is done by two functions :create_ncc_model and find_ncc_model Cooperate to complete . Below we have prepared a picture , To explain this matching

3.1 create_ncc_model and find_ncc_model function

The function prototype :

  • create_ncc_model(Template : : NumLevels, AngleStart, AngleExtent, AngleStep, Metric : ModelID)

Template: Image of the template , It can be a rectangle , It can also be a circle .

NumLevels: Is the hierarchy of templates , If one layer , Just use “auto”

AngleStart, AngleExtent, AngleStep: These three represent the rotation of the angle template , Starting angle 、 Termination angle 、 Rotation step .

Metric: Matching criteria

ModelID: Template label

  • find_ncc_model(Image : : ModelID, AngleStart, AngleExtent, MinScore, NumMatches, MaxOverlap, SubPixel, NumLevels : Row, Column, Angle, Score)

Image: The detected image

ModelID: Template number

AngleStart, AngleExtent: angle

MinScore: Match points

NumMatches: matching , Number of templates hit

MaxOverlap,

SubPixel:

NumLevels: Number of formwork layers

Row, Column, Angle, Score: Return value , Hit rows 、 Column 、 angle 、 The score is .

Four 、 Case study

1  Open a grayscale image file :

read_image (Image, 'F:/images/hugeImg/gtexst.jpg')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_update_window ('off')

2 Generate template  

gen_circle (Circle, 210,692, 41)
area_center (Circle, Area, RowRef, ColumnRef)
reduce_domain (Image, Circle, ImageReduced1)
disp_image(ImageReduced1,WindowHandle)

gen_circle (Circle, 701,854, 59)
area_center (Circle, Area, RowRef, ColumnRef)
reduce_domain (Image, Circle, ImageReduced2)
disp_image(ImageReduced2,WindowHandle)

  Generate two circular templates  

create_ncc_model (ImageReduced1 , 5, 0, 10, 'auto', 'use_polarity', ModelID1)
get_ncc_model_params(  ModelID1 ,NumLevels, AngleStart, AngleExtent, AngleStep, Metric)

create_ncc_model (ImageReduced2 , 5, 0, 10, 'auto', 'use_polarity', ModelID2)
get_ncc_model_params(  ModelID2 ,NumLevels, AngleStart, AngleExtent, AngleStep, Metric)

3 Template matching  

find_ncc_model (Image, ModelID1, 0, 10, 0.5, 1, 0.5, 'true', 0, Row1, Column1, Angle, Score1)
find_ncc_model (Image, ModelID2, 0, 10, 0.5, 1, 0.5, 'true', 0, Row2, Column2, Angle, Score2)

4 Mark the matching area  

dev_set_draw ('margin')
dev_set_colored (12)
dev_set_line_width (3)
dev_display (Image)

gen_rectangle1(Rectangle1, Row1-41, Column1-41, Row1+41, Column1+41)
gen_rectangle1(Rectangle2, Row2-59, Column2-59, Row2+59, Column2+59)
dev_set_window(WindowHandle)
dev_display(Rectangle1)
dev_display(Rectangle2)

 

  5、 ... and 、 Enumeration of related functions

5.1 Possible antecedents :


draw_region
reduce_domain
threshold

5.2 Possible post items :

find_ncc_model
get_ncc_model_params
clear_ncc_model
write_ncc_model
set_ncc_model_origin
set_ncc_model_param

5.3 Possible alternatives :

create_shape_model
create_scaled_shape_model
create_aniso_shape_model
create_template_rot
 

原网站

版权声明
本文为[Mr anhydrous]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231152017728.html