2

As you can see in the below, the blue color is detected in the image and the new image is popped up. Instead of that i want to just know if the color is detected instead of showing the image. something like this,

if blueColor in image:
    print "blue color detected"

original code from documentation.

import cv2
import numpy as np

cap = cv2.VideoCapture(0)
while(1):

    # Take each frame
    _, frame = cap.read()

    # Convert BGR to HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # define range of blue color in HSV
    lower_blue = np.array([110,50,50])
    upper_blue = np.array([130,255,255])

    # Threshold the HSV image to get only blue colors
    mask = cv2.inRange(hsv, lower_blue, upper_blue)

    # Bitwise-AND mask and original image
    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()

Atul
  • 546
  • 4
  • 16
  • Any color image probably has some blue in it. What do you mean by "if the color is detected"? – tom10 Feb 01 '16 at 02:48
  • 2
    if `mask` as at least one pixel different from 0, then you have detected blue. You can use `np.any` or `cv2.countNonZero` for this – Miki Feb 01 '16 at 02:49
  • @Miki Thanks for your answer. – Atul Feb 04 '16 at 19:22
  • May I suggest to check [this](https://stackoverflow.com/questions/58340960/python-script-to-check-if-red-color-is-present-in-image-and-call-other-function) if not done already? – Svestis Feb 12 '22 at 17:46

0 Answers0