Challenge

Project 2

OBJECTIVE: Apply Prewitt filter & Laplacian filter to the given image & determine the filter which determines the edges better than the other filter

SOLUTION PROCEDURE:

  • We read the image with the help of the imread function from opencv with the image name as input
  • We add the salt & pepper noise over the image with the help of user defined function
    • Copy of the image is created to create the noisy image
    • If the length of the image.shape is 2 then it indicates the image is gray scale so it will have only one channel. Hence black is 0 and white is 255.
    • Else then it indicates the image is color so it will have three channels (R,G,B). Hence black is (0,0,0) and white is (255,255,255)
    • We create a matrix with random values (r) with the help of random function from numpy
    • We randomly assign the pixels of the noisy image as black & white with the checking condition as (r < r/2) and (r > 1-(r/2)) respectively.
  • We convert the image to grey colour with the help of cvtColor function from opencv.
    • Reason for converting to the grey image from Colour is because grey scale simplifies the work of algorithms used to extract features from images and reduces computational requirements to achieve good performance.
  • Prewitt filter:
    • Prewitt filter is one of the first order derivative filters which involves taking derivative along x & y direction. Edges in the image are determined by the spike in the intensity. Unique factor of the prewitt filter is the uniform weightage is given to the neighbouring pixels.
    • User defined function is used to apply the prewitt filter to the grey image to detect the edges
      • Gaussian Blur function from opencv is used as the preprocessing step to remove the amount of noise and remove speckels within the image. This helps to avoid detection of false edges by the following filters.
      • Standard Vertical filter & horizontal filter of the prewitt function are defined inside the function
      • Vertical filter & horizontal filter are convoluted with the image separately with the use of filter2D function.
      • With the help of the function convertScaleAbs,we take the absolute values & converts it to unsigned int for both the Horizontally filtered & vertically filtered arrays.
      • Finally Output image is formed with the addWeighted function which is used to calculate the weighted sum of the horizontally filtered & vertically filter image arrays giving equal weights of 0.5 to both.
    • Laplacian filter:
      • Laplacian filter is one of the second order derivative filters which involves taking second order partial derivative along x & y direction. Edges are identified by the zero crossing. x & y directional filters are combined into single filter in the Laplacian filter.
      • User defined function is used to apply the Laplacian filter to the grey image to detect the edges
        • Gaussian Blur function from opencv is used as the preprocessing step to remove the amount of noise and remove speckels within the image. This helps to avoid detection of false edges by the following filters.
        • Laplacian function from opencv is used to apply the Laplacian filter to the image which is pre processed by the gaussian filter with the depth same as original image.

FUNCTION SYNTAX:

  • cvtColor(image, cv2.COLOR_BGR2GRAY)

Function is used to convert the given image to another colour format, in this case we have converted the given image to grey colour

  • GaussianBlur(image):

Function is used to convert remove the noise and speckles of the image with the help of the gaussian filter.

  • filter2D (image, ddepth, filter):

Function is used to create the convolution of the two matrices with the image & filter with the depth of the output image. Depth of -1 will give the output image same as input image.

  • convertScaleAbs(array):

Function is used to take the absolute values & converts it to unsigned int

  • addWeighted(array1, weight1, array2, weight2, gamma)

Function is used to determine the array 1 with weight 1 & array 2 with weight 2 which will be added by the gamma (additional scalar value).

  • Laplacian(image, ddpeth):

Function is used to apply the Laplacian filter to the image with the depth of the output image as an input.

CODE: Jupyter notebook attached

OUTPUT:

Grey Image with prewitt filter:

Grey Image with Laplacian filter:

Comparison of both the filters:

CONCLUSION:

Based on the above image, we are able to notice the below key points

  1. Edges are thin & clear in the image returned by the Laplacian filter while the edges are thick & fine in the image returned by the prewitt filter
  2. Rear tail lamp profile & fuel inlet hole are captured completely by the Laplacian but the only half of the profiles are captured by the prewitt filter (b)
  3. Sill bottom on the both sides & both doors are captured clearly by the Laplacian but the outer edges are only captured by the prewitt filter (c)
  4. A pillar region of the BIW is captured definitely to inform the user about the thickness of the section at that location by the Laplacian but only one of the edges are captured by the prewitt(d)
  5. Trunk profile along with the bumper are captured defining the rear shape of the car by the Laplacian but the bumper bottom profile alone is captured by the prewitt(e)
  6. Both sides of the BIW section where the doors are fitted is clearly marked by the edges captured by the Laplacian but the prewitt has again captured only one of the edges of the section(f)

Based on the above said points, we are able to conclude that the Laplacian filter is picking the edges much better compared to the prewitt filter.

References:

https://www.askpython.com/python-modules/opencv-filter2d

https://www.geeksforgeeks.org/python-opencv-filter2d-function/

https://docs.huihoo.com/opencv/2.4-documentation/modules/core/doc/operations_on_arrays.html

https://riptutorial.com/opencv/example/28254/datatype

https://www.geeksforgeeks.org/python-program-to-detect-the-edges-of-an-image-using-opencv-sobel-edge-detection/

https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0029740#:~:text=The%20main%20reason%20why%20grayscale,algorithm%20and%20reduces%20computational%20requirements.



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.