This repository was archived by the owner on Jul 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfindcolor.py
More file actions
43 lines (32 loc) · 1.4 KB
/
findcolor.py
File metadata and controls
43 lines (32 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
### All color codes are in hsv format ###
""" Run using 'python3 findcolor.py --image *name_of_test_img*'"""
import cv2
import numpy as np
import argparse
# Read image through script line
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="Path to image")
args = vars(ap.parse_args())
image = cv2.imread(args["image"])
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) #Converts RGB to HSV
# Manually load in image
# image = cv2.imread('testim.png')
#------------------------------------------------------------------------#
# boundaries = [([100, 50, 0], [255, 180, 104])] #RGB Space
#HSV Space
# boundaries = [([163, 56, 74], [179, 255, 255])] #Red
# boundaries = [([11, 153, 172], [24, 255, 255])] #Orange
boundaries = [([25, 91, 234], [31, 255, 255])] #Yellow
#------------------------------------------------------------------------#
for(lower, upper) in boundaries:
# create NumPy arrays from the boundaries
lower = np.array(lower, dtype = "uint8")
upper = np.array(upper, dtype = "uint8")
#find colors in boundaries and apply mask
colormask = cv2.inRange(image, lower, upper) # creates a filter given range of colors
output = cv2.bitwise_and(image, image, mask=colormask) # applies color filter
# show the images
cv2.imwrite("coloredimg.jpg", output)
cv2.imshow("images", np.hstack([image,output]))
cv2.waitKey(0)
cv2.destroyAllWindows()