Zai*_*lux 6 python opencv overloading rectangles
cv2.rectangle 有两种调用方式:
来源:https : //docs.opencv.org/4.1.2/d6/d6e/group__imgproc__draw.html#ga07d2f74cadcf8e305e810ce8eed13bc9
我称矩形如下:
cv2.rectangle(img=cv2_im, pt1=a, pt2=b, 颜色=(0, 255, 0), 厚度=3, lineType=cv2.LINE_AA)
错误信息:
cv2.rectangle(img=cv2_im, pt1=a, pt2=b, color=(0, 255, 0), depth=3, lineType=cv2.LINE_AA) TypeError: rectangle() 缺少必需的参数 'rec' (pos 2) )
我不明白为什么应用程序试图调用该方法的重载版本。U 明确定义版本 1 调用。我尝试使用 (x,y) 等更改变量 a 但它不起作用。正确的方法调用仅在我第一次调用 retangle() 时有效,之后它希望我使用它的重载版本。
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)
Run Code Online (Sandbox Code Playgroud)在第二次调用之间,我使用了 cv2 选择性搜索并设置了以下内容: cv2.setUseOptimized(True) cv2.setNumThreads(4)
希望你们看到我做错了什么。
Zai*_*lux 14
好吧,这很遗憾我现在才发现,昨天在这个问题上解决了几个小时......
元组中的值是浮点数。
> a = (x*width,y*height) b = (x_max*width, y_max*height)
Run Code Online (Sandbox Code Playgroud)
将它们更改为 int 并丢失后逗号值后,它可以工作。
a = (int(x*width),int(y*height))
Run Code Online (Sandbox Code Playgroud)
我也发现自己犯了这个cv2.rectangle()错误。尝试了以上所有方法:
# Data types were ok:
# int for coordinates
# float32 for the image
>>> pt1 = (x, y)
>>> print(pt1)
(4, 10)
>>> print(image.dtype)
'float32'
Run Code Online (Sandbox Code Playgroud)
但我仍然有错误。发生的情况是,我正在构建 RGB 图像,然后将其更改为 BGR,如下所示:
# Image from RGB to BGR
image = image[:, :, ::-1]
Run Code Online (Sandbox Code Playgroud)
问题是此操作返回同一数组的新视图(它不会更改其内部存储方式)。因此,为了真正将图像通道从 RGB 排列为 BGR,您需要使用 opencv 来完成:
# Image from RGB to BGR
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
Run Code Online (Sandbox Code Playgroud)
这解决了这个问题,因为该操作改变了图像数组的内部存储结构,因此 opencv 可以使用它。