Project 2
- Prewitt Operator
Attached below is the function to perform the Prewitt operation on an image.
def prewitt_operator(image: ndarray, gaussian_filter_size: int, gaussian_standard_deviation: float) -> ndarray:
"""
:param image: An np.ndarray() corresponding to the Pixel Intensity of the Image.
:param gaussian_filter_size: An integer corresponding to kernel size of the gaussian filter to apply on image.
:param gaussian_standard_deviation: A float corresponding to the standard deviation used to compute the gaussian filter to apply to image.
:returns : An np.ndarray() of the image onto which the Prewitt filter has been applied.
"""
image = cv2.GaussianBlur(image, (gaussian_filter_size,gaussian_filter_size), gaussian_standard_deviation)
vertical_filter = np.array([[-1,0,1],[-1,0,1],[-1,0,1]])
vertical_filtered_img = cv2.filter2D(image, -1, vertical_filter)
hortizontal_filter = np.array([[-1,-1,-1],[0,0,0],[1,1,1]])
hortizontal_filtered_img = cv2.filter2D(image, -1 , hortizontal_filter)
abs_grad_x = cv2.convertScaleAbs(vertical_filtered_img)
abs_grad_y = cv2.convertScaleAbs(hortizontal_filtered_img)
gradient = cv2.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
return gradient
Arguments:
- image - An ndarray() corresponding to the pixel intensity of the image, read using functions such as cv2.imread()
- gaussian_filter_size - An integer corresponding to the size of the kernel, it is used to compute the gaussian filter to apply to the image.
- gaussian_standard_deviation - A float corresponding to the standard deviation used to compute the gaussian filter to be applied to the image.
Steps:
- A grayscale image of the image is passed to
- A gaussian filter is computed using arguments passed to the function.
- A vertical and horizontal filter is identified to be applied to the image.
- The filters need to be selected in such a way that equal weightage is applied to the neighbouring elements of the pixel of interest.
- The vertical and horizontal filters are applied to the image separately.
- The magnitude of the two images (vectors) gives the result of the filter.
Result:

- Laplacian Operator
Attached below is the function to perform the Laplacian operation on an image.
def laplacian_operator(image: ndarray, gaussian_filter_size: int, gaussian_standard_deviation: float, kernel_size: int = 1) -> ndarray:
"""
:param image: An np.ndarray() corresponding to the Pixel Intensity of the Image.
:param gaussian_filter_size: An integer corresponding to the size of filter to construct the gaussian filter.
:param gaussian_standard_deviation: A float corresponding to the standard deviation of the gaussian filter
which is to be applied on the image.
:param kernel_size: An integer corresponding to the kernel size of the Laplacian filter to be applied on the image.
:returns : An np.ndarray() of the image onto which the Laplacian filter has been applied.
"""
image = cv2.GaussianBlur(image, (gaussian_filter_size, gaussian_filter_size), gaussian_standard_deviation)
filtered_img = cv2.Laplacian(image, cv2.CV_16S, kernel_size)
filtered_img = cv2.convertScaleAbs(filtered_img)
return filtered_img
Arguments:
- image: An np.ndarray() corresponding to the Pixel Intensity of the Image.
- gaussian_filter_size: An integer corresponding to the size of the filter to construct the gaussian filter.
- gaussian_standard_deviation: A float corresponding to the standard deviation of the gaussian filter
which is to be applied to the image. - kernel_size: An integer corresponding to the kernel size of the Laplacian filter to be applied to the image.
Steps:
- A grayscale image of the image is passed to
- A gaussian filter is computed using arguments passed to the function.
- A Laplacian filter is applied to the image using cv2.Laplacian()
Result:

- Laplacian vs Prewitt
- The Laplacian is a second-order derivative of the pixel intensity of an image, whereas the Prewitt operator is the first-order derivative.
- The Prewitt operator needs to be applied separately to both axes of an image, ie the output is dimensionally variant, whereas Laplacian is dimensionally invariant.
- The Accuracy of the Prewitt filter appears to be lower than the Laplacian filter.
- The Laplacian filter is better than Prewitt for this particular image.