Skip to content

Commit

Permalink
Add HSV picker script
Browse files Browse the repository at this point in the history
  • Loading branch information
PinkBlueSky authored and e3l committed Aug 6, 2022
1 parent c974a2b commit 18740bd
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions scripts/hsv_picker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'''
Script that allows you to select an image, select a pixel on the image, and calculate the desired HSV low/high range
given a set of tolerances. It also shows you the masked image if that range were to be used.
Modified from https://github.com/alieldinayman/hsv-color-picker/blob/master/HSV%20Color%20Picker.py
'''

import cv2
import numpy as np
import datetime

hue_bound = 180
sat_bound = 255
val_bound = 255

hue_tolerance = 10
sat_tolerance = 10
val_tolerance = 40


# Returns boundaries for a value given +- tolerance, boundaries, and if upper or lower bound
def get_bound(value, tolerance, upper_bound):
lower_bound = 0 # by default. Upper bound is set by caller

upper_value = (value + tolerance) if (value + tolerance < upper_bound) else upper_bound
lower_value = (value - tolerance) if (value - tolerance >= lower_bound) else lower_bound

# Return the final bound (as a tuple)
return lower_value, upper_value


def pick_color(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
pixel = image_hsv[y, x]

# Calculate HSV bounds from pixel
hue = get_bound(pixel[0], hue_tolerance, hue_bound)
sat = get_bound(pixel[1], sat_tolerance, sat_bound)
val = get_bound(pixel[2], val_tolerance, val_bound)

lower = np.array([hue[0], sat[0], val[0]])
upper = np.array([hue[1], sat[1], val[1]])

print(datetime.datetime.now().strftime('%I:%M:%S'), lower, upper)

# Display masked image
image_mask = cv2.inRange(image_hsv, lower, upper)
cv2.imshow("Mask", image_mask)


file_path = '../images_2/image_1.png'
image_src = cv2.imread(file_path)
cv2.imshow("BGR", image_src)

# Convert to HSV
image_hsv = cv2.cvtColor(image_src, cv2.COLOR_BGR2HSV)
cv2.imshow("HSV", image_hsv)

# Triggers color picker when image is clicked
cv2.setMouseCallback("BGR", pick_color)

cv2.waitKey(0)
cv2.destroyAllWindows()

0 comments on commit 18740bd

Please sign in to comment.