Welcome to Raspberry Pi 4B repository
1.Histogram of color pictures
Color pictures Histogram
Color histogram refers to the color distribution in an image.
The color value of each pixel in the picture can be represented by a ternary value (R, G, B), for example (0,0,0) means black, (0,0,255) means blue, the size of each component range is 0 ~ 255.
A color image can be formed by stacking three channels of R, G, and B. We can think of stacking three pieces of paper of the same size, and then looking down from the top. The image you see is the original color image.
For example, if the resolution of the image is 320 * 240, then each channel has a length of 320, a width of 240, and a unit of pixels. The total number of pixels is 3 * 320 * 240.
Method 1:
We can use matplotlib's sub-library pyplot, which provides a drawing framework similar to Matlab, matplotlib is a Python drawing package. Drawing the histogram is mainly achieved by calling the hist() function, which will draw the histogram according to the data source and pixel level.
The form of hist () function is as follows:
hist (data source, pixel level)
Parameter Description:
Data source: one-dimensional array, need to straighten the picture through the function ravel ()
Pixel level: 256, which means [0, 255]
ravel () be used to reduce the multidimensional array to a one-dimensional array.
The form of ravel () function is as follows:
One-dimensional array = multi-dimensional array.ravel ()
eg: plt.hist (img.ravel (), 256)
#ravel () Two-dimensional to one-dimensional 256 gray level grouping
Method 2:
OpenCV provide following function.
cv2.calcHist(image,channels,mask,histSize,ranges[,hist[,accumulate]])
Parameter Description:
l Image: input image, it should be enclosed in brackets []
l channels: The channel of the incoming image, if it is a grayscale image, only one channel, the value is 0. If it is a color image, there are 3 channels, the value is 0,1,2, we can choose one of this value, it should be enclosed in brackets []
l mask: Mask image. If the whole picture is counted, it is none. If the part picture is counted, you need to construct the corresponding inflammation mask to calculate.
l histSize: The number of gray levels, it should be enclosed in brackets []
l ranges: The range of pixel values, range [0,256].
Code:
|
|
|
|
|
After running the following program, three pictures will be displayed in the jupyterLab control interface, as shown below.
【original picture】
【plot draws the histogram】
【Opencv draws the histogram】