A Python-based implementation of a Harry Potter style invisibility cloak using OpenCV and computer vision techniques.
- Real-time invisibility effect: Makes bright red objects appear transparent
- Background capture: Automatically captures and stores background for replacement
- Color detection: Uses HSV color space for robust red color detection
- Morphological operations: Cleans up the mask for smoother invisibility effect
- Mirror mode: Flips the video for a more natural user experience
- Camera testing: Built-in camera test functionality
- Python 3.7 or higher
- Webcam/Camera
- Bright red colored cloth or object (to act as the invisibility cloak)
-
Clone or download this project
git clone <repository-url> cd invisibility-cloak
-
Install required packages
pip install -r requirements.txt
Or install manually:
pip install opencv-python numpy
python invisibility_cloak.pyWhen you run the script, you'll see a menu:
- Option 1: Test Camera - Verify your camera is working properly
- Option 2: Start Invisibility Cloak - Begin the main application
- Start the application and choose option 2
- Move out of camera view when prompted (you have 3 seconds)
- Stay out of frame for about 6 seconds while background is captured
- Put on something bright red (shirt, cloth, scarf, etc.)
- Move back into the camera view - you should now be invisible!
- Press 'q' to quit the application
- Background Capture: The script captures 60 frames of the background to allow camera adjustment
- Color Detection: Uses HSV color space to detect bright red colors (handles red's wrap-around in HSV)
- Mask Creation: Creates binary masks to isolate red regions
- Morphological Operations: Cleans and refines the mask using opening and dilation
- Image Segmentation: Separates the frame into cloak and non-cloak regions
- Replacement: Replaces cloak pixels with corresponding background pixels
The script detects red using two HSV ranges:
- Range 1: Hue 0-10°, Saturation 120-255, Value 70-255
- Range 2: Hue 170-180°, Saturation 120-255, Value 70-255
- Use good, consistent lighting
- Avoid shadows and harsh lighting changes
- Natural daylight works best
- Use bright, solid red fabric or clothing
- Avoid patterns or mixed colors
- Larger surface area works better than small objects
- Keep the camera stable (use a tripod if possible)
- Ensure the background is stationary
- Position yourself at an appropriate distance from the camera
- If the effect isn't working, try adjusting the lighting
- Make sure your red object is bright enough
- Ensure minimal background movement
- Check that your camera drivers are up to date
To detect a different color, modify the HSV ranges in the create_invisibility_cloak() function:
# For green color detection
lower_green = np.array([40, 40, 40])
upper_green = np.array([80, 255, 255])
mask = cv2.inRange(hsv, lower_green, upper_green)You can modify the morphological operations:
# Change kernel size for different smoothing effects
kernel = np.ones((5, 5), np.uint8) # Larger kernel = more smoothing
# Adjust iterations for stronger effects
mask = cv2.morphologyEx(mask, cv2.MORPH_DILATE, kernel, iterations=2)The invisibility cloak creates a magical effect where anything bright red becomes transparent, revealing the background behind it. Perfect for creating Harry Potter-style magic effects!
- Works best with solid, bright red colors
- Requires stable lighting conditions
- Background must remain stationary
- Performance depends on camera quality and computer specs
Feel free to fork this project and submit pull requests for improvements!
This project is open source and available under the MIT License.
- Inspired by the Harry Potter invisibility cloak
- Built using OpenCV computer vision library
- Thanks to the open-source community for the amazing tools