用c++编写固定值截断代码 ,值为0.2以下或0.8以上给栅格状态赋为free,0.2到0.8之间为occupied
时间: 2024-02-01 21:15:38 浏览: 31
以下是一种可能的实现方式:
```c++
// 假设有一个 double 类型的数组 grid 存储了栅格的值
const double THRESHOLD_LOW = 0.2;
const double THRESHOLD_HIGH = 0.8;
for (int i = 0; i < GRID_SIZE; ++i) {
if (grid[i] < THRESHOLD_LOW || grid[i] > THRESHOLD_HIGH) {
// 固定值截断,赋值为 free
grid[i] = FREE_VALUE;
} else {
// 在阈值范围内,赋值为 occupied
grid[i] = OCCUPIED_VALUE;
}
}
```
其中,`THRESHOLD_LOW` 和 `THRESHOLD_HIGH` 分别表示固定值截断的下限和上限,`FREE_VALUE` 和 `OCCUPIED_VALUE` 分别表示 free 和 occupied 的状态值。这段代码遍历整个栅格数组,对每个栅格的值进行判断,如果超出阈值范围则赋为 free,否则赋为 occupied。
相关问题
将以下c++代码,按照value为0.2以下或0.8以上给栅格状态赋为0,value为0.2到0.8之间赋为1的思想进行修改“ for (GridMapIterator iterator(gridMap); !iterator.isPastEnd(); ++iterator) { float value = (gridMap.at(layer, *iterator) - dataMin) / (dataMax - dataMin); if (isnan(value)) value = -1; else value = cellMin + min(max(0.0f, value), 1.0f) * cellRange; size_t index = getLinearIndexFromIndex(iterator.getUnwrappedIndex(), gridMap.getSize(), false); // Reverse cell order because of different conventions between occupancy grid and grid map. occupancyGrid.data[nCells - index - 1] = value; }”
以下是修改后的代码:
```
for (GridMapIterator iterator(gridMap); !iterator.isPastEnd(); ++iterator) {
float value = (gridMap.at(layer, *iterator) - dataMin) / (dataMax - dataMin);
if (isnan(value))
value = -1;
else {
if (value < 0.2 || value > 0.8) {
value = 0;
} else {
value = 1;
}
}
size_t index = getLinearIndexFromIndex(iterator.getUnwrappedIndex(), gridMap.getSize(), false);
// Reverse cell order because of different conventions between occupancy grid and grid map.
occupancyGrid.data[nCells - index - 1] = value;
}
```
在原来的代码中,我们将`value`限制在0到1之间。现在,我们在else语句块中添加了条件语句,判断`value`是否小于0.2或大于0.8。如果是,则将栅格状态赋为0,否则将其赋为1。这样就实现了按照value为0.2以下或0.8以上给栅格状态赋为0,value为0.2到0.8之间赋为1的功能。
arcpy 栅格计算器将栅格小于0的栅格的值改为0
使用 arcpy 进行栅格计算器中的将栅格小于0的栅格的值改为0,可以使用 Con 函数。以下是一个示例代码,其中假设要对名为 "input_raster" 的栅格图层进行操作,将小于0的像元值改为0,输出结果为名为 "output_raster" 的新栅格图层。
```python
import arcpy
# 设置工作空间
arcpy.env.workspace = "C:/data"
# 定义输入和输出栅格图层
input_raster = arcpy.Raster("input_raster")
output_raster = "output_raster.tif"
# 使用 Con 函数将小于0的像元值改为0
result = arcpy.sa.Con(input_raster < 0, 0, input_raster)
# 保存结果为新的栅格图层
result.save(output_raster)
```
在上述代码中,首先使用 arcpy 的 env 模块设置工作空间,然后定义输入和输出栅格图层。接着,使用 Con 函数判断输入栅格图层中的像元值是否小于0,如果是,则将其值改为0,否则保持原值。最后,使用 save 方法将结果保存为新的栅格图层 output_raster。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)