3

I tried to create it by writing the command:

myRect = cv2.Rect(p1, p2)

but it does not find it. It writes:

Cannot find reference 'Rect' in 'imported module cv2'.

thanks for helping!

to make it clearer: I don't want to draw a rectangle on an image. I want to create a Rect object to apply methods on, such as area().

Amit Keinan
  • 149
  • 8
  • 1
    thanks, but I don't want to draw a rectangle on an image. I want to create a Rect object to apply methods on, such as area(). – Amit Keinan May 26 '18 at 17:40
  • 2
    There is none in the python interface... You can use a tuple in place where a cv::Rect is needed. e.g. `(0,0,100, 100)` this is a "rect" with the top left corner in (0,0) and a size of 100 width and height – api55 May 26 '18 at 21:00

1 Answers1

3

You can try something like this:

import cv2
import numpy as np

img = cv2.imread('input.jpg')
myRect = img[280:340, 330:390]

280:340, 330:390 means: get a rectangle that begins at 280th row and 330th column and ends at 340th row and 390 column. So coordinates of the rectangle would be: (280,330), (280,390), (340,330), (340,390)

Ishara Madhawa
  • 3,039
  • 4
  • 19
  • 35