├── LICENSE.md ├── Readme.md ├── main.py ├── requirements.txt └── util.py /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 computervisiondeveloper 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # color-detection-opencv 2 | 3 | Color detection with Python and OpenCV ! 4 | 5 | [![Watch the video](https://img.youtube.com/vi/aFNDh5k3SjU/0.jpg)](https://www.youtube.com/watch?v=aFNDh5k3SjU) 6 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | from PIL import Image 3 | 4 | from util import get_limits 5 | 6 | 7 | yellow = [0, 255, 255] # yellow in BGR colorspace 8 | cap = cv2.VideoCapture(2) 9 | while True: 10 | ret, frame = cap.read() 11 | 12 | hsvImage = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 13 | 14 | lowerLimit, upperLimit = get_limits(color=yellow) 15 | 16 | mask = cv2.inRange(hsvImage, lowerLimit, upperLimit) 17 | 18 | mask_ = Image.fromarray(mask) 19 | 20 | bbox = mask_.getbbox() 21 | 22 | if bbox is not None: 23 | x1, y1, x2, y2 = bbox 24 | 25 | frame = cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 5) 26 | 27 | cv2.imshow('frame', frame) 28 | 29 | if cv2.waitKey(1) & 0xFF == ord('q'): 30 | break 31 | 32 | cap.release() 33 | 34 | cv2.destroyAllWindows() 35 | 36 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | opencv-python==4.6.0.66 2 | numpy==1.23.4 3 | Pillow==9.2.0 4 | -------------------------------------------------------------------------------- /util.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | 4 | 5 | def get_limits(color): 6 | c = np.uint8([[color]]) # BGR values 7 | hsvC = cv2.cvtColor(c, cv2.COLOR_BGR2HSV) 8 | 9 | hue = hsvC[0][0][0] # Get the hue value 10 | 11 | # Handle red hue wrap-around 12 | if hue >= 165: # Upper limit for divided red hue 13 | lowerLimit = np.array([hue - 10, 100, 100], dtype=np.uint8) 14 | upperLimit = np.array([180, 255, 255], dtype=np.uint8) 15 | elif hue <= 15: # Lower limit for divided red hue 16 | lowerLimit = np.array([0, 100, 100], dtype=np.uint8) 17 | upperLimit = np.array([hue + 10, 255, 255], dtype=np.uint8) 18 | else: 19 | lowerLimit = np.array([hue - 10, 100, 100], dtype=np.uint8) 20 | upperLimit = np.array([hue + 10, 255, 255], dtype=np.uint8) 21 | 22 | return lowerLimit, upperLimit 23 | --------------------------------------------------------------------------------