在 Python 中从一个坐标系到另一个坐标系的图像变换
Image transformation from one coordinate system into another in Python
我有一张尺寸为 (1280, 1792, 4) 的图片,是一张天气预报图。据说左上角对应地理坐标纬度:55.78,经度:-135.0,右下角对应的是纬度:21.94,经度:-67.5。例如这张地图上的一个地理点(纬度:35.78,经度:-100.0)我想知道原始图片上的像素坐标是多少。
我试着在上面画了一个点,一切顺利,但我需要知道原始图像上的原始像素坐标是多少...
fig, ax = plt.subplots()
weather_coordinates = json.load(open('weather.json'))
ax.scatter(-70.935242, 40.730610, edgecolors='red', linewidths=2, zorder=2)
ax.imshow(
mpimg.imread('weather.png'),
extent=(
weather_coordinates.get('top_left').get('longitude'),
weather_coordinates.get('bottom_right').get('longitude'),
weather_coordinates.get('bottom_right').get('latitude'),
weather_coordinates.get('top_left').get('latitude')),
zorder=1)
plt.show()
您可以使用 numpy 的 interp() 函数来实现这一点。它在第一个参数中获取要转换的值,然后是输入范围和输出范围作为数组,因此 interp(100, [0, 200], [500, 1000]) 将 return 750。
由于您同时需要 y 和 x 像素值,因此需要像这样调用它两次:
from numpy import interp
# the range of the latitudes and longitudes
latRange = [21.94, 55.78] # flipped from descending to ascending
lonRange = [-135.0, -67.5]
# the range of y and x pixels
yRange = [0, 1280]
xRange = [0, 1792]
lat = 35.78
lon = -100.0
yPixel = yRange[1] - interp(lat, latRange, yRange) # flipped again
xPixel = interp(lon, lonRange, xRange)
请注意,由于该函数仅适用于增加范围,因此我们必须将纬度范围 [55.78, 21.94] 翻转为 [21.94, 55.78],然后从最大纬度中减去 yPixel 以获得正确答案.
我有一张尺寸为 (1280, 1792, 4) 的图片,是一张天气预报图。据说左上角对应地理坐标纬度:55.78,经度:-135.0,右下角对应的是纬度:21.94,经度:-67.5。例如这张地图上的一个地理点(纬度:35.78,经度:-100.0)我想知道原始图片上的像素坐标是多少。
我试着在上面画了一个点,一切顺利,但我需要知道原始图像上的原始像素坐标是多少...
fig, ax = plt.subplots()
weather_coordinates = json.load(open('weather.json'))
ax.scatter(-70.935242, 40.730610, edgecolors='red', linewidths=2, zorder=2)
ax.imshow(
mpimg.imread('weather.png'),
extent=(
weather_coordinates.get('top_left').get('longitude'),
weather_coordinates.get('bottom_right').get('longitude'),
weather_coordinates.get('bottom_right').get('latitude'),
weather_coordinates.get('top_left').get('latitude')),
zorder=1)
plt.show()
您可以使用 numpy 的 interp() 函数来实现这一点。它在第一个参数中获取要转换的值,然后是输入范围和输出范围作为数组,因此 interp(100, [0, 200], [500, 1000]) 将 return 750。 由于您同时需要 y 和 x 像素值,因此需要像这样调用它两次:
from numpy import interp
# the range of the latitudes and longitudes
latRange = [21.94, 55.78] # flipped from descending to ascending
lonRange = [-135.0, -67.5]
# the range of y and x pixels
yRange = [0, 1280]
xRange = [0, 1792]
lat = 35.78
lon = -100.0
yPixel = yRange[1] - interp(lat, latRange, yRange) # flipped again
xPixel = interp(lon, lonRange, xRange)
请注意,由于该函数仅适用于增加范围,因此我们必须将纬度范围 [55.78, 21.94] 翻转为 [21.94, 55.78],然后从最大纬度中减去 yPixel 以获得正确答案.