When we perform masking, we see for either black or white or any colour and perform operations according to it. We can also perform bitwise operations on the image when we need to extract only the required part of the image.
Consider the following input images:
The Bitwise operations we can perform on them are:
and finally Bitwise NOT corresponds to A' and B' with respect to set theory.
Results with a subject and the OpenCV logo
Masking is the process of hiding or covering a particular area, so with this code we try to get actual desired PNG's (With transparent background) from fake supposed PNG's you might have downloaded by performing some logical operations. The part with the background gets masked and we are left with the desired image. We do this by checking the RGB values of the input image with a reference value, here we do that using logical operators.
- We define a Mat container
image
which takes in an input image from the user. - We then call our function
Mat masking(Mat image)
which takes in the user input image as it's argument and returns the final image as another Mat container aka image. - The function achieves it's simple masking operation by comparing the RGB values of pixels with the RGB values of colours to be masked/ignored. if their values match we do not write anything to our new image. If the values of the RGB to be neglected don't match with the current pixel RGB values, we then write it to our new transparent image.
- The program then returns the new image, saves it and displays it on the user screen.
- (Optional) If you are facing include errors try changing
#include <opencv2/opencv.hpp>
to#include <opencv4/opencv2/opencv.hpp>
which might potentially resolve your error, or a simple google search will help. - Navigate to ../Pixels_Seminar/4_cv_basics/5_masking
- Open terminal in this folder
- run
make clean
to lean out any previous builds - type
make SRC=main.cpp
to build the executable - You should now have an imshow window open showing you the output as well as a newly created image in your
assets
folder namedbg_free.png
which you can view with your image viewer (recommended)