警告消息mayBeLonLat(x):使用R中的裁剪功能,CRS为NA

问题描述 投票:0回答:1

我想将256x256像素的图像裁剪为许多64x64像素的小图像。

enter image description here

我使用该功能:

crop(raster,extent(xmin,xmax,ymin,ymax))

我可以根据需要获得64x64像素的图像,但是有白色边框,这意味着裁剪后的图像被“压缩”为64x64像素。

enter image description here

来自此警告消息,但我不知道如何避免它。

Warning message:

In couldBeLonLat(x) : CRS is NA. Assuming it is longitude/latitude

这里是我的代码:

#load picture
setwd("C:/Users/PC/Desktop/R/Image test/New folder/")
img_path <- "Test6.jpg"
raster <- brick(img_path, package="raster", full.names=TRUE)

size = 64
gap <- 8

#number of time to paste image
number_im_h <- dim(raster)[2]/gap-(size/gap)+1
number_im_v <- dim(raster)[1]/gap-(size/gap)+1

r_list <- list()

ind <-0

for(k in 1:number_im_h){
  for(j in 1:number_im_v){
    ind <- ind+1
    r_list[[ind]] <- crop(raster,extent(as.numeric(j-1)*gap,as.numeric(j-1)*gap+size,as.numeric(k-1)*gap,as.numeric(k-1)*gap+size))
    setwd("C:/Users/PC/Desktop/R/Image test/cropped")
    slice <- r_list[[ind]]
    jpeg(filename=paste0("image_",ind,".jpg"), width=size,height=size,units="px")
    plotRGB(slice)
    dev.off()
  }
}

如何解决?

r crop raster
1个回答
0
投票

这里是一个稍微改写的版本,有点简单,并显示警告应如何消失。为了获得更好的帮助,您应该提供示例数据集(图片)

library(raster)
raster <- brick("Test6.jpg")
# set the crs so that plot knows the data are not lonlat
crs(raster) <- "+proj=utm +zone=1 +datum=WGS84"

size = 64
gap <- 8
n_h <- ncol(raster)/gap - (size/gap)+1
n_v <- nrow(raster)/gap - (size/gap)+1

r_list <- list()
path <- "C:/Users/PC/Desktop/R/Image test/cropped"
ind <- 0
for(k in 1:n_h){
  for(j in 1:n_v){
    ind <- ind+1
    e <- extent((j-1)*gap, (j-1)*gap+size, (k-1)*gap, (k-1)*gap+size)
    r_list[[ind]] <- crop(raster, e)
    f <- file.path(path, paste0("image_",ind,".jpg"))
    jpeg(filename=f, width=size, height=size, units="px")
    plotRGB(r_list[[ind]])
    dev.off()
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.