- import cv2: It means to import cv2 library to access its functions in our current program.
- import: The import keyword is used to import other modules into a Python script.
- import numpy as np: It means to import numpy library to access its functions in our current program and 'as np' means numpy has given short form np
- img = cv2.imread(path): To read an image in Python using OpenCV we use cv2.imread() function.
- grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY): cv2.cvtColor is used to convert an image from one color space to another. cv2.COLOR_BGR2GRAY is used for BGR Gray conversion.
- grey = cv2.medianBlur(grey, 5): The function cv2.medianBlur() computes the median of all the pixels under the kernel window and the central pixel is replaced with this median value. This is highly effective in removing salt-and-pepper noise. In short, it is used for smoothening the image.
- edges = cv2.adaptiveThreshold(grey,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,9,9): adaptiveThreshold is used when image has different lighting conditions in different areas. It calculate the threshold for a small regions of the image. cv2.ADAPTIVE_THRESH_MEAN_C : threshold value is the mean of neighbourhood area. cv2.THRESH_BINARY: It is used to set the pixel intensity.
- color = cv2.bilateralFilter(img, 9, 250, 250): bilateralFilter is used for smoothening images and reducing noise, while preserving edges
- cartoon = cv2.bitwise_and(color, color, mask = edges): It is used here to invert the color.
- cv2.imshow("Cartoon", cartoon): It is used to show the image.
- cv2.imwrite("cartoon.jpg", cartoon): It is used to save the cartoon image in the same place from where the image is extracted.
- cv2.waitKey(0): cv2.waitKey() is a keyboard binding function. Its argument is the time in milliseconds. The function waits for specified milliseconds for any keyboard event. If you press any key in that time, the program continues. If 0 is passed, it waits indefinitely for a key stroke. It can also be set to detect specific key strokes.
- cv2.destroyAllWindows(): It simply destroys all the windows we created.