Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 423
Zailux Avatar asked Jan 22 '20 13:01

Zailux


People also ask

How does cv2 rectangle work?

cv2. rectangle(img, pt1, pt2, color, thickness, lineType, shift) Draws a simple, thick, or filled up-right rectangle. The function rectangle draws a rectangle outline or a filled rectangle whose two opposite corners are pt1 and pt2. Parameters img Image.

What does cv2 rectangle return?

cv2. rectangle() method is used to draw a rectangle on any image. Parameters: image: It is the image on which rectangle is to be drawn.

How do you fill a cv2 rectangle?

To fill the rectangle we use the thickness = -1 in the cv2. rectangle() function.


1 Answers

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))
like image 55
Zailux Avatar answered Sep 24 '22 23:09

Zailux