2017-05-02 35 views
1

我有一個從.tif圖像文件導入的大型柵格對象。我想在距離中心的給定徑向距離處分析這個光柵的像素,以便識別我可以在圖像中注意到的某種軸對稱現象(下圖)。爲此,我想從圖像中心提取與給定半徑(和寬度)的圓形條相交的像素值(如下圖所示)。從柵格中提取沿着定製的圓形條(恆定的徑向距離)的值

我已經探索了幾個選項來做到這一點,包括提取功能和成像器包。在imager軟件包中,您可以很容易地沿着行或列提取值,但是我找不到可以按照我需要的自定義形狀提取值的函數。

我可能會使用帶有SpatialPolygons參數的提取函數,但是爲此我必須輸入多邊形對象中所有點的位置,並且這需要非常高密度的點位置(因爲我認爲這些點,由線元素連接)。此外,我想改變提取像素值(以及後來的平均值)的條的數量密度,因此這種方法既乏味又不靈活。因此,我想知道您是否有任何建議來解決這個問題。

> str(imRaster) 
Formal class 'RasterLayer' [package "raster"] with 12 slots 
    [email protected] file :Formal class '.RasterFile' [package "raster"] with 13 slots 
    .. .. [email protected] name  : chr "C:\\Users\\Nandu\\input_images\\All\\101_1A_1000ms.tif" 
    .. .. [email protected] datanotation: chr "INT2U" 
    .. .. [email protected] byteorder : chr "little" 
    .. .. [email protected] nodatavalue : num -Inf 
    .. .. [email protected] NAchanged : logi FALSE 
    .. .. [email protected] nbands  : int 1 
    .. .. [email protected] blockrows : int 1 
    .. .. [email protected] blockcols : int 1392 
    .. .. [email protected] driver  : chr "gdal" 
    .. .. [email protected] open  : logi FALSE 
    [email protected] data :Formal class '.SingleLayerData' [package "raster"] with 13 slots 
     . 
     . 
     . 

可替換地,如果有其它的方法(比通過導入的.tif圖像光柵其他),這將允許這樣的操作,這可能也是有用的。請告訴我。

在此先感謝!

編輯:爲了使問題重現,我進一步在這裏添加了我想要使用的圖像的鏈接。它是一個TIFF圖像,並且可以從這裏

Link to the tiff image that I am trying to process in R

library(tiff) 
library(raster) 
imRaster = raster(file_path to the image) 
plot(imRaster) 
xy = cbind(684.4228, 599.0458) # Gives a rough location of the center in the image coordinates 

此代碼可運行以獲得上述的光柵和可視化它被下載。希望能幫助到你!

enter image description here

+1

這是一個有趣的問題。請提供[最小,可重現的示例](http://stackoverflow.com/help/mcve)。因此,你可能會看看[如何編寫一個好的重現R例子](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – loki

+0

謝謝@洛基。我已經添加了一個tiff圖像的鏈接和一個示例代碼,立即開始。希望能幫助到你。讓我知道是否需要其他任何東西。 – Pradical2190

回答

1

讓我們通過加載柵格和分配一個座標參考系統(CRS)開始。我們使用度量單位,以便圖像中的1個像素表示地理柵格的僞「」中的一個「米」。

library(raster) 

imRaster <- raster("~/Downloads/46-ECN300448-216.tif") 
[email protected] <- CRS("+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378137 +b=6378137 +units=m +no_defs") 

plot(imRaster, col = grey.colors(255)) 

之後,正如你所說,我們使用您的中心點xy並將其轉換爲一個SpatialPoint

xy <- cbind(684.4228, 599.0458) 
xySp <- SpatialPoints(coords = xy) 
plot(xySp, add = T) 

Image with Piont

然後我們使用rgeos包來創建圍繞該點兩個徑向緩衝區。在這個例子中,我們使用200和300像素/米。

library(rgeos) 

outer <- 300 
inner <- 200 

bufOut <- gBuffer(xySp, width = outer) 
bufIn <- gBuffer(xySp, width = inner) 

strip <- bufOut - bufIn 
plot(strip, add = T, col = "#FF000050") 

Image with buffer

最後,我們可以使用strip來掩蓋光柵圖像和計算統計數據(甚至使用原始值)與getValues()

m <- mask(imRaster, mask = strip) 

# plot(m) # plot the mask if you want to see what it looks like 

mean(getValues(m), na.rm = T) 
# [1] 17004.24 

重要提示:只有具有多邊形內的中心依然是屏蔽後的細胞。你可以通過使用緩衝區來解決這個問題。

+0

好方法。但是,我無法簡單地通過減去兩個幾何來生成條。我會使用函數'gDifference'來獲取條狀對象...是僅僅是我嗎? –

+0

@ G.Cocca,我在Windows和Ubuntu(16.04 LTS)系統上都檢查了這種方法。最新的R和軟件包版本。你有沒有試過更新你的軟件包?然而'gDifference'也有訣竅。 @ Pradical2190,這個解決方案能滿足你嗎? – loki

+1

@loki,感謝您的解決方案。它運作良好!我還想在這個答案之前注意到,我正在尋找這樣做的方法,並意識到可以通過使用光柵包本身的提取函數並使用緩衝區參數來實現。但是,您的方法具有將所選區域作爲SpatialPolygons類獲得的優點,並且稍後可以更方便地進行操作。因此我也接受了你的回答。謝謝! – Pradical2190

相關問題