Python 为像素指定新值

Python 为像素指定新值,python,r,r-raster,Python,R,R Raster,对于任何光栅数据集,我希望为最大像素值分配1,为其他行分配0 我试了很多方法,但到目前为止还没有找到任何解决办法 下面是一个示例代码: library(dplyr) library(raster) library(rgdal) ras=raster("D:/Rtool/DATA/DigitalTerrainModel/SJER2013_DTM.tif") 此光栅具有5060列和4299行。我想要的是为rowMax值指定“1”,为其他值指定“0” 我试过: df=data.frame(ras[]

对于任何光栅数据集,我希望为最大像素值分配
1
,为其他行分配
0

我试了很多方法,但到目前为止还没有找到任何解决办法

下面是一个示例代码:

library(dplyr)
library(raster)
library(rgdal)
ras=raster("D:/Rtool/DATA/DigitalTerrainModel/SJER2013_DTM.tif")
此光栅具有
5060列和4299行
。我想要的是为rowMax值指定
“1”
,为其他值指定
“0”

我试过:

df=data.frame(ras[])

summarise_each(df, funs(max(., na.rm=TRUE)))
但是,对于整个光栅高程值,我只有一个最大值,这不是我想要的

在python上,我使用了
df.eq(df.max(1),0).astype(int)

希望这有帮助

# create fake raster
fakeRaster <- raster(matrix(ncol = 10, nrow = 10))

# place values 1 to number of cells; so the right most value
# of each row should be the highest
values(fakeRaster) <- 1:ncell(fakeRaster)

# introduce higher values somewhere; test points
values(fakeRaster)[75] <- 300
values(fakeRaster)[1] <- 300
values(fakeRaster)[100] <- 300
values(fakeRaster)[81] <- 300
values(fakeRaster)[45] <- 300

# extract values as matrix
dataRaster <- values(fakeRaster, format = "matrix")

# get every row; evalute if equal to max, if yes give 1, if no give 0
# then transpose the output
dataRaster <- t(apply(dataRaster, 1, FUN = function(x) ifelse(x == max(x, na.rm = T), 1, 0) ))

# reassign value to raster
values(fakeRaster) <- dataRaster
你最终得到了一个向量(我想),这给了你一个问题。您可以尝试上面示例中使用的值(光栅,format=“matrix”)。

希望这有帮助

# create fake raster
fakeRaster <- raster(matrix(ncol = 10, nrow = 10))

# place values 1 to number of cells; so the right most value
# of each row should be the highest
values(fakeRaster) <- 1:ncell(fakeRaster)

# introduce higher values somewhere; test points
values(fakeRaster)[75] <- 300
values(fakeRaster)[1] <- 300
values(fakeRaster)[100] <- 300
values(fakeRaster)[81] <- 300
values(fakeRaster)[45] <- 300

# extract values as matrix
dataRaster <- values(fakeRaster, format = "matrix")

# get every row; evalute if equal to max, if yes give 1, if no give 0
# then transpose the output
dataRaster <- t(apply(dataRaster, 1, FUN = function(x) ifelse(x == max(x, na.rm = T), 1, 0) ))

# reassign value to raster
values(fakeRaster) <- dataRaster

你最终得到了一个向量(我想),这给了你一个问题。您可以尝试上面示例中使用的值(光栅,format=“matrix”)。

数据准备

# Load packages
library(raster)

## Create example raster layer

# create an empty raster layer
r <- raster(ncol = 10, nrow = 10)
# assign values to cells
values(r) <- 1:ncell(r)
# Inspect the raster layer
plot(r)
解决方案2:这只使用
光栅

# Initialize an empty list to store the results of each row
raster_list <- list()

# A for loop to crop the image and assign 0 or 1 to see if it is the maximum of that row

for (i in 1:nrow(r)){
  # Get the extent of each row
  temp_ext <- extent(r, r1 = i, r2 = i, c1 = 1, c2 = ncol(r))
  # Crop the raster for only one row
  temp_r <- crop(r, temp_ext)
  # Get the maximum of one row
  temp_max <- max(values(temp_r), na.rm = TRUE)
  # Use raster algebra
  temp_r[temp_r != temp_max] <- 0
  temp_r[temp_r == temp_max] <- 1

  # Save the results to the raster_list
  raster_list <- c(raster_list, temp_r)

}

# Merge all rasters
m <- do.call(merge, raster_list)

# Examine the result
plot(m)
#初始化空列表以存储每行的结果

光栅列表数据准备

# Load packages
library(raster)

## Create example raster layer

# create an empty raster layer
r <- raster(ncol = 10, nrow = 10)
# assign values to cells
values(r) <- 1:ncell(r)
# Inspect the raster layer
plot(r)
解决方案2:这只使用
光栅

# Initialize an empty list to store the results of each row
raster_list <- list()

# A for loop to crop the image and assign 0 or 1 to see if it is the maximum of that row

for (i in 1:nrow(r)){
  # Get the extent of each row
  temp_ext <- extent(r, r1 = i, r2 = i, c1 = 1, c2 = ncol(r))
  # Crop the raster for only one row
  temp_r <- crop(r, temp_ext)
  # Get the maximum of one row
  temp_max <- max(values(temp_r), na.rm = TRUE)
  # Use raster algebra
  temp_r[temp_r != temp_max] <- 0
  temp_r[temp_r == temp_max] <- 1

  # Save the results to the raster_list
  raster_list <- c(raster_list, temp_r)

}

# Merge all rasters
m <- do.call(merge, raster_list)

# Examine the result
plot(m)
#初始化空列表以存储每行的结果


光栅列表我尝试了您的代码,但所有高程值都被指定为“0”。每行都有一个最大值,当我尝试你的代码时,它取一个最大值,只有一个值决定其他值。我想要的是代码进入每一行,找到最大值并指定为1,然后其他0并对其他行执行相同操作。@omersan我已更新了我的解决方案。现在,解决方案使用包
dplyr
中的函数。请测试一下它是否有用。@ömersarı我还应用了另一个解决方案,只需要
光栅
包。谢谢。它工作得很好。我是R公司的新员工,但我仍在努力熟悉一些代码类型@mer sarıGreat。你知道在处理光栅图像时哪个解决方案更快吗?我尝试了你的代码,但所有高程值都被指定为“0”。每行都有一个最大值,当我尝试你的代码时,它取一个最大值,只有一个值决定其他值。我想要的是代码进入每一行,找到最大值并指定为1,然后其他0并对其他行执行相同操作。@omersan我已更新了我的解决方案。现在,解决方案使用包
dplyr
中的函数。请测试一下它是否有用。@ömersarı我还应用了另一个解决方案,只需要
光栅
包。谢谢。它工作得很好。我是R公司的新员工,但我仍在努力熟悉一些代码类型@mer sarıGreat。你知道在处理光栅图像时哪个解决方案更快吗?谢谢,但每行都有自己的最大值,该值必须用于指定1和其他0。我也尝试了你的代码,但与上面的答案一样,一个最大值用于与每行中的其他值进行比较,这就是为什么整个矩阵变为0。每行必须有一个最大值value@din我的回答似乎正确。它给你“1”,对应于每行的最大值,其他地方为“0”。或者你要求的是不同的东西?这就是我所要求的,但它并没有给出我所期望的结果。也许可以发布一个你的示例数据和期望的结果。@ömersarı都很好。但是,我相信它应该给出与上述代码相同的结果。如果要进行编辑,则它将位于应用中的最大值,其中na.rm=TRUE;顺便说一句,我还检查了三个代码的运行时间;代码1:1.401822秒,代码2:1.395232秒(第一个代码)代码3:0.08543992秒(本答案中的代码);但只要你找到答案,一切都好。干杯谢谢,但是每一行都有自己的最大值,这个值必须用来分配1和其他0。我也尝试了你的代码,但就像上面的答案一样,一个最大值用于与每一行中的其他值进行比较,这就是为什么整个矩阵变为0。每一行必须有一个最大值value@din我的回答似乎正确。它给你“1”,对应于每行的最大值,其他地方为“0”。或者你要求的是不同的东西?这就是我所要求的,但它并没有给出我所期望的结果。也许可以发布一个你的示例数据和期望的结果。@ömersarı都很好。但是,我相信它应该给出与上述代码相同的结果。如果要进行编辑,则它将位于应用中的最大值,其中na.rm=TRUE;顺便说一句,我还检查了三个代码的运行时间;代码1:1.401822秒,代码2:1.395232秒(第一个代码)代码3:0.08543992秒(本答案中的代码);但只要你找到答案,一切都好。干杯