【问题标题】:OpenCV Python: How to warpPerspective a large image based on transform inferred from small regionOpenCV Python:如何根据从小区域推断的变换来扭曲透视大图像
【发布时间】:2020-11-13 18:02:43
【问题描述】:

根据 Adrian Rosenbrock 博客:https://www.pyimagesearch.com/2014/08..,我正在使用 cv2.getPerspectiveTransform() 和 cv2.warpPerspective() 扭曲图像。

但是在我的情况下,我有一个图像,我只能选择要变形的区域 B,但需要变形(自上而下的视图)整个较大的图像 A。

从较小区域 B 推断出的透视变换的参数可以应用到全图 A 上吗?这可能吗?enter image description here

【问题讨论】:

  • 假设您没有先从图像 A 裁剪区域 B,然后是的,但是您需要提供足够的输出大小来覆盖扭曲的图像 A。试试看你得到什么或提供你的 4 套您发布的图像的输入和输出坐标。
  • 请出示您的代码。欢迎来到堆栈溢出。请阅读帮助中心 (stackoverflow.com/help) 中的信息指南,特别是“如何提出一个好问题”(stackoverflow.com/help/how-to-ask) 和“如何创建最小的、可重现的示例”( stackoverflow.com/help/minimal-reproducible-example).
  • 我正在使用来自stackoverflow.com/questions/19695702/… 的 Python 代码,由 Gstav 发布,但不理解这个概念。 src 坐标是小区域 B 的坐标,dst 坐标是大整个图像 A 的坐标吗? B 的形状是否必须是正方形才能使代码起作用?谢谢!

标签: computer-vision


【解决方案1】:

这是证明红色方块中的矩阵适用于 Python OpenCV 中的整个图像的一种方法。

这里我根据它的顶部和左侧尺寸将四边形矫正成一个矩形。

输入:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-07
    • 2017-12-24
    • 2017-02-12
    • 2018-10-16
    • 1970-01-01
    • 2012-10-06
    • 2012-12-20
    相关资源
    最近更新 更多