Question
Solution Preview
This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.
import numpy as np#Load NumPy library for easier numerical computationsfrom PIL import Image
from PIL import ImageDraw
import random
import pdb
def normalize(input_im):
base = input_im.min()
roof = input_im.max()
diff = roof - base
scale = diff/255
input_im = input_im - base
output = input_im/scale
return output
# Load image with PIL, convert to grayscale and convert to numpy array
def np_from_img(fname):
im = Image.open(fname)
return np.asarray(im.convert('L'), dtype=np.int32)
# save numpy array as PIL and then save image as file
def save_as_img(ar, fname):
Image.fromarray(normalize(ar).round().astype(np.uint8)).save(fname)
#Normalize the numpy array to 8 bit image consistency
def norm(ar):
return 255.*np.absolute(ar)/np.max(ar)
#Rotate kernel for convolution by 180 degree.(else its correlation)
def conv_transform(kernel):
kernel_cp = np.copy(kernel)
if kernel.ndim == 1:
nCol = kernel.shape[0]
for i in range(nCol):
kernel_cp[i] = kernel[kernel.shape[0]-i-1]
else:
nRow = kernel.shape[0]
nCol = kernel.shape[1]
for i in range(nRow):
for j in range(nCol):
kernel_cp[i][j] = kernel[kernel.shape[0]-i-1][kernel.shape[1]-j-1]
return kernel_cp...