In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you the "Python+OpenCV image processing histogram statistics example analysis", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Python+OpenCV image processing histogram statistics example analysis" this article.
1. Histogram Overview (1) basic Concepts
Histogram is another interpretation of an image, which describes the gray distribution of the whole image. The x axis of the histogram represents the gray value (0,255), and the y axis represents the number of pixels of the same gray value in the picture, so through the histogram we can have an intuitive understanding of the brightness, grayscale distribution and contrast of the image.
(2) terms in histograms
BINS
As mentioned earlier, the x-axis in the histogram represents the gray value, and the grayscale of a grayscale image has 256 levels, so do we need to mark each level on an axis? Or what if what we need is not the distribution of each gray value, but a range of grayscale distribution? So we call each required range of gray values a BIN, that is, all grayscale levels are divided into several groups, and each group is a BIN.
DIMS
Represents the number of parameters of the image we collect. Histogram if we collect only one parameter of grayscale value, then the value of that parameter is 1.
RANGE
Represents the range of statistical grayscale values, the general range is [0-255]
two。 Histogram drawing (1) reading image information
The first thing in the computer vision series is to read image information:
"" Author:XiaoMadate:2021/10/24 "" # call required package import cv2import matplotlib.pyplot as plt img0 = cv2.imread ('E:\ From Zhihu\ For the desk\ cvseven.jpeg') img1 = cv2.cvtColor (img0, cv2.COLOR_BGR2GRAY) # converted to grayscale image h, w = img1.shape [: 2] print (h, w) cv2.namedWindow ("W0") cv2.imshow ("W0", img1) cv2.waitKey (delay = 0)
The image information is as follows:
419 636
(2) drawing histogram
The functions used to draw the histogram are as follows:
Hist = cv2.calcHist (images, channels, mask, histSize, ranges, accumulate)
Images: original image
Channels: the specified channel [0] represents a grayscale image. If the image read in is not a grayscale image, the value can be [0], [1], [2], respectively, to represent the channel Bmai Gmyr.
Mask: mask image, which is None when drawing the whole image
Number of histSize:BIN
Ranges: range of pixel valu
Accumulate: cumulative identification, which can generally be omitted
Histogram of grayscale image
# drawing histogram hist = cv2.calcHist ([img1], [0], None, [256,0255]) plt.plot (hist, color = 'lime', label =' histogram', linestyle ='-') plt.legend () plt.savefig ('E:\ From Zhihu\ For the desk\ cvseven1.jpeg') plt.show ()
It can be seen that the pixels with higher brightness are still in the majority in this grayscale image, that is, the overall brightness is higher.
Color histogram
Read the color image and draw a histogram of a channel
"Author:XiaoMadate:2021/10/24" # call the required package import cv2import matplotlib.pyplot as plt plt.rcParams ['font.family'] =' SimHei' # change the global Chinese font to bold img0 = cv2.imread ('E:\ From Zhihu\ For the desk\ cvseven.jpeg') img1 = cv2.cvtColor (img0, cv2.COLOR_BGR2GRAY) h, w = img1.shape [: 2] print (h, w) cv2.namedWindow ("W0") cv2.imshow ("W0") Img0) cv2.waitKey (delay = 0) # drawing histogram hist = cv2.calcHist ([img0], [0], None, [256,255]) plt.plot (hist, color = 'lime', label =' Blue Channel histogram', linestyle ='-', alpha = 1) plt.legend () plt.savefig ('E:\ From Zhihu\ For the desk\ cvseven1.jpeg') plt.show ()
The picture above is a histogram of the blue channel.
3. Mask histogram (1) basic Concepts
If we do not need the histogram of the whole image, but the histogram of a region, we only need to draw a picture, set the part that needs to be counted to white and the part that does not need to be set to black, and form a mask image.
(2) implementation code
Get the mask map
# # get the mask mask = np.zeros (img0.shape, np.uint8) # set each pixel to 0, that is, black mask [109 mask 309, 212 img0.shape 412] = 255 # Select a specific area to set to white img0_1 = cv2.bitwise_and (img0, mask) # Image and operation to get the mask cv2.namedWindow ("W1") cv2.imshow ("W1", img0_1) cv2.waitKey (delay = 0)
Draw mask histogram
# draw the mask histogram # # get the mask mask = np.zeros (img1.shape, np.uint8) # set each pixel to 0 Black mask [109 img1 309, 212 img1 412] = 255 # Select a specific area set to white img1_1 = cv2.bitwise_and (img1, mask) # Image and operation to get the mask cv2.namedWindow ("W1") cv2.imshow ("W1", img1_1) cv2.waitKey (delay = 0) # # draw the mask histogram and part of the image histogram hist1 = cv2.calcHist ([img1], [0], mask, [256] [0,255]) # Mask histogram Parameters need to be modified hist2 = cv2.calcHist ([img1], [0], None, [0255]) plt.plot (hist1, color = 'baked, label =' mask histogram', linestyle ='-') plt.plot (hist2, color = 'ritual, label =' original histogram', linestyle ='-.') plt.legend () plt.savefig ('E:\ From Zhihu\ For the desk\ cvseven2.jpeg') plt.show ()
The resulting image is as follows:
4. Hmurs histogram (1) basic concepts
H (Hue)-S (Saturation) histogram, i.e. hue-saturation histogram
To draw the histogram, you need to convert the source RGB image to the HSV (hue, saturation, brightness) color space.
Img0_2 = cv2.cvtColor (img0, cv2.COLOR_BGR2HSV) # convert RGB space to HSV space cv2.namedWindow ("W2") cv2.imshow ("W2", img0_2) cv2.waitKey (delay = 0)
(2) draw two-dimensional Hmurs histogram
Refer to here: OpenCV official website
# # drawing Hmer S histogram hist3 = cv2.calcHist ([img0_2], [0Magne1], None, [180,256], [0180,0256]) # the explanation given on the official website: channel = [0jue 1] because we need to deal with both the H and S planes; bins = [180256] H plane is 180 and S plane is 256 Range = [0180 0256] hue value is between 0 and 180, saturation is between 0 and 256 plt.imshow (hist3) plt.savefig ('E:\ From Zhihu\ For the desk\ cvseven3.jpeg') plt.show ()
The resulting image is as follows:
The X axis in the image above represents S (saturation) and the Y axis represents H (hue).
The peak value in this picture is mainly distributed between S (0-50) and H (20-80). As for why the peak value is less, my guess is that the color change in the original image is not obvious, so it is impossible to draw too many obvious peaks.
The above is all the contents of the article "sample Analysis of histogram Statistics of Python+OpenCV Image processing". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.