I am trying to draw rectangles on top of frames I record from my laptop's camera using opencv2 in python.
However, the image returned from the cv2.rectangle function is None
. Why?
import numpy as np
import cv2
# details of rectangle to be drawn.
x, y, h, w = (493, 305, 125, 90)
cap = cv2.VideoCapture(0)
while 1:
ret, frame = cap.read()
if not ret or not frame:
# camera didn't give us a frame.
continue
# attempt to draw a rectangle.
img = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
# This prints out None every time! why?
print str(img)
if not img:
continue
# We never get here since img is None. :/
cv2.imshow('img', img)
I've tried adding checks as you see for if the frame from the camera is null. I've also tried making sure that the rectangle actually fits in the frame.
I'm sure my camera's working since I can successfully imshow
the frame.
rectangle does not return anything
[edit:] in opencv2.4.x but it does return the image in opencv3.0 / python
also note, that the very popular py_tutrorials are for 3.0, so don't get confused ;)
import numpy as np
import cv2
# details of rectangle to be drawn.
x, y, h, w = (493, 305, 125, 90)
cap = cv2.VideoCapture(0)
while 1:
ret, frame = cap.read()
if not ret or not frame:
# camera didn't give us a frame.
continue
# draw a rectangle.
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
# show it ;)
cv2.imshow('img', frame)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With