0

How to detect (True/False) if the frame contains at least one for example rgb(213, 39, 27) pixel? I want to process frame only when the detection is true.

import cv2

VIDEO_URL = ''
cap = cv2.VideoCapture(VIDEO_URL)

fps = cap.get(cv2.CAP_PROP_FPS)
wait_ms = int(1000/fps)

while True:
    ret, frame = cap.read()
    img = cv2.cvtColor(frame, cv2.IMREAD_COLOR)[815:970, 360:1920]

    #image processing here

    if cv2.waitKey(wait_ms) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

mateuszm
  • 35
  • 6
  • It is pretty unlikely you'll get that right with no tolerance whatsoever in a video. Normally you'd use a range, or a tolerance. What if the lighting changes a fraction? What are you actually trying to do? – Mark Setchell Jun 29 '20 at 20:33
  • Does this answer your question? https://stackoverflow.com/questions/38082004/opencv-inrange-function – tiberius Jun 29 '20 at 20:34
  • I would suggest you to install this extension in your vs code - https://marketplace.visualstudio.com/items?itemName=gsGupta.opencv-snippets&ssr=false It directly provides you with snippets of such things. – Gauri Shankar Gupta Jul 02 '20 at 20:27

1 Answers1

1

You can use hsv images to detect any color.

Eg-Let You want to identify blue color in your image - Include this code inside your while loop.

hsvimg = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lb=np.array([94,80,2])
ub=np.array([126,255,255])
mask = cv2.inRange(hsvimg, lb, ub)   
if 255 in mask:
    print("Blue color present")

This code determines the blue color. You can find any other color by changing the HSV range.