A histogram is a graphical representation of the distribution of pixel values in an image. In image processing, a histogram can be used to analyze the brightness, contrast, and overall intensity of an image. It is plotted in a x-y chart, the x-axis of a histogram represents the pixel values ranging from 0 to 255, while the y-axis represents the number of pixels in the image that have a given value. A histogram can show whether an image is predominantly dark or light, and whether it has high or low contrast. It can also be used to identify any outliers or unusual pixel values.
To better understand the histogram, plot an image and draw some squares and rectangles and fill them with the color value of 0, 50, 100, 150, 175, 200 and 255, as shown in the left-side of Figure 6. The coordinates of each square/rectangle are also displayed in the image, so we can easily calculate how many pixels for each color.
In the histogram plot in the right-side, x-axis is the color value from 0 to 255, and yaxis is the number of pixels. The y value is 20,000 at x=0, meaning there are 20,000 pixels that have a color value of 0, from the left-side image we can see the black rectangle (color: 0) is from (0, 0) to (200, 100), the total number of black pixels is 200 x 100 = 20,000. The histogram plot also shows the color value 0 has 20,000 pixels. The white square (color: 255) has 40,000 pixels. In the same way, calculate the number of pixels for other colors, there are 20,000 pixels for color values of 50, 100, 150, 175 and 200.
This is how the histogram works, the number of pixels and the color values are shown in the histogram.
Below is the code to create the above image and histogram plot.
- def show_histogram():
- img = np.zeros((400, 400), np.uint8)
- cv2.rectangle(img, (200,0), (400, 100), (100), -1)
- cv2.rectangle(img, (0, 100), (200, 200), (50), -1)
- cv2.rectangle(img, (200, 100), (400, 200), (150), -1)
- cv2.rectangle(img, (0,200), (200, 300), (175), -1)
- cv2.rectangle(img, (0, 300), (200, 400), (200), -1)
- cv2.rectangle(img, (200,200), (400, 400), (255), -1)
- fig = plt.figure(figsize=(6, 4))
- fig.suptitle('Histogram', fontsize=20)
- plt.xlabel('Color Value', fontsize=12)
- plt.ylabel('# of Pixels', fontsize=12)
- plt.hist(img.ravel(), 256, [0, 256])
- plt.show()
Explanations:
Line 2 Create a numpy array as a blank canvas with all zeros.
Line 3 - 8 Draw squares and rectangles and fill them with specific color values.
Line 9 – 12 Define a plot using matplotlib library, set title and X, Y-axis labels.
Line 13 Create a histogram using matplotlib function.
Line 14 Show the histogram plot
Histograms can be used for various purposes in image processing, below is a list of some of the use cases, Image equalization, by modifying the distribution of pixel values in the histogram, it is possible to improve the contrast and overall appearance of an image.
Thresholding, by analyzing the histogram, it is possible to determine the optimal threshold value for separating the foreground and background of an image. Color balance, by analyzing the histograms of individual color channels, it is possible to adjust the color balance of an image.
In conclusion, histograms are an important tool in image processing for analyzing and manipulating the distribution of pixel values in an image.
0 comments:
Post a Comment