1

I am having an issue with color detection in OpenCV 3.4. I will present a picture of my problem below.

import numpy as np
import cv2 


img= cv2.imread("C:\\Users\Stefan_Cepa\\Desktop\\dataset2\\set\\A6.png")

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

lower_range = np.array([30,150,150])
upper_range = np.array([255,255,180])

mask = cv2.inRange(hsv, lower_range, upper_range)
output = cv2.bitwise_and(img, img, mask = mask)
cv2.imshow("images", np.hstack([img, output]))
cv2.imshow('mask', mask)

while(True):
    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()

As you can see I've set my lower and upper bounds to detect Red color in an image, but for some reason, as you can see in image below, I am not getting any results. Any tips & tricks would be extremely helpful! Thank you in advance! enter image description here

2
  • Why the hue in 30 (that is 60 in the hue wheel) will give red? it looks more like yellowish... red will be more like 0 -15 and since it is at the beginning and end of the wheel, you should also get like 170-180. Commented Mar 3, 2018 at 19:38
  • Mixing hsv and bgr values @api55 Commented Mar 3, 2018 at 19:39

1 Answer 1

2

You are using the hsv colorspace but you are providing ranges of bgr values. they are incompatible.

For hsv:

For HSV, Hue range is [0,179], Saturation range is [0,255] and Value range is [0,255]. Different softwares use different scales. So if you are comparing OpenCV values with them, you need to normalize these ranges. source: docs.opencv.org/3.2.0

Your code almost looks like this: http://pyimagesearch.com/2014/08/04/opencv-python-color-detection. Only they use bgr.

Solution:

Convert your mask-ranges to hsv or load images as bgr.

Red on hsv is on hue 0 so you probably would need a combined mask of 170-180 hue and 0-10 hue.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much! I've fixed it! I will mark this as accepted! But i have one more question. It is really hard to discriminate the color of the object and yellow color on the object. Any suggestions?
You could sample the color of the transistors body and create a very tight mask for it - if you have consistent lightning that "might" work. Else maybe you should try to get out geometrical featurs (edge detection) and try to isolate those small bands from bigger blobs - not sure, depends strongly on your image quality over time Not sure though, you have lots of reflection as well. If you can get clean edges with (Canny? ) you could invert the found edges and multiply them so you get thin black spacers around the edges on the image ... between rings and body
I think i understand but not sure how can i then detect color after using Canny ? Sorry if question is stupid i am new to OpenCV and computer vision in general . Thing is, when i detect color (e.g Red ) i need to know in program i've detected Red color on image. Because there is an equation dependent of color of bands on the object ( object is called resistior Resistor ) . Also, i need to know the ordering of detected colors ( e.g yellow is first band red is 2nd etc) . Any suggestions? And thank you a lot for your help by the way.
My cv times are past. Canny gives you a b/w picture - lots of black, few white lines. threshold it so all near to black values are 0 and all lighter values are 255. subtract this from your image and normalize your colorvalues if they drop below 0 ( or create a 0/1 mask and multiply it with your images bgr) - you should get 0 where the canny is white and your former colors where canny is black.
Or you inspekt this armageddon421.de/?p=279 and find that github.com/armageddon421/ResCan or this sites.google.com/site/ohmcalcucsb and take it from there...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.