如何解决向后扭曲图像调整大小
现在,我正在尝试通过对计算出的矩阵M进行反向扭曲来转换原始图像。但是我不知道如何在反翘曲过程中确定 dst图像的高度和宽度 。这是一些细节。
1。使用SIFT算法在图像1和图像2中找到特征和描述符
image1
image2
2。在方程Ax = B中找到A,B和M矩阵
3。使用双线性插值法执行向后弯曲
4。我想使用向后警告获取的图像如下。
接下来是我的代码。 我应该如何确定高度和宽度?
import cv2
import numpy as np
import random
def L2_distance(vector1,vector2):
distance = np.sqrt(np.sum((vector1 - vector2) ** 2))
return distance
步骤1:使用SIFT查找特征点和描述符
# <main>
img1 = cv2.imread("../resources/image/building.jpg")
img2 = cv2.imread("../resources/image/building_temp.jpg")
threshold = 300
sift = cv2.xfeatures2d.SIFT_create(0)
kp1,des1 = sift.detectAndCompute(img1,None)
kp2,des2 = sift.detectAndCompute(img2,None)
distance = []
for idx_1,des_1 in enumerate(des1):
dist = []
for idx_2,des_2 in enumerate(des2):
dist.append(L2_distance(des_1,des_2))
distance.append(dist)
distance = np.array(distance)
min_dist_idx = np.argmin(distance,axis=1)
min_dist_value = np.min(distance,axis=1)
points = []
for idx,point in enumerate(kp1):
if min_dist_value[idx] >=300:
continue
x1,y1 = point.pt
x2,y2 = kp2[min_dist_idx[idx]].pt
x1 = int(np.round(x1))
y1 = int(np.round(y1))
x2 = int(np.round(x2))
y2 = int(np.round(y2))
points.append([(x1,y1),(x2,y2)])
步骤2。得到等式Ax = B中的A,B矩阵和变换矩阵M
A = []
B = []
for idx,point in enumerate(points):
A.append(list(point[0]) + [1,0])
A.append([0,0] + list(point[0]) + [1])
B.append(point[1][0])
B.append(point[1][1])
A = np.array(A)
B = np.array(B)
inverse = np.linalg.inv(np.dot(A.T,A))
X = np.dot(inverse,A.T)
X = np.dot(X,B)
M = list(X) + [0,1]
M = np.array(M).reshape((3,3))
# inverse of matrix M
M_ = np.linalg.inv(M)
Step3。我的向后翘曲方法
h,w = img1.shape[:2]
size_vec = np.dot(M,np.array([[h,w,1]]).T)
h = abs(int(size_vec[0,0]))
w = abs(int(size_vec[1,0]))
dst = np.zeros((h,3))
h_,w_ = dst.shape[:2]
for row_ in range(h_):
for col_ in range(w_):
# bilinear
vec = np.dot(M_,np.array([[col_,row_,1]]).T)
c = vec[0,0]
r = vec[1,0]
c_left = int(c) # 버림
c_right = min(int(c + 1),w - 1) # 올림
r_top = int(r) # 버림
r_bottom = min(int(r + 1),h - 1) # 올림
s = c - c_left
t = r - r_top
"""
transformed coordinates as follow
for example c = x = 1.7,r = y = 2.3
c_left = x1 = 1,c_ right = x2 = 2
r_top = y1 = 2,r_bottom = y2 = 3
| (y1,x1) (y1,x2)
|
(y',x') ----------------> |
| (y,x)
| (y2,x1) (y2,x2)
<transformed img> <original img>
"""
intensity = (1 - s) * (1 - t) * img1[r_top,c_left] \
+ s * (1 - t) * img1[r_top,c_right] \
+ (1 - s) * t * img1[r_bottom,c_left] \
+ s * t * img1[r_bottom,c_right]
dst[row_,col_] = intensity
dst = dst.astype(np.uint8)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。