Challenge

Project 2

import cv2
import numpy as np

#Prewitt_filtered_image
#Defining prewitt filter

def prewitt_filter(image):
    vertical_filter = np.array([[-1,-1,-1],[0,0,0],[1,1,1]])
    horizontal_filter = np.array([[-1,0,1],[-1,0,1],[-1,0,1]])
    vertical_filtered = cv2.filter2D(image,-1,vertical_filter)
    horizontal_filtered =cv2.filter2D(image,-1,horizontal_filter)
    abs_grad_x = cv2.convertScaleAbs(vertical_filtered)
    abs_grad_y = cv2.convertScaleAbs(horizontal_filtered)
    grad = cv2.addWeighted(abs_grad_x,0.5,abs_grad_y,0.5,0)      # blending gradients
    return grad 

image = cv2.imread('Challenge_2_1638960812.png')      #Reading original image

gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)  '''converting BGR to RGB,because 
                                                        cv2. reads image in BGR format 
                                                        whereas image in RGB format 
                                                        originally'''
prewitt_edges = prewitt_filter(gray_image)           # calling prewitt function 

cv2.imwrite('prewitt_image.png',np.hstack((gray_image,prewitt_edges)))  # saving


#Laplacian_filtered_image
#Defining laplacian filter

def laplacian_filter(image):
    laplacian_filtered = cv2.Laplacian(image,cv2.CV_64F) '''this function detects edges 
                                                            at zero crossing'''
 
    abs_grad = cv2.convertScaleAbs(laplacian_filtered)   '''converts the output from 
                                                           laplacian operator to 
                                                           uint8'''
    return abs_grad
image = cv2.imread('Challenge_2_1638960812.png')      #Reading image

gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)   #Converting image BGR to RGB

Laplacian_edges = laplacian_filter(gray_image)        #calling function

cv2.imwrite('Laplacian_image.png',np.hstack((gray_image,Laplacian_edges))) #saving 


#comparision of prewitt vs laplacian output 

cv2.imwrite('prewitt_VS_laplacian.png',np.hstack((prewitt_edges,Laplacian_edges)))

'''Comparing prewitt & laplacian outputs:

Laplacian output is better than prewiit filter output:

1.laplacian filter is second order derivative along x , y direction combined in single 
  filter.but prewitt is first order derivatives in which edges dectection done in two 
  steps i.e vertical filter & horizontal filter then we combine both.
   
2.As laplacian is second order derivative it marks edges at zero crossing 
  i.e where value of derivative changes negative to positive and vice versa 
   
3.In laplacian output edges are fine and visible but in prewitt output edges are thick 
  and some edges are invisible. 
'''  


   
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.

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.