How can I accurately convert the remote sensing image into a matrix in Rstudio?

library(raster)
#> 载入需要的程辑包:sp
library(rgdal)
#> rgdal: version: 1.5-23, (SVN revision 1121)
#> Geospatial Data Abstraction Library extensions to R successfully loaded
#> Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
#> Path to GDAL shared files: C:/Users/liuyi/Documents/R/win-library/4.1/rgdal/gdal
#> GDAL binary built with GEOS: TRUE 
#> Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
#> Path to PROJ shared files: C:/Users/liuyi/Documents/R/win-library/4.1/rgdal/proj
#> PROJ CDN enabled: FALSE
#> Linking to sp version:1.4-5
#> To mute warnings of possible GDAL/OSR exportToProj4() degradation,
#> use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
#> Overwritten PROJ_LIB was C:/Users/liuyi/Documents/R/win-library/4.1/rgdal/proj
library(remote)
#> 载入需要的程辑包:Rcpp
library(rayshader)
library(oceanmap)
#> Warning: 程辑包'oceanmap'是用R版本4.1.1 来建造的
#> 载入需要的程辑包:maps
#> 载入需要的程辑包:mapdata
#> 载入需要的程辑包:extrafont
#> Registering fonts with R

setwd("D:/nf11")
r1<-raster("D:/nf11/20160207.tif")
r2<-raster("D:/nf11/20161020.tif")
r3<-raster("D:/nf11/20161223.tif")
e<-raster::extent(236400,468600,3553200,3789900)
r1_crop<-raster::crop(r1,e)
r2_crop<-raster::crop(r2,e)
r3_crop<-raster::crop(r3,e)
r_r1_crop<-raster_to_matrix(r1_crop)
r_r2_crop<-raster_to_matrix(r2_crop)
r_r3_crop<-raster_to_matrix(r3_crop)
r11<-raster2matrix(r1_crop)
r111<-as.matrix(r1_crop)

Created on 2021-08-31 by the reprex package (v2.0.1)

The matrix values ​​I converted after trying the above are all NA
Related issues .tif files

Hello @yiboliu ,
welcome in the community.
I hope that you already have an answer to your question.
If so please answer your own question and indicate it as solved.
If not we can only redo your analysis if you provide us a reproducible example .
By providing us a small tif file (e.g. a data file from one of the packages) that leads to the same problem, you make it easier for us to help you.

Dear @HanOostdijk
Thank you for your enthusiastic help. I have re-modified the code according to reprex and attached the relevant tif file link

I deleted the post above because the reprex I provided gave different results than my standard session.
I will come back on this.

Editted with info about package tiff.

I downloaded the tif-file and I can view its contents (a rotated rectangle) in a browser.
However, when I use your code (see the reprex output below) I get an error saying the raster function can not create a RasterLayer object.
This is different from your situation so maybe the file got corrupted during transport (??) .
I assume you had no messages as a result of using the raster function (? )
What is the result of summary(r1)?
Not sure how to continue.

library(raster)
#> Warning: package 'raster' was built under R version 4.1.1
#> Loading required package: sp
library(rgdal)
#> rgdal: version: 1.5-23, (SVN revision 1121)
#> Geospatial Data Abstraction Library extensions to R successfully loaded
#> Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
#> Path to GDAL shared files: D:/tools/R/Packages/rgdal/gdal
#> GDAL binary built with GEOS: TRUE 
#> Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
#> Path to PROJ shared files: D:/tools/R/Packages/rgdal/proj
#> PROJ CDN enabled: FALSE
#> Linking to sp version:1.4-5
#> To mute warnings of possible GDAL/OSR exportToProj4() degradation,
#> use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
#> Overwritten PROJ_LIB was D:/tools/R/Packages/rgdal/proj

setwd("D:/data/R/RStudio_Community")
list.files(path=".",pattern = "*.tif") 
#> [1] "20160207.tif"
r1<-raster("./20160207.tif") 
#> Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create a RasterLayer object from this file.

e<-raster::extent(236400,468600,3553200,3789900)
r1_crop<-raster::crop(r1,e)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'crop': object 'r1' not found
r_r1_crop<-raster_to_matrix(r1_crop)
#> Error in raster_to_matrix(r1_crop): could not find function "raster_to_matrix"
r11<-raster2matrix(r1_crop)
#> Error in raster2matrix(r1_crop): could not find function "raster2matrix"
r111<-as.matrix(r1_crop)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'as.matrix': object 'r1_crop' not found
Created on 2021-08-31 by the reprex package (v2.0.0)

It was possible for me to read the file with the package tiff :

library(tiff)
setwd("D:/data/R/RStudio_Community")
list.files(path=".",pattern = "*.tif") 
#> [1] "20160207.tif"

r1<-readTIFF("./20160207.tif") 
#> Warning in readTIFF("./20160207.tif"): TIFFReadDirectory: Unknown field with tag
#> 33550 (0x830e) encountered
#> Warning in readTIFF("./20160207.tif"): TIFFReadDirectory: Unknown field with tag
#> 33922 (0x8482) encountered
#> Warning in readTIFF("./20160207.tif"): TIFFReadDirectory: Unknown field with tag
#> 34735 (0x87af) encountered
#> Warning in readTIFF("./20160207.tif"): TIFFReadDirectory: Unknown field with tag
#> 34737 (0x87b1) encountered
#> Warning in readTIFF("./20160207.tif"): TIFFReadDirectory: Unknown field with tag
#> 42113 (0xa481) encountered
class(r1)
#> [1] "matrix" "array"
dim(r1) 
#> [1] 7891 7751
Created on 2021-08-31 by the reprex package (v2.0.0)

Dear @HanOostdijk
Thank you again for your enthusiastic help. I tried again and found that the matrix converted after reinstalling the R package became the correct matrix. I am very happy to communicate with you on the R language. I am a beginner of the R language. I hope I can consult you in the future

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.