Post

10. Histogram

10. Histogram

Histogram


Prerequisites

1
python

1. Histogram

Histogram is analysis tool about image pixels. There’s no special functions but it’ll make you good decision the more you know. For example, we can usually manually decide which value is good for threshold on a image.

2. Histogram Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import cv2 as cv
import os
import matplotlib.pyplot as plt

def histogram(img):

    channels = img.shape[2] if len(img.shape) == 3 else 1

    colors = ['gray', 'blue', 'green', 'red']
    labels = ['Gray', 'Blue', 'Green', 'Red']

    plt.figure(figsize=(10, 5))

    if channels == 1:
        hist = cv.calcHist([img], [0], None, [256], [0, 256])
        plt.plot(hist, color='gray', label='Gray')

    else:
        for i in range(channels):
            hist = cv.calcHist([img], [i], None, [256], [0, 256])
            plt.plot(hist, color=colors[i], label=labels[i])

    plt.title('Histogram')
    plt.xlabel('Pixel Intensity')
    plt.ylabel('Frequency')
    plt.xlim([0, 256])
    plt.legend()
    plt.grid()
    plt.show()
1
2
3
4
5
import ImageProcessing as ip
dddd
if __name__ == "__main__":
    img = ImageUtils.readImage(ImageUtils.getDataPathWithFile("cat.png"))
    ip.histogram(img)

"opencv-python-10-01.png"

This post is licensed under CC BY 4.0 by the author.