简体   繁体   English

cv2 minAreaRect 中心旋转 90 度?

[英]cv2 minAreaRect center rotated by 90 degrees?

I'm not sure if I encountered a bug or if I'm missing something (opencv 4.4.0.46 and 4.5.3.56, maybe others).我不确定我是否遇到了错误或遗漏了什么(opencv 4.4.0.46 和 4.5.3.56,也许是其他)。

I'm trying to find the rotated bounding box for this image:我正在尝试找到此图像的旋转边界框:

在此处输入图片说明

This is the result:这是结果:

在此处输入图片说明

here is the code:这是代码:

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)

This code works fine for the "thunder" sample image and it looks like all the sample code I could find around.这段代码适用于“thunder”示例图像,它看起来像我能找到的所有示例代码。 Am I missing something?我错过了什么吗? Thanks谢谢

You are using np.column_stack(np.where(thresh > 0)) to find the contours.您正在使用np.column_stack(np.where(thresh > 0))来查找轮廓。 OpenCV uses (x, y) notation whereas NumPy uses (row, col) . OpenCV 使用(x, y)表示法,而 NumPy 使用(row, col) You can check it by printing coords .您可以通过打印coords来检查它。

I suggest using OpenCV functions where possible.我建议尽可能使用 OpenCV 函数。 The following code works.以下代码有效。

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

See Contour Features .请参阅轮廓特征

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM