A method of halcon ring type defect detection (polar coordinate transformation method)

Introduction

Ring inspection is often encountered in our daily defect inspection. For example, the defect detection of glass bottle mouth, the defect detection of coins, the defect detection of rubber pads...
Our detection of rings is not like the detection of rectangles and other objects. The detection area is relatively regular, which is convenient for us to use morphology to screen the area, but the circle We found some problems with ring items, such as using rectangular shapes to learn to influence the boundary processing of rings and so on. In this case, we can use the method of polar coordinate transformation to convert the ring into a rectangle to deal with the defect.

legend:
Example 1Insert picture description here
Insert picture description here

Polar coordinate transformation

definition

In image processing, especially in the process of processing circular and circular images, polar coordinate transformation is used. Convert a square image into a circle, that is, "square to circle". Or realize the conversion of a circle into a square "round to square".

We circular image is converted into a rectangular image is called polar coordinate transformed image ; and vice versa for the anti-polar image transform

principle

Polar coordinates must have been heard by anyone who has studied high school mathematics. In a general coordinate system, (x, y) is used to describe the position of a point, while in a polar coordinate system, the distance to the origin ρ and the angle θ are used To describe the location of the point.
First of all, normal images are in a rectangular coordinate system, which can be regarded as a two-dimensional plane. To transform the image coordinates, we must first transform the position to a new coordinate system, and then assign the value to it.
There are two values ​​that need to be determined: 1. Position 2. Value . That is, the value and position of any pixel after transformation.
Here: the value does not change after the change. That is to solve the changing positional relationship .
The key of polar coordinate transformation and its inverse transformation is to determine the pixel value of each pixel on the image after polar coordinate transformation according to the image before polar coordinate transformation. That is to find the corresponding relationship of geometric coordinates between "circle graph" and "square graph".

Proof:
Insert picture description here
As shown in the figure above, the key to realize the transformation of polar coordinates is to find any point P(i,j) on the circle graph, and the corresponding point p(m,n) on the square graph, and then realize all the points on the circle graph through interpolation algorithm Assignment of pixels.

On the square graph, the number of rows and columns is M and N respectively. Each column on the square graph corresponds to each radius on the circle graph. There is a length scaling factor delta_r = M/R in the radius direction, and the circumferential direction is divided into N etc. Points, that is, the angle factor is delta_t = 2π/N;

On the circle chart, the image coordinates (i, j) and the world coordinates (x, y) have the following transformation relationship: x = j-R, y = R-i;

Then, the radius of point P in the figure is r = sqrt(x x + y y), and the angle theta = arctan(y/x);

The number of rows corresponding to the point P on the circle graph on the square graph is r/delta_r;

The number of columns corresponding to the point P on the circle chart on the square chart is n = thata/delta_t.

The above is the basic principle of polar coordinate transformation, combined with the corresponding interpolation algorithm, the polar coordinate transformation of the image can be realized.

Polar coordinate transformation in Halcon

Polar coordinate transformation operators in halcon:
polar_trans_image_ext (for images); polar_trans_region (for regions); polar_trans_contour_xld (for XLD contours).
The inverse polar coordinate transformation operator in halcon:
polar_trans_image_inv (for images); polar_trans_region_inv (for regions); polar_trans_contour_xld_inv (for XLD contours).

1. Polar_trans_image_ext operator usage and parameter analysis

Operator prototype:
polar_trans_image_ext(Image: PolarTransImage: Row, Column, AngleStart, AngleEnd, RadiusStart, RadiusEnd, Width, Height, Interpolation :)
Parameter description:
Image (input_object) (multi-channel) image (-array) → object (byte * / int2 * / uint2 * / real *)* Allows computing devices to
input images.
PolarTransImage (output_object) (multi-channel) image (-array) → object (byte/int2/uint2/real number)
output image.
Row (input_control) number → (real number/integer)
the row coordinate of the arc center.
Default value: 256
Recommended value: 0, 16, 32 , 64, 128 , 240 , 256 , 480 , 512
Column (input_control) number → (real number/integer)
the column coordinates of the arc center.
Default value: 256
Recommended value: 0, 16, 32,
64, 128, 256, 320, 512, 640 AngleStart (input_control) number → (real number)
the ray angle to be mapped to the first column of the output image.
Default value: 0.0
Suggested values: 0.0, 0.78539816,
1.57079632 , 3.141592654, 6.2831853 , 12.566370616 AngleEnd (input_control) number → (real number)
the ray angle to be mapped to the last column of the output image.
Default value: 6.2831853
Recommended value: 0.0,
0.78539816 , 1.57079632, 3.141592654, 6.2831853 , 12.566370616 RadiusStart (input_control) number → (real number/integer)
the radius of the circle to be mapped to the first line of the output image.
Default value: 0
Recommended value: 0, 16, 32
, 64, 100,
128 , 256, 512 Typical value range: 0≤RadiusStart RadiusEnd (input_control) number → (real number/integer)
to be mapped to the circle in the last line of the output image radius.
Default value: 100
Recommended value: 0, 16, 32
, 64, 100 , 128, 256, 512 Typical value range: 0≤RadiusEnd
Width (input_control) number → (integer)
the width of the output image.
Default value: 512
Recommended value: 256, 320 , 512 , 640, 800, 1024
Typical value range: 0≤width≤32767
Height(Input_control) number → (integer)
the height of the output image.
Default value: 512
Recommended value: 240, 256, 480 , 512 , 600, 1024
Typical value range: 0≤height≤32767
Interpolation (input_control) character string → (string)
interpolation method for transformation.
Default value: "nearest_neighbor"
list of values:'bilinear','nearest_neighbor'

