Images_and_Channels / channel_functions.py
DebasishDhal99's picture
Update channel_functions.py
bd67e2f
raw
history blame
3.99 kB
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[:,:,2]
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[:,:,0]
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,cv2.COLOR_BGR2RGB))
else:
plt.imshow(plot_arr)
plt.colorbar(orientation= 'vertical', shrink = 0.7, pad = 0.01)
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))
else:
plot = ax.imshow(plot_arr, cmap = cmap)
plt.colorbar(plot, orientation= 'vertical', ax = ax)
# plt.colorbar(orientiation= 'vertical', shrink = 0.7, pad = 0.1)
ax.set_title('Image in the {} channel'.format(channel_name))
def channel_distribution_plotter(img_array):
img_array = img_array[:,:,:3] #Not considering the A channel, if it's a RGBA image.
plt.subplot(2,2,1)
plt.hist(img_array[:,:,2].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[:,:,1].ravel(),bins=256,color='blue');
plt.title("Blue Channel")
plt.subplot(2,2,4)
plt.imshow(cv2.cvtColor(img_array,cv2.COLOR_BGR2RGB))
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()
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[:,:,2]
green_channel = img_arr[:,:,1]
blue_channel = img_arr[:,:,0]
print(np.max(red_channel), np.max(green_channel), np.max(blue_channel))
if original_image_plot == 'yes':
plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB), alpha=original_image_opacity)
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(np.unique(which_channel_dominates))
#Map the color code to the image
plot = plt.imshow(which_channel_dominates, cmap=cmap, alpha=channel_opacity)
#Customize the ticks of the colorbar
plt.colorbar(plot, orientation='vertical',
# ticks=[1,2,3,4,5,6],
ticks = [],
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)