Convolution:
Things to remember:
a) Reverse kernel horizontally and vertically before applying
b) Output Image Size.rows = InputImageSize.rows - KernelImageSize.rows + 1
Output Image Size.rows = InputImageSize.cols- KernelImageSize.cols+ 1
c) Normalize the kernel so that overflows won't happen
d) Truncate Pixels >255 to 255 and pixels <0 (negative) to 0
Things to remember:
a) Reverse kernel horizontally and vertically before applying
b) Output Image Size.rows = InputImageSize.rows - KernelImageSize.rows + 1
Output Image Size.rows = InputImageSize.cols- KernelImageSize.cols+ 1
c) Normalize the kernel so that overflows won't happen
d) Truncate Pixels >255 to 255 and pixels <0 (negative) to 0
def convolve(image, kernel):
'''Convolve the given image and kernel
Inputs:
image - a single channel (rows, cols) image with dtype uint8
kernel - a matrix of shape (d, d) and dtype float
Outputs:
output - an output array of the same dtype as image, and of shape
(rows - d + 1, cols - d + 1)
Every element of output should contain the application of the kernel to the
corresponding location in the image.
Output elements that result in values that are greater than 255 should be
cropped to 255, and output values less than 0 should be set to 0.
'''
oimage = None
# Insert your code here.----------------------------------------------------
# Get size of kernel, image and initialize output array
kernel_row= kernel.shape[0]
kernel_col= kernel.shape[1]
iimage_row= image.shape[0]
iimage_col= image.shape[1]
print image.shape
print kernel.shape
oimage_row= iimage_row-kernel_row +1
oimage_col= iimage_col-kernel_col +1
#Initialize array as UINT8
oimage = np.zeros((oimage_row,oimage_col),dtype=np.uint8)
print oimage.shape
# Reverse kernel; Use Special trick of -1
rkernel = kernel[::-1,::-1]
#Perform Convolution: Product and Sum
for i in range(0,oimage_row):
for j in range(0,oimage_col):
image_buf= image[i:i+kernel_row,j:j+kernel_col]
image_buf_prod=image_buf*rkernel
pixelf= image_buf_prod.sum();
if pixelf < 0:
oimage[i,j]=0
else:
if pixelf > 255:
oimage[i,j] = 255
else:
# convert float value as uint8
oimage[i,j] = np.uint8(pixelf)
#print oimage
No comments:
Post a Comment