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

2024/11/19 3:27:40

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 = recta = (x*width,y*height)b = (x_max*width, y_max*height)if (IoU is not False and IoU > 0.5):#second and further callscv2.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.

Answer

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))
https://en.xdnf.cn/q/70981.html

Related Q&A

Converting xls to csv in Python 3 using xlrd

Im using Python 3.3 with xlrd and csv modules to convert an xls file to csv. This is my code:import xlrd import csvdef csv_from_excel():wb = xlrd.open_workbook(MySpreadsheet.xls)sh = wb.sheet_by_name(S…

HTMLParser.HTMLParser().unescape() doesnt work

I would like to convert HTML entities back to its human readable format, e.g. £ to £, ° to etc.Ive read several posts regarding this question Converting html source content into read…

What security issues need to be addressed when working with Google App Engine?

Ive been considering using Google App Engine for a few hobby projects. While they wont be handling any sensitive data, Id still like to make them relatively secure for a number of reasons, like learnin…

Supporting multiple Python module versions (with the same version of Python)

I looked around but cannot find a clear answer to my question.I have a very legitimate need for supporting N-versions of the same Python module.If they are stored in the same same package/directory, th…

ImportError: cannot import name signals

Im using Django 1.3.0 with Python 2.7.1. In every test I write the following imports I get the importError above:from django.utils import unittest from django.test.client import ClientThe full stack tr…

Return a Pandas DataFrame as a data_table from a callback with Plotly Dash for Python

I would like to read a .csv file and return a groupby function as a callback to be displayed as a simple data table with "dash_table" library. @Lawliets helpful answer shows how to do that wi…

Nose: How to skip tests by default?

I am using Pythons nose and I have marked some of my tests as "slow", as explained in the attrib plugin documentation.I would like to skip all "slow" Tests by default when running n…

SQLAlchemy ORM select multiple entities from subquery

I need to query multiple entities, something like session.query(Entity1, Entity2), only from a subquery rather than directly from the tables. The docs have something about selecting one entity from a s…

How to ensure data is received between commands

Im using Paramiko to issue a number of commands and collect results for further analysis. Every once in a while the results from the first command are note fully returned in time and end up in the out…

Format Excel Column header for better visibility and Color

I have gone through many posts but did not found the exact way to do the below. Sorry for attaching screenshot(Just for better visibility) as well , I will write it also. Basically it looks like -Name…