R 语言初步

之前听说过这个,但是实际从来没有接触过。今天突然有兴趣了,于是开始摸索着写点。
会写点学习过程中发现的坑和一些经验。

2 Likes

不知道 python 是否抢了 R 的一部分饭碗(在数据科学方面) :thinking:

2 Likes

学着用了一下

实验步骤

1. 载入实验所用到的库:

library(FIELDimageR)  
library(raster)

2. 载入图片:

EX1 <- stack("EX1_RGB.tif")  
plotRGB(EX1, r = 1, g = 2, b = 3)

框选:
EX1.Crop <- fieldCrop(mosaic = EX1) # For heavy images (large, high resolution, etc.) please use: fast.plot=T

3. 旋转图片

EX1.Rotated <- fieldRotate(mosaic = EX1.Crop, clockwise = F, h=F) # h=horizontal 

4. 使用植被指数去除土壤

EX1.RemSoil <- fieldMask(mosaic = EX1.Rotated, Red = 1, Green = 2, Blue = 3, index = "HUE")

土壤的存在会在从图像中提取的数据中引入偏差。因此,从图像中去除土壤是农业科学中图像分析最重要的步骤之一。使用功能:fieldMask

5. 生成网格化的图片

EX1.Shape<-fieldShape(mosaic = EX1.RemSoil,ncols = 14, nrows = 9)
(将划分出9行14列的方格,为下一步矩阵量化植被做覆盖率做准备)
注意:地块按从左到右、从上到下的升序标识,均匀分布在选定区域内。

图示

6. 为每个区块标号、排序

DataTable<-read.csv("DataTable.csv",header = T)    

fieldMap<-fieldMap(fieldPlot=DataTable$Plot, 

fieldColumn=DataTable$Row,fieldRow=DataTable$Range, decreasing=T)  

fieldMap

7. 为"fieldShape" 加入信息 :

EX1.Shape<-fieldShape(mosaic = EX1.RemSoil, ncols = 14, nrows = 9, fieldMap = fieldMap, fieldData = DataTable, ID = "Plot")

定义PlotName:

[email protected]

8. 合并成3×7 与 7×9

9. 建立植被指数

EX1.Indices.myIndex<- fieldIndex(mosaic = EX1.RemSoil$newMosaic, Red = 1, Green = 2, Blue = 3,  index = c("NGRDI","BGI"), myIndex = c("(Red-Blue)/Green","Red/Green","Blue/Green"))

10. 使用热力图衡量生长情况

栅格函数提取通过函数适配试验数据。参数n.core用于加速绘图提取(并行处理)。

EX1.Info<- fieldInfo(mosaic = EX1.Indices,fieldShape = EX1.Shape$fieldShape)  

[email protected]  

EX1.Info<- fieldInfo(mosaic = EX1.Indices,fieldShape = EX1.Shape$fieldShape, n.core = 3)  

[email protected]  
 
##可视化
fieldPlot(fieldShape=EX1.Info$fieldShape,fieldAttribute="Yield", mosaic=EX1.Indices, color=c("white","black"), alpha = 0.5)  

fieldPlot(fieldShape=EX1.Info$fieldShape,fieldAttribute="myIndex", mosaic=EX1.Indices, color=c("red","blue"), alpha = 0.5)

最终处理结果:

1 Like