1. Basic meaning and drawing of image histograms
Let's start with one of python's three great swordsmen, matplotlib.
We all know matlab as a tool is recognized as a drawing cow, but I would like to say that python under the matplotlib this super swordsman is also very powerful, because python in recent years is only hot, so the heat is not as high as matlib, but matlib can be achieved as a function of python are almost can be achieved.
Let's get back to the point and start with how matplotlib can simply draw a histogram.
import numpy as np import as plt x=(0,5,0.1) y=(x) (x,y)
It couldn't be simpler, could it? Let's use it to plot the image of a sin(x) function.
So how does matplotlib work with CV?
import cv2 import as plt o=("image\\") histb = ([o],[0],None,[256],[0,255]) (histb,color='b') ()
The result was (and understandably so, right?):
For color images we can also map separately for BGR.
import cv2 import as plt o=("image\\") histb = ([o],[0],None,[256],[0,255]) histg = ([o],[1],None,[256],[0,255]) histr = ([o],[2],None,[256],[0,255]) (histb,color='b') (histg,color='g') (histr,color='r') ()
Histograms are also plotted using the following functions: function hist
Function: Plot histograms based on data source and pixel level.
Syntax: hist(data source, pixel level)
Data source: image, must be a one-dimensional array.
Pixel level: typically 256, meaning [0,255]
Function:Reduces a multidimensional array to a one-dimensional array. Format: one-dimensional array = multidimensional array.ravel()
import cv2 import as plt o=("image\\") ("o",o) () () ((),256)
Statistical histograms and plotting
The sum of the image is drawn using OpenCV and the horizontal coordinate represents the pixel value such as [0,255] and the vertical coordinate represents the number of pixel values.
Drawing Functions:
hist = ( images, channels, mask, histSize, ranges, accumulate )
Where hist returns the result as a histogram, the returned histogram, a two-dimensional array.
imageThe original image: the image needs to be enclosed in "[ ]" to be used.
channels:
The channel number needs to be enclosed in middle brackets When the input image is a grayscale image, it has the value [0]; the color image can be [0], [1], [2]. Corresponding to channels B, G, and R, respectively.
mask: mask imageCounts the histogram of the entire image, set to None. masks the image when counting the histogram of a part of the image.
histSize
Number of BINS, e.g., [256].
ranges
Pixel value range RANGE
accumulateThe default value is false. if set to true, the histogram is not zeroed out at the start of allocation. This parameter allows a single histogram to be calculated from multiple objects or used to update the histogram in real time. The cumulative result of multiple histograms is used to calculate a histogram for a set of images.
Use OpenCV to draw a histogram:
import cv2 import as plt o=("image\\") histb = ([o],[0],None,[256],[0,255]) histg = ([o],[1],None,[256],[0,255]) histr = ([o],[2],None,[256],[0,255]) (histb,color='b') (histg,color='g') (histr,color='r') ()
3. Histograms using masks - histograms, masks
Masking is honestly the use of the black part of the mask to cover off the original part of the image, also known as filtering out. So how do we do this? First we need to create a mask:
mask=(,np.uint8) mask[200:400,200:400]=255
First create an all 0's that matches the size of the original image, then we specify the specified range as white. Then pass it into the function:
import cv2 import numpy as np import as plt Show Histogram image=("image\\",cv2.IMREAD_GRAYSCALE) mask=(,np.uint8) mask[200:400,200:400]=255 histMI=([image],[0],mask,[256],[0,255]) histImage=([image],[0],None,[256],[0,255]) (histImage) (histMI)
The result:
Masking Principle:
Truthfully it's a relationship between with and or, with being that if one doesn't work, none of them do. Or is that if one works, all works.
And our masking principle is mainly used with the operation;
Calculation = cv2.bitwise_and(image1, image2)
import cv2 import numpy as np import as plt image=("image\\",0) mask=(,np.uint8) mask[200:400,200:400]=255 mi=cv2.bitwise_and(image,mask) ('original',image) ('mask',mask) ('mi',mi) () ()
4. Histogram equalization principle and function
This is how it is defined in Wikipedia:
The counterpart on the image is:
Prerequisite: if an image occupies all possible gray levels and is uniformly distributed.
CONCLUSION: The image has high contrast and varied grayscale tones.
Appearance: images are rich in detail and higher quality.
Algorithm:
- 1. Calculate the cumulative histogram
- 2. Convert cumulative histograms to intervals
- 3. In cumulative histograms, raw values with similar probabilities are treated as identical.
1. Calculate the probabilistic situation of the occurrence of gray levels
𝑟𝑘: kth gray level
𝑛𝑘: number of pixels in the kth gray level
N: number of total pixels within the image
L: gray level maximum, gray value interval [0,L-1]
2. Transformation functions
We represent the formula in a picture that is:
This completes the computation of the equalized histogram from the original image.
Although both are similar. However, the distribution of the right side is more balanced, and the neighboring pixel-level probability sum is approximately equal to the high probability. It can be applied to medical image processing, license plate recognition, face recognition.
The corresponding function is:dst = ( src )
import cv2 import numpy as np import as plt img = ('image\\',cv2.IMREAD_GRAYSCALE) equ = (img) ((),256) () ((),256)
Let's deal with lena, that's it:
5. Subplotting
We sometimes want to compare several graphs in one big picture in order to facilitate the comparison, so how do we go about it? Is there a function to accomplish this operation.
subplot(nrows, ncols, plot_number)
rows indicates the number of rows, ncols indicates the number of columns, and plot_number indicates the first. subplot(2,3,4) then indicates 2 rows and 3 columns and the fourth plot.
When each parameter is less than 10, you can just write three numbers as "subplot(234)".
import cv2 import numpy as np import as plt img = ('image\\',cv2.IMREAD_GRAYSCALE) equ = (img) (121),((),256) (122),((),256)
imshow(X, cmap=None)
X denotes the image to be drawn and cmap denotes colormap, the color map, which defaults to RGB(A) color space.
Grayscale image : colormap, color map, defaults to RGB(A) color space use parameter cmap=
Color image : colormap, color map, default is RGB(A) color space, if use opencv to read in the image, the default space is BGR, need to adjust the color space to RGB.
import cv2 import as plt o = ('image\\') g=(o, cv2.COLOR_BGR2GRAY) (221) (o),('off') (222) (o,cmap=),('off') (223) (g),('off') (224) (g,cmap=),('off')
The first figure is: a color image, using the default parameters.
The second figure is: a color image, using the parameter cmap=
The third figure is: a gray image, using the default parameters
The fourth figure is: a gray image, using the parameter cmap=
Then only the fourth figure is correct.
For color images:
import cv2 import as plt img = ('image\\') b,g,r=(img) img2=([r,g,b]) (121) (img),('off') (122) (img2),('off')
You have to split the BGR and then merge it to RGB to make it work.
6. Histogram equalization comparison
import cv2 import as plt img = ('image\\',cv2.IMREAD_GRAYSCALE) equ = (img) (221) (img,cmap=),('off') (222) (equ,cmap=),('off') (223) ((),256) (224) ((),256)
This article on python OpenCV image histogram processing is introduced to this article, more related python OpenCV content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!