CARTOONIFY IMAGE
As you might know, sketching or creating a cartoon doesn’t always need to be done manually.
Nowadays, many apps can turn your photos into cartoons.
But what if I tell you,
that you can create your own effect witha a few lines of code?
There is a library called OpenCV which provides a common
infrastructure for computer vision applications and has optimized-machine-learning
algorithms. It can be used to recognize objects, detect, and produce high-resolution images.
To create a cartoon effect, we need to pay attention to two things; edge and color palette.
Those are what make the differences between a photo and a cartoon.
OpenCv
But first off all,let's see what OpenCv actually is and how it works.
OpenCV is an open-source python library used for computer vision and machine learning.
It is mainly aimed at real-time computer vision and image processing.
It is used to perform different operations on images which transform them using different techniques.
It majorly supports all languages like Python, C++, Android, Java, etc. It is easy to use and in demand due to its features.
It is used in creating image processing or rendering application using different languages.
Now that we know that, let's get to the actual process of creating a cartoonify image
Steps
1. First off all,we import the necessary modules like :
-CV2: Imported to use OpenCV for image processing
- easygui: Imported to open a file box. It allows us to select any file from our system.
-Numpy: Images are stored and processed as numbers. These are taken as arrays.
-Imageio: Used to read the file which is chosen by file box using a path.
-Matplotlib: This library is used for visualization and plotting.
-OS: For OS interaction. Here, to read the path and save images to that path.
-Flask: Flask is a micro web framework written in Python.
2. In this step, we will build the main window of our application, where the buttons,
labels, and images will reside.
3. Now, just think, how will a program read an image? For a computer, everything is just numbers.
We will convert our image into a numpy array
Imread is a method in cv2 which is used to store images in the form of numbers.
This helps us to perform operations according to our needs.
The image is read as a numpy array, in which cell values depict R, G, and B values of a pixel.
4. Transforming an image to grayscale
cvtColor(image, flag) is a method in cv2 which is used to transform an image into the colour-space mentioned as ‘flag’.
Our first step is to convert the image into grayscale.
We use the BGR2GRAY flag. This returns the image in grayscale.
A grayscale image is stored as grayScaleImage.
5. Smoothening a grayscale image
To smoothen an image, we simply apply a blur effect.
This is done using medianBlur() function. The center pixel is assigned a mean value of all the pixels which fall under the kernel.
In turn, creating a blur effect.
6. Retrieving the edges of an image
-Highlighted Edges
-Smooth colors
6.1 Try to retrieve the edges and highlight them. This is attained by the adaptive thresholding technique.
The threshold value is the mean of the neighborhood pixel values area minus the constant C. C is a constant that is subtracted from the mean or weighted sum of the neighborhood pixels.
Thresh_binary is the type of threshold applied, and the remaining parameters determine the block size.
6.2.Preparing a Mask Image
We prepare a lightened color image that we mask with edges at the end to produce a cartoon image.
We use bilateralFilter which removes the noise. It can be taken as smoothening of an image to an extent.
It’s similar to BEAUTIFY or AI effect in cameras of modern mobile phones.
7. Giving a Cartoon Effect
So, let’s combine the two specialties. This will be done using MASKING. We perform bitwise and on two images to mask them. Remember, images are just numbers?
Yes, so that’s how we mask edged image on our “BEAUTIFY” image.
This finally CARTOONIFY our image!
Bibliography :
- https://projectworlds.in/image-to-cartoon-python-opencv-machine-learning/
- https://towardsdatascience.com/turn-photos-into-cartoons-using-python-bb1a9f578a7e
- https://towardsdatascience.com/using-opencv-to-catoonize-an-image-1211473941b6
- https://data-flair.training/blogs/cartoonify-image-opencv-python/
Niciun comentariu:
Trimiteți un comentariu