Ask Your Question
0

Select HSV Hue from -30 to 30 in Python?

asked 2018-02-15 00:38:44 -0600

masterenol gravatar image

updated 2018-02-15 00:43:47 -0600

image description The color wheel shown has a Hue Range of 0 to 179 I believe. I wish to black out the colors with hue range of -30 to 30 as shown in the right color wheel. Unfortunately, I tried the following number sets for 'lower_hsv' and 'upper_hsv', but they do not work:

20,-20: "error: list index out of range"

-20,20: only blacks out range 0 to 20.

20,160: blacks out everything except what I wanted blacked out.

160,20: "error: list index out of range"

Can someone please tell me how I can get the image to display as shown on the right color wheel? I will need to black out several different colors on a different image.

import cv2
import numpy as np


image = cv2.imread("hsvColorCircle.png")
imageHSV = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
image_lower_hsv = np.array([0, 200, 220])
image_upper_hsv = np.array([20, 255, 255])
imageMask = cv2.inRange(imageHSV, image_lower_hsv, image_upper_hsv)
_2, imageMaskContours, imageMaskHierarchy = cv2.findContours(imageMask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
imageCNT = imageMaskContours[0]
imageIndex = -1
imageThickness = 4
imageColor = (0, 0, 0)
cv2.drawContours(image,imageMaskContours,imageIndex,imageColor,-1)
imageResize = cv2.resize(image,(640,480))
cv2.imshow('image', imageResize)

cv2.waitKey(0)
cv2.destroyAllWindows

image description

C:\fakepath\hsvColorCircle.png

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2018-02-15 01:42:18 -0600

berak gravatar image

unfortunately, you cannot do this directly, but have to create 2 seperate masks, [150,180] and [0,30] , then combine them, like:

# -30 to 0
image_lower_hsv = np.array([150, 200, 220])
image_upper_hsv = np.array([180, 255, 255])
imageMask1 = cv2.inRange(imageHSV, image_lower_hsv, image_upper_hsv)
# 0 to 30
image_lower_hsv = np.array([0, 200, 220])
image_upper_hsv = np.array([30, 255, 255])
imageMask2 = cv2.inRange(imageHSV, image_lower_hsv, image_upper_hsv)
# combine masks
finalMask = cv2.bitwise_or(imageMask1, imageMask2)
edit flag offensive delete link more

Comments

Cool, thanks!

masterenol gravatar imagemasterenol ( 2018-02-15 03:04:22 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-02-15 00:38:44 -0600

Seen: 4,990 times

Last updated: Feb 15 '18