Invisible Cloak Using OpenCv

Harry potter Invisible cloak

In childhood almost every one amazed with this harry potter cloak because it makes him invisible.What if you also can do it? Well,Today we make this magical cloak using python.

Here I am using red cloth and u can use any color cloth.The only thing varies is the HSV values of a particular color.

For more information about HSV(Hue,Saturation,value),you can visit: https://www.lifewire.com/what-is-hsv-in-design-1078068

Actually this works on Algorithm called Color Detection and segmentation

  1. Extracting the static background frame

As the background is static we can do with a single capture right ? Well yes but the image captured is a bit dark compared to when multiple frames are captured. Hence capturing multiple images of static background is good.We use a while loop to capture multiple images.

2. Extracting red color image

we determine the region covered by the red cloth using the hsv values of the red color cloth. First convert the image to HSV using OpenCv. For my cloth the typical ranges are as follows

Actually we do the above process for every frame in the video so we run while loop and extract the portion of Red cloth.

3. We make the complete mask of the red color

mask = mask1+mask2

We have to Eliminate the small regions of red color in order to avoid false detection and random glitches in the output.The below line of code will do that

mask=cv2.morphologyEx(mask,cv2.MORPH_OPEN,np.ones((3,3),np.uint8))

4. Replacing Red portion with the background image

Using bit wise operations we can replace the mask with the static background captured earlier.

img[np.where(mask==255)] = background[np.where(mask==255)]

Finally display the image and experience the magical effect.

out.write(finalOutput)
cv2.imshow("magic",finalOutput)
key = cv2.waitKey(1)
if key == 27: #press Esc to exit
break

Below is the full code

After you run the code wait for few seconds so that the webcam captures the back ground image of your room.Then came in front of the camera and become invisible!!..

To generate HSV values of any color

Now we try to extract yellow color from a car image.We create track-bars of H,S,V values and we adjust them to get exact portion of the yellow color. Below is the code to create track-bars in Opencv.

First we take the possible range of the H,S,V values later we adjust to get the required mask.

Initial values of hsv
Before Adjusting HSV values
Final HSV values
After Adjusting the HSV values

In this way we can Extract mask of any color. Here the HSV values are HSV(min) = [0,136,168] and HSV(max) = [39,255,255].

Hope you all enjoyed this project.If you have any doubts regarding this feel free to ask me at sayedshoyab1333@gmail.com . Thank you!!..

Data Science Enthusiastic and Python Lover .