Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
警告消息couldBeLonLat(x):CRS在R中使用crop功能时为NA_R_Crop_Raster - Fatal编程技术网

警告消息couldBeLonLat(x):CRS在R中使用crop功能时为NA

警告消息couldBeLonLat(x):CRS在R中使用crop功能时为NA,r,crop,raster,R,Crop,Raster,我想将256x256像素的图像裁剪成许多64x64像素的小图像 我使用以下功能: crop(raster,extent(xmin,xmax,ymin,ymax)) 我得到了我想要的64x64像素的图像,但有一个白色边框,这意味着裁剪后的图像被压缩为64x64像素 它来自这个警告信息,但我不知道如何避免它 Warning message: In couldBeLonLat(x) : CRS is NA. Assuming it is longitude/latitude 这是我的代码: #l

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

我使用以下功能:

crop(raster,extent(xmin,xmax,ymin,ymax))
我得到了我想要的64x64像素的图像,但有一个白色边框,这意味着裁剪后的图像被压缩为64x64像素

它来自这个警告信息,但我不知道如何避免它

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()
  }
}

如何解决它?

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

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()
  }
}