Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does cv2.rectangle return None instead of an image?

Tags:

python

opencv

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.

like image 947
dangerChihuahua007 Avatar asked Dec 16 '22 03:12

dangerChihuahua007


1 Answers

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)
like image 112
berak Avatar answered Dec 21 '22 22:12

berak