import numpy as np
import matplotlib.image as mtimg
import matplotlib.pyplot as mtplot
def get_image_size(src):
### give the corner points of the object in the image
### return the points of the scanned image
(tl, tr, br, bl) = src
# compute the width of the new image, which will be the
# maximum distance between bottom-right and bottom-left
# x-coordiates or the top-right and top-left x-coordinates
widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
maxWidth = max(int(widthA), int(widthB))
# compute the height of the new image, which will be the
# maximum distance between the top-right and bottom-right
# y-coordinates or the top-left and bottom-left y-coordinates
heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
maxHeight = max(int(heightA), int(heightB))
# now that we have the dimensions of the new image, construct
# the set of destination points to obtain a "birds eye view",
# (i.e. top-down view) of the image, again specifying points
# in the top-left, top-right, bottom-right, and bottom-left
# order
dst = np.array([
[0, 0],
[maxWidth - 1, 0],
[maxWidth - 1, maxHeight - 1],
[0, maxHeight - 1]])
return dst
Функция get_image_size возвращает точку dst
def four_point_transform(src, dst):
# compute the perspective transform matrix and then apply it
# the transform is found by solving ax = b
# for perspective projection, the 3x3 matrix has 8 unknows
# [t11 t12 t13] [s1] [v1]
# T = [t21 t22 t23] [s2] = [v2]
# [t31 t32 1 ] [ 1] [v3]
#
# where d1 = v1/v3 and d2 = v2/v3.
#
# t11*s1 + t12*s2 + t13 = v1 = d1*v3 = d1*(t31*s1+t32*s2+1)
# t21*s1 + t22*s2 + t23 = v2 = d2*v3 = d2*(t31*s1+t32*s2+1)
# t31*s1 + t32*s2 + 1 = v3
A = np.zeros((8, 8))
b = np.zeros((8, 1))
# assigning values
# ******* your code here ********
# now put the solution x into a 3x3 matrix T
# the t33 element = 1
x = np.linalg.solve(A, b)
x = np.concatenate((x, [[1]]), axis=0)
T = x.reshape(3,3)
# We need the inverse transformation, so R = np.inv(T)
R = np.linalg.inv(T)
# the r33 should be 1, so normalize it.
R = R/R[2][2]
return R
Функция four_point_transform заключается в том, что я хочу найти матрицу перспективы
def perspective_projection(size, image, R):
### Convert the original 3D image to 2D
# img is the dst picture
img = np.zeros((size[0], size[1], 3))
[w, h, c] = np.array(image).shape
v = np.zeros((3,1))
v[2] = 1
# transformation is done by finding the 'conor' of points in dst picture
# *********** your code here *************
warped = img.astype(np.uint8)
# return the warped image
return warped
Функция перспективного проецирования, которую я хочуполучить деформированное изображение
Как выполнить функцию "four_point_transform" и функцию перспективного проецирования, чтобы получить деформированное изображение?
Это оригинальное изображение Это моя цель