Computer vision OpenCV Python:如何基于从小区域推断的变换透视大图像

Computer vision OpenCV Python:如何基于从小区域推断的变换透视大图像,computer-vision,Computer Vision,根据Adrian Rosenbrock的博客,我正在使用cv2.getPerspectiveTransform()和cv2.warpPerspective()扭曲图像: 但是,在我的例子中,我有一个图像,我只能选择要扭曲的区域B,但需要扭曲(自顶向下视图)整个较大的图像A 从较小区域B推断出的透视变换参数能否应用于完整图像A?这可能吗?这里有一种方法可以证明红方块中的矩阵适用于Python OpenCV中的整个图像 在这里,我根据四边形的顶部和左侧尺寸将其校正为矩形 输入: 假设您没有首先从

根据Adrian Rosenbrock的博客,我正在使用cv2.getPerspectiveTransform()和cv2.warpPerspective()扭曲图像:

但是,在我的例子中,我有一个图像,我只能选择要扭曲的区域B,但需要扭曲(自顶向下视图)整个较大的图像A


从较小区域B推断出的透视变换参数能否应用于完整图像A?这可能吗?

这里有一种方法可以证明红方块中的矩阵适用于Python OpenCV中的整个图像

在这里,我根据四边形的顶部和左侧尺寸将其校正为矩形

输入:



假设您没有首先从图像A裁剪区域B,那么是的,但是您需要提供足够的输出大小来覆盖扭曲的图像A。尝试一下,看看您得到了什么,或者为您发布的图像提供了4组输入和输出坐标。请显示您的代码。欢迎来到堆栈溢出。请阅读帮助中心()中的信息指南,特别是“如何提出一个好问题”和“如何创建一个最小的、可复制的示例”。我正在使用Gstav发布的Python代码,但不理解其概念。src坐标是小区域B的坐标,dst坐标是大整体图像A的坐标?B的形状必须是正方形才能使代码工作吗?谢谢
import numpy as np
import cv2
import math

# read input
img = cv2.imread("red_quadrilateral.png")
hh, ww = img.shape[:2]

# specify input coordinates for corners of red quadrilateral in order TL, TR, BR, BL as x,
input = np.float32([[136,113], [206,130], [173,207], [132,196]])

# get top and left dimensions and set to output dimensions of red rectangle
width = round(math.hypot(input[0,0]-input[1,0], input[0,1]-input[1,1]))
height = round(math.hypot(input[0,0]-input[3,0], input[0,1]-input[3,1]))
print("width:",width, "height:",height)

# set upper left coordinates for output rectangle
x = input[0,0]
y = input[0,1]

# specify output coordinates for corners of red quadrilateral in order TL, TR, BR, BL as x,
output = np.float32([[x,y], [x+width-1,y], [x+width-1,y+height-1], [x,y+height-1]])

# compute perspective matrix
matrix = cv2.getPerspectiveTransform(input,output)
print(matrix)

# do perspective transformation setting area outside input to black
# Note that output size is the same as the input image size
imgOutput = cv2.warpPerspective(img, matrix, (ww,hh), cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0,0,0))

# save the warped output
cv2.imwrite("red_quadrilateral_warped.jpg", imgOutput)

# show the result
cv2.imshow("result", imgOutput)
cv2.waitKey(0)
cv2.destroyAllWindows()