Challenge

Project 2

1.Script two functions to perform edge detection on a given image using Prewitt filter and Laplacian filter

import numpy as np
import cv2
image = cv2.imread('Challenge_2.png')


cv2.imshow('IMAGE_WINDOW',image)
cv2.waitKey(0)
def prewitt_filter(img):
    
    horizontal_filter = np.array([[-1,-1,-1],[0,0,0],[1,1,1]])
    
    vertical_filter = horizontal_filter.T
    
    vertical_filtered = cv2.filter2D(img,-1,vertical_filter)
    
    horizontal_filtered = cv2.filter2D(img,-1,horizontal_filter) 
    
    prewitt_img = cv2.add(vertical_filtered,horizontal_filtered)
    
    return prewitt_img
                                  
prewitt_img = prewitt_filter(image)

cv2.imwrite('prewitt_filtered_img.png',prewitt_img)

cv2.imshow('window',prewitt_img)
cv2.waitKey(0)
def laplacian_filter(img):
    
    
    laplacian_img = cv2.Laplacian(img,-1)
    
    return laplacian_img
    
    
laplacian_img = laplacian_filter(image)

cv2.imwrite('laplacian_img.png',laplacian_img)

cv2.imshow('window',laplacian_img)
cv2.waitKey(0)

2.Compare both the images and justify which output is better than the other

compare_img = np.hstack((prewitt_img,laplacian_img))

cv2.imshow('window',compare_img)
cv2.waitKey(0)

By comparing the both the prewitt and laplacian filtered images the edges detected in the laplacian flitered image are more

thin and desirable than prewitt filtered image

so laplacian filtered image is better than the prewitt filtered image

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.