├── README.md ├── invs_cloak.py └── testhsv.py /README.md: -------------------------------------------------------------------------------- 1 | # InvisibilityCloak 2 | An OpenCV project for creating an Invisibility Cloak 3 | 4 | Detects red cloth and creates a mask. Replace the mask with camera feed using bitwise AND operations 5 | 6 | HSV range can be calculated by 7 | 1. trial and error 8 | 2. using the testhsv.py file and using trackbar values to perfectly choose a range 9 | -------------------------------------------------------------------------------- /invs_cloak.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | cap = cv2.VideoCapture(0) 5 | 6 | #The first live feed is to capture the background without the subject 7 | while True: 8 | ret, frame = cap.read() 9 | f = cv2.flip(frame, 1) 10 | cv2.imshow('result', f) 11 | if cv2.waitKey(1) & 0xFF == ord('p'): 12 | break 13 | 14 | cap.release() 15 | cv2.destroyAllWindows() 16 | 17 | cap = cv2.VideoCapture(0) 18 | 19 | #The second live feed is for the actual result. Subject can now enter the feed 20 | while True: 21 | ret, frame = cap.read() 22 | frame = cv2.flip(frame, 1) 23 | 24 | kernel = np.ones((2,2), np.uint8) #create a kernel for applying morphology 25 | hsv_frame = cv2.cvtColor(frame, cv2.COLOR_RGB2HSV) 26 | 27 | #created an HSV range by trial and error 28 | lhsv = np.array([115, 100, 150]) 29 | uhsv = np.array([255, 255, 255]) 30 | 31 | #create mask based on hsv range 32 | mask = cv2.inRange(hsv_frame, lhsv, uhsv) 33 | 34 | #apply closing (morphologyex) on the mask to remove the grains in the mask 35 | clos_mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) 36 | 37 | mask_inv = cv2.bitwise_not(clos_mask) 38 | 39 | #create two results - bitwise and on the normal mask to extract the first feed around the cloth, result1 for bitwise and on the inverted mask to extract the current camera feed 40 | result = cv2.bitwise_and(f, f, mask=clos_mask) 41 | result1 = cv2.bitwise_and(frame, frame, mask=mask_inv) 42 | 43 | #add both results on top of each other 44 | r = cv2.add(result, result1) 45 | 46 | #uncomment the next two lines to see the mask and original feed 47 | #cv2.imshow('frame', frame) 48 | #cv2.imshow('mask_inv', closing) 49 | 50 | cv2.imshow('result', r) 51 | 52 | if cv2.waitKey(1) & 0xFF == ord('q'): 53 | break 54 | 55 | 56 | 57 | cap.release() 58 | cv2.destroyAllWindows() -------------------------------------------------------------------------------- /testhsv.py: -------------------------------------------------------------------------------- 1 | # USAGE: You need to specify a filter and "only one" image source 2 | # python ColorPicker.py --filter RGB --image /path/image.png 3 | # or 4 | # python ColorPicker.py --filter HSV --webcam 5 | 6 | import cv2 7 | import argparse 8 | from operator import xor 9 | 10 | def callback(value): 11 | pass 12 | 13 | def setup_trackbars(range_filter): 14 | cv2.namedWindow("Trackbars", 0) 15 | 16 | for i in ["MIN", "MAX"]: 17 | v = 0 if i == "MIN" else 255 18 | for j in range_filter: 19 | cv2.createTrackbar("%s_%s" % (j, i), "Trackbars", v, 255, callback) 20 | 21 | def get_arguments(): 22 | ap = argparse.ArgumentParser() 23 | ap.add_argument('-f', '--filter', required=True, 24 | help='Range filter. RGB or HSV') 25 | ap.add_argument('-i', '--image', required=False, 26 | help='Path to the image') 27 | ap.add_argument('-w', '--webcam', required=False, 28 | help='Use webcam', action='store_true') 29 | ap.add_argument('-p', '--preview', required=False, 30 | help='Show a preview of the image after applying the mask', 31 | action='store_true') 32 | args = vars(ap.parse_args()) 33 | 34 | if not xor(bool(args['image']), bool(args['webcam'])): 35 | ap.error("Please specify only one image source") 36 | 37 | if not args['filter'].upper() in ['RGB', 'HSV']: 38 | ap.error("Please speciy a correct filter.") 39 | 40 | return args 41 | 42 | def get_trackbar_values(range_filter): 43 | values = [] 44 | for i in ["MIN", "MAX"]: 45 | for j in range_filter: 46 | v = cv2.getTrackbarPos("%s_%s" % (j, i), "Trackbars") 47 | values.append(v) 48 | 49 | return values 50 | 51 | def main(): 52 | args = get_arguments() 53 | range_filter = args['filter'].upper() 54 | 55 | if args['image']: 56 | image = cv2.imread(args['image']) 57 | 58 | if range_filter == 'RGB': 59 | frame_to_thresh = image.copy() 60 | else: 61 | frame_to_thresh = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) 62 | else: 63 | camera = cv2.VideoCapture(0) 64 | 65 | setup_trackbars(range_filter) 66 | 67 | while True: 68 | if args['webcam']: 69 | ret, image = camera.read() 70 | 71 | if not ret: 72 | break 73 | 74 | if range_filter == 'RGB': 75 | frame_to_thresh = image.copy() 76 | else: 77 | frame_to_thresh = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) 78 | 79 | v1_min, v2_min, v3_min, v1_max, v2_max, v3_max = get_trackbar_values(range_filter) 80 | thresh = cv2.inRange(frame_to_thresh, (v1_min, v2_min, v3_min), (v1_max, v2_max, v3_max)) 81 | 82 | if args['preview']: 83 | preview = cv2.bitwise_and(image, image, mask=thresh) 84 | cv2.imshow("Preview", preview) 85 | else: 86 | image = cv2.flip(image, 1) 87 | cv2.imshow("Original", image) 88 | 89 | thresh = cv2.flip(thresh, 1) 90 | cv2.imshow("Thresh", thresh) 91 | 92 | if cv2.waitKey(1) & 0xFF is ord('q'): 93 | break 94 | 95 | # USAGE: You need to specify a filter and "only one" image source 96 | # python ColorPicker.py --filter RGB --image /path/image.png 97 | # or 98 | # python ColorPicker.py --filter HSV --webcam 99 | if __name__ == '__main__': 100 | main() 101 | --------------------------------------------------------------------------------