[Python] Image histogram statistics

grayscale histogram

The grayscale histogram summarizes the grayscale information of the image. Simply put, it is the number of pixels and the occupancy rate in each grayscale image. Creating a histogram is nothing more than two steps, counting the histogram data, and then using Plotting library to plot histograms.

Histogram data

First of all, we need to understand some terms related to functions to facilitate understanding of its application and processing in the python3 library.

BINS: If an image contains pixel values ​​from 0 to 255, 256 values ​​are needed to display its histogram. But what if you don't need to know the number of pixels for each pixel value, but just the number of pixels between two pixel values? Then the range 0 to 255 can be equally divided, and each divided range group is a BIN box.

For example, if you only want to know the number of pixels whose pixel values ​​are between 0 to 15, 16 to 31, ..., 240 to 255, you can divide the range 0 to 255 into 16 parts, and calculate each part separately sum.

DIMS: The number of parameters of the data. If only the gray value is considered for the collected data, the value is 1.

RANGE: Grayscale value range, usually [0, 256], that is, all grayscale value ranges.

draw histogram

① cv2.imread function

Function prototype:

cv2.imread(const string & filename, int flag)

Parameter Description:

  • string & filename: file name;
  • flag: A value of 0 is to convert the image into a gray image; a value of 1 is a color image;

② plt.hist function

Statistical histogram data using matplotlib.

Function prototype:

plt.hist(x, bins=10, range=None, normed=False, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False)

Parameter Description:

  • x: specify the data to draw the histogram;
  • bins: specifies the number of histogram bars;
  • range: Specifies the upper and lower bounds of the histogram data, which includes the maximum and minimum values ​​of the plotted data by default;
# import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('/data/image.jpg', 1)
# img_np = np.array(img) 
plt.hist(img.reshape([-1]), 256, [0,256]);
plt.show()

Parameter description: Histogram plt.hist() and density map for matplotlib visualization_plot.hist_Xiaowen Big Data Blog-CSDN Blog

③ cv2.calcHist function

Statistical histogram data using opencv.

Function prototype:

cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]])

Parameter Description:

  • images: input images;
  • channels: Select the channels of the image. If the input image is a grayscale image, its value is [0]; if the input image is a color image, its value can be [0] [1] [2], corresponding to BGR;
  • mask: The mask is an np array of the same size as image. The part that needs to be processed can be specified as 1 through the mask array, and the part that does not need to be processed can be specified as 0. Generally, it is set to None, which means processing the entire image;
  • histSize: how many bin columns to use, generally 256;
  • ranges: the range of pixel values, generally [0, 255] means 0~255;
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('/data/image.jpg', 0)
histr = cv2.calcHist([img], [0], None, [256], [0, 256])
plt.plot(histr, color = 'b')
plt.xlim([0, 256])
plt.show()

Plot Three Channels Simultaneously

The enumerate function returns the subscript corresponding to the current element, so [i] is [0] [1] [2], corresponding to three channels.

The plt.xlim() function is used to specify the plotting range of the x-axis.

import cv2
from matplotlib import pyplot as plt
img = cv2.imread('/data/image.jpg', 1) 

color = ('b','g','r')
for i, col in enumerate(color):
    histr = cv2.calcHist([img], [i], None, [256], [0, 256])
    plt.plot(histr, color = col)
    plt.xlim([0, 256])
plt.show()

Guess you like

Origin blog.csdn.net/m0_64140451/article/details/131747969