cv2.rectangle() calls overloaded method, although I give other parameter
Asked Answered
G

3

7

cv2.rectangle has two ways of calling:

  • img = cv.rectangle( img, pt1, pt2, color[, thickness[, lineType[, shift]]] )
  • img = cv.rectangle( img, rec, color[, thickness[, lineType[, shift]]]

source:https://docs.opencv.org/4.1.2/d6/d6e/group__imgproc__draw.html#ga07d2f74cadcf8e305e810ce8eed13bc9

I call rectangle as following:

cv2.rectangle(img=cv2_im, pt1=a, pt2=b, color=(0, 255, 0), thickness=3, lineType=cv2.LINE_AA)

Error Message:

cv2.rectangle(img=cv2_im, pt1=a, pt2=b, color=(0, 255, 0), thickness=3, lineType=cv2.LINE_AA) TypeError: rectangle() missing required argument 'rec' (pos 2)

I do not understand why the application tries to call the overloaded version of the method. U explicitly define version 1 call. I tried changing the variable a with (x,y) etc. but it doesn't work. The correct method call only works the first time I call the retangle() afterwards it expects me to use the overloaded version of it.


  • Python 3.7.5 64 bit
  • Pillow 7.0.0
  • numpy 1.18.1
  • opencv-contrib-python 4.1.2.30

    imgname='fly_1.jpg'   
    im = Image.open(imgname)
    cv2_im = np.array(im)
    
    #x,y,w,h aus Image Labeler
    box= [505.54, 398.334, 1334.43, 2513.223]
    x,y,w,h = box
    a = (x, y)
    b = (x+w, y+h)
    
    #First rectanglecall
    cv2.rectangle(img=cv2_im, pt1=a, pt2=b, color=(0, 255, 0), thickness=3, lineType=cv2.LINE_AA)
    #calls two cv2 methods which shouldn't influence rectangle
    rects = getRegionProposals(im,'f',normalized=True)   
    
    
    for i,rect in enumerate(rects):
    
         x, x_max, y, y_max = rect
         a = (x*width,y*height)
         b = (x_max*width, y_max*height)
    
         if (IoU is not False and IoU > 0.5):
             #second and further calls
             cv2.rectangle(img=cv2_im, pt1=a, pt2=b, color=(0, 255, 0), thickness=3, lineType=cv2.LINE_AA)
    

In between the second call I used cv2 selective search and set the following: cv2.setUseOptimized(True) cv2.setNumThreads(4)

Hope u guys see what I'm doin wrong.

Garb answered 22/1, 2020 at 13:50 Comment(0)
G
15

okay this is sad that I just found out now, after being on this problem yesterday for hours ...

The Values in the tuples were floats.

> a = (x*width,y*height) b = (x_max*width, y_max*height)

After changing them to int, and losing the after comma values it works.

a = (int(x*width),int(y*height))
Garb answered 22/1, 2020 at 14:6 Comment(1)
import numpy as np import cv2 img = np.zeros( (800, 800), dtype=np.int32 ) st_pt = (2, 4) ed_pt = (100, 150) color = (255, 0, 0) img = cv2.rectangle( img, start_point = st_pt, end_point = ed_pt, color = color, thickness = 2 ) plt.figure(figsize=(4, 4)) plt.imshow(img) plt.figure()Toein
A
5

I also found myself with this cv2.rectangle() error. Tried all of the above:

# Data types were ok:
# int for coordinates
# float32 for the image
>>> pt1 = (x, y)
>>> print(pt1)
(4, 10)
>>> print(image.dtype)
'float32'

But I still had the error. What happened is that I was building a RGB image and I was changing it to BGR this way:

# Image from RGB to BGR
image = image[:, :, ::-1]

The problem is that this operation returns a new view of the same array (it does not change how it is internally stored). So for truly permuting the image channels from RGB to BGR you need to do it using opencv:

# Image from RGB to BGR
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

That solved the issue because that operation changes the image array internal storage structure so opencv can work with it.

Ajaajaccio answered 10/3, 2021 at 8:23 Comment(0)
B
3

I got the same error while using cv2.rectangle(). I found that one of the coordinates value in my code was float. So i resolved it by typecasting. Lets suppose in your case pt1 and pt2 are (1.5,2) and (2,6.5) respectively. So simply you can convert the value to integer.

pt1=(int(1.5),2)
pt2=(2,int(6.5))

I hope it will be helpful for you.

Boatwright answered 24/8, 2020 at 14:26 Comment(2)
you should be aware though, that you solution results in rounded results. e.g. int(1.9) = 1. Same goes for my solution though :DGarb
Well, Its as per the requirement. floor and ceil are the other options..Boatwright

© 2022 - 2024 — McMap. All rights reserved.