Ask Your Question
0

How to find the exact colour range in HSV?

asked 2017-07-12 04:23:55 -0600

16pgce005 gravatar image

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])**
mask = cv2.inRange(hsv, lower_blue, upper_blue)
res = cv2.bitwise_and(frame,frame, mask = mask)

cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
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 flag offensive close merge delete

Comments

in the same manner??

gfx

gfx gravatar imagegfx ( 2017-07-12 04:50:19 -0600 )edit

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

16pgce005 gravatar image16pgce005 ( 2017-07-13 05:32:23 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-07-13 04:50:32 -0600

alkasm gravatar image

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:

cspaceFilter in use

edit flag offensive delete link more

Comments

ok. thanks. will try to use your suggestion.

16pgce005 gravatar image16pgce005 ( 2017-07-13 05:33:58 -0600 )edit

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

gfx gravatar imagegfx ( 2017-07-14 12:15:33 -0600 )edit

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*/ 

gfx gravatar imagegfx ( 2017-07-14 12:19:59 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-07-12 04:23:55 -0600

Seen: 4,820 times

Last updated: Jul 13 '17