Python OpenCV Invisible Cloak – The Art of Disappearing

Have you ever wondered if you could be invisible like Harry Potter or a superhero? While it may seem like a concept limited to the world of fiction, it is now possible to create an invisible cloak using OpenCV.

OpenCV is a library of programming functions used for real-time computer vision, and it has many practical applications in robotics, augmented reality, and face recognition. This technology has opened up the possibility of creating an invisible cloak that can make objects disappear in real-time.

At TechVidan, we will explore the technology behind invisible cloaks and provide a step-by-step guide to creating one using OpenCV.

What is Invisible Cloak?

An invisible cloak is a technology that can make an object or person invisible. It uses different methods like optical camouflage, cloaking devices, or computer vision to achieve this effect. While it was once only seen in movies and books, recent advancements in technology have made it possible to create an invisible cloak using OpenCV, a programming library for real-time computer vision.

What are the technologies behind Invisible Cloak?

The technology behind invisible cloaks involves different methods, such as optical camouflage, cloaking devices, and computer vision. Optical camouflage uses cameras, projectors, and special fabrics to blend an object into the background. Cloaking devices use unique materials to bend light around an object and make it invisible. Computer vision uses cameras and algorithms to manipulate an object’s appearance in real-time.

OpenCV is a programming library for computer vision, which uses techniques like background subtraction, image segmentation, and chroma keying to remove an object from the image and replace it with its surroundings. Understanding this technology is essential for creating effective and realistic invisible cloaks for various applications.

How OpenCV works behind the Invisible Cloak?

To create an invisible cloak using OpenCV, you first need to capture the background image without the object. Then, using a camera, you capture a live video of the scene with the object. OpenCV uses background subtraction to remove the background and create a mask that shows only the object. Using image segmentation, it detects the object’s edges and contours and then uses chroma keying to replace the object’s pixels with the background pixels, effectively making it invisible. This happens in real time, creating a continuous effect. However, the effectiveness of the cloak depends on factors like lighting, object color, and camera quality.

Prerequisites for Invisible Cloak Using Python OpenCV

It is important to have a solid understanding of the Python programming language and the OpenCV library. Apart from this, you should have the following system requirements.

1. Python 3.7 and above
2. Any Python editor (VS code, Pycharm)
3. Plane Red color cloak

Download Python OpenCV Invisible Cloak Project

Please download the source code of the OpenCV Invisible Cloak Project from the following link: Python OpenCV Invisible Cloak Project Code.

Installation

Open Windows cmd as administrator

1. To install the opencv library, run the command from the cmd.

pip install opencv-python

Let’s Implement it

To implement it, follow the below step.

1. First of all, we are importing all the necessary libraries that are required during implementation.

import cv2 
import numpy as np

2. It creates a video writer object named ‘out’ using the OpenCV function ‘VideoWriter_fourcc()’. The ‘XVID’ codec is used for encoding the video, and the output video will have a frame rate of 20 frames per second and a frame size of 300 pixels width and 300 pixels height. The output video will be saved as ‘output.avi’.

video = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', video, 20, (300, 300))

3. This will open the laptop’s camera and store the camera input in the camera variable.

cap = cv2.VideoCapture(0)

4. Initializing two variables named ‘counter’ and ‘bg’ to zero.

counter = 0
bg = 0

5. This part of the code reads frames from the default camera and stores the last captured frame as the background image. It uses the cap.read() function that returns a boolean value indicating whether the operation was successful and the captured frame. The values of ‘ret’ and ‘bg’ are assigned to these two values respectively. The ret variable is later used in the while loop to check if the video capture is still running.

for i in range(60): 
    ret, bg = cap.read() 

6. This code is flipping the array ‘bg’ horizontally, which means that the columns are being reversed. The original contents of each row remain in the same order, but they are now in reverse order from left to right.

bg = np.flip(bg, axis=1) 

7. It creates a while loop that will continue running as long as the video capture object cap is able to successfully open the video file or camera stream.

while(cap.isOpened()): 

8. Checks whether the ret value is False. If ret is False, it means that a frame was not successfully read from the video capture object. In this case, the break statement is executed, which terminates the while loop and exits the program.

ret, img = cap.read() 
    if not(ret):
        break

9. The counter variable is incremented by one each time a frame is successfully read from the video capture object.

counter += 1

10. The flipped image is stored back into the IMG variable.

img = np.flip(img, axis=1)

11. Converting BGR image to HSV.

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

12. The code detects red in an image by setting a range of colors for hue, saturation, and brightness. It then creates a mask that highlights the areas in the image where the red color falls within the specified range.

lower_red = np.array([0, 125, 50]) 
    upper_red = np.array([20, 255, 255]) 
    mask1 = cv2.inRange(hsv, lower_red, upper_red) 

13. The code identifies and highlights the areas in an image that contain a specific range of red colors with hue values between 170 to 180 and a saturation value between 120 to 255. It does this by creating a binary mask where the pixels within the specified range are white, and the pixels outside the range are black.

lower_red = np.array([170, 120, 50])
    upper_red = np.array([180, 255, 255])
    mask2 = cv2.inRange(hsv, lower_red, upper_red)

14. Add the binary mask mask1 and mask2 together by performing an element-wise addition operation.

mask1 += mask2 

15. The code performs image processing operations to improve the binary mask created earlier. The first operation called a morphological opening, is used to remove small objects and noise from the mask. The second operation inverts the mask to create a new mask with white and black pixels swapped.

mask1 = cv2.morphologyEx(mask1, cv2.MORPH_OPEN, np.ones((3,3), np.uint8))
dilation_kernel = np.ones((10, 10), np.uint8)
dilation = cv2.dilate(mask1, dilation_kernel, iterations= 1)
mask2 = cv2.bitwise_not(mask1)

16. The code creates two new images by applying a bitwise AND operation. The first image contains only the pixels from the original image that are not detected as red, and the second image contains only the pixels from the background that corresponds to the area where the red color was detected in the original image.

result1 = cv2.bitwise_and(img, img, mask=mask2)
result2 = cv2.bitwise_and(bg, bg, mask=mask1)

17. This code blends two images using equal weighting to create a new image called Output. Then, it writes the resulting image into a video file.

Output = cv2.addWeighted(result1, 1, result2, 1, 0)
out.write(Output)

18. Whatever Output is there that will be shown on the output window with the name TechVidvan. If the user presses ‘q’, then the whole program will stop.

cv2.imshow('TechVidvan', Output)
    cv2.waitKey(1)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

Note:- From steps 8 to 18, you have to include this under the while loop.

19. After pressing ‘q’, these lines of code ensure that all resources used by the video capture, processing and display operations are properly released and closed, avoiding potential memory leaks or other issues that could arise from leaving those resources open.

cap.release()
out.release()
cv2.destroyAllWindows()

Python OpenCV Invisible Cloak Output

opencv invisible cloak

Conclusion

An invisible cloak created using OpenCV is a really cool example of what can be done with computer vision and image processing. By using techniques like color detection and background subtraction, you can make someone appear invisible by covering their body with the background image.

OpenCV has a lot of helpful tools to make this happen, but it can be affected by things like lighting and camera angle. This project is both exciting and difficult and shows how powerful computer vision can be. If we continue to make advancements, there could be even more amazing possibilities, like using it for augmented reality or special effects in movies.

TechVidvan Team

The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today’s tech industry.