Invisible Cloak using python and Opencv
Hi, everyone. We’re trying to implement some magic today by creating code in our code editor. So you’ve heard about Harry Potter’s Invisible Cloak and want to get your hands on one so you can go wherever you want and experience what it’s like to be invisible.
Coder === Magician
About Invisible Cloak Project
We will create the invisible cloak using an image processing technique called Color detection and segmentation. In order to make this project, you’ll need a single-color cloth.
Note:
- Cloth color should be unique relative to the background. (i.e. If the cloth is green then the background shouldn’t contain any green color)
- In good lighting conditions, it works best.
- Try to choose red, green or blue cloth, because these three colors are easier to detect.
we will use a red cloth for this project for Color detection is a technique where we can detect any color in a given range of HSV color space.
Lets try in the code
To install opencv, run the below command
!Pip install opencv-python
Step 1 — Import necessary packages and Initialize the camera
import cv2import numpy as npimport time
Step 2 — Find the available camera
#check for available cameraall_camera_idx_available = []for camera_idx in range(10):cap = cv2.VideoCapture(camera_idx,cv2.CAP_DSHOW)if cap.isOpened():print(f'Camera index available: {camera_idx}')all_camera_idx_available.append(camera_idx)cap.release()else:print("not available")
Step 3 - capture the background
# now capture the video check for avialble camera devices as mentioned abovevideo = cv2.VideoCapture(0)print(video.isOpened())time.sleep(3) #timer to on the camfor i in range(60): #loopig to get a good image rel,background = video.read() #read the background if not rel: raise RuntimeError("Couldn't capture an image")background = np.flip(background, axis=1) #filp the captured img
Step 4— Detect the cloth:
while(video.isOpened()): # untill the video is oprenrel,img=video.read()if rel==False:break #checking weather its workingimg=np.flip(img,axis=1)hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) #convert to hsv detect colours betterlower_red = np.array([0,120,50])upper_red = np.array([10,255,255])mask1 = cv2.inRange(hsv, lower_red, upper_red)lower_red = np.array([170,120,70]) #range for red we wantupper_red = np.array([180,255,255])mask2 = cv2.inRange(hsv, lower_red, upper_red)mask1 = mask1+mask2 #detect all ranges of redmask1 = cv2.morphologyEx(mask1, cv2.MORPH_OPEN, np.ones((3,3), np.uint8),iterations=2) #removing the noice from datamask1 = cv2.morphologyEx(mask1, cv2.MORPH_DILATE, np.ones((3,3), np.uint8))mask2 = cv2.bitwise_not(mask1)res1 = cv2.bitwise_and(img,img, mask=mask2) #person who holds the cloakres2 = cv2.bitwise_and(background,background, mask=mask1)#to replace the background image in place of the colour red combining resultsfinal = cv2.addWeighted(res1, 1, res2, 1, 0)cv2.imshow("final",final)key = cv2.waitKey(1)#to close the windowif key== ord('c'):breakvideo.release()cv2.destroyAllWindows()
why we used hsv?
HSV stands for HUE, SATURATION, and VALUE (or brightness). It is a cylindrical color space
applying mask :
res1 = cv2.bitwise_and(img,img, mask=mask2) #person who holds the cloakres2 = cv2.bitwise_and(background,background, mask=mask1)
combining the mask :
#to replace the background image in place of the colour red combining resultsfinal = cv2.addWeighted(res1, 1, res2, 1, 0)
We implemented color detection and segmentation technique. In this opencv project, we learned about morphological operations, masking, and other image processing
Done
Hope the tutorial was helpful. If there is anything we missed out, do let us know through comments.😇
❤️❤️❤️❤️❤️❤️❤️Thanks for reading❤️❤️❤️❤️❤️❤️❤️❤️