2017-04-20 63 views
0

現在我用PIL讀取一個jpeg圖像,得到RGB的值。使用PIL(Python)在jpeg圖像中查找給定的RGB值?

雖然我可以合併所有的RGB,然後找到等於rgb給定值的寬度和高度。

有沒有更有效的方法或功能來實現這個目標?

Weather Radar Maps of USA

我的最終目標是讓這一形象,包括經緯度信息的dBZ數據。

所以第一步,我需要得到的圖像座標等於給定的RGB。

+0

你什麼意思?你有一個rgb值,你想在圖像中找到匹配的像素座標? – Dashadower

+0

你的問題不清楚 –

+0

@Dashadower是的,那是我的目標。 –

回答

0

使用NumPy的argwhere是實現您想要的簡單方法。例如,可以獲取這些像素的空間座標與RGB值[105, 171, 192]這樣的:

In [118]: from skimage import io 

In [119]: import numpy as np 

In [120]: img = io.imread('https://i.stack.imgur.com/EuHas.png') 

In [121]: ref = [105, 171, 192] 

In [122]: indices = np.argwhere(np.all(img == ref, axis=-1)) 

In [123]: indices 
Out[123]: 
array([[ 71, 577], 
     [ 79, 376], 
     [ 79, 386], 
     [ 95, 404]], dtype=int64) 

以下代碼段使其顯而易見的是,上面的結果是正確的:

import matplotlib.pyplot as plt 

fig, (ax1, ax2) = plt.subplots(1, 2) 

ax1.imshow(img) 
ax1.set_title('Original map') 
ax1.set_axis_off() 

ax2.imshow(img) 
ax2.set_title('Pixels with RGB = ' + str(ref)) 
for y, x in indices: 
    ax2.add_artist(plt.Circle((x, y), 8, color='r')) 

Pixels highlighted

+0

很好,謝謝。那麼有沒有辦法從圖像中獲取地球上的座標? –

+0

是的,絕對。您需要對圖像進行地理配準。看看[這個線程](https://gis.stackexchange.com/questions/228489/how-to-convert-image-pixel-to-latitude-and-longitude)來了解如何將圖像座標轉換爲緯度和經度。 – Tonechas

+0

對不起。我只是解決座標問題而忘記標記。-_- –