[英]Find lat/long coordinates from pixel point in .geotiff using python and gdal
[英]Pixel coordinates from point A to B
我有一個灰度50 x 50 像素的圖片作為 numpy 2D 數組。
每個像素都是從左上角[0, 0]到右下角[50, 50]的坐標。
我如何獲得從點 A 到 B 的線上的每個像素的坐標,這些點可以是任何給定的像素對,即 A[19, 3] 到 B[4, 4] 或 A[3, 12] 到 B [0, 33]?
示例:從 A [ 4, 9 ] 到 B[ 12, 30 ] 的線穿過哪些像素?
提前致謝
埃沃
如果您希望這樣做,您可以插入圖像以提取線條輪廓,這樣坐標不必是整數:
from scipy.ndimage import map_coordinates
from skimage.data import coins
from matplotlib import pyplot as plt
import numpy as np
npts = 128
rr = np.linspace(30, 243, npts) # coordinates of points defined here
cc = np.linspace(73, 270, npts)
image = coins()
# this line extracts the line profile from the image
profile = map_coordinates(image, np.stack((rr, cc)))
# can visualise as
fig, ax = plt.subplots(ncols=2)
ax[0].matshow(image)
ax[0].plot(cc, rr, 'w--')
ax[1].plot(profile) # profile is the value in the image along the line
我發現有助於計算線(矢量)上窗簾點的像素值。 下面的代碼:
coordA = [0,0]
coordB = [3,4]
def intermid_pix(coorA, coorB, nb_points=8):
x_axis = (coorB[0] - coorA[0]) / (nb_points + 1)
y_axis = (coorB[1] - coorA[1]) / (nb_points + 1)
rounded = [[round(coorA[0] + i * x_axis), round(coorA[1] + i * y_axis)]
for i in range(1, nb_points + 1)]
rounded_trim = []
[rounded_trim.append(x) for x in rounded if x not in rounded_trim]
return rounded_trim
coordinates = intermed_pix(coordA, coordB, nb_points=8)
print(coordinates)
output:
[[0, 0], [1, 1], [1, 2], [2, 2], [2, 3], [3, 4]]
使用scikit-image line()像這樣:
import numpy as np
from skimage.draw import line
from skimage import io
# Create image
img = np.zeros((50, 50), dtype=np.uint8)
# Get row and col of pixels making line from (4,9) to (12,30)
rr, cc = line(4, 9, 12, 30)
print(rr)
array([ 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9, 9, 10, 10,
10, 11, 11, 12, 12])
print(cc)
array([ 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30])
# Visualise as image instead of list
img[rr, cc] = 255
io.imsave('result.jpg',img)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.