March 2019
Intermediate to advanced
532 pages
13h 2m
English
Therefore, the code for calculating the histogram for a full grayscale image (without a mask) is as follows:
image = cv2.imread('lenna.png')gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)hist = cv2.calcHist([gray_image], [0], None, [256], [0, 256])
In this case, hist is a (256, 1) array. Each value (bin) of the array corresponds to the number of pixels (frequency) with the corresponding tone value.
To plot histograms with Matplotlib, you can use plt.plot(), providing the histogram and the color to show the histogram (example, color='m'). The following color abbreviations are supported—'b'—blue, 'g'—green, ‘r’—red, 'c'—cyan, 'm'—magenta, 'y'—yellow, 'k'—black, and 'w'—white. The full code for this example ...