[datatable-help] Create a new window

swright71234 swright7 at uga.edu
Wed Jun 29 15:18:02 CEST 2016


Hello all, 

I am trying to perform image analysis with the following code. The code
works fine and is not the issue here. I would like for the completed images
to pop-up in a new and bigger when each image goes through the analysis.
Does anyone have any suggestions? 


par(mfrow=c(2,1))
for (i in 1: length(my_files))
{
  img <- brick(my_files[i])
  #crop the photo using previously defined parameters
  #my_img2 <- crop(img, my_extent)
  my_img2<-img
  #change band names(to make it easier to code)
  names(my_img2) <- c("b1", "b2", "b3")
  #remove the trend from the flash for each band ----------------------
  #define coordinates from the image
  x <- coordinates(my_img2)[,1]
  y <- coordinates(my_img2)[,2]
  #Regression for each layer, removing the polynomial trend
  #band1
  reg <- lm(my_img2$b1[] ~ x+ y+I(x^2)+I(y^2) )
  b <- coefficients(reg)
  my_img2$pred <- b[1] + b[2] *  x+ b[3]* y+ b[4]*x^2+b[5]*y^2
  my_img2$b1   <- my_img2$b1[] - my_img2$pred[]
  #band2
  reg <- lm(my_img2$b2[] ~ x+ y+I(x^2)+I(y^2) )
  b <- coefficients(reg)
  my_img2$pred <- b[1] + b[2] *  x+ b[3]* y+ b[4]*x^2+b[5]*y^2
  my_img2$b2   <- my_img2$b2[] - my_img2$pred[]
  #band3
  reg <- lm(my_img2$b3[] ~ x+ y+I(x^2)+I(y^2) )
  b <- coefficients(reg)
  my_img2$pred <- b[1] + b[2] *  x+ b[3]* y+ b[4]*x^2+b[5]*y^2
  my_img2$b3   <- my_img2$b3[] - my_img2$pred[]
  #
  my_img2 <-dropLayer(my_img2, "pred")
  my_img2 <- improve_contrast(my_img2)
  #
  #Plot 2 images so user can see the original plus the added
  #The aspect is changed to make it easier to click on the knots
  par(mfrow=c(2,1))
  plotRGB(my_img2,asp=3)
  plotRGB(my_img2,asp=3)
  
  print("Input 0 and hit Enter for no knots or Enter if knots")
  numberknots <- readline(paste("Knots for ", my_files[i],": ", sep=' '))
  #If knotsforeachpiece = 0 we can skip labeling the knots
  if(numberknots==0){
    #Add to dataframe that the piece didn't have any knots
    lknots   <- data.frame(board = my_files[i], x = NA, y = NA,area=NA)
    lknots      <- as.data.frame(lknots)
    my_knots <- rbind(my_knots, lknots)
    name<-my_files[i]
    name<-gsub(".tif","",name)
    
    #Write the images
    #image(my_img2)
    #my_img2
    #png(paste(name,"_Knots", ".png", sep=''))
    #par(mfrow=c(1,1))
    #plotRGB(my_img2)
    #plot(my_img2.poly, add = TRUE, col = "red", border ="red")
    #dev.off()
    
    
    #Copy file when done
    #file.copy(my_files[i], "Complete")
  }
  else   {
    #Identify clusters
    my_img2.mtrx <- as.matrix(my_img2)
    clusters <- kmeans(my_img2.mtrx, centers=7)
    my_img2$cl2 <- fitted(clusters)[,1]
    my_img2$cl  <- clusters$cluster
    mV <- minValue(my_img2$cl2)
    #pknots will be identified when cluster is equal to minimum value,
otherwise not a knot
    my_img2$pknots <- ifelse(my_img2$cl2[] == mV,1,NA)
    my_img2.poly <- rasterToPolygons(my_img2$pknots, dissolve = TRUE)
    my_img2.poly <- disaggregate(my_img2.poly)
    my_img2.poly$area <- sapply(slot(my_img2.poly, "polygons"), slot,
"area")
    plot(my_img2.poly, add = TRUE, col = "red", border ="red")
    #Joe Add
    #Did Algorithm correctly identify all the knots?
    #The identification of other stuff is not important since user will
identify the correct ones
    print("Input 0 & Enter if not all knots were identified")
    print("It does not matter if other stuff was identified but all knots
must be identified")
    print("Hit enter if all knots were identified")
    knotscorrect <- readline(paste("Knots Correct for ", my_files[i],": ",
sep=' '))
    #If knotsforeachpiece = 0 we can skip labeling the knots
    if(knotscorrect==0){
      #Copy file when done
      file.copy(my_files[i], "Incomplete")
    }
    else   {
      #Start locator for knots
      print("Select knots with mouse and when finished hit esc")
      my_points   <- locator(type ="p", pch ="x")
      my_pointsXY <- rasterToPoints(my_img2$pknots)
      #The following code line doesn't work but was included in the code you
sent me
      #my_points2  <- identify.map(my_pointsXY[,1], my_pointsXY[,2])
      
     
      lknots   <- data.frame(board = my_files[i], x = my_points$x, y =
my_points$y)
      #This isn't used for anything ???
      coordinates(lknots) <- ~x+y
      
      #problem when more points than identified (either algorithm doesn't
identify
      #Or user clicks outside knot (easy to do on small knots)
      my_img2.poly$isKnot <- as.vector(over(my_img2.poly, lknots)[,1])
      my_img2.poly <- subset(my_img2.poly, !is.na(my_img2.poly$isKnot))
      lknots      <- as.data.frame(lknots)
      lknots$area <- my_img2.poly$area
      #I would like to save the image with the ID'ed knots but this doesn't
work well due to the white space
      name<-my_files[i]
      name<-gsub(".tif","",name)
      #Save raster picture with knots identified
      png(paste(name,"_Knots", ".png", sep=''),width = 6000, height = 6000)
      par(mfrow=c(1,1))
      plotRGB(my_img2)
      plot(my_img2.poly, add = TRUE, col = "red", border ="red")
      dev.off()
      #rbind dataframe
      my_knots <- rbind(my_knots, lknots)
      #Copy file when done
      #file.copy(my_files[i], "Complete")
    }
  }
}


I would really appreciate any help!



--
View this message in context: http://r.789695.n4.nabble.com/Create-a-new-window-tp4722299.html
Sent from the datatable-help mailing list archive at Nabble.com.


More information about the datatable-help mailing list