File size: 5,781 Bytes
0898363
 
 
 
 
8c863f0
f19b0c8
8c863f0
 
1c552db
8c863f0
 
 
 
 
 
 
1c552db
8c863f0
 
 
 
 
 
 
 
 
eda86ae
 
8c863f0
202eafe
a77476d
8c863f0
 
 
 
 
2a850af
 
5d445d9
8c863f0
 
a77476d
8c863f0
 
 
 
f89a787
 
d414425
f89a787
a19d6e8
f89a787
 
 
 
8c863f0
f89a787
 
6e5e24d
 
 
 
a19d6e8
f89a787
 
 
2cc9665
f89a787
8c863f0
 
 
 
 
1c552db
6ad333a
 
1c552db
8c863f0
1c552db
8c863f0
 
 
 
 
 
 
1c552db
8c863f0
 
 
a3f3361
54cab1d
8c863f0
 
 
 
1c552db
8c863f0
1c552db
 
 
 
8c863f0
 
 
 
 
 
d6e5112
8c863f0
d6e5112
8c863f0
 
ebacab7
8c863f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebacab7
8c863f0
 
7c684da
667e093
 
7e20959
667e093
8c863f0
 
667e093
8c863f0
 
 
2fadbe6
1a35520
8c863f0
 
 
 
be8ca19
7c684da
 
fd59ae6
7c684da
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import numpy as np
import cv2

def individual_channel_image(img_arr, channel= 'r', ax=None):
  img_arr = img_arr[:,:,0:3]
  
  if channel in ['r','red','Red']:
    plot_arr = img_arr[:,:,0]
    channel_name = 'Red'
    cmap = 'Reds'
  if channel in ['g','green','Green']:
    plot_arr = img_arr[:,:,1]
    channel_name = 'Green'
    cmap = 'Greens'
  if channel in ['b','blue','Blue']:
    plot_arr = img_arr[:,:,2]
    channel_name = 'Blue'
    cmap = 'Blues'
  
  if channel not in ['r','red','Red','g','green','Green','b','blue','Blue']:
    plot_arr = img_arr 
    channel_name = 'Original'

  if ax is None:
    if channel_name == 'Original':
      # plt.imshow(cv2.cvtColor(img_arr,cv2.COLOR_BGR2RGB))
      plt.imshow(cv2.cvtColor(np.flip(img_arr,axis=-1),cv2.COLOR_BGR2RGB))
    else:
      plt.imshow(plot_arr, cmap = cmap)
      plt.colorbar(orientation= 'vertical', shrink = 0.7, pad = 0.01, fraction = 0.046,)
      plt.title('Image in the {} channel'.format(channel_name))
      plt.show()

  if ax is not None:
    if channel_name == 'Original':
      # ax.imshow(cv2.cvtColor(img_arr,cv2.COLOR_BGR2RGB))
      ax.imshow(cv2.cvtColor(np.flip(img_arr, axis=-1),cv2.COLOR_BGR2RGB))
      # ax.imshow(cv2.cvtColor(img_arr,cv2.COLOR_RGB2BGR))
    else:
      plot = ax.imshow(plot_arr, cmap = cmap)
      plt.colorbar(plot, orientation= 'vertical', ax = ax, fraction = 0.046,)
      # plt.colorbar(orientiation= 'vertical', shrink = 0.7, pad = 0.1)
      ax.set_title('Image in the {} channel'.format(channel_name))


def individual_channel_image_final(img_arr, channel='Red'):
    if channel in ['Red','Green','Blue']:
        fig, ax = plt.subplots(figsize = (15,10))
        individual_channel_image(img_arr, channel= channel)
        plt.tight_layout()
        plt.show()
        fig.canvas.draw()
        image_array = np.array(fig.canvas.renderer.buffer_rgba())
        return image_array

    if channel in ['All']:
        fig, ax = plt.subplots(2,2, figsize = (12,10))
        individual_channel_image(img_arr, channel='r', ax=ax[0,0])
        individual_channel_image(img_arr, channel='g', ax=ax[0,1])
        individual_channel_image(img_arr, channel='b', ax=ax[1,0])
        individual_channel_image(img_arr, channel='full', ax=ax[1,1])
        plt.tight_layout()
        plt.show()
        fig.canvas.draw()
        image_array = np.array(fig.canvas.renderer.buffer_rgba())
        plt.close(fig)
        return image_array

