在执行透视变形时,如何避免部分图像被剪切?
How can I avoid parts of images from getting cut, when performing perspective warping?
我正在尝试转动图像的视角,以便获得提供前视图视角的结果。我正在使用 cv2.WarpPerspective 函数。但是,在执行扭曲时,图像的某些部分会被切断。我怎样才能避免这种情况?我想到的一个选项是找到图像特定部分的变换矩阵,然后将该矩阵应用于整个图像。但是,该方法并未产生理想的结果。
我使用的代码是:
import numpy as np
import cv2
from google.colab.patches import cv2_imshow
img = cv2.imread("drive/My Drive/Images_for_Adarsh/DSC_0690.JPG")
height,width = 1000,1500
img = cv2.resize(img,(width,height))
pts1 = np.float32([[ 250, 0],[1220, 300],[1300, 770],[ 250, 860]])
pts2 = np.float32([[0,0],[width,0],[width,height],[0,height]])
matrix = cv2.getPerspectiveTransform(pts1,pts2)
print(matrix.shape)
print(matrix)
imgOutput = cv2.warpPerspective(img,matrix,(width,height))
cv2_imshow(imgOutput)
cv2.imwrite("drive/My Drive/PerspectiveWarp-Results1/0690_coarse/0690([[ 250, 0],[1220, 300],[1300, 770],[ 250, 860]]).JPG",imgOutput)
输入图像:
扭曲的图像:
这是一种在 Python/OpenCV 中扭曲图像并添加额外 space 的简单方法,它将包含更多输入,但输入之外的区域为透明。
输入:
import numpy as np
import cv2
# read input
img = cv2.imread("building.jpg")
# resize
height,width = 1000,1500
img = cv2.resize(img, (width,height))
# specify conjugate coordinates and shift output on left and top by 500
pts1 = np.float32([[ 250, 0],[1220, 300],[1300, 770],[ 250, 860]])
pts2 = np.float32([[+500,+500],[width+500,+500],[width+500,height+500],[+500,height+500]])
# compute perspective matrix
matrix = cv2.getPerspectiveTransform(pts1,pts2)
print(matrix.shape)
print(matrix)
# convert image to BGRA with opaque alpha
img = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
# do perspective transformation setting area outside input to transparent
# extend output size so extended by 500 all around
imgOutput = cv2.warpPerspective(img, matrix, (width+1000,height+1000), cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0,0,0,0))
# resize output, since it is too large to post
imgOutput = cv2.resize(imgOutput, (width,height))
# save the warped output
cv2.imwrite("building_warped.png", imgOutput)
# show the result
cv2.imshow("result", imgOutput)
cv2.waitKey(0)
cv2.destroyAllWindows()
注意:应该可以使用矩阵将输入角投影到输出域并计算所需的输出大小以容纳所有扭曲的输入。
我正在尝试转动图像的视角,以便获得提供前视图视角的结果。我正在使用 cv2.WarpPerspective 函数。但是,在执行扭曲时,图像的某些部分会被切断。我怎样才能避免这种情况?我想到的一个选项是找到图像特定部分的变换矩阵,然后将该矩阵应用于整个图像。但是,该方法并未产生理想的结果。
我使用的代码是:
import numpy as np
import cv2
from google.colab.patches import cv2_imshow
img = cv2.imread("drive/My Drive/Images_for_Adarsh/DSC_0690.JPG")
height,width = 1000,1500
img = cv2.resize(img,(width,height))
pts1 = np.float32([[ 250, 0],[1220, 300],[1300, 770],[ 250, 860]])
pts2 = np.float32([[0,0],[width,0],[width,height],[0,height]])
matrix = cv2.getPerspectiveTransform(pts1,pts2)
print(matrix.shape)
print(matrix)
imgOutput = cv2.warpPerspective(img,matrix,(width,height))
cv2_imshow(imgOutput)
cv2.imwrite("drive/My Drive/PerspectiveWarp-Results1/0690_coarse/0690([[ 250, 0],[1220, 300],[1300, 770],[ 250, 860]]).JPG",imgOutput)
输入图像:
扭曲的图像:
这是一种在 Python/OpenCV 中扭曲图像并添加额外 space 的简单方法,它将包含更多输入,但输入之外的区域为透明。
输入:
import numpy as np
import cv2
# read input
img = cv2.imread("building.jpg")
# resize
height,width = 1000,1500
img = cv2.resize(img, (width,height))
# specify conjugate coordinates and shift output on left and top by 500
pts1 = np.float32([[ 250, 0],[1220, 300],[1300, 770],[ 250, 860]])
pts2 = np.float32([[+500,+500],[width+500,+500],[width+500,height+500],[+500,height+500]])
# compute perspective matrix
matrix = cv2.getPerspectiveTransform(pts1,pts2)
print(matrix.shape)
print(matrix)
# convert image to BGRA with opaque alpha
img = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
# do perspective transformation setting area outside input to transparent
# extend output size so extended by 500 all around
imgOutput = cv2.warpPerspective(img, matrix, (width+1000,height+1000), cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0,0,0,0))
# resize output, since it is too large to post
imgOutput = cv2.resize(imgOutput, (width,height))
# save the warped output
cv2.imwrite("building_warped.png", imgOutput)
# show the result
cv2.imshow("result", imgOutput)
cv2.waitKey(0)
cv2.destroyAllWindows()
注意:应该可以使用矩阵将输入角投影到输出域并计算所需的输出大小以容纳所有扭曲的输入。