[英]Affine transformation of with Python
从 MATLAB 翻译我的代码我正在尝试使用 Python 3 对 TERRA-ASTER 卫星图像进行仿射变换,
import numpy as np
import matplotlib.pyplot as plt
from skimage import transform as tf
from skimage import io, exposure
naivasha_rgb = io.imread('naivasha.tif')
使用来自四个场景角的坐标对 (src,dst) 的变换矩阵。
# upper left
# lower left
# upper right
# lower right
movingpoints = np.array([
[36.214332,-0.319922],
[36.096003,-0.878267],
[36.770406,-0.400443],
[36.652213,-0.958743]])
fixedpoints = np.array([
[0,0],
[0,4200],
[4100,0],
[4100,4200]])
使用 Skimage package 的功能
tform = tf.estimate_transform('affine',movingpoints,fixedpoints)
newnaivasha_rgb = tf.warp(naivasha_rgb,tform.inverse)
tform 与 MATLAB 中的相同,但已转置,但翘曲会创建平面绿色图像,而不是 MATLAB 中的预期图像(请参阅下面的 WeTransfer 链接)。
newnaivasha_rgb = exposure.rescale_intensity(newnaivasha_rgb,
out_range='uint8')
plt.figure()
io.imshow(newnaivasha_rgb)
plt.axis('off')
plt.show()
有任何想法吗? 根据我搜索的解决方案,数值表示低对比度或没有图像,具体取决于我使用 tform 还是 tform.inverse 和 tf.warp。
从WeTransfer下载图像 (50 MB),包括输入图像 naivasha.tif 和 output 图像 naivasha_georef_python.png 和 naivasha_georef_matlab.jpg。
感谢 rickhg12hs 的建议,我现在能够解决问题。 我将经度和纬度值转换为相对于图像的像素坐标,然后仿射变换产生预期的结果。 谢谢rickhg12hs!
import numpy as np
import matplotlib.pyplot as plt
from skimage import transform as tf
from skimage import io
#%%
naivasha_rgb = io.imread('naivasha.tif')
#%%
plt.figure()
io.imshow(naivasha_rgb)
plt.axis('off')
plt.show()
#%%
UL = [-0.319922,36.214332]
LL = [-0.878267,36.096003]
UR = [-0.400443,36.770406]
LR = [-0.958743,36.652213]
#%%
# 4200 rows correspond to the latitudes [1]
# 4100 cols correspond to the longitudes [0]
movingpoints = np.array([
[0.,4100.*(UL[1]-LL[1])/(UR[1]-LL[1])],
[4200.*(UL[0]-LL[0])/(UL[0]-LR[0]),0.],
[4200.*(UL[0]-UR[0])/(UL[0]-LR[0]),4100.],
[4200.,4100.*(LR[1]-LL[1])/(UR[1]-LL[1])]])
#%%
fixedpoints = np.array([
[0.,0.],
[4200.,0.],
[0.,4100.],
[4200.,4100.]])
#%%
fixedpoints = np.fliplr(fixedpoints)
movingpoints = np.fliplr(movingpoints)
#%%
tform = tf.estimate_transform('affine',movingpoints,fixedpoints)
#%%
newnaivasha_rgb = tf.warp(naivasha_rgb,tform)
#%%
plt.figure()
plt.imshow(np.flipud(newnaivasha_rgb),
extent=[36.096003,36.770406,-0.319922,-0.958743])
ax=plt.gca()
ax.invert_yaxis()
plt.savefig('newnaivasha_rgb.png')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.