-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] add color_picker.py for ease of picking HSV lower and upper…
… ranges to use with TrackDLO
- Loading branch information
1 parent
149728b
commit 733b79e
Showing
5 changed files
with
79 additions
and
7 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import cv2 | ||
import argparse | ||
import numpy as np | ||
|
||
def nothing(x): | ||
pass | ||
|
||
|
||
parser = argparse.ArgumentParser(description="Pick HSV color threshold") | ||
parser.add_argument("path", help="Indicate /full-or-relative/path/to/image_file.png") | ||
args = parser.parse_args() | ||
img_path = f"{args.path}" | ||
|
||
# Create a window | ||
cv2.namedWindow('image') | ||
|
||
# create trackbars for color change | ||
cv2.createTrackbar('HMin','image',0,179,nothing) # Hue is from 0-179 for Opencv | ||
cv2.createTrackbar('SMin','image',0,255,nothing) | ||
cv2.createTrackbar('VMin','image',0,255,nothing) | ||
cv2.createTrackbar('HMax','image',0,179,nothing) | ||
cv2.createTrackbar('SMax','image',0,255,nothing) | ||
cv2.createTrackbar('VMax','image',0,255,nothing) | ||
|
||
# Set default value for MAX HSV trackbars. | ||
cv2.setTrackbarPos('HMax', 'image', 179) | ||
cv2.setTrackbarPos('SMax', 'image', 255) | ||
cv2.setTrackbarPos('VMax', 'image', 255) | ||
|
||
# Initialize to check if HSV min/max value changes | ||
hMin = sMin = vMin = hMax = sMax = vMax = 0 | ||
phMin = psMin = pvMin = phMax = psMax = pvMax = 0 | ||
|
||
img = cv2.imread(img_path) | ||
img = cv2.resize(img, (640, 480)) | ||
output = img | ||
waitTime = 33 | ||
|
||
while(1): | ||
|
||
# get current positions of all trackbars | ||
hMin = cv2.getTrackbarPos('HMin','image') | ||
sMin = cv2.getTrackbarPos('SMin','image') | ||
vMin = cv2.getTrackbarPos('VMin','image') | ||
|
||
hMax = cv2.getTrackbarPos('HMax','image') | ||
sMax = cv2.getTrackbarPos('SMax','image') | ||
vMax = cv2.getTrackbarPos('VMax','image') | ||
|
||
# Set minimum and max HSV values to display | ||
lower = np.array([hMin, sMin, vMin]) | ||
upper = np.array([hMax, sMax, vMax]) | ||
|
||
# Create HSV Image and threshold into a range. | ||
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) | ||
mask = cv2.inRange(hsv, lower, upper) | ||
output = cv2.bitwise_and(img,img, mask= mask) | ||
|
||
# Print if there is a change in HSV value | ||
if( (phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax) ): | ||
print("(hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d)" % (hMin , sMin , vMin, hMax, sMax , vMax)) | ||
phMin = hMin | ||
psMin = sMin | ||
pvMin = vMin | ||
phMax = hMax | ||
psMax = sMax | ||
pvMax = vMax | ||
|
||
# Display output image | ||
cv2.imshow('image',output) | ||
|
||
# Wait longer to prevent freeze for videos. | ||
if cv2.waitKey(waitTime) & 0xFF == ord('q'): | ||
break | ||
|
||
cv2.destroyAllWindows() |