Рисование и обрезка Повернутый прямоугольник с координатами - PullRequest
0 голосов
/ 27 сентября 2019

Я пытаюсь нарисовать и обрезать повернутый прямоугольник в opencv python, но я не знаю, как это сделать.У меня есть x, y координаты ширины и высоты прямоугольника и я искал, как найти контуры этих координат x, y для использования cv2.minAreaRect (), но не могу сделать это.

Я даю координаты линии итакже нарисовал линию, чтобы показать вам. Я хочу нарисовать прямоугольник высотой 2 и шириной, такой же, как линия.Линейные координаты: x1, y1, x2, y2 = 542, 771, 1757, 977

image with line where I want to draw and crop rectangle

Я не могу объяснить это правильно, пожалуйста, поймите, что я хочу.

Я попробовал это с помощью следующего скрипта, но я не знаю, как найти контуры от 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()
...