Hengyuan cloud_ Notes on papers on [histogram] in CV field

Posted by mickfuzz on Thu, 13 Jan 2022 20:24:47 +0100

Source: Hengyuan cloud community( Hengyuan zhixiangyun)

Original address| histogram

Original author | inster

Learning objectives

  • Master the histogram calculation and display of images
  • Understand mask applications
  • Familiar with histogram equalization and adaptive equalization

1 gray histogram

1.1 principle

Histogram is a method of data statistics, and organizes the statistical values into a series of bin defined by the implementation. Among them, bin is a concept often used in histogram, which can be translated as "straight bar" or "group distance". Its value is the feature statistics calculated from the data, which can be such as gradient, direction, color or any other feature.

Image Histogram is a histogram used to represent the brightness distribution in a digital image. It plots the number of pixels of each brightness value in the image. In this histogram, the left side of the abscissa is a darker area and the right side is a brighter area. Therefore, the data in the histogram of a darker image is mostly concentrated in the left and middle parts, while the image with overall brightness and only a small amount of shadow is the opposite.Note: the histogram is drawn according to the gray image, not the color image. Assuming that there is information of an image (gray value 0 - 255), the range of known numbers contains 256 values, so this range can be divided into sub regions (i.e. bins) according to a certain law. For example:

\( \left[0,255\right] = \left[0,15\right]\bigcup\left[16,30\right]\cdots\bigcup\left[240,255\right] \)
Then count the number of pixels of each bin(i). The following figure can be obtained (where x-axis represents bin and y-axis represents the number of pixels in each bin):

Some terms and details of histogram:

  • Dims: the number of features to be counted. In the above example, dims = 1 because only gray values are counted.
  • Bins: the number of sub segments of each feature space, which can be translated as "straight bar" or "group distance". In the above example, bins = 16.
  • Range: the value range of the feature to be counted. In the above example, range = [0, 255].

Significance of histogram:

  • Histogram is a graphical representation of pixel intensity distribution in an image.
  • It counts the number of pixels for each intensity value.
  • The histograms of different images may be the same

1.2 calculation and drawing of histogram

We use the method in OpenCV to count the histogram and draw it with matplotlib.
API:

cv2.calcHist(images,channels,mask,histSize,ranges[,hist[,accumulate]])

Parameters:

  • images: original image. When a function is passed in, it should be enclosed in brackets [], for example: [img].
  • Channels: if the input image is a grayscale image, its value is [0]; If it is a color image, the input parameters can be [0], [1], [2], which correspond to channels B, G and R respectively.
  • Mask: mask image. To count the histogram of the whole image, set it to None. But if you want to count the histogram of a part of the image, you need to make a mask image and use it. (examples follow)
  • Histsize: number of bin. It should also be enclosed in square brackets, for example: [256].
  • ranges: pixel value range, usually [0256]

Example:
As shown in the figure below, draw the corresponding histogram

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
# 1 read directly in the form of gray image
img = cv.imread('./image/cat.jpeg',0)
# 2 Statistical grayscale image
histr = cv.calcHist([img],[0],None,[256],[0,256])
# 3 draw grayscale map
plt.figure(figsize=(10,6),dpi=100)
plt.plot(histr)
plt.grid()
plt.show()

1.3 mask application

Mask is to block the image to be processed with the selected image, figure or object to control the area of image processing.

In digital image processing, we usually use two-dimensional matrix array for mask== The mask is a binary image composed of 0 and 1. The mask image is used to mask the image to be processed, in which the 1 value area is processed, and the 0 value area is shielded and will not be processed.

The main uses of the mask are:

  • Extracting region of interest: perform "and" operations with the pre-made region of interest mask and the image to be processed to obtain the region of interest image. The image value in the region of interest remains unchanged, while the image value outside the region of interest is 0.
  • Shielding function: mask some areas on the image so that they do not participate in processing or calculation of processing parameters, or only process or count the shielding area.
  • Structural feature extraction: the structural features similar to the mask in the image are detected and extracted by similarity variable or image matching method.
  • Special shape image making

Mask is widely used in remote sensing image processing. When extracting roads, rivers or houses, a mask matrix is used to filter the pixels of the image, and then the features or signs we need are highlighted.

