0

I hope you are great!

I am very beginner to r, and I am having a hard time figuring out two things. The first one is that I need to find the number of pixels I have in an image, which I already look for, but none of the codes work out. For the second part, you will need some context. I am working on getting the ndvi -and other bands- of a furrow-irrigated rice field, which has an upper part and a lower part. I need to divide my image into these parts, the top part, the middle part, and the bottom part. But I can't find a code for that either.

Can someone help me, please?

##Attached is the image I am working on##

##Code I am using##  
library(raster)
library(cowplot)
library(tidyverse)
library(ggsn)
library(ggpubr)
library(sf)
library(ggplot2)
library(ggpubr)
library(gridExtra)
library(grid)
library(rgdal)
library(sf)

require(raster)

grey <- raster("SentFurrowField2021WB.tif")

load.image("SentFurrowField2021WB.tif")

rgb <- brick(grey)

plot(grey)

getwd()

list.files()


##number of bands in Raster

nbands(grey)

##what are the bands

bandnr(grey)


View(grey)

library(tidyverse)

band1 <- raster("SentFurrowField2021WB.tif",band =1)

plot(band1)

band1df <- as.data.frame(band1, xy = TRUE) %>% drop_na()

View(band1df)

band1

band2 <- raster("SentFurrowField2021WB.tif", band =2)

band2

plot(band2)

band2df <- as.data.frame(band2, xy = TRUE) %>% drop_na()

View(band2df)

band3 <- raster("SentFurrowField2021WB.tif", band =3)

band3

plot(band3)

band3df <- as.data.frame(band3, xy = TRUE) %>% drop_na()

View(band3df)

band4 <- raster("SentFurrowField2021WB.tif", band =4)

band4

plot(band4)

band4df <- as.data.frame(band4, xy = TRUE) %>% drop_na()

View(band4df)

#names(band2)

#names(band1)

#names(band1df)

library(raster)

#> Loading required package: sp

ggspatial::grey()

##I think this is the plot function that you are currently using
plot(grey)

#you can transform the raster to EPSG:4326 (lat/lon)

grey <- projectRaster(grey, crs = crs("+init=epsg:4326"))

plot(grey)

#you could also use ggspatial + ggplot, which keeps the projection

#but has axis labels in lat/lon

library(ggspatial)

#> Loading required package: ggplot2

ggplot() +
  layer_spatial(grey, aes(fill = stat(band1))) +
  scale_fill_continuous(na.value = NA)

#> Warning: Removed 8792 rows containing missing values (geom_raster).



  [1]: https://i.stack.imgur.com/ymg1x.png
  • Welcome to SO. Please see https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example on how to make a great example so that people easier can help you. – harre Jul 22 '22 at 15:12
  • 1
    Possible duplicate: https://stackoverflow.com/questions/18701591/how-to-compute-the-area-or-number-of-pixels-for-selected-region-in-r – harre Jul 22 '22 at 15:13

0 Answers0