gpt4 book ai didi

opencv - 从图像中裁剪出倾斜框的有效方法是什么?

转载 作者:行者123 更新时间:2023-12-02 16:52:49 24 4
gpt4 key购买 nike

图片:

enter image description here

我有边界多边形的坐标(不是确切的矩形)。
如何有效旋转并从图像中提取仪表?

array([[337, 300],
[574, 348],
[567, 378],
[329, 337]], dtype=int32)

我已经能够旋转图像了
(h, w) = im.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(im, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)

但这也改变了盒子的坐标。

预期产量:

enter image description here

最佳答案

我认为您希望从中获得4点的透视变换:

enter image description here

对此:

enter image description here

#!/usr/bin/env python3

import cv2

# Set width and height of output image
W, H = 600, 200

# Load input image
img = cv2.imread('speedo.png')

# Define points in input image: top-left, top-right, bottom-right, bottom-left
pts0 = np.float32([[337,300],[574,348],[567,378],[329,337]])

# Define corresponding points in output image
pts1 = np.float32([[0,0],[W,0],[W,H],[0,H]])

# Get perspective transform and apply it
M = cv2.getPerspectiveTransform(pts0,pts1)
result = cv2.warpPerspective(img,M,(W,H))

# Save reult
cv2.imwrite('result.png', result)

您可以使用 ImageMagick 轻松完成相同的操作,该文件安装在大多数Linux发行版中,并且可用于macOS和Windows。在终端或命令提示符中,您只需运行:
magick speedo.png -distort perspective '337,300 0,0 574,348 200,0 567,378 200,100 329,337 0,100' -crop 200x100+0+0 result.png

关键字:Python,命令行,shell,ImageMagick,OpenCV,变形,透 View ,扭曲,仿射,4点,4点,变换,图像,图像处理。

关于opencv - 从图像中裁剪出倾斜框的有效方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57207975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com