We use cv Calchist() to find the histogram of the complete image. What if you want to find the histogram of some areas of the image? Just create a white mask image on the area where you want to find the histogram, otherwise create black, and then pass it as a mask.
Example:

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
# 1. Read directly in the form of gray image
img = cv.imread('./image/cat.jpeg',0)
# 2. Create mask
mask = np.zeros(img.shape[:2], np.uint8)
mask[400:650, 200:500] = 255
# 3. Mask
masked_img = cv.bitwise_and(img,img,mask = mask)
# 4. Count the gray image of the image after the mask
mask_histr = cv.calcHist([img],[0],mask,[256],[1,256])
# 5. Image display
fig,axes=plt.subplots(nrows=2,ncols=2,figsize=(10,8))
axes[0,0].imshow(img,cmap=plt.cm.gray)
axes[0,0].set_title("Original drawing")
axes[0,1].imshow(mask,cmap=plt.cm.gray)
axes[0,1].set_title("Mask data")
axes[1,0].imshow(masked_img,cmap=plt.cm.gray)
axes[1,0].set_title("Post mask data")
axes[1,1].plot(mask_histr)
axes[1,1].grid()
axes[1,1].set_title("Gray histogram")
plt.show()

2 histogram equalization

2.1 principle and Application

Imagine if the pixel values of most pixels in an image are concentrated in a small gray value range? If an image is bright as a whole, the number of pixel values should be high. Therefore, its histogram should be stretched horizontally (as shown in the figure below), which can expand the distribution range of image pixel values and improve the image contrast. This is what histogram equalization should do.

"Histogram equalization" is to change the gray histogram of the original image from a relatively concentrated gray interval to a distribution in a wider gray range. Histogram equalization is to stretch the image nonlinearly and redistribute the image pixel values so that the number of pixels in a certain gray range is roughly the same.
This method improves the overall contrast of the image, especially when the pixel value distribution of useful data is close, it is widely used in X-ray images, which can improve the display of skeleton structure. In addition, it can better highlight details in overexposed or underexposed images.

When using opencv for histogram statistics, the following are used:
API:

dst = cv.equalizeHist(img)

Parameters:

  • img: grayscale image
    return:
  • dst: results after equalization

Example

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
# 1. Read directly in the form of gray image
img = cv.imread('./image/cat.jpeg',0)
# 2. Equalization treatment
dst = cv.equalizeHist(img)
# 3. Result display
fig,axes=plt.subplots(nrows=2,ncols=2,figsize=(10,8),dpi=100)
axes[0].imshow(img,cmap=plt.cm.gray)
axes[0].set_title("Original drawing")
axes[1].imshow(dst,cmap=plt.cm.gray)
axes[1].set_title("Results after equalization")
plt.show()

2.2 adaptive histogram equalization

For the above histogram equalization, we consider the global contrast of the image. Indeed, after histogram equalization, the contrast of the picture background is changed. It is too dark in the cat leg, and we lose a lot of information. Therefore, in many cases, the effect is not good. As shown in the figure below, we lost a lot of information due to the brightness of the statue in the next two images.

In order to solve this problem, adaptive histogram equalization is needed. At this time, the whole image will be divided into many small blocks, which are called "tiles" (the default size of tiles in OpenCV is 8x8), and then histogram equalization will be carried out for each small block. Therefore, in each region, the histogram will be concentrated in a small region). If there is noise, the noise will be amplified. To avoid this, use contrast limits. For each small block, if the bin in the histogram exceeds the upper limit of contrast, the pixels in the histogram are evenly dispersed into other bins, and then histogram equalization is carried out.

Finally, in order to remove the boundary between each block, bilinear difference is used to splice each block.
API:

cv.createCLAHE(clipLimit, tileGridSize)

Parameters:

  • clipLimit: contrast limit. The default value is 40
  • tileGridSize: block size. The default value is 8 * 88 * 8

Example:

import numpy as np
import cv2 as cv
# 1. Read the image in the form of grayscale image
img = cv.imread('./image/cat.jpeg',0)
# 2. Create an adaptive equalization object and apply it to the image
clahe = cv.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
cl1 = clahe.apply(img)
# 3. Image display
fig,axes=plt.subplots(nrows=1,ncols=2,figsize=(10,8),dpi=100)
axes[0].imshow(img,cmap=plt.cm.gray)
axes[0].set_title("Original drawing")
axes[1].imshow(cl1,cmap=plt.cm.gray)
axes[1].set_title("Results after adaptive equalization")
plt.show()


summary

  • Gray histogram:
    --Histogram is a graphical representation of pixel intensity distribution in an image.
    --It counts the number of pixels for each intensity value.
    --The histograms of different images may be the same

    cv.calcHist(images,channels,mask,histSize,ranges [,hist [,accumulate]])
  • Mask
    --Create a mask and pass it through the mask to obtain the histogram of the region of interest
  • Histogram equalization: a method to enhance image contrast

    cv.equalizeHist(): The input is a gray image and the output is a histogram equalization image
  • Adaptive histogram equalization
    --The whole image is divided into many small blocks, and then each small block is histogram equalized, and finally spliced

    clahe = cv.createCLAHE(clipLimit, tileGridSize)

Inster has shared several notes. You can also review previous posts on children's shoes you are interested in

1,Federal learning
2,Working principle of convolutional neural network
3,opencv
4,Geometric transformation

Topics: Deep Learning CV