Challenge

Project 2

1. code:

import numpy as np
import cv2

def display_img(disp_img, title ='img_display'):
    cv2.imshow(title,disp_img)
    cv2.waitKey(0)

def read_img(path,chn=3):
    img_matrix = cv2.imread(path,chn)
    display_img(img_matrix)
    return img_matrix

def gaussian_smooth(grey_img):
    # Img Gaussian blur/smooth (Low Pass Filter)
    G_Blur_img = cv2.GaussianBlur(grey_img,(5,5), 5)
    return G_Blur_img

## perwitt Filter
def perwittFilter(grey_img,gaussian_img):
    # Finding Img gradient x and y
    vert_filter = np.array([[-1,0,1], [-1,0,1], [-1,0,1]]) # perWitt vertical filter
    # perwitt Horizontal filter, transpose of vertical filter
    horz_filter = np.array([[-1,-1,-1], [0,0,0], [1,1,1]]) 
    perWitt_grad_x = cv2.filter2D(gaussian_img, -1, horz_filter)
    perWitt_grad_y = cv2.filter2D(gaussian_img, -1, vert_filter)
    # Magnitude of gradient
    abs_grad_x = cv2.convertScaleAbs(perWitt_grad_x) # gradient of img in x direction
    abs_grad_y = cv2.convertScaleAbs(perWitt_grad_y) # gradient of img in y direction
    # Blends images, magnitude - sobel gradient magnitude
    grad = cv2.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
    return grad

input_img = read_img('assignment_2/Challenge_2.PNG',0) #convert to greyscale image
G_smooth_img = gaussian_smooth (input_img)

perwitt_img = perwittFilter(input_img,G_smooth_img)
display_img(perwitt_img,'perwitt_edge_img')

laplace_img = cv2.Laplacian(input_img, -1,ksize=3)
display_img(laplace_img,'laplace_edge_img')

perwitt image:

laplace image:

perwitt_edge vs laplace_edge

-> perwitt filter is first order derivative, laplace filter is second order derivative

-> laplace filter edges are much sharper continuous and better than perwitt filter edges.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.