R raster recognizing black color raster image -


the code below produces 2 boxes on image. planning analyze pixels within boxes further.

i want put condition if along edge of box, there black color (or similar color such grey) pixel don't proceed. how can specify such condition?

in below example, in case of red square don't want proceed further has black pixels @ top right hand corner. while proceed in case of green square doesn't have black color pixel along it's edge.

library(raster) r1 <- brick(system.file("external/rlogo.grd", package="raster")) x <- crop(r1, extent(0,50,0,50)) plotrgb(x) plot(extent(c(0,20,0,20)), lwd=2, col="red", add=true) plot(extent(c(21,35,0,10)), lwd=2, col="green", add=true) 

that not defined in case color made of rgb values. here general solution adapt. 'flatten' these single channel taking average, , test smallest value being below threshold (white 255, 255, 255 in rgb, black 0,0,0) @ boundary

proceed <- function(f, e, threshold) {     lns <- as(as(e, 'spatialpolygons'), 'spatiallines')     v <- unlist(extract(f, lns))     ifelse( min(v, na.rm=true) < threshold, false, true) }  # flat <- mean(x) # not sophisticated see # http://stackoverflow.com/questions/687261/converting-rgb-to-grayscale-intensity flat <- sum(x * c(0.2989, 0.5870, 0.1140)) proceed(flat, extent(c(0,20,0,20)), 100) proceed(flat, extent(c(21,35,0,10)), 100) 

(much improved after seeing jbaums' solution; gone)