【问题标题】:cv2.rectangle() calls overloaded method, although I give other parametercv2.rectangle() 调用重载方法,虽然我给出了其他参数
【发布时间】:2020-05-08 16:51:36
【问题描述】:

cv2.rectangle 有两种调用方式:

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

来源: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,线型=cv2.LINE_AA)

错误信息:

cv2.rectangle(img=cv2_im, pt1=a, pt2=b, color=(0, 255, 0), 粗细=3, lineType=cv2.LINE_AA) TypeError: rectangle() 缺少必需的参数 'rec' (pos 2)

我不明白为什么应用程序会尝试调用方法的重载版本。 U 明确定义版本 1 调用。 我尝试用 (x,y) 等更改变量 a ,但它不起作用。正确的方法调用仅在第一次我调用 retangle() 之后它希望我使用它的重载版本。


  • Python 3.7.5 64 位
  • 枕头 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)
    

在第二次调用之间,我使用了 cv2 选择性搜索并设置了以下内容: cv2.setUseOptimized(True) cv2.setNumThreads(4)

希望你们看到我做错了什么。

【问题讨论】:

    标签: python opencv overloading rectangles


    【解决方案1】:

    好吧,在昨天解决这个问题几个小时之后,我现在才发现这很遗憾......

    元组中的值是浮点数。

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

    将它们更改为 int 并丢失逗号后的值后,它可以工作。

    a = (int(x*width),int(y*height))
    

    【讨论】:

    • 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, 厚度 = 2) plt.figure(figsize=(4, 4)) plt.imshow(img) plt.figure ()
    【解决方案2】:

    我也发现自己遇到了这个cv2.rectangle() 错误。尝试了以上所有方法:

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

    但我仍然有错误。发生的事情是,我正在构建一个 RGB 图像,并以这种方式将其更改为 BGR:

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

    问题是这个操作返回了同一个数组的一个新视图(它不会改变它的内部存储方式)。因此,要真正将图像通道从 RGB 转换为 BGR,您需要使用 opencv:

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

    这解决了问题,因为该操作更改了图像数组内部存储结构,因此 opencv 可以使用它。

    【讨论】:

      【解决方案3】:

      我在使用cv2.rectangle() 时遇到了同样的错误。我发现我的代码中的坐标值之一是浮点数。所以我通过类型转换解决了它。 假设在您的情况下 pt1pt2 分别是 (1.5,2) 和 (2,6.5)。因此,您可以简单地将值转换为整数。

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

      希望对你有帮助。

      【讨论】:

      • 您应该知道,您的解决方案会产生四舍五入的结果。例如int(1.9) = 1。我的解决方案也是如此:D
      • 嗯,根据要求。 floorceil 是其他选项..
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-10
      • 1970-01-01
      • 2022-11-12
      • 2011-09-15
      • 2023-03-26
      • 2021-08-30
      相关资源
      最近更新 更多