Targeting the same spot in an image for loops

19 views
Skip to first unread message

Josh Knight

unread,
Apr 4, 2020, 1:03:01 PM4/4/20
to FIELDimageR: A R package to analyze orthomosaic images from field trials.

Hello,

This package is really amazing and I think can be extremely useful to the science community especially. Amazing work and thank you for the work you have done!

Question:
I am trying to loop through a bunch of pictures and I want to target the same spot in the images to extract and analyze without hand drawing the area of interest each time. For example, target the same set square size in the center of the images through every loop.

I am not sure if there is something with the polygonShape function where you can input coordinates of a picture and it will target that through every loop.

Please let me know what you think!

Thank you very much!
Josh

fmatias

unread,
Apr 4, 2020, 2:33:35 PM4/4/20
to FIELD...@googlegroups.com

Josh,

Thank you I appreciate your email and your question.

 

Unfortunately, you can't input coordinates yet. But I believe you can use 3 different strategies:

1)      1) The whole image area will be the shapefile:

ShapeFile<-fieldPolygon(mosaic, extent=T)

 

2)      2) Select any shape format with 5 points in the center:

ShapeFile<-fieldPolygon(mosaic, nPolygon = 1, nPoint = 5)

 

3)      3) Select a specific square shape with 4 points in the center:

ShapeFile<-fieldShape(mosaic, ncols = 1, nrows = 1)

 

If you decide to use number 1, you can follow the script at the link https://github.com/filipematias23/FIELDimageR#P19

If you decide to use number 2 or 3, you should first draw one shapefile using only one image as an example and then you should use this shapefile to all other images. In other words:

 

# Images names (folder directory: "./images/")
pics
<-list.files("./images/")

# Vegetation indices
index
<- c("BGI","VARI","SCI")

# NEW PART: Same shapefile for all images (for example, using image 1 to select the center area and applying this same shapefile for all other images)
EX
.image1<-stack(paste("./images/",pics[1],sep = ""))

# Option 2:
EX
.L.Shape<-fieldPolygon(mosaic=EX. image1, nPolygon = 1, nPoint = 5)

# Option 3:
EX
.L.Shape <-fieldShape(mosaic=EX. image1, ncols = 1, nrows = 1)

############
### Loop ###
############

system
.time({ # system.time: used to compare the processing time using loop and parallel
EX
.Table.Loop<-NULL
for(i in 1:length(pics)){
  EX
.L1<-stack(paste("./images/",pics[i],sep = ""))
  plotRGB
(EX.L1)
  EX
.L2<-fieldMask(mosaic=EX.L1, index="BGI", cropValue=0.8, cropAbove=T, plot=F) # Select one index to identify leaves and remove the background
  EX
.L3<-fieldMask(mosaic=EX.L2$newMosaic, index="VARI", cropValue=0.1, cropAbove=T, plot=F) # Select one index to identify demaged area in the leaves  
  EX
.L4<-fieldIndex(mosaic=EX.L2$newMosaic, index=index, plot=F) # Indices
  EX
.L5<-stack(EX.L3$mask, EX.L4[[index]]) # Making a new stack raster with new layers (demage area and indices)
  EX
.L.Info<- fieldInfo(mosaic=EX.L5, fieldShape=EX.L.Shape$fieldShape, projection=F) # projection=F (Ignore projection. Normally used only with remote sensing images)
  plot
(EX.L5,col = grey(1:100/100))
  EX
.Table.Loop<-rbind(EX.Table.Loop, EX.L.Info$plotValue) # Combine information from all images in one table
}})

rownames
(EX.Table.Loop)<-pics
EX.Table.Loop

 

Let me know if this answers your question. Also, help me to develop more scripts showing how to use FIELDimageR and help more people.

Best,

Filipe


Josh Knight

unread,
Apr 4, 2020, 5:33:44 PM4/4/20
to FIELD...@googlegroups.com
Thank you so much Filipe!

Taking a quick look, this looks like it answered my question.
I will test this later and see if I can get this to work!
Thank you so much again. Ill try to put an example together that I think could help others.

Josh

On Sat, Apr 4, 2020 at 11:33 AM fmatias <filipem...@gmail.com> wrote:

Josh,

Thank you I appreciate your email and your question.

 

I believe you can use 3 different strategies:

1)      1) The whole image area will be the shapefile:

ShapeFile<-polygonShape(mosaic, extent=T)

 

2)      2) Select any shape format with 5 points in the center:

ShapeFile<-polygonShape(mosaic, nPolygon = 1, nPoint = 5)

 

3)      3) Select a specific square shape with 4 points in the center:

ShapeFile<-fieldShape(mosaic, ncols = 1, nrows = 1)

 

If you decide to use number 1, you can follow the script at the link https://github.com/filipematias23/FIELDimageR#P19

If you decide to use number 2 or 3, you should first draw one shapefile using only one image as an example and then you should use this shapefile to all other images. In other words:

 

# Images names (folder directory: "./images/")
pics
<-list.files("./images/")

# Vegetation indices
index
<- c("BGI","VARI","SCI")

# NEW PART: Same shapefile for all images (for example, using image 1 to select the center area and applying this same shapefile for all other images)
EX
.image1<-stack(paste("./images/",pics[1],sep = ""))

# Option 2:

EX
.L.Shape<-polygonShape(mosaic=EX. image1, nPolygon = 1, nPoint = 5)

# Option 3:
EX
.L.Shape <-fieldShape(mosaic=EX. image1, ncols = 1, nrows = 1)

############
### Loop ###
############

system
.time({ # system.time: used to compare the processing time using loop and parallel
EX
.Table.Loop<-NULL
for(i in 1:length(pics)){
  EX
.L1<-stack(paste("./images/",pics[i],sep = ""))
  plotRGB
(EX.L1)
  EX
.L2<-fieldMask(mosaic=EX.L1, index="BGI", cropValue=0.8, cropAbove=T, plot=F) # Select one index to identify leaves and remove the background
  EX
.L3<-fieldMask(mosaic=EX.L2$newMosaic, index="VARI", cropValue=0.1, cropAbove=T, plot=F) # Select one index to identify demaged area in the leaves  

  EX
.L4<-indices(mosaic=EX.L2$newMosaic, index=index, plot=F) # Indices
  EX
.L5<-stack(EX.L3$mask, EX.L4[[index]]) # Making a new stack raster with new layers (demage area and indices)
  EX
.L.Info<- getInfo(mosaic=EX.L5, fieldShape=EX.L.Shape$fieldShape, projection=F) # projection=F (Ignore projection. Normally used only with remote sensing images)
  plot
(EX.L5,col = grey(1:100/100))
  EX
.Table.Loop<-rbind(EX.Table.Loop, EX.L.Info$plotValue) # Combine information from all images in one table
}})

rownames
(EX.Table.Loop)<-pics
EX.Table.Loop


 

Let me know if this answers your question. Also, help me to develop more scripts showing how to use FIELDimageR and help more people.

Best,

Filipe


--
You received this message because you are subscribed to the Google Groups "FIELDimageR: A R package to analyze orthomosaic images from field trials." group.
To unsubscribe from this group and stop receiving emails from it, send an email to FIELDimageR...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/FIELDimageR/1791be7f-e9e6-4c23-99f8-472ace3f5fe6%40googlegroups.com.


--
Reply all
Reply to author
Forward
0 new messages