Machine Vision_HALCON_Example Practice_1. Detecting Circles


I. Introduction

The previous article (User Guide/Quick Guide) has almost finished the basic content of HALCON, and also ran a simple example during the learning process-positioning a paper clip in a single background. After running the example, I immediately feel that I can do it, but if you want to get started to solve a practical problem at this time (assuming you have no other image processing experience like me), it is likely to be difficult.

Take me as an example, I found a picture at random, and wanted to locate the target figure in the picture - the coin.
insert image description here

So, following the steps in the paperclip example, using the grayscale image for thresholding, the resulting image looks like this.
insert image description here
Leaving aside the big red block in the background, even the text with similar colors is preserved. This is not what I want to keep . But it’s normal, because the image processing used is based on morphology. Simply speaking, it is the matching of color, shape, and area. It is relatively foolish. In fact, the computer does not recognize coins.

Then how to remove the unwanted elements in these backgrounds? After some research, I know two commonly used operations—erosion and dilation. Because this article does not mainly introduce their implementation, so we only briefly talk about the effects of their processing.

corrosion
Corrosion in nature. Suppose you have an iron plate. After corrosion, it may become rusty and have many holes like the picture below. The areas with large holes may be damaged and relatively fragile; and Some areas that remain relatively intact may have been chunky, stronger sections (assuming corrosion acts equally on each area).
insert image description here
And the erosion in image processing, after acting on the example picture in the introduction, the following picture is obtained. It can be seen that the effect is actually similar to the corrosion of iron plates. Large graphics are better preserved, while some small graphics disappear directly (corroded); some small holes on large graphics become larger after corrosion.
insert image description here
dilation
When it comes to inflation, it is natural to think of blowing a balloon. After the balloon is inflated, it becomes bigger (of course it may burst). Dilation in image processing is similar (but not exploding), which is the opposite operation to erosion. Think of a pixel/region in an image as a balloon that, when inflated, becomes larger. Let’s expand the sample image, the original densely packed fonts become obviously thicker, just like the ink bleeds on the paper.
insert image description here

Ok, that's about it for corrosion and expansion (it's enough to understand its effect in the application chapter). Now start to run an official sample Inspection of Ball Bonding , search for the keyword ball in the HDevelop sample program , you can find the sample project, select ball.hdev to open.

2. Detect circular

This is the original picture of the example. Now we need to find and mark the black circular sticky interface, and then learn its program line by line (more than 50 lines in total).
insert image description here
The first few lines of the HALCON program,

* ball.hdev: Inspection of Ball Bonding
* 
dev_update_window ('off')
dev_close_window ()
dev_open_window (0, 0, 728, 512, 'black', WindowID)

The ones starting with * (asterisk) are comments, and the remaining three lines prefixed with dev_ are not directly related to graphics processing. Their functions are:

  1. It will turn off the automatic output of the image variable to the graphics window (set to 'off', another advantage is that when you select the image variable, only the selected image variable will be displayed in the graphics window, which is convenient for observation, otherwise you need to manually select it again to display/ Clear)
  2. close the active graphics window
  3. And open a 728×512 black background window.

Next, read the original image,

read_image (Bond, 'die/die_03')

Although the image variable is automatically displayed in the active window in the above code, it will be ignored when you step through the program. The following uses dev_display to explicitly output the read image to the graphics window.

dev_display (Bond)
set_display_font (WindowID, 14, 'mono', 'true', 'false')
disp_continue_message (WindowID, 'black', 'true')
stop ()

The remaining two sentences seem to be system operators, but they are actually predefined external functions. The effect is to display text below the image, and then stop() suspends program execution.
insert image description here
Next up is an old friend, thresholding.

threshold (Bond, Bright, 100, 255)

insert image description here

After thresholding is a new operator shape_trans , which can transform the shape of the region. The parameter filled here is 'rectangle2', which is the smallest rectangle circumscribing in the input area. In the above thresholded image, there is obviously a black area at the top, that is, there is no pixel. This part of the area is not needed in the subsequent processing. How to remove it requires a circumscribing rectangle.

shape_trans (Bright, Die, 'rectangle2')

insert image description here
The next few steps are to draw a box around the bounding rectangle, display a predefined text prompt, and then pause.

dev_set_color ('green')
dev_set_line_width (3)
dev_set_draw ('margin')
dev_display (Die)
disp_continue_message (WindowID, 'black', 'true')
stop ()

insert image description here
You may ask, what is the use of having such a rectangle? Don't worry, it will be used in
the next operator reduce_domain ,

