-
Libraries to Import
Import the Numpy and Open cv2 libraries
pip commands to install numpy and opencv
1. pip install numpy 2. pip install opencv-python
import cv2 import numpy as np
-
Read the Image
img = cv2.imread("image//nature.jpg") cv2.imshow("input-1",img)
If you want to resize the image window, add the below code
pic = cv2.resize(img, (width,height))
-
Convert the image to gray
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) cv2.imshow("gray-2", gray)
-
Blur the gray converted image
Syntax:
cv2.medianBlur(input_image, kernel_size)
Code:
gray = cv2.medianBlur(gray,3) cv2.imshow("blur-3", gray)
-
Perform Adaptive Threshold
AdaptiveThreshold function performs the adaptive threshold to find dark edges
syntax:
adaptiveThreshold(src, dst, maxValue, adaptiveMethod, thresholdType, blockSize, C)
Code:
edges = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,3,5)
Depending on the Image, change the numbers in the code in order to get best output
-
Apply Bilateral Filter
A bilateral filter is a non-linear, edge-preserving, and noise-reducing smoothing filter for images.
Syntax:
cv2.bilateralFilter ( src, dst, d, sigmaColor,sigmaSpace, borderType = BORDER_DEFAULT )
To know more about bilateral Filter: Link
Code:
color = cv2.bilateralFilter(img,2,250,250) cv2.imshow("bilateral-5",color)
Depending on the Image, change the numbers in the code in order to get best output
-
Final Output
Performing bitwise_and on blurred image and masking the edged image.
Syntax:
bitwise_and(source1_array, source2_array,destination_array, mask)
Code:
cartoon = cv2.bitwise_and(color,color,mask=edges) cv2.imshow("output-6",cartoon)