Invisible Cloak using OpenCV

Have you ever seen Harry Potter’s  Invisible Cloak  If not then let's have a look.

 
 
Was it wonderful? Do you surprised how this can be possible? If yes, then this post is going to help you. You will be able to make such invisible cloak by your own but yes, not in real way but it is all about graphics.

Algorithm behind the idea

  • First, we take the background frame ( for some second ) and store in the memory.
  • Then, we will take the frame with cloth.
  • Then, detect and segment the portion of the cloth of pre-determined color. For Example here we will use Red. [ known as Color Detection and Image Segmentation ]
  • Then, that segment will be replaced by the background, we have stored in the memory, segment.
  • At last, create the final image to make the magic.

Note: All the things will be done in python due to the availability of the various libraries mainly numpy.

Python Code:

  • First of all, importing all the required libraries.
python code
 
  • To make a video of the output screen, we are required the first two lines of code. Then we take the background image( for some second )and filp it and save as image. 
python code
 
  • All the code, from this,  will execute till the output window is opened. Here first we take the frame with the red cloth, flip it and save as image. Then we have to transform the image into HSV  format( another method of image reading like RGB ). And save that transformed image also.
python code

  • Then we create one mask using two mask one with lower value (0 to 10) of Red color in HSV format and the other one with higher value (170 to 180) of Red color in HSV format.
HSV color model

python code

  • The mask may contain some noise. For that, removal of noise using morphological transformation and dilation, to be done on the image. And after that we make mask2 ( purpose of this can be understood later ) using mask1 by using cv2.bitwise_not (). Then we save the mask2 image also.
python code

  • Then we are applying mask1 on the background and mask2 on the image using cv2.biwise_and() and each time we save the images to see what happed in each step. This will continue till we press the 'ESC' key [ Ascii value is 27 ]
python code

  • At the end release the output window and destroy the window.
python code

Output of the python code

python code output image
python code output imagepython code output imagepython code output imagepython code output imagepython code output image
python code output image
python code output imagepython code output image


The video that was recorded

          

Conclusion

From this post we have learnt how to make the invisible cloak  using open-cv and numpy in python.


Comments

Popular posts from this blog

Distance measurement using opencv

Object Detection (Part I)