Image Processing for Python — Adjusting to the Ground Truth | by Tonichi Edeza | Dec, 2020

0
54

Ground Truth Algorithm for Beginners

Tonichi Edeza
Old Street (Image by Author)
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from skimage.io import imread, imshow
import skimage.io as skio
from skimage import img_as_ubyte, img_as_float
overcast = imread("image_overcast.PNG")
plt.determine(num=None, figsize=(8, 6), dpi=80)
imshow(overcast);
Rustic Road (Image by Author)
fig, ax = plt.subplots(1,1, figsize=(8, 6), dpi = 80)
patch = Rectangle((70,175), 10, 10, edgecolor='r', facecolor='none')
ax.add_patch(patch)
ax.imshow(overcast);
Red Rectangle Added
coord = Rectangle.get_bbox(patch).get_points()
print(coord)
Coordinate Array
fig, ax = plt.subplots(1,1, figsize=(8, 6), dpi = 80)
ax.imshow(overcast[int(coord[0][1]):int(coord[1][1]),
int(coord[0][0]):int(coord[1][0])]);
image_patch = overcast[int(coord[0][1]):int(coord[1][1]),
int(coord[0][0]):int(coord[1][0])]
image_max = (overcast / image_patch.max(axis=(0, 1))).clip(0, 1)
image_mean = ((overcast * image_patch.imply())
/ overcast.imply(axis=(0, 1))).clip(0,255).astype(int)
fig, ax = plt.subplots(1,2, figsize=(15, 10), dpi = 80)
f_size = 19
ax[0].imshow(image_max)
ax[0].set_title('Max Adjusted', fontsize = f_size)
ax[0].set_axis_off()
ax[1].set_title('Mean Adjusted', fontsize = f_size)
ax[1].imshow(image_mean);
ax[1].set_axis_off()
fig.tight_layout()
Max and Mean Adjusted Images
fig, ax = plt.subplots(1,1, figsize=(8, 6), dpi = 80)
patch1 = Rectangle((100,300), 10, 10, edgecolor='r',
facecolor='none')
patch2 = Rectangle((200,250), 10, 10, edgecolor='r',
facecolor='none')
patch3 = Rectangle((200,190), 10, 10, edgecolor='r',
facecolor='none')
ax.add_patch(patch1)
ax.add_patch(patch2)
ax.add_patch(patch3)
ax.imshow(overcast);
Selecting Different Patches
coor1 = Rectangle.get_bbox(patch1).get_points()
coor2 = Rectangle.get_bbox(patch2).get_points()
coor3 = Rectangle.get_bbox(patch3).get_points()
image_patch1 = overcast[int(coor1[0][1]):int(coor1[1][1]),
int(coor1[0][0]):int(coor1[1][0])]
image_patch2 = overcast[int(coor2[0][1]):int(coor2[1][1]),
int(coor2[0][0]):int(coor2[1][0])]
image_patch3 = overcast[int(coor3[0][1]):int(coor3[1][1]),
int(coor3[0][0]):int(coor3[1][0])]
fig, ax = plt.subplots(1,3, figsize=(15, 12), dpi = 80)
ax[0].imshow(image_patch1)
ax[1].imshow(image_patch2)
ax[2].imshow(image_patch3);
Ground Truth Patches
patch_list = [image_patch1, image_patch2, image_patch3]
image_max_list = [(overcast / patch.max(axis=(0, 1))).clip(0, 1) for
patch in patch_list]
image_mean_list = [((overcast * patch.imply()) / overcast.imply(axis=(0, 1))).clip(0, 255).astype(int) for
patch in patch_list]
def patch_plotter(max_patch, mean_patch):
fig, ax = plt.subplots(1,2, figsize=(15, 10), dpi = 80)
f_size = 19
ax[0].imshow(max_patch)
ax[0].set_title('Max Adjusted Patch', fontsize = f_size)
ax[0].set_axis_off()
ax[1].set_title('Mean Adjusted Patch', fontsize = f_size)
ax[1].imshow(mean_patch);
ax[1].set_axis_off()
fig.tight_layout()
for i in vary(3):
patch_plotter(image_max_list[i], image_mean_list[i])
First Patch
Second Patch
Third Patch
def ground_truth(symbol, patch_names, patch_coordinates):
f_size = 25
figure_size = (17,12)
patch_dict = dict(zip(patch_names, patch_coordinates))
coord = []

fig1, ax_1 = plt.subplots(1, 3, figsize = figure_size)
for n, ax in enumerate(ax_1.flatten()):
#Create Rectangles
key = record(patch_dict.keys())[n]
patch = Rectangle(patch_dict[key], 5, 5, edgecolor='r',
facecolor='none')
coord.append(Rectangle.get_bbox(patch).get_points())
ax.add_patch(patch);

#Show and Format Images
ax.imshow(symbol)
ax.set_title(f'{key}', fontsize = f_size)
ax.set_axis_off()
fig1.tight_layout()

fig2, ax_2 = plt.subplots(1, 3, figsize = figure_size)

for n, ax in enumerate(ax_2.flatten()):
#Show and Format Rectangles
key = record(patch_dict.keys())[n]
ax.imshow(symbol[int(coord[n][0][1]) : int(coord[n][1][1]),
int(coord[n][0][0]) : int(coord[n][1][0])]);
ax.set_title(key, fontsize = f_size)
ax.set_axis_off()

fig2.tight_layout()

fig3, ax_3 = plt.subplots(1, 3, figsize = figure_size)
for n, ax in enumerate(ax_3.flatten()):
patch =symbol[int(coord[n][0][1]) : int(coord[n][1][1]),
int(coord[n][0][0]) : int(coord[n][1][0])]

image_max = (symbol / patch.max(axis=(0, 1))).clip(0, 1)
ax.imshow(image_max)
ax.set_title(f'Max : {patch_names[n]}', fontsize = f_size)
ax.set_axis_off()

fig3.tight_layout()ground_truth(overcast,
['First', 'Second', ' Third'],
[(105, 275), (50, 45), (330, 105)])
Ground Truth Adjusted Images

LEAVE A REPLY

Please enter your comment!
Please enter your name here