reduce_domain (Bond, Die, DieGrey)

It will cut out the image of the rectangular area from the original image.
insert image description here
Here, the size of the cropped image has not changed. You can see that the black background is still there. This is equivalent to removing the area outside the rectangle, but retaining Background/Canvas (there is also an operator in HALCON).
The above steps (obtaining the target rectangle and cropping the image with the rectangle) are very common, and this is usually done to obtain the ROI area.

Next, threshold the cropped area again, and this time keep the area with low gray value (closer to 0, close to black, close to 255, close to white), and the black area at the top of the image actually has no pixels, so it is not selected.

threshold (DieGrey, Wires, 0, 50)

insert image description here
The next step is a new operator fill_up_shape . Its function is to fill the holes with certain characteristics in the region. The characteristic represented by the following parameters is the area, and the range is 1-100. That is to fill the void in the range,

fill_up_shape (Wires, WiresFilled, 'area', 1, 100)

Let's take a look at the effect after zooming in. Before and
insert image description here
after processing,
insert image description here
it is still obvious that the small round holes in the area are filled. You may ask, isn't it full, why did it disappear? Since the pattern is nearly black, it appears to disappear after filling.

Then there are some additional operations, displaying the negative of the original image, filling the processed area with red,

dev_display (Bond)
dev_set_draw ('fill')
dev_set_color ('red')
dev_display (WiresFilled)
disp_continue_message (WindowID, 'black', 'true')
stop ()

insert image description here
The next step is a very useful operation - the opening operation (the name of opening in halcon is related to the opening operation), its principle is actually the combination of corrosion and expansion, that is, corrosion first and then expansion,

opening_circle (WiresFilled, Balls, 15.5)

The effect after processing is not very outrageous, but after understanding the effect mentioned at the beginning of the article, it is not difficult to imagine.
insert image description here
Next, switch the output color and re-output the processed area,

dev_set_color ('green')
dev_display (Balls)
disp_continue_message (WindowID, 'black', 'true')
stop ()

insert image description here
The next three operators, first is connection to find the connected parts in the area;
the second is the new operator select_shape , which finds the shape with the target characteristics, and the parameter filled in here is 'circularity', which is a circle-like figure;
the third The first operator is the literal meaning, sorting the graphics by position (the parameter here means sorting in ascending order of column values).

connection (Balls, SingleBalls)
select_shape (SingleBalls, IntermediateBalls, 'circularity', 'and', 0.85, 1.0)
sort_region (IntermediateBalls, FinalBalls, 'first_point', 'true', 'column')

insert image description here
Then, change the color to display the comparison of the original image, and enter the prompt text,

dev_display (Bond)
dev_set_colored (12)
dev_display (FinalBalls)
disp_continue_message (WindowID, 'black', 'true')
stop ()

insert image description here
Finally,
smallest_circle determines the smallest circumscribed circle of these circular areas, and outputs the output coordinates and radius to the graphics window after processing.

smallest_circle (FinalBalls, Row, Column, Radius)
NumBalls := |Radius|
Diameter := 2 * Radius
meanDiameter := mean(Diameter)
minDiameter := min(Diameter)
dev_display (Bond)
disp_circle (WindowID, Row, Column, Radius)
dev_set_color ('white')
disp_message (WindowID, 'D: ' + Diameter$'.4', 'image', Row - 2 * Radius, Column, 'white', 'false')
dev_update_window ('on')

insert image description here

So far, the sample program has been processed.

After that, I used the example method to process the sample image at the beginning of the article, and got the following effect
insert image description here

3. Summary

The whole process is actually not complicated. After all, we just call the operator and don't need to pay attention to the details of the algorithm.
But here is still a little summary of the common operations involved,

  • Thresholding, after getting the picture, it is usually thresholded first to facilitate subsequent processing.
  • Select ROI, usually get a shape, and then use this shape to crop the original image, which can reduce the amount of subsequent image processing and reduce the interference area.
  • Corrosion and expansion, the opening operation (opening) in the above example includes erosion and expansion, you can also use them separately, and the common one is closing operation (closing). These operations are usually used to remove the noisy areas in the image background and fill in the gaps.
  • Based on the graphic selection of the features, this step is after finding the connected area (connection).

The operations such as thresholding and selecting ROI mentioned above can be realized through UI interaction, which is more convenient than direct parameter adjustment.

Guess you like

Origin blog.csdn.net/BadAyase/article/details/129020404