[halcon] Intuitive understanding and application of grayscale histogram

Grayscale histogram

Abscissa: 0~255 represents the range of gray value

Vertical coordinate: It is the number of pixels under different grayscale values!

Then the essence of the grayscale histogram is to count the number of pixels at different grayscales!

Its intuitive purpose is to check the distribution of grayscale!

Related functions:

Global threshold segmentation threshold 

threshold (ImageChannel3, Regions, 248, 255)

Select a grayscale range and segment the pixels in the subrange.

 binary_threshold

binary_threshold (ImageChannel3, Region, 'max_separability', 'dark', UsedThreshold)

If your background and product are very clear, each gray level has a peak (as shown in the picture above) 

This function automatically produces a grayscale value that can well distinguish the product from the background.

'max_separability' is an algorithm choice

'dark' means you want the darker part!

UsedThreshold is the gray value automatically calculated based on the algorithm!

mean_image

mean_image (Image, ImageMean, 13, 13)

The literal meaning is to find the average of the images. The professional term is the [famous]  mean filter .

The first parameter is the original picture, the second parameter is the filtered picture, and the last two parameters represent the size of the convolution kernel (that is, the average size of a range)

The specific algorithm process is that a convolution kernel, like a grid mask, takes out 13*13 pixels from the original image each time , and calculates an average value for the central pixel through the surrounding pixels. The convolution kernel scans the entire image from beginning to end. (The edge of the picture has a larger convolution kernel size, and the pixels are filled in through mirroring)

Then the value of each pixel of the image obtained in this way is the average of the surrounding pixels of the original image! 

Let’s take a look at the effect of mean filtering:

Original picture
Original picture

After mean filtering
After mean filtering

 After mean filtering, the first impression is that the picture becomes blurred and the contours are not so clear!

"The mean filter seems to have no effect!" I once thought so.

But in fact, when we perform certain filtering operations on pictures, sometimes it is not for the macroscopic picture display effect , but for the microscopic value of each pixel !

Such as mean filtering! After getting each pixel value, combine it with another operator dyn_threshold to achieve the goal!

dyn_threshold

The dyn_threshold operator literally means dynamic threshold , also known as  local threshold . There is one more prefix  dyn_  than the global threshold threshold  . Whether it is dynamic threshold or global threshold, their purpose is to do image segmentation ! Pick out the parts that interest us. The advantage of dynamic threshold  is that it can perform segmentation under uneven lighting conditions!

Let’s take a look at this picture:

 The lighting is obviously uneven, the one on the left is darker and the one on the right is brighter! That is to say, the product is a whole, which is what we are interested in, and needs to be segmented. However, the left side of the product becomes darker and the right side becomes brighter. If we use a global threshold, it will be difficult for us to segment the product from the picture. The global segmentation effect is as follows:

 Dynamic threshold (local threshold) can solve this problem! The idea is to compare a pixel with surrounding pixels. If it is  higher than the average value of surrounding pixels by a certain value, it will be segmented. This reflects the local characteristics. This avoids the problem of uneven lighting.

The question now is  where does the average value of surrounding pixels come from? That's right, it's mean_image.

mean_image and dyn_threshold  are generally used together.

First, do a mean filter ( mean_image ) on the image, and then use the dynamic threshold ( dyn_threshold  ) to compare each pixel of the original image with each pixel of the average image , and segment out a certain value higher than the average. of pixels.

mean_image (Image, ImageMean, 27, 27)
dyn_threshold (Image, ImageMean, RegionDynThresh, 5, 'light') 

dyn_threshold parameter explanation:

The fourth parameter (5) means that if it is 5 higher than the average, then this pixel is what we want.

On the other hand, if a black pixel has a value of 1 and is surrounded by black pixels with a value of 1, then the average value must also be 1, and such pixels will be eliminated.

Therefore, the core idea of ​​dynamic thresholding is to find pixels that have a larger difference from surrounding pixels. This solves the problem of uneven light.

mean_image convolution kernel size selection:

The larger the convolution kernel, the larger the average range, which means the larger the "local"! If we set the convolution kernel to be smaller, let's see what happens:

 The edges of some products are extracted to be hollow. This is because the pixels in the middle area of ​​the product edge line are similar, and the average local area is relatively small, so the middle pixels will be almost the same as the average pixels, so they will not be selected. .

At this time, we need to increase the size of the convolution kernel. The size of the convolution kernel must be larger than the edge you want to select, so that the average value will be lower and there will be no hollow problem.

Example reference

There is also a small example of dynamic threshold for your reference:

[halcon] Example analysis of dynamic threshold_code bean's blog-CSDN blog's previous [halcon] series of articles introduced the global and threshold methods, threshold. But when the background light of our picture is uneven, we need a dynamic threshold method and use different thresholds for different brightness areas. For example: this picture has uneven light and shade. We have no way to use the global threshold to select all the "chess pieces". When dynamic threshold is compared according to the surrounding environment, we first need to do a mean filter: After doing this mean filter A "light and dark background" is obtained, which is not the final result, but a reference object for dynamic thresholding. Use light to represent the brighter part with offset=15 compared to the average value. Use dark to represent and average https://blog.csdn.net/songhuangong123/article/details/125152743#:~:text=%E5%8F%91%E5%B8%83-,%E3%80%90halcon% E3%80%91%E4%BE%8B%E5%AD%90%E5%88%86%E6%9E%90%E4%B9%8B%E5%8A%A8%E6%80%81%E9% 98%88%E5%80%BC,-code%20bean

 summary:

This article introduces three commonly used functions for segmentation based on grayscale histograms:

  1.  threshold  global threshold segmentation
  2.  binary_threshold
  3.  dyn_threshold dynamic threshold segmentation
  4.  mean_image mean filter.

For more information, the segmentation operator can be found in the Segmentation partition in the example code of halcon:

Guess you like

Origin blog.csdn.net/songhuangong123/article/details/128985521