2. Polar_trans_region_inv operator usage and parameter analysis

Operator prototype:
polar_trans_image_inv(PolarImage: XYTransImage: Row, Column, AngleStart, AngleEnd, RadiusStart, RadiusEnd, Width, Height, Interpolation :)
Parameter description:
PolarImage (input_object) (multichannel-) image (-array) → object (byte * / int2 * / uint2 * / real *) * Allows
input images for computing devices .
XYTransImage (output_object) (multi-channel) image (-array) → object (byte/int2/uint2/real number)
output image.
Row (input_control) number → (real number/integer)
the row coordinate of the arc center.
Default value: 256
Recommended value: 0, 16, 32,
64, 128, 240, 256, 480, 512 Typical value range: 0≤line≤32767
Column (input_control) number → (real number/integer)
the column coordinates of the arc center .
Default value: 256
Recommended value: 0, 16, 32,
64, 128, 256, 320, 512, 640 Typical value range: 0≤column≤32767
AngleStart(Input_control) number → (real number)
the ray angle to which the first column of the input image is mapped.
Default value: 0.0
Recommended value: 0.0 , 0.78539816, 1.57079632,
3.141592654 , 6.2831853 AngleEnd (input_control) number → (real number)
the ray angle to which the last column of the input image is mapped.
Default value: 6.2831853
Recommended value: 0.0,
0.78539816 , 1.57079632, 3.141592654 , 6.2831853 RadiusStart (input_control) number → (real number/integer)
The radius of the circle to which the first line of the input image is mapped.
Default value: 0
Recommended value: 0, 16, 32
, 64,
100 , 128, 256, 512 Typical value range: 0≤RadiusStart RadiusEnd (input_control) number → (real number/integer)
to map the last line of the input image to The radius of the circle.
Default value: 100
Recommended value: 0, 16, 32
, 64, 100 , 128, 256, 512 Typical value range: 0≤RadiusEnd
Width (input_control) number → (integer)
the width of the output image.
Default value: 512
Recommended values: 256, 320, 512 , 640, 800, 1024
Typical value range: 0≤width≤32767
Height (input_control) number → (integer)
the height of the output image.
Default value: 512
Recommended value: 240, 256, 480 , 512 , 600, 1024
Typical value range: 0≤height≤32767
Interpolation (input_control) character string → (string)
interpolation method for transformation.
Default value: "nearest_neighbor"
list of values:'bilinear','nearest_neighbor'

Ideas and steps for ring defects detection

1. Use Blob analysis to determine the position of the center of the circle and the radius of the circle, and the angle of the start and end of the transformation.
2. Use XLD contour fitting to accurately locate the circle center and other parameters. (This step is used for precise positioning, and some inspections are not needed)
3. Perform polar coordinate transformation
4. Process the image after polar coordinate transformation
5. Perform reverse polar coordinate transformation
6. Display defect area

Halcon example

inspect_bottle_mouth.hdev

Introduction

*This example checks whether the bottle mouth is defective.

Program interpretation

Portal, click the blue font below to jump to the interpretation article or copy the link below to the browser.
HALCON sample program inspect_bottle_mouth.hdev glass bottle mouth defect detection
https://editor.csdn.net/md/?articleId=94733605

Analysis of ideas

*First, detect the basic shape of the bottle,
*Edge detection and circle fitting.
*Then, transform the neck area by polar coordinate transformation.
*Afterwards, use dynamic thresholds in the transformed image to
detect defects. Finally, the results are displayed.

Application example 1

Project Introduction

Insert picture description here
Claim: Detect defects in the three red circles on the edge.
Original image
Insert picture description here

