0

I am using numpy and cv2 to find the average color of an image, and am able to successfully get a result with the code. How can I set a threshold for this result to execute some code if the average color is a certain result?

Source Code:

import cv2
import numpy
myimg = cv2.imread('image.jpg')
avg_color_per_row = numpy.average(myimg, axis=0)
avg_color = numpy.average(avg_color_per_row, axis=0)
print(avg_color)

Result:

[ 197.53434769  217.88439451  209.63799938]

I would like to end up with something like this:

if B > number and G > number and R > number:
    doSomething()

I referenced this question to get the above code: How to find the average colour of an image in Python with OpenCV?

Bran
  • 19
  • 6
  • Try referring this link : https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html – Suraj Mar 28 '20 at 02:16
  • You make it too hard. Just use cv2.mean(myimg). For conditionals, see https://realpython.com/python-conditional-statements/, for example, or https://www.w3schools.com/python/python_conditions.asp. Please try to do some searching on Google before asking questions here or read the Python/OpenCV documentation. – fmw42 Mar 28 '20 at 02:34
  • @fmw42 I didn't need help with conditional statements as a concept, I just don't know how to make a conditional statement with the result that I am given from print(avg_color). I've looked around on Google and haven't been able to find an example of someone doing this. – Bran Mar 28 '20 at 03:48
  • You have an array for the average color, which I assume gives you the values for each channel. So just use that resulting average and select an element of the array for each channel and some value for the threshold for each channel as a testl. So replace your R, G, B with avg_color[0], avg_color[1] and avg_color[2]. What do I not understand about your question? – fmw42 Mar 28 '20 at 04:16
  • @fmw42 I see, that is actually exactly what I didn't understand. Thank you for this explanation. This works perfectly for what I needed! – Bran Mar 28 '20 at 05:26

0 Answers0