Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
Python OpenCV 365 day learning plan, enter the graphics realm with eraser. This blog is the 50th in the series.
Learned in the previous
Histograms have been partially covered in previous blogs, so you can review them for yourself.
Histogram is an analysis tool in the process of image processing, which is to use gray value or from the perspective of gray level statistics of image features.
From a statistical point of view, the histogram counts the number of occurrences of each gray level of the image. The abscissa is the pixel gray level, and the ordinate is the number of the gray level.
If there is a5x5
, the histogram is the statistics of each pixel value.
The above two figures are the explanation of histogram statistics, but the histogram is visually presented not as a table but as a graph, so the histogram can be obtained by drawing the above data. In the histogram, the X-axis represents 256 gray levels of the 8-bit bitmap, and the Y-axis represents the pixel point format corresponding to the gray level.
Based on this, there is another knowledge to be added, which is called normalized histogram. In most cases, the X-axis in the histogram represents gray level, while the Y-axis represents the frequency of gray level, specifically, The Times/total, for example, the above table can be modified as:
BINS and RANGE are some new words that need to be noted once the concept of the histogram is understood.
DIMS
: Based on the current knowledge, there is only one gray value, which is 1, which represents the number of parameters collected when the histogram is collected;RANGE
: Statistical grayscale range. For grayscale images, the range is[0255]
;BINS
: Parameter number of sub-levels, the approximate meaning can be understood as, the gray level of the previous group processing, generally keep the default.
BINS to be more specific, for a greyscale image, the BINS range is [0,255], which defaults to 256 and can be divided into 16 BINS if grouped in a group of 16 greyscale levels.
Histogram drawing
This blog has covered the drawing of histograms, so let’s take a look at the whole picture.
The code implements a histogram based on the plt.hist() function, which is prototyped as plt.hist(X,BINS)
Run the following code, when looking for images, try to find the color distribution is more even, do not white or black area is too large, otherwise the histogram effect is not obvious.
import cv2 as cv
import matplotlib.pyplot as plt
src = cv.imread("./2.png".0)
cv.imshow("src", src)
plt.hist(src.ravel(), 256)
plt.show()
cv.waitKey()
cv.destroyAllWindows()
Copy the code
One other thing to note about this code is the src.ravel() function, which is used to reduce a two-dimensional array to a one-dimensional one, as shown in the following figure.
If BINS are set to 16, the result is as follows.
Histogram drawing in Python OpenCV
The cv.calcHist function is used in OpenCV to calculate the histogram of the image. This article focuses on the return value of this function.
import cv2 as cv
import matplotlib.pyplot as plt
src = cv.imread("./2.png".0)
cv.imshow("src", src)
hist = cv.calcHist([src], [0].None[256], [0.255])
print(hist)
print(len(hist))
cv.waitKey()
cv.destroyAllWindows()
Copy the code
Hist output format is as follows:
# omit part of output data... [115] [122] [115] [142] [148] [152] [61] [6] [0.]], 256Copy the code
As you can see, the hIST returned is an array of 256 numbers. The elements in the array are the statistics of each gray level. Once you have the hIST, you can plot it using the plt.plot function.
import cv2 as cv
import matplotlib.pyplot as plt
src = cv.imread("./2.png".0)
cv.imshow("src", src)
ret_hist = cv.calcHist([src], [0].None[256], [0.255])
print(ret_hist)
print(len(ret_hist))
plt.plot(ret_hist)
plt.show()
cv.waitKey()
cv.destroyAllWindows()
Copy the code
Mask drawing histogram
Next, explain the histogram of mask rendering. By mask, select a part of the image to draw. White area of the mask represents transparency and can display the picture, while black area represents opacity and cannot display the picture.
The principle of mask can also read the previous blog to learn, summed up is the following two sentences
- The part of the original image corresponding to the black position in the mask is no longer displayed, and the gray value is set to 0.
- Retain the original value of the part corresponding to the white position in the original image and mask.
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
src = cv.imread("./2.png".0)
cv.imshow("src", src)
print(src.shape)
mask = np.zeros(src.shape, np.uint8)
# row followed by column
mask[10:150.40: 200] = 255
mask_img = cv.bitwise_and(src, mask)
cv.imshow("mask", mask_img)
ret_hist = cv.calcHist([src], [0].None[256], [0.255])
mask_hist = cv.calcHist([src], [0], mask, [256], [0.255])
plt.plot(ret_hist)
plt.plot(mask_hist)
plt.show()
cv.waitKey()
cv.destroyAllWindows()
Copy the code
Histogram equalization knowledge supplement
Histogram equalization can make the image have all possible gray levels and evenly distribute the gray levels of pixel values. The specific steps are as follows.
- Calculate the cumulative histogram
- Convert the accumulated histogram
You can refer to the gray matrix below. The image size is 5×5 and has 8 gray levels, ranging from [0,7]. The normalized statistical histogram and cumulative statistical histogram are as follows.
Based on the above figure, the original grayscale space can be converted into two types: one is in the original grayscale space range, namely [0,7]; the other is in a larger range.
First look at the realization of equalization in the original range, with the current cumulative probability multiplied by 7 (gray level maximum), get the new gray level.
The number of new gray level and original gray level can be compared.
Before equalization, if the gray level is divided into two groups, the data are as follows:
0 ~ 3
: 4 pixels, the number of statistical pixels is 19;4 ~ 7
: 4 pixels, the number of pixels is 6. After equalization, if the gray level is divided into two groups, the data are as follows:0 ~ 3
: 4 pixels, the number of statistical pixels is 11;4 ~ 7
: 4 pixels, the statistical number of pixels is 14.
If you want to get a larger range of histograms, you just need to multiply the cumulative probability by a larger gray level. You can try by yourself.
Eraser section
I hope you learned something from today’s hour, and I’ll see you in the next blog