Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deeply understand Python two-dimensional histogram

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What this article shares to you is about how to deeply understand the Python two-dimensional histogram, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Preface

Only by counting the gray value of a pixel, it can be turned into an one-dimensional histogram. The two-dimensional histogram can count the hue and saturation of pixels and can be used to find the color histogram of the image.

1. Two-dimensional histogram in OpenCV

OpenCV still uses the cv2.calcHist () function to find the color histogram of the image, but it is different when specifying parameters.

The basic format of the cv2.calcHist () function is as follows:

Hist = cv2.calcHist (image, channels, mask, histSize, ranges)

The original image specified by the image parameter should be converted from BGR color space to HSV color space, and the actual parameters should be enclosed in square brackets.

When the channels parameter is set to [0jue 1], hue and saturation are processed at the same time

When the histSize parameter sets the bins value to [180256], it indicates that the hue is 180 and the saturation is 256.

When the ranges parameter is set to [0180,0256], the range of hue and saturation are [0180] and [00.2565] respectively.

The color histogram returned by the cv2.calcHist () function can be displayed directly using the cv2.show () function.

Import cv2import numpy as npimport matplotlib.pyplot as pltimg = cv2.imread ('XIAN.jpg') cv2.namedWindow (' orininal', cv2.WINDOW_NORMAL) cv2.imshow ('orininal', img) img2 = cv2.cvtColor (img, cv2.COLOR_BGR2HSV) hist = cv2.calcHist ([img2], [0meme 1], None, [180,256]) cv2.namedWindow (' 2D Histology, cv2.WINDOW_NORMAL) cv2.imshow ('2D Hist') Hist) cv2.waitKey (0) cv2.destroyAllWindows () plt.imshow (hist, interpolation = 'nearest') # draw color histogram plt.show () # display color histogram

The color histogram returned by the cv2.calcHist () function is a two-dimensional array with a size of 180cm 256. It is a grayscale image when displayed with the cv2.imshow () function, which can not directly show the color distribution.

You can use the matplotlib.pyplot.imshow () function to draw two-dimensional histograms with different colors.

2. Two-dimensional histogram in Numpy

The np.histogram2d () function of Numpy is used to calculate the two-dimensional histogram. The basic format is as follows:

Hist, xedges, yedges = np.histogram2d (x, y, bins, range)

Hist is the returned histogram

Xedges is the BINS boundary value of the histogram of the returned x

Yedges is the BINS boundary value of the histogram of the returned y

X and y are the one-dimensional arrays converted from the corresponding channels of the original image.

Bins is the value of BINS, such as [180256]

Range is a range of pixels in the format [[0180], [0256]]

Img = cv2.imread ('building.jpg') cv2.imshow (' orininal', img) img2 = cv2.cvtColor (img, cv2.COLOR_BGR2HSV) h, s, v = cv2.split (img2) hist, x, y = np.histogram2d (h.ravel (), s.ravel (), [180,256], [0,180], [0256]) cv2.imshow ('2D histology, hist) cv2.waitKey (0) cv2.destroyAllWindows () plt.imshow (hist, interpolation = 'nearest') plt.show ()

You can use the matplotlib.pyplot.imshow () function to draw two-dimensional histograms with different colors.

3. Histogram example 1. Use the Numpy function to calculate the histogram import cv2import numpy as npimport matplotlib.pyplot as pltimg = cv2.imread ('home.jpg') plt.figure (figsize = (252.25)) imgrgb = cv2.cvtColor (img, cv2.COLOR_BGR2RGB) plt.subplot (2,2,1) plt.title (' Original') plt.axis ('off') plt.imshow (imgrgb) histb E1 = np.histogram (img [0] .ravel (), 256,256, [0,255]) # calculate B channel histogram histg, e2 = np.histogram (img [1] .ravel (), 256,256, [0,255]) # calculate G channel histogram histr, e3 = np.histogram (img [2] .ravel (), 256,256, [0255]) # calculate R channel histogram plt.subplot (2,2,2) plt.plot (histb) Color ='b') plt.plot (histg, color ='g') plt.plot (histr, color ='r') plt.title ('Hist') img2 = cv2.cvtColor (img, cv2.COLOR_BGR2HSV) # convert color space to HSVh, s, v = cv2.split (img2) hist, x, y=np.histogram2d (h.ravel (), s.ravel (), [180,256], [0] ], [0256]]) # calculate the color histogram plt.subplot (2,2,3) plt.title ('2Dhistl') # set the subgraph window title plt.imshow (hist, interpolation = 'nearest' Cmap = 'gray') # draw color histogram plt.show () # display color histogram

2. Use OpenCV function to calculate histogram # 2. Use the OpenCV function to calculate the histogram import cv2import numpy as npimport matplotlib.pyplot as pltimg = cv2.imread ('flower.jpg') plt.figure (figsize = (25p25)) imgrgb=cv2.cvtColor (img, cv2.COLOR_BGR2RGB) plt.subplot (2,2,1) plt.imshow (imgrgb) plt.title (' Original') plt.axis ('off') histb = cv2.calcHist ([img], [0], None [0255]) # calculate B channel histogram histg = cv2.calcHist ([img], [1], None, [256th], [0255]) # calculate G channel histogram histr = cv2.calcHist ([img], [2], None, [256], [0255]) # calculate R channel histogram plt.subplot (2,2,2) plt.plot (histb, color= 'b') plt.plot (histg) Color= 'g') plt.plot (histr, color= 'r') plt.title ('Hist') img2=cv2.cvtColor (img,cv2.COLOR_BGR2HSV) hist = cv2.calcHist ([img2], [0,1], None, [180,256], [0,180,0256]) plt.subplot (2,2,3) plt.title (' 2Dhistl') # set subimage window title plt.imshow (hist,interpolation = 'nearest'') Cmap = 'gray') # draw color histogram plt.show () # display color histogram

The above is how to gain an in-depth understanding of the Python two-dimensional histogram. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report