-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgb_histogram.py
40 lines (29 loc) · 1.06 KB
/
rgb_histogram.py
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
30
31
32
33
34
35
36
37
38
39
40
import cv2, os, random, glob
import matplotlib.pyplot as plt
breeds = glob.glob('./images/*')
selected_images = []
for folder in breeds:
images_in_folder = glob.glob(os.path.join(folder, '*.jpg'))
selected_image = random.choice(images_in_folder)
selected_images.append(selected_image)
# Plotting the images with their corresponding RGB histograms
plt.figure(figsize=(12, 4))
for i, image_path in enumerate(selected_images):
image = cv2.imread(image_path)
# Splitting the image into its RGB channels
channels = cv2.split(image)
colors = ('r', 'g', 'b')
plt.subplot(1, 4, i + 1)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title(f'Image {i + 1}')
plt.axis('off')
for j, color in enumerate(colors):
histogram = cv2.calcHist([channels[j]], [0], None, [256], [0, 256])
plt.plot(histogram, color=color, label=f'{color.upper()} channel')
plt.xlabel('Intensity')
plt.ylabel('Pixel Count')
plt.xlim(0, 250)
plt.ylim(0, 800)
plt.legend()
plt.tight_layout()
plt.show()