Invisibility and more — openCV C++

Madara Premawardhana
Geek Culture
Published in
4 min readSep 13, 2021

Who doesn’t like magic? “Howard, if I may interject. Is spending time on magic tricks is how you really wanna spend your time?” Said Sheldon Cooper. But here I go with a more logical trick which Sheldon will be able to figure out.

But let me put my work through simply as well. First we need a video feed. And as we know, a video is a series of images moving with respect to time- hence the movie. And you also know images are made with pixels. So if we alter and tamper with these pixels like crazy monkeys we can make simple tricks.

We can easily take the video feed like this!

VideoCapture cap(0);

But what if you are trying to take the video even without having a web-cam? Jeepers! are you crazy? Then let’s give you a message saying “No you can’t do this because you don’t have a video feed!”

if(!cap.isOpened()){ 
cout << “No you can’t do this because you don’t have a video feed” << endl;
return -1; }

Then what? Yes. You need to declare the image you need to trick people and also each image which the video is made with. Let’s call them background and image respectively. Then we need to get the capture we got from video feed and put it to the background as the base layer.

Mat img;
Mat background;
for (int i = 0; i < 60; i++)
{
cap >> background;
}

Now that we have them all, let’s take another variable and assign each image that video is made up into HSV. HSV is Hue-Saturation-Value.

Photo by Robert Katzki on Unsplash

Here’s an explanation of how those colors work. Hue is which color from the visible spectrum which you need. Saturation is how much of brilliance you need (Low saturation is dull and high saturation is spectacular!) and Value is basically what shade of it you need- either very dark or light.

How our image’s Reds and Greens and Blues will be converted to HSV.

Then we need masks. These masks will help us to extract which color we need from the images converted to HSV to make them invisible. Basically what it does is take a specific range we ask it to and make a pact with us that it’ll take them and do magic with it using openCV’s inbuilt functions.

There is a lower and upper range of colors we need. For an example, you can get the values into an array looking at an HSV distribution like in the image below. Those colors inside the circle are mostly lying inside the range shown.

Then we’ll be morphing the two masks and create a kernel. Then let’s ,make mask 2 as input and output mask 2 as it’s NOT or just the opposite. We will then put AND operator between two masks of the frame and call it resolution1. Then AND operator between backgrounds as inputs and call it resolution2. Both of these AND operations are bitwise.

Making the two resolution values as input arrays, we will then Add weights of the to and say what to show and what to hide and what will be the final output. This happens inside openCV function addWeighted

Then it’s just a matter of displaying the video feed and release windows. Viola!

while (true)
{
Mat frame;
cap >> frame;
if (frame.empty())
break;
Mat hsv;
cvtColor(frame, hsv, COLOR_RGB2HSV);
Mat mask1, mask2;inRange(hsv, Scalar(30, 120, 70), Scalar(50, 255, 255), mask1);
inRange(hsv, Scalar(50, 120, 70), Scalar(75, 255, 255), mask2);
mask1 = mask1 + mask2;
Mat kernel = Mat::ones(3, 3, CV_32F);
morphologyEx(mask1, mask1, MORPH_OPEN, kernel);
morphologyEx(mask1, mask1, MORPH_DILATE, kernel);
bitwise_not(mask1, mask2);Mat res1, res2, final_output;bitwise_and(frame, frame, res1, mask2);
bitwise_and(background, background, res2, mask1);
addWeighted(res1, 1, res2, 1, 0, final_output);
imshow("Magic !!!", final_output);
char c = (char)waitKey(20);
if (c == 27)
break;
frame.release(), hsv.release(), mask1.release(), mask2.release(), res1.release(), res2.release(), final_output.release();
}

Now, I’ve made this to make Green invisible. In case you need to use other colors, check with your HSV image and choose lower and upper bounds as necessary.

I’ve uploaded the code to github here : https://github.com/MadaraPremawardhana/Invisibility-Cloak-C-/commit/1ab72c2f5c92d03246c79c7eab9e3f377173252c

Here’s the basics. Go crazy!

--

--

Madara Premawardhana
Geek Culture

PhD Student at the University of Buckingham, School of Computing