Commit
·
8c863f0
1
Parent(s):
ff067cb
Create channel_functions.py
Browse files- channel_functions.py +113 -0
channel_functions.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def individual_channel_image(img_arr, channel= 'r', ax=None):
|
| 2 |
+
img_arr = img_arr[:,:,0:3]
|
| 3 |
+
|
| 4 |
+
if channel in ['r','red','Red']:
|
| 5 |
+
plot_arr = img_arr[:,:,2]
|
| 6 |
+
channel_name = 'Red'
|
| 7 |
+
cmap = 'Reds'
|
| 8 |
+
if channel in ['g','green','Green']:
|
| 9 |
+
plot_arr = img_arr[:,:,1]
|
| 10 |
+
channel_name = 'Green'
|
| 11 |
+
cmap = 'Greens'
|
| 12 |
+
if channel in ['b','blue','Blue']:
|
| 13 |
+
plot_arr = img_arr[:,:,0]
|
| 14 |
+
channel_name = 'Blue'
|
| 15 |
+
cmap = 'Blues'
|
| 16 |
+
|
| 17 |
+
if channel not in ['r','red','Red','g','green','Green','b','blue','Blue']:
|
| 18 |
+
plot_arr = img_arr
|
| 19 |
+
channel_name = 'Original'
|
| 20 |
+
|
| 21 |
+
if ax is None:
|
| 22 |
+
if channel_name == 'Original':
|
| 23 |
+
plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
|
| 24 |
+
else:
|
| 25 |
+
plt.imshow(plot_arr)
|
| 26 |
+
plt.colorbar(orientation= 'vertical', shrink = 0.7, pad = 0.01)
|
| 27 |
+
plt.title('Image in the {} channel'.format(channel_name))
|
| 28 |
+
plt.show()
|
| 29 |
+
|
| 30 |
+
if ax is not None:
|
| 31 |
+
if channel_name == 'Original':
|
| 32 |
+
ax.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
|
| 33 |
+
else:
|
| 34 |
+
plot = ax.imshow(plot_arr, cmap = cmap)
|
| 35 |
+
plt.colorbar(plot, orientation= 'vertical', ax = ax)
|
| 36 |
+
# plt.colorbar(orientiation= 'vertical', shrink = 0.7, pad = 0.1)
|
| 37 |
+
ax.set_title('Image in the {} channel'.format(channel_name))
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def channel_distribution_plotter(img_array):
|
| 43 |
+
|
| 44 |
+
img_array = img_array[:,:,:3] #Not considering the A channel, if it's a RGBA image.
|
| 45 |
+
|
| 46 |
+
plt.subplot(2,2,1)
|
| 47 |
+
plt.hist(img_array[:,:,2].ravel(),bins=256,color='red');
|
| 48 |
+
plt.title("Red Channel")
|
| 49 |
+
|
| 50 |
+
plt.subplot(2,2,2)
|
| 51 |
+
plt.hist(img_array[:,:,1].ravel(),bins=256,color='green');
|
| 52 |
+
plt.title("Green Channel")
|
| 53 |
+
|
| 54 |
+
plt.subplot(2,2,3)
|
| 55 |
+
plt.hist(img_array[:,:,1].ravel(),bins=256,color='blue');
|
| 56 |
+
plt.title("Blue Channel")
|
| 57 |
+
|
| 58 |
+
plt.subplot(2,2,4)
|
| 59 |
+
plt.imshow(cv2.cvtColor(img_array,cv2.COLOR_BGR2RGB))
|
| 60 |
+
plt.title("Original Image")
|
| 61 |
+
|
| 62 |
+
plt.suptitle("Pixel values distribution in each channel\nx-axis: pixel values, y-axis: number of pixels")
|
| 63 |
+
plt.tight_layout()
|
| 64 |
+
|
| 65 |
+
plt.show()
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def which_channel_dominates(img_arr, original_image_plot = 'yes', original_image_opacity = 0.3, channel_opacity = 0.7):
|
| 71 |
+
cmap = mcolors.ListedColormap(['red', 'green', 'blue', 'white', 'black', 'gray'])
|
| 72 |
+
img_arr = img_arr[:,:,:3]
|
| 73 |
+
|
| 74 |
+
red_channel = img_arr[:,:,2]
|
| 75 |
+
green_channel = img_arr[:,:,1]
|
| 76 |
+
blue_channel = img_arr[:,:,0]
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
print(np.max(red_channel), np.max(green_channel), np.max(blue_channel))
|
| 80 |
+
|
| 81 |
+
if original_image_plot == 'yes':
|
| 82 |
+
plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB), alpha=original_image_opacity)
|
| 83 |
+
|
| 84 |
+
which_channel_dominates = np.zeros((img_arr.shape[0],img_arr.shape[1]))
|
| 85 |
+
|
| 86 |
+
red_greater_green = np.greater(red_channel,green_channel)
|
| 87 |
+
red_greater_blue = np.greater(red_channel,blue_channel)
|
| 88 |
+
green_greater_blue = np.greater(green_channel,blue_channel)
|
| 89 |
+
|
| 90 |
+
#Red is greatest if red is greater than green and blue
|
| 91 |
+
|
| 92 |
+
which_channel_dominates[(red_greater_green & red_greater_blue)] = 1
|
| 93 |
+
which_channel_dominates[green_greater_blue & (~red_greater_green)] = 2
|
| 94 |
+
which_channel_dominates[~green_greater_blue & (~red_greater_blue)] = 3
|
| 95 |
+
|
| 96 |
+
which_channel_dominates[(red_channel == green_channel) & (red_channel == blue_channel)] = 6
|
| 97 |
+
which_channel_dominates[(red_channel == 255) & (blue_channel == 255) & (green_channel == 255)] = 4
|
| 98 |
+
which_channel_dominates[(red_channel == 0) & (blue_channel == 0) & (green_channel == 0)] = 5
|
| 99 |
+
|
| 100 |
+
print(np.unique(which_channel_dominates))
|
| 101 |
+
|
| 102 |
+
#Map the color code to the image
|
| 103 |
+
plot = plt.imshow(which_channel_dominates, cmap=cmap, alpha=channel_opacity)
|
| 104 |
+
|
| 105 |
+
#Customize the ticks of the colorbar
|
| 106 |
+
plt.colorbar(plot, orientation='vertical',
|
| 107 |
+
# ticks=[1,2,3,4,5,6],
|
| 108 |
+
ticks = [],
|
| 109 |
+
label='Dominant Color Channel'
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
text = "Which channel dominates in the image below?\nWhite : R=G=B=255, Black : R=G=B=0\nGray : 0 < R=G=B< 255"
|
| 113 |
+
plt.title(text)
|