我需要将imageA
投影到imageB
。我编写了以下代码来做到这一点。
函数project
将imageA
投射到imageB
上。它接受三个参数:
下面的代码很慢,我不确定是否有更好的方法可以做到这一点。同时也会显示错误消息libpng error: Invalid IHDR data
def project(imageA, imageB, homography):
x0=0
y0=0
h,w=imageB.shape[0],imageB.shape[1]
H = np.linalg.inv(homography)
combined=imageB.copy()
# xs = zip(*markers)[0]
# ys = zip(*markers)[1]
for i in range(x0, x0+w):
for j in range(y0, y0+h):
# x_prime, y_prime=markers[j][0], markers[j][1]
# x_prime, y_prime=markers[0][0]+i, markers[0][1]+j
x_prime, y_prime=i, j
# The target point
p_prime = np.array([[x_prime],[y_prime],[1]])
# The source point based on inverse homography
p = np.dot(H, p_prime)
p = p/p[-1]
if ((p[0] >=0) & (p[0] <= imageA.shape[1])) & ((p[1] >= 0) & (p[1] <= imageA.shape[0])):
chk = bilinear_interpolate(imageA,p[0], p[1])
combined[y_prime,x_prime] = chk
else:
continue
def bilinear_interpolate(im, x, y):
x = np.asarray(x)
y = np.asarray(y)
x0 = np.floor(x).astype(int)
x1 = x0 + 1
y0 = np.floor(y).astype(int)
y1 = y0 + 1
x0 = np.clip(x0, 0, im.shape[1]-1);
x1 = np.clip(x1, 0, im.shape[1]-1);
y0 = np.clip(y0, 0, im.shape[0]-1);
y1 = np.clip(y1, 0, im.shape[0]-1);
Ia = im[ y0, x0 ]
Ib = im[ y1, x0 ]
Ic = im[ y0, x1 ]
Id = im[ y1, x1 ]
wa = (x1-x) * (y1-y)
wb = (x1-x) * (y-y0)
wc = (x-x0) * (y1-y)
wd = (x-x0) * (y-y0)
return wa*Ia + wb*Ib + wc*Ic + wd*Id
我不想在OpenCV中使用现有的投影方法