有没有什么帮助器方法来绘制一个旋转的矩形,这个矩形大概是由cv2.minAreaRect()返回的,比如((x1,y1),(x2,y2),angle)?cv2.rectangle()不支持角度。由于返回的元组不属于"RotatedRect“类(因为它似乎不是在Python绑定中实现的),因此没有points()方法,如C++教程"Creating Bounding rotated boxes and ellipses for contours¶"中所示。
如何从直线绘制旋转的矩形-围绕中心点或给定的第一个点旋转?
发布于 2020-01-29 11:19:42
下面是一个绘制旋转矩形的具体示例。其思想是使用Otsu's threshold获取二进制图像,然后使用cv2.findContours()查找轮廓。我们可以使用cv2.minAreaRect()获得旋转的矩形,使用cv2.boxPoints()获得四个角点。要绘制矩形,可以使用cv2.drawContours()或cv2.polylines()。
输入->输出


代码
import cv2
import numpy as np
# Load image, convert to grayscale, Otsu's threshold for binary image
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Find contours, find rotated rectangle, obtain four verticies, and draw
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
rect = cv2.minAreaRect(cnts[0])
box = np.int0(cv2.boxPoints(rect))
cv2.drawContours(image, [box], 0, (36,255,12), 3) # OR
# cv2.polylines(image, [box], True, (36,255,12), 3)
cv2.imshow('image', image)
cv2.waitKey()https://stackoverflow.com/questions/18207181
复制相似问题