简体   繁体   English

如何计算R中选定区域的像素面积或像素数?

[英]How to compute the area or number of pixels for selected region in R?

I am drawing a region over a raster files but I need to know how many pixels are covered in this box (by means the area): the raster file is 1440 pixels*720 lines``(25km*25km) . 我正在栅格文件上绘制一个区域,但我需要知道此框中覆盖了多少像素(通过面积表示):栅格文件为1440 pixels*720 lines``(25km*25km)

example: 例:

   saf <- stack(system.file("external/rlogo.grd", package="raster")) 
    plotRGB( saf )
    e <- drawExtent()

So after this I drew e as a box but how many pixels/how much is the area? 因此,在此之后我将e绘制为一个框,但是面积为多少像素/多少? Thanks 谢谢

Try using raster::crop ... 尝试使用raster::crop ...

crop(saf , e )
#class       : RasterBrick 
#dimensions  : 40, 50, 2000, 3  (nrow, ncol, ncell, nlayers)
#resolution  : 1, 1  (x, y)
#extent      : 23, 73, 26, 66  (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=merc 
#data source : in memory
#names       : red, green, blue 
#min values  :   0,     0,    0 
#max values  : 255,   255,  255

And if you just want the number of cells... 如果您只想要细胞数量...

ncell( crop(saf , e ) )
#[1] 2000

And to eliminate NA.... 并消除NA。

x <- crop( saf , e )
ncell( ! is.na(x[]) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM