计算二进制图像的面积

huangapple go评论88阅读模式
英文:

Calculate area from binary image

问题

抱歉,我只会为您提供代码的翻译部分:

提前说明一下,我无法提供一个最小可行示例,因为我不知道该怎么做。
我有一个二进制掩模在owin中。基本上,我的数据包含五个不同大小的成团点。我试图获取它们的面积。

据我所知,伟大的spatstat包没有此功能。

首先,我将owin转换为raster图层,使用`raster_mask <- raster(as.im(dd))`。
之后,我按如下方式检测成团点:
```R
library(igraph)
clusters <- clump(raster_mask)

现在我可以绘制clusters,每个成团点都有自己的颜色。
就我所理解的而言,area(clusters)函数应该提供它们的面积,但我收到了一个警告

该函数仅适用于具有经度/纬度坐标的Raster*对象

为了进行控制,我使用了crs(clusters),看到了一个NA。所以我用以下代码设置了crs:

crs(clusters) <- "+proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs +type=crs"

现在我有一个CRS,但仍然收到相同的警告。此时我的数据如下所示:

> print(areas)
class      : RasterLayer 
dimensions : 135, 129, 17415  (nrow, ncol, ncell)
resolution : 0.9790567, 1.224028  (x, y)
extent     : 490715.5, 490841.8, 5429337, 5429502  (xmin, xmax, ymin, ymax)
crs        : +proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs +type=crs 
source     : memory
names      : layer 
values     : 1.198393, 1.198393  (min, max)

所以我尝试将CSR更改为longlat。
在寻找成团点之前,使用以下代码并收到一个错误。

> crs(raster_mask) <- "+proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs +type=crs"
> raster_mask <- projectExtent(raster_mask, "+proj=longlat +datum=WGS84")
> plot(raster_mask)
Error in .plotraster2(x, col = col, maxpixels = maxpixels, add = add,  : 
  no values associated with this RasterLayer

我可能完全错误了。如果有人有提供最小工作示例的想法,我将不胜感激。我希望有人能帮助我从二进制图像掩模中获取面积。欢迎任何有关定位的帮助!

提前感谢您!


<details>
<summary>英文:</summary>

Ahead, I&#39;m not able to provide a minimum working example since I don&#39;t know how.
I have an binary mask in an owin. Basically, my data contains five clumped dots of different size. I try to get their area.

As far as I know, the great spatstat package has no function for this.

First, I convert the owin to an raster layer with `raster_mask &lt;- raster(as.im(dd))`.
After that, I detect the clumps as follows:

library(igraph)
clusters <- clump(raster_mask)

Now I can plot `clusters` with each of the five clusters in its own colour. 
And as far as I understand the function `area(clusters)` should provide me with their area, but instead I receive an warning 
&gt;This function is only useful for Raster* objects with a longitude/latitude coordinates

For control, I use `crs(clusters)` and see an NA. So I set crs with

crs(clusters) <- "+proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs +type=crs"

Now I have an CRS, but receive the same warning. My data at this points looks as follows:

> print(areas)
class : RasterLayer
dimensions : 135, 129, 17415 (nrow, ncol, ncell)
resolution : 0.9790567, 1.224028 (x, y)
extent : 490715.5, 490841.8, 5429337, 5429502 (xmin, xmax, ymin, ymax)
crs : +proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs +type=crs
source : memory
names : layer
values : 1.198393, 1.198393 (min, max)


So I try to change the CSR to longlat.
Before I search for the clumps, using this code and receiving an error.

> crs(raster_mask) <- "+proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs +type=crs"
> raster_mask <- projectExtent(raster_mask, "+proj=longlat +datum=WGS84")
> plot(raster_mask)
Error in .plotraster2(x, col = col, maxpixels = maxpixels, add = add, :
no values associated with this RasterLayer

I might be completely wrong. If someone has an idea how to provide a minimal working example I would be glad to provide it. I hope someone could help me to receive areas from an binary image mask. Any help for orientation is welcome!

Thank you ahead!


</details>


# 答案1
**得分**: 2

在`spatstat`中,您可以使用`connected`函数来识别连接的组件。

如果`dd`是您的原始二进制掩模(类别为`"owin"`的对象),那么`P <- connected(dd)`将返回一个具有分类值的像素图像`P`。每个分类值对应一个连接的组件。对此图像进行绘制,`plot(P)`,将显示原始数据的彩色版本,其中每个连接的组件都用不同的颜色填充。

您可以从中计算每个连接的组件的面积。一种简洁的方法是将连接的组件视为镶嵌中的瓷砖。
示例:

```R
library(spatstat)
## 创建示例数据
dd <- dilation(redwood, 0.5, polygonal=FALSE)
## 找到连接的组件
P <- connected(dd)
## 转换为镶嵌
B <- tess(image=P)
## 计算每个瓷砖的面积
answer <- tile.areas(B)
英文:

> As far as I know, the great spatstat package has no function for this.

In spatstat you can use the function connected to identify the connected components.

If dd is your original binary mask (object of class &quot;owin&quot;), then
P &lt;- connected(dd) returns a pixel image P with categorical values. Each categorical value corresponds to a connected component. A plot of this image, plot(P), would show a coloured version of the original data, with each connected component filled with a different colour.

You can calculate the areas of each connected component from this. A neat way is to treat the connected components as the tiles in a tessellation.
Example:

library(spatstat)
## create example data
dd &lt;- dilation(redwood, 0.5, polygonal=FALSE)
## find connected components
P &lt;- connected(dd)
## convert to a tessellation
B &lt;- tess(image=P)
## compute area of each tile
answer &lt;- tile.areas(B)

huangapple
  • 本文由 发表于 2023年7月4日 23:16:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定