【问题标题】:cv2 minAreaRect center rotated by 90 degrees?cv2 minAreaRect 中心旋转 90 度?
【发布时间】:2021-09-25 02:29:42
【问题描述】:

我不确定我是否遇到错误或遗漏了什么(opencv 4.4.0.46 和 4.5.3.56,也许还有其他)。

我正在尝试查找此图像的旋转边界框:

这是结果:

代码如下:

import cv2
import numpy as np

base_image = cv2.imread("so_sample.png", 0)

thresh = cv2.threshold(base_image, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

coords = np.column_stack(np.where(thresh > 0))
rect = cv2.minAreaRect(coords)
print("RECT", rect)

box = np.int0(cv2.boxPoints(rect))
drawImg = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)
drawImg = cv2.copyMakeBorder(drawImg, 0, 100, 0, 100, cv2.BORDER_REPLICATE) # to see the whole box
cv2.drawContours(drawImg, [box], 0,(0,0,255), 2)

cv2.imshow("base_image", base_image)
cv2.imshow("thresh", thresh)
cv2.imshow("drawImg", drawImg)
cv2.waitKey(0)

此代码适用于“thunder”示例图像,它看起来就像我能找到的所有示例代码。我错过了什么吗?谢谢

【问题讨论】:

    标签: python opencv bounding-box


    【解决方案1】:

    您正在使用np.column_stack(np.where(thresh > 0)) 来查找轮廓。 OpenCV 使用 (x, y) 表示法,而 NumPy 使用 (row, col)。 可以打印coords查看。

    我建议尽可能使用 OpenCV 函数。 以下代码有效。

    coords, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    rect = cv2.minAreaRect(np.vstack(coords))
    

    Contour Features

    【讨论】:

      猜你喜欢
      • 2018-03-21
      • 1970-01-01
      • 1970-01-01
      • 2021-05-24
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      • 2015-03-22
      • 2022-01-13
      相关资源
      最近更新 更多