# How to find the exact colour range in HSV?

import cv2 import numpy as np

cap = cv2.VideoCapture(0)

while(1):

_, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
**lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])**

cv2.imshow('frame',frame)
cv2.imshow('res',res)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break


cv2.destroyAllWindows()

In the example given above, for blue colour, exact upper bound and lower bound has been defined. How to calculate it for other colours in the HSV?

edit retag close merge delete

@gfx in any manner. i'm interested in understanding and the desired outcome.

Sort by » oldest newest most voted

I recently answered a question that dove in deep to inRange() on Stack Overflow. It goes into what the HSV colorspace is, how to think about the values, and how to create your own inRange() function.

For finding your own values to work on an image, it's probably best practice to create a program which shows trackbars for the min and max values of each color channel and adjust them yourself till you get the hang of it. Or just pop an image in the program whenever you need to. I created a python program that does this; you can grab it from GitHub if you want.

This is what it looks like in use: more

mine it is a question but an answer in the same time .... here the code:

cvtColor(dest_image, imgHSV, CV_RGB2HSV); /dest_image = input/
inRange(imgHSV, Scalar(a,b,c), Scalar(a1,b1,c1), imgThresh); /* a,b,c, = rgb color 0 to 255, a1,b1,c1 is light par 0 to 255/
imgThresh.copyTo(desTresh4);
if( v1 == 0 ){ erosion_type = MORPH_RECT; }
else if( v1 == 1 ){ erosion_type = MORPH_CROSS; }
else if( v1 == 2) { erosion_type = MORPH_ELLIPSE; }
else{ erosion_type = MORPH_RECT; }
elementDilation = getStructuringElement( erosion_type, Size( 2n + 1, 2n+1 ), Point( n, n ) );
elementErosion = getStructuringElement( erosion_type, Size( 2h + 1, 2h+1 ), Point( h, h ) );
dilate( desTresh4, desTresh3, elementDilation ); / pus ... erode and dilate func/
erode( desTresh3, desTresh2, elementErosion );  /desTresh2 = output n & h only 1-3-5-7-9-11 value*/ 

Official site

GitHub

Wiki

Documentation