简体   繁体   English

使用坐标绘制和裁剪旋转矩形

[英]Drawing and cropping Rotated Rectangle with coordinates

I am trying to draw and crop rotated rectangle into opencv python but I don't know how to do it.我正在尝试将旋转的矩形绘制并裁剪为 opencv python 但我不知道该怎么做。 I have x,y coordinates width and height of rectangle and searched about how to find contours of that x,y coordinates for using cv2.minAreaRect() but can't make it.我有矩形的 x,y 坐标宽度和高度,并搜索了如何找到该 x,y 坐标的轮廓以使用 cv2.minAreaRect() 但无法做到。

I am giving line coordinates and also drawn a line on it to show you.where I want to draw rectangle of height 2 and width same as line.我给出了线坐标并在其上画了一条线来展示给你。我想在哪里绘制高度为 2 且宽度与线相同的矩形。 Line Coordinates: x1, y1, x2, y2 = 542, 771, 1757, 977线坐标:x1, y1, x2, y2 = 542, 771, 1757, 977

带有我要绘制和裁剪矩形的线条的图像

I can not explain it properly please understand what I want.我无法正确解释它请理解我想要什么。

I tried it with following script but I don't know how to find contours from x, coordinates我用以下脚本尝试过,但我不知道如何从 x, 坐标中找到轮廓

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()

It appears to be basically duplicate question to Draw rectangle in OpenCV . 在 OpenCV 中绘制矩形似乎基本上是重复的问题。 It obviously contains a definition of rectangle for OpenCV.它显然包含 OpenCV 的矩形定义。

If you're having acutally trouble to get coordinates , then we need to know what is the goal.如果您在获取坐标时遇到困难,那么我们需要知道目标是什么。 If the camera has static view, then it would be obviusly trivial, just using an editor like Gimp to read them.如果相机有 static 视图,那么它显然是微不足道的,只需使用像 Gimp 这样的编辑器来阅读它们。 I sense it might be a moving camera or some other complication stand in a way...我觉得它可能是一个移动的相机或其他一些复杂的东西......

If you meant something different, you should really express what do you want to achieve .如果您的意思不同,您应该真正表达您想要实现的目标

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

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