在r中计数相同值的像素

发布于 2025-01-29 10:39:44 字数 142 浏览 4 评论 0原文

我需要在R中计算相同值的像素的帮助。我正在做一个主论文,其中一部分是在洪水之前和之后计算一个区域的NDWI,因此我可以计算出水下的表面。从高于0之后的洪水值之后的NDWI的结果中,在水下,低于0的结果不在。因此,我有点卡在这里BCS,我不知道如何计算高于值0的所有像素。

I need help with counting pixels of the same value in R. I'm doing a master thesis and part of it is calculating the NDWI of an area before and after floods so I can calculate the surface under water. From the results of NDWI after the flood values that are above 0 are under water and those under 0 are not. So I'm kinda stuck here bcs I can't figure out how to calculate all pixels above value 0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花间憩 2025-02-05 10:39:44

使用您的Dropbox数据:

library(terra)
ndwi <- rast('Clip_ndwi_poslije1.tif')
ndwi_vec <- values(ndwi, mat = FALSE)
> length(which(ndwi_vec > 0))
[1] 21157
> length(which(ndwi_vec <= 0))
[1] 13217
> dim(ndwi)[1]
[1] 220
> dim(ndwi)[1]*dim(ndwi)[2]
[1] 58300
> sum(length(which(ndwi_vec > 0)), length(which(ndwi_vec <= 0)))
[1] 34374
> table(is.na(ndwi[])) #  see below

FALSE  TRUE 
34374 23926 

表方法来自 sof-gis?s ,让您开始。

Using your dropbox data:

library(terra)
ndwi <- rast('Clip_ndwi_poslije1.tif')
ndwi_vec <- values(ndwi, mat = FALSE)
> length(which(ndwi_vec > 0))
[1] 21157
> length(which(ndwi_vec <= 0))
[1] 13217
> dim(ndwi)[1]
[1] 220
> dim(ndwi)[1]*dim(ndwi)[2]
[1] 58300
> sum(length(which(ndwi_vec > 0)), length(which(ndwi_vec <= 0)))
[1] 34374
> table(is.na(ndwi[])) #  see below

FALSE  TRUE 
34374 23926 

Table approach is from SOF-GIS ?s, and get you started.

闻呓 2025-02-05 10:39:44

您的数据

library(terra)
r <- rast("Clip_ndwi_poslije1.tif")

可以用大于零(或不)的值计数单元格:

freq(r > 0)
#     layer value count
#[1,]     1     0 13217
#[2,]     1     1 21157

true == 1,因此您拥有21,157个单元格,值大于零。

但是要准确地计算这些单元格所覆盖的区域,您需要首先获取每个单元的区域:

a <- cellSize(r)

然后

b <- ifel(r > 0, a, NA)
# or the equivalent
# b <- mask(a, r>0, maskvalue=0)

global(b, "sum", na.rm=TRUE)
#            sum
#ndwiii 19013036

在这种情况下总和这些区域,这类似于(错误)假设 naminal naminal 空间分辨率对每个网格单元都是有效的。

global(r > 0, "sum", na.rm=TRUE) * prod(res(r))
#             sum
#ndwiii 19041300

Your data

library(terra)
r <- rast("Clip_ndwi_poslije1.tif")

You could count the cells with values that are larger than zero (or not) like this:

freq(r > 0)
#     layer value count
#[1,]     1     0 13217
#[2,]     1     1 21157

TRUE == 1, so you have 21,157 cells with a value larger than zero.

But to accurately compute the area covered by these cells, you need to first get the area of each cell:

a <- cellSize(r)

And then sum these areas

b <- ifel(r > 0, a, NA)
# or the equivalent
# b <- mask(a, r>0, maskvalue=0)

global(b, "sum", na.rm=TRUE)
#            sum
#ndwiii 19013036

In this case, that is similar to (incorrectly) assuming that the nominal spatial resolution is valid for each grid cell.

global(r > 0, "sum", na.rm=TRUE) * prod(res(r))
#             sum
#ndwiii 19041300
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文