def channel_distribution_plotter(img_array):

    img_array = img_array[:,:,:3] #Not considering the A channel, if it's a RGBA image.

    fig, ax = plt.subplots(figsize=(8,8))
    plt.yticks([])
    plt.xticks([])
    
    plt.subplot(2,2,1)
    plt.hist(img_array[:,:,0].ravel(),bins=256,color='red');
    plt.title("Red Channel")

    plt.subplot(2,2,2)
    plt.hist(img_array[:,:,1].ravel(),bins=256,color='green');
    plt.title("Green Channel")

    plt.subplot(2,2,3)
    plt.hist(img_array[:,:,2].ravel(),bins=256,color='blue');
    plt.title("Blue Channel")

    plt.subplot(2,2,4)
    plt.imshow(cv2.cvtColor(np.flip(img_array, axis=-1),cv2.COLOR_BGR2RGB))
    # plt.imshow(cv2.cvtColor(img_array,cv2.COLOR_RGB2BGR))    
    plt.title("Original Image")

    plt.suptitle("Pixel values distribution in each channel\nx-axis: pixel values, y-axis: number of pixels")
    plt.tight_layout()
    
    plt.show()
    fig.canvas.draw()
    image_array = np.array(fig.canvas.renderer.buffer_rgba())
    return image_array
     


def which_channel_dominates(img_arr, original_image_plot = 'yes', original_image_opacity = 0.3, channel_opacity = 0.7):
    cmap = mcolors.ListedColormap(['red', 'green', 'blue', 'white', 'black', 'gray'])
    img_arr = img_arr[:,:,:3]

    red_channel = img_arr[:,:,0]
    green_channel = img_arr[:,:,1]
    blue_channel = img_arr[:,:,2]


    print("Red channel max. = ", np.max(red_channel), "Green max. = ", np.max(green_channel), "Blue max. = ",np.max(blue_channel))


    which_channel_dominates = np.zeros((img_arr.shape[0],img_arr.shape[1]))

    red_greater_green = np.greater(red_channel,green_channel)
    red_greater_blue = np.greater(red_channel,blue_channel)
    green_greater_blue = np.greater(green_channel,blue_channel)

    #Red is greatest if red is greater than green and blue

    which_channel_dominates[(red_greater_green & red_greater_blue)] = 1
    which_channel_dominates[green_greater_blue & (~red_greater_green)] = 2
    which_channel_dominates[~green_greater_blue & (~red_greater_blue)] = 3

    which_channel_dominates[(red_channel == green_channel) & (red_channel == blue_channel)] = 6
    which_channel_dominates[(red_channel == 255) & (blue_channel == 255) & (green_channel == 255)] = 4
    which_channel_dominates[(red_channel == 0) & (blue_channel == 0) & (green_channel == 0)] = 5

    print("Unique elements of channel dominat array are: - ",np.unique(which_channel_dominates))

    #Map the color code to the image
    fig, ax = plt.subplots(figsize=(8,8))

    if original_image_plot == 'yes':
       plt.imshow(cv2.cvtColor(np.flip(img_arr, axis=-1),cv2.COLOR_BGR2RGB), alpha=original_image_opacity)

    plot = plt.imshow(which_channel_dominates, cmap=cmap, alpha=channel_opacity)

    
    #Customize the ticks of the colorbar
    plt.colorbar(plot, orientation='vertical', 
                ticks = [],
                fraction=0.032, 
                pad=0.04,
                label='Dominant Color Channel'
                )
    
    text = "Which channel dominates in the image below?\nWhite : R=G=B=255, Black : R=G=B=0\nGray : 0 < R=G=B< 255"
    plt.title(text)
    fig.canvas.draw()
    # plt.plot()
    image_array = np.array(fig.canvas.renderer.buffer_rgba())
    return image_array