How can I check if a specific color is in a photo?

I would like to check if a certain color is on a photo and as a result no image output but information to:
Color found: Yes/No
In which area of the photo: Pixelarea or similar.
Background: It is about being able to automatically assign test strips with 6 different values (color codes are displayed to me).
Is this possible with OpenCV?
Are there any examples (Python or C++)?
Thanks,
Peter


I have used this samplecode, but it only shows the result as a image.

# -*- coding: utf-8 -*-
import numpy as np
import cv2

#https://learnopencv.com/install-opencv-on-windows/
#img_path = "d:\_phyton\opencv_color.jpg"
img_path = "d:\_phyton\Teststreifenklein.jpg"
ranges = {
    #"red": ([0, 0, 128], [60, 60, 255]),  # Blau GrĂĽn Rot
#    "yellow": ([0, 190, 210], [128, 255, 255]),  # B G R
    # many more
		
		# ph Wert
		#"F1": ([92, 217, 238], [92, 217, 238]),    # B G R
		"F1": ([17, 145, 180], [36, 165, 190]),    # B G R
#		"F2": ([90, 197, 235], [90, 197, 235]),  # B G R
#		"F3": ([76, 156, 227], [76, 156, 227]),  # B G R
#		"F4": ([64, 88, 202], [64, 88, 202]),  # B G R
#		"F5": ([66, 76, 207], [66, 76, 207]),  # B G R
#		"F6": ([72, 74, 206], [72, 74, 206]),  # B G R
		
}
 
image = cv2.imread(img_path)
 
# Alternative
#cam = cv2.VideoCapture(0)
#check, image = cam.read()
x = 0

for key in ranges.keys():
    lowerRange = np.array(ranges[key][0], dtype="uint8")
    upperRange = np.array(ranges[key][1], dtype="uint8")
 
 
    mask = cv2.inRange(image, lowerRange, upperRange)
    output = cv2.bitwise_and(image, image, mask=mask)
    #if x == 0:
    #    print('output')
    #    print(mask)
    # show the images
    cv2.imshow(key, output)
    cv2.waitKey(0)
cv2.destroyAllWindows()

please add an example

I added it in my first post

1 Like

python, not phyton.

backslashes in strings have special meaning. your string literals are close to broken. if you had written “Teststreifen” with a lower case first letter, you’d be in trouble now ("\t").

work in a color space where colors are easier to express, like HSV/HSL.

if you want to evaluate the color of each of six patches on one strip, you need to locate the strip and its six patches, then take a region from inside of each patch, and take the average or median of those pixels. that’s a good sample of each patch’s color.

the picture seems usable but remember, colors are relative to both the illumination (daylight/incandescent vs cheap LED lighting) and the settings of the camera (white balance/color temperature). you don’t need to worry if each patch can appear to have one of a few clearly distinguishable colors… or if you have the reference card that comes with the batch of strips (printed on the packaging)

1 Like

Okay. Thank you, but the logical procedure is clear to me (I have been involved in CAD for years) but i do not know the functions of Opencv. Are there any examples for this problem?

not for this problem, of course, because it’s a specific problem.

generally, work with OpenCV: OpenCV modules. there are tons of tutorials of varying quality to pick through. and all the APIs are documented, and grouped into modules.

ok what specifically do you need? you know cv.imread(). you seem aware of numpy’s existence, so you surely know how to “slice” an array and how to call np.mean(). color space conversions can be done with cv.cvtColor().