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.
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.
- 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.
- 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.
- 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.
- 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.
- 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 ]
Comments
Post a Comment