Handler

read_image (Image, 'C:/Users/Administrator/Desktop/1.png') 
rgb3_to_gray (Image, Image, Image, ImageGray) 
dev_open_window_fit_image (ImageGray, 0, 0, -1, -1, WindowHandle) 
dev_set_draw ('margin') 
dev_set_line_width (3) 
dev_set_draw ('margin') 
* dev_set_draw ('fill') 
get_image_size (ImageGray, Width, Height) 
binary_threshold (GrayImage, Region, 'max_separability', 'dark', UsedThreshold) 
connection (Region, ConnectedRegions) 
select_shape_std (ConnectedRegions, SelectedRegions, 'max_area', 70) 
smallest_circle (SelectedRegions, Row2, Column2, Radius) 
gen_circle (Circle, Row2, Column2, Radius) 
reduce_domain (ImageGray, Circle, ImageReduced2) 
gray_closing_rect (ImageReduced2, ImageClosing1, 10, 4) 
polar_trans_image_ext (ImageClosing1, PolarTransImage1, Row2, Column2, 0, 6.28319, Radius-100, Radius-10, 6.28*Radius, 90, 'nearest_neighbor') 
mean_image (PolarTransImage1, ImageMean1, 500, 2) 
dyn_threshold (PolarTransImage1, ImageMean1, RegionDynThresh1, 10, 'dark') 
connection (RegionDynThresh1, ConnectedRegions1) 
select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and',100, 9000000) 
union1 (SelectedRegions1, RegionUnion) 
closing_rectangle1 (RegionUnion, RegionClosing1, 10, 20) 
connection (RegionClosing1, ConnectedRegions3) 
opening_rectangle1 (ConnectedRegions3, RegionOpening1, 10, 20) 
polar_trans_region_inv (RegionOpening1, XYTransRegion1, Row2, Column2, 0, 6.28319, Radius-100, Radius-10, 6.28*Radius, 90, Width, Height, 'nearest_neighbor') 
smallest_circle (XYTransRegion1, Row, Column, Radius1) 
gen_circle (Circle1, Row, Column, Radius1) 
dev_display (ImageGray) 
dev_display (Circle1)

Treatment effect

Insert picture description here

Application example 2

Project Introduction

Insert picture description here
Claim: As shown in the figure, the figure shows the position of the solder joints in the red circle.
Original image
Insert picture description here

Handler

read_image (Image, 'C:/Users/Administrator/Desktop/实际图_副本.bmp') 
rgb3_to_gray (Image, Image, Image, ImageGray) 
dev_open_window_fit_image (ImageGray, 0, 0, -1, -1, WindowHandle) 
dev_set_draw ('margin') 
dev_set_line_width (5) 
get_image_size (ImageGray, Width, Height) 
auto_threshold (ImageGray, Regions, 2) 
count_obj (Regions, Number) 
select_obj (Regions, ObjectSelected, Number) 
fill_up (ObjectSelected, RegionFillUp1) 
connection (RegionFillUp1, ConnectedRegions1) 
select_shape_std (ConnectedRegions1, SelectedRegions1, 'max_area', 70) 
smallest_circle (SelectedRegions1, Row2, Column2, Radius) 
gen_circle (Circle, Row2, Column2, Radius) 
reduce_domain (ImageGray, Circle, ImageReduced2) 
polar_trans_image_ext (ImageReduced2, PolarTransImage1, Row2, Column2, 0, 6.28319, Radius-210, Radius, 6.28*Radius, 210, 'nearest_neighbor') 
mean_image (PolarTransImage1, ImageMean1, 200, 5) 
dyn_threshold (PolarTransImage1, ImageMean1, RegionDynThresh1, 45, 'dark') 
closing_circle (RegionDynThresh1, RegionClosing, 15.5) 
connection (RegionClosing, ConnectedRegions2) 
select_shape (ConnectedRegions2, SelectedRegions, ['width','height'], 'and', [380,80], [500,160]) 
polar_trans_region_inv (SelectedRegions, XYTransRegion1, Row2, Column2, 0, 6.28319, Radius-210, Radius, 6.28*Radius, 210, Width, Height, 'nearest_neighbor') 
smallest_rectangle2 (XYTransRegion1, Row3, Column3, Phi, Length1, Length2) 
gen_rectangle2 (Rectangle, Row3, Column3, Phi, Length1, Length2) 
dev_display (ImageGray) 
dev_display (Rectangle)

Treatment effect

Insert picture description here
References:
https://blog.csdn.net/guduruyu/article/details/65436931

Guess you like

Origin blog.csdn.net/cashmood/article/details/104709546