Search code examples
opencvcolorfilter

How to apply two color thresholds to image OpenCV


I am currently trying to detect for two certain colors in a certain image. I am trying to filter an image to display pixels in a certain range. I know that to find one color, you input an upper and lower bound like so

COLOR_MIN = np.array([0, 0, 130], np.uint8)
COLOR_MAX = np.array([90, 145,255], np.uint8)

dst1 = cv2.inRange(img, COLOR_MIN, COLOR_MAX)

And I simply apply dst1 to the image and everything works just as it should. An image is displayed with only pixels in those ranges. However, I would like to search for two specific ranges of colors. Should I apply the two color ranges to the image separately to get two different images, and then blend the image together? Or is there a more efficient way of displaying an image whose pixels fit in two different color ranges?


Solution

  • Aha! Found it. You can make a similar filter for your second color and then simply use the bitwise or operator | combining the two filters dst1 and dst2.