|
| 1 | +# Python code for Multiple Color Detection |
| 2 | + |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import cv2 |
| 6 | + |
| 7 | + |
| 8 | +# Capturing video through webcam |
| 9 | +webcam = cv2.VideoCapture(0) |
| 10 | + |
| 11 | +# Start a while loop |
| 12 | +while(1): |
| 13 | + |
| 14 | + # Reading the video from the |
| 15 | + # webcam in image frames |
| 16 | + _, imageFrame = webcam.read() |
| 17 | + |
| 18 | + # Convert the imageFrame in |
| 19 | + # BGR(RGB color space) to |
| 20 | + # HSV(hue-saturation-value) |
| 21 | + # color space |
| 22 | + hsvFrame = cv2.cvtColor(imageFrame, cv2.COLOR_BGR2HSV) |
| 23 | + |
| 24 | + # Set range for red color and |
| 25 | + # define mask |
| 26 | + red_lower = np.array([136, 87, 111], np.uint8) |
| 27 | + red_upper = np.array([180, 255, 255], np.uint8) |
| 28 | + red_mask = cv2.inRange(hsvFrame, red_lower, red_upper) |
| 29 | + |
| 30 | + # Set range for green color and |
| 31 | + # define mask |
| 32 | + green_lower = np.array([25, 52, 72], np.uint8) |
| 33 | + green_upper = np.array([102, 255, 255], np.uint8) |
| 34 | + green_mask = cv2.inRange(hsvFrame, green_lower, green_upper) |
| 35 | + |
| 36 | + # Set range for blue color and |
| 37 | + # define mask |
| 38 | + blue_lower = np.array([94, 80, 2], np.uint8) |
| 39 | + blue_upper = np.array([120, 255, 255], np.uint8) |
| 40 | + blue_mask = cv2.inRange(hsvFrame, blue_lower, blue_upper) |
| 41 | + |
| 42 | + # Morphological Transform, Dilation |
| 43 | + # for each color and bitwise_and operator |
| 44 | + # between imageFrame and mask determines |
| 45 | + # to detect only that particular color |
| 46 | + kernal = np.ones((5, 5), "uint8") |
| 47 | + |
| 48 | + # For red color |
| 49 | + red_mask = cv2.dilate(red_mask, kernal) |
| 50 | + res_red = cv2.bitwise_and(imageFrame, imageFrame, |
| 51 | + mask = red_mask) |
| 52 | + |
| 53 | + # For green color |
| 54 | + green_mask = cv2.dilate(green_mask, kernal) |
| 55 | + res_green = cv2.bitwise_and(imageFrame, imageFrame, |
| 56 | + mask = green_mask) |
| 57 | + |
| 58 | + # For blue color |
| 59 | + blue_mask = cv2.dilate(blue_mask, kernal) |
| 60 | + res_blue = cv2.bitwise_and(imageFrame, imageFrame, |
| 61 | + mask = blue_mask) |
| 62 | + |
| 63 | + # Creating contour to track red color |
| 64 | + contours, hierarchy = cv2.findContours(red_mask, |
| 65 | + cv2.RETR_TREE, |
| 66 | + cv2.CHAIN_APPROX_SIMPLE) |
| 67 | + |
| 68 | + for pic, contour in enumerate(contours): |
| 69 | + area = cv2.contourArea(contour) |
| 70 | + if(area > 300): |
| 71 | + x, y, w, h = cv2.boundingRect(contour) |
| 72 | + imageFrame = cv2.rectangle(imageFrame, (x, y), |
| 73 | + (x + w, y + h), |
| 74 | + (0, 0, 255), 2) |
| 75 | + |
| 76 | + cv2.putText(imageFrame, "Red Colour", (x, y), |
| 77 | + cv2.FONT_HERSHEY_SIMPLEX, 1.0, |
| 78 | + (0, 0, 255)) |
| 79 | + |
| 80 | + # Creating contour to track green color |
| 81 | + contours, hierarchy = cv2.findContours(green_mask, |
| 82 | + cv2.RETR_TREE, |
| 83 | + cv2.CHAIN_APPROX_SIMPLE) |
| 84 | + |
| 85 | + for pic, contour in enumerate(contours): |
| 86 | + area = cv2.contourArea(contour) |
| 87 | + if(area > 300): |
| 88 | + x, y, w, h = cv2.boundingRect(contour) |
| 89 | + imageFrame = cv2.rectangle(imageFrame, (x, y), |
| 90 | + (x + w, y + h), |
| 91 | + (0, 255, 0), 2) |
| 92 | + |
| 93 | + cv2.putText(imageFrame, "Green Colour", (x, y), |
| 94 | + cv2.FONT_HERSHEY_SIMPLEX, |
| 95 | + 1.0, (0, 255, 0)) |
| 96 | + |
| 97 | + # Creating contour to track blue color |
| 98 | + contours, hierarchy = cv2.findContours(blue_mask, |
| 99 | + cv2.RETR_TREE, |
| 100 | + cv2.CHAIN_APPROX_SIMPLE) |
| 101 | + for pic, contour in enumerate(contours): |
| 102 | + area = cv2.contourArea(contour) |
| 103 | + if(area > 300): |
| 104 | + x, y, w, h = cv2.boundingRect(contour) |
| 105 | + imageFrame = cv2.rectangle(imageFrame, (x, y), |
| 106 | + (x + w, y + h), |
| 107 | + (255, 0, 0), 2) |
| 108 | + |
| 109 | + cv2.putText(imageFrame, "Blue Colour", (x, y), |
| 110 | + cv2.FONT_HERSHEY_SIMPLEX, |
| 111 | + 1.0, (255, 0, 0)) |
| 112 | + |
| 113 | + # Program Termination |
| 114 | + cv2.imshow("Multiple Color Detection in Real-TIme", imageFrame) |
| 115 | + if cv2.waitKey(10) & 0xFF == ord('q'): |
| 116 | + cap.release() |
| 117 | + cv2.destroyAllWindows() |
| 118 | + break |
0 commit comments