-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
103 lines (89 loc) · 4.88 KB
/
Copy pathmain.py
File metadata and controls
103 lines (89 loc) · 4.88 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
#Creating a color histogram for three seperate channel in RGB
def color_histogram_encode(image_path, bins):
'''Args:
- image_path: string, providing a path to the image file, accepted by Pillow library.
- bins: int, amount of bins to use in the histogram (in other words, amount of bars to show).
This algorithm converts an image into an array of pixels in an RGB channel.
Then by going through every single pixel, it divides color intensity of each pixel into groups of Red, Green, and Blue channels.
It also takes into account the number of bins, which will decide what color intensities are "close" enough.
For example, with bin=8, it will take the range 0-32 level of intensity as one first group.
In other words, all of those intensities will be added to the first bar you see in the histogram.'''
image = Image.open(image_path)
#Converting image to array using Numpy
image = np.array(image)
#This array will contain all groups generated by bins
histogram = np.zeros((bins, bins, bins))
#By doing mathematical operation we can successfully put each intensity level into the appropiate groups,
#even if amount of bins is not ideal for the maximum of 255 levels of intensity.
bin_size_without_rem = 256//bins
rem = 256%bins
if rem!=0:
for i in range(image.shape[0]):
for j in range(image.shape[1]):
r, g, b = image[i, j][:3]
if r<rem*(bin_size_without_rem+1):
r_bin=r//(bin_size_without_rem+1)
else:
r_bin=(r-rem*(bin_size_without_rem+1))//bin_size_without_rem + rem
if g<rem*(bin_size_without_rem+1):
g_bin=g//(bin_size_without_rem+1)
else:
g_bin=(g-rem*(bin_size_without_rem+1))//bin_size_without_rem + rem
if b<rem*(bin_size_without_rem+1):
b_bin=b//(bin_size_without_rem+1)
else:
b_bin=(b-rem*(bin_size_without_rem+1))//bin_size_without_rem + rem
histogram[r_bin, g_bin, b_bin] += 1 #This is just a counter of how many pixels have a specific level of R,G,B intensities.
else:
for i in range(image.shape[0]):
for j in range(image.shape[1]):
r, g, b = image[i, j][:3]
r_bin = r // bin_size_without_rem
g_bin = g // bin_size_without_rem
b_bin = b // bin_size_without_rem
histogram[r_bin, g_bin, b_bin] += 1 #This is the same as the one above.
#By dividing all amounts of pixels that have specific levels of R,G,B intensities to a total amount of pixels that are in the image,
#we get frequency of each intensity.
histogram /= image.shape[0] * image.shape[1]
#This divides each of those frequency to appropiate channels - Red, Green, and Blue.
r_hist = histogram.sum(axis=(1, 2))
g_hist = histogram.sum(axis=(0, 2))
b_hist = histogram.sum(axis=(0, 1))
#In return we get lists of frequencies of each color channels where each one has a length of "bins".
return r_hist, g_hist, b_hist
#Plotting a color histogram using list of frequencies of each color channels.
def plot_color_histograms(r_hist, g_hist, b_hist):
'''Args:
- r_hist: list or numpy array, that contains frequencies of each red color intensity.
- g_hist: list or numpy array, that contains frequencies of each green color intensity.
- b_hist: list or numpy array, that contains frequencies of each blue color intensity.
This just uses matplotlib to plot the data received from the function color_histogram_encode.
Nothing too crazy.'''
bins = len(r_hist) #This is still user's input, but it just uses length of one of channels data.
bin_edges=np.arange(0,256,256/bins) #This is basically just frequencies. This will later be used as X coordinates in a histogram.
plt.figure(figsize=(14, 5))
#Creating Red Channel histogram
plt.subplot(1, 3, 1)
plt.bar(bin_edges, r_hist, width=(256 / bins), color='red', edgecolor='black')
plt.title('Red Channel')
plt.xlabel('Intensity')
plt.ylabel('Frequency')
#Creating Green Channel histogram
plt.subplot(1, 3, 2)
plt.bar(bin_edges, g_hist, width=(256 / bins), color='green', edgecolor='black')
plt.title('Green Channel')
plt.xlabel('Intensity')
plt.ylabel('Frequency')
#Creating Blue Channel histogram
plt.subplot(1, 3, 3)
plt.bar(bin_edges, b_hist, width=(256 / bins), color='blue', edgecolor='black')
plt.title('Blue Channel')
plt.xlabel('Intensity')
plt.ylabel('Frequency')
plt.tight_layout()
plt.show()
r_hist, g_hist, b_hist = color_histogram_encode("examples/Cat.png", 40)
plot_color_histograms(r_hist, g_hist, b_hist)