Python 绘制和裁剪带坐标的旋转矩形
我试图在opencv python中绘制并裁剪旋转的矩形,但我不知道怎么做。 我有矩形的x,y坐标宽度和高度,并搜索了如何使用cv2.minarealect()找到该x,y坐标的轮廓,但无法实现 我给出了线坐标,并在上面画了一条线来告诉你们。我想画一个高度为2,宽度与线相同的矩形。 直线坐标:x1,y1,x2,y2=542771757977 我无法解释清楚,请理解我的意思 我尝试了下面的脚本,但我不知道如何从x坐标中找到轮廓Python 绘制和裁剪带坐标的旋转矩形,python,python-3.x,opencv,image-processing,drawrectangle,Python,Python 3.x,Opencv,Image Processing,Drawrectangle,我试图在opencv python中绘制并裁剪旋转的矩形,但我不知道怎么做。 我有矩形的x,y坐标宽度和高度,并搜索了如何使用cv2.minarealect()找到该x,y坐标的轮廓,但无法实现 我给出了线坐标,并在上面画了一条线来告诉你们。我想画一个高度为2,宽度与线相同的矩形。 直线坐标:x1,y1,x2,y2=542771757977 我无法解释清楚,请理解我的意思 我尝试了下面的脚本,但我不知道如何从x坐标中找到轮廓 import cv2 import numpy as np de
import cv2
import numpy as np
def main():
img = cv2.imread("/home/infinity/Pictures/1.png")
# points for test.jpg
cnt = np.array([
[[64, 49]],
[[122, 11]],
[[391, 326]],
[[308, 373]]
])
print("shape of cnt: {}".format(cnt.shape))
rect = cv2.minAreaRect(cnt)
print("rect: {}".format(rect))
# the order of the box points: bottom left, top left, top right,
# bottom right
box = cv2.boxPoints(rect)
box = np.int0(box)
print("bounding box: {}".format(box))
cv2.drawContours(img, [box], 0, (0, 0, 255), 2)
# get width and height of the detected rectangle
width = int(rect[1][0])
height = int(rect[1][1])
src_pts = box.astype("float32")
# corrdinate of the points in box points after the rectangle has been
# straightened
dst_pts = np.array([[0, height-1],
[0, 0],
[width-1, 0],
[width-1, height-1]], dtype="float32")
# the perspective transformation matrix
M = cv2.getPerspectiveTransform(src_pts, dst_pts)
# directly warp the rotated rectangle to get the straightened rectangle
warped = cv2.warpPerspective(img, M, (width, height))
# cv2.imwrite("crop_img.jpg", warped)
cv2.waitKey(0)
if __name__ == "__main__":
main()
这似乎基本上是一个重复的问题。它显然包含OpenCV的矩形定义 如果你很难得到坐标,那么我们需要知道目标是什么。如果相机有静态视图,那么它显然很简单,只需使用像Gimp这样的编辑器来读取它们。我感觉它可能是一个移动的摄像机或其他复杂的站在某种程度上 如果你的意思不同,你应该真正表达你想要实现什么