## Python + opencv: histograms

Machine vision 001 2020-11-16 01:29:16
python opencv histograms

## Python+OpenCV： Histogram (Histograms)

### theory

What is histogram ? You can think of a histogram as a graph or graph , It provides you with an overall concept of image intensity distribution . It is x Axis is pixel value ( Range from 0 To 255, Not always ),y The axis is the figure with the corresponding number of pixels in the image .

It's just another way to understand images . By looking at the histogram of an image , You can compare the contrast of the image 、 brightness 、 There is an intuitive understanding of the intensity distribution . Now almost all image processing tools provide histogram features .

You can see the image and its histogram .( remember , This histogram is to draw a grayscale image , Instead of color images ).

The left area of the histogram represents the number of darker pixels in the image , The area on the right shows the number of brighter pixels .

From the histogram , You can see more dark areas than bright areas , And the middle tone ( The pixel value is in the middle , such as 127 about ) The number is very small .

Get the histogram

Now we know what a histogram is , We can see how to get it .

OpenCV and Numpy It's built-in . Before using these functions , We need to know some terms related to histogram .

BINS: The histogram above shows the number of pixels per pixel value , From 0 To 255. in other words , You need 256 Values to display the histogram above .

But think about it , If you don't need to find the number of pixels for all pixel values separately , It's looking for the number of pixels in the pixel value range , What will happen ? for example , You need to find something between 0 To 15, then 16 To 31 The number of pixels between ,……, 240 to 255.

You will only need 16 To represent the histogram . This is it. OpenCV Examples of histograms given in the tutorial .

So what you have to do is simply divide the histogram into 16 Sub part , The value of each sub part is the sum of all the pixel counts in it .

Each of these sub parts is called “BIN”. In the first case ,BINS The quantity of is 256( One box per pixel ), And in the second case , Only 16 individual BINS. stay OpenCV In the document ,bin from histSize The term means .

DIMS: It's the number of parameters that we collect data . In this case , We only collect data about one thing , The strength value . Here is 1.

RANGE: This is the range of intensity values you want to measure . Usually , It is [0,256], I.e. all strength values .

### Example

``````####################################################################################################
# Image histogram (Image Histograms)
def lmc_cv_image_histograms():
"""
The functionality : Image histogram .
"""
# Image histogram (Image Histograms)
x = range(256)
hist1 = lmc_cv.calcHist([image], [0], None, [256], [0, 256])
hist2, bins2 = np.histogram(image.ravel(), 256, [0, 256])
# create a window
pyplot.figure('Image Display 1')
titles = ['Original Image', 'Histogram in OpenCV', 'Histogram in Numpy']
# Show the original image
pyplot.subplot(1, 3, 1)
pyplot.imshow(image, 'gray')
pyplot.title(titles[0])
pyplot.xticks([])
pyplot.yticks([])
# Show histogram
pyplot.subplot(1, 3, 2)
pyplot.plot(x, hist1)
pyplot.title(titles[1])
# pyplot.xticks([])
# pyplot.yticks([])
pyplot.subplot(1, 3, 3)
pyplot.plot(x, hist2)
pyplot.title(titles[2])
# pyplot.xticks([])
# pyplot.yticks([])
# pyplot.axis('on')
# Display window
pyplot.show()
# Displays the histogram of each channel of the color image
image = lmc_cv.cvtColor(image, lmc_cv.COLOR_BGR2RGB)
color = ('r', 'g', 'b')
pyplot.figure('Image Display 2')
# Show the original image
pyplot.subplot(2, 2, 1)
pyplot.imshow(image, 'gray')
pyplot.title(titles[0])
pyplot.xticks([])
pyplot.yticks([])
# Show the original image histogram
pyplot.subplot(2, 2, 2)
for i, col in enumerate(color):
histr = lmc_cv.calcHist([image], [i], None, [256], [0, 256])
pyplot.plot(histr, color=col)
pyplot.xlim([0, 256])
pyplot.title('R/G/B Histogram')
pyplot.subplot(2, 2, 3)
pyplot.xticks([])
pyplot.yticks([])
pyplot.subplot(2, 2, 4)
for i, col in enumerate(color):
histr = lmc_cv.calcHist([image], [i], mask, [256], [0, 256])
pyplot.plot(histr, color=col)
pyplot.xlim([0, 256])