2017-07-24 34 views
0

我試圖將座標從經度/緯度轉換爲像素,但在此過程中我失去了點。將座標轉換爲失去點的像素

,我正在使用的代碼如下:

from matplotlib import pyplot as plt 
import numpy as np 
import pandas as pd 

df=pd.read_csv('cords.csv') 
cords=df.as_matrix(columns=['x','y']) 
gt=[7.6445503225, 5.4065168747250134e-06, 0.0, 45.07436634583334, 0.0, -5.406516856707135e-06] 

index=np.zeros(cords.shape) 
index[:,1]=((cords[:,1] - gt[3])/gt[5]).round() 
index[:,0]=((cords[:,0] - gt[0])/gt[1]).round() 
index=index.astype(int) 
index[:,0]=index[:,0]-min(index[:,0])+1 
index[:,1]=index[:,1]-min(index[:,1])+1 
row=max(index[:,1]) 
col=max(index[:,0]) 
image=np.zeros([row+1,col+1]) 
for i in range(0,len(index)): 
    image[index[i,1],index[i,0]]=255 

如果我繪製的座標或索引點,我得到這個: enter image description here

如果我繪製我得到這個圖片: enter image description here

正如你所看到的,在將緯度/經度轉換爲像素數字時有一些缺失點。黃色是255值,紫色是0值。 這怎麼解決?

在這裏,你會發現我使用cords.csv

這裏你可以找到與需要被設置到每個像素的值的座標的座標。 cords_valus.csv

+0

什麼是'gt'在做什麼? –

+0

它是從經緯度到軸座標的轉換向量。使用GDAL獲得。 –

回答

0

當你的小數點座標被四捨五入爲圖像的整數索引時,在舍入過程中似乎有一個奇怪的...可能在底層座標中有一些週期性。無論如何,我會提供以下解決方案,使您的圖像看起來不錯,即使它不能解決問題的根源。在scipy.ndimage模塊中使用image moropholgy。

import scipy.ndimage as spim 
from skimage.morphology import square 

im = spim.binary_closing(input=image, structure=square(3)) 
+0

謝謝,但不是看起來不錯的問題。我需要在那裏放置正確的值,那不會是255,它們會被用作另一個矢量。如果你需要我可以給你正確的價值觀。無論如何Tnx的建議。 –

+0

當然,向我發送矢量數據,我會看看。我的電子郵件地址是我在Gmail上的SO用戶名。 – 2cynykyl

+0

我已經添加一個鏈接到文件的問題! TNX –

0

我不是非常自豪,因爲它感覺有點kludgey以這種方式使用字典鍵,但它似乎工作:

import scipy as sp 
import pandas as pd 
import matplotlib.pyplot as plt 

a = pd.DataFrame.from_csv('cords_value.csv') 

x = sp.array(a['x']) 
y = sp.array(a['y']) 
v = sp.array(a['value']) 

xdict = {x_: i for x_, i in zip(sp.unique(x), range(len(sp.unique(x))))} 
ydict = {y_: i for y_, i in zip(sp.unique(y), range(len(sp.unique(y))))} 

im = sp.zeros([list(xdict.values())[-1]+1, list(ydict.values())[-1]+1]) 
for i in range(len(v)): 
    im[xdict[x[i]], ydict[y[i]]] = v[i] 

plt.imshow(im, cmap=plt.cm.spectral) 

我想不出其他以浮點數(x和y)作爲索引的方式,將它們映射到整數列表(用於將值映射到圖像上)。結果看起來不錯,我認爲:

enter image description here

相關問題