R raster recognizing black color raster image

后端 未结 1 1529
耶瑟儿~
耶瑟儿~ 2021-01-16 12:43

The code below produces two boxes on my image. I am planning to analyze pixels within those boxes further.

I want to put a condition that if along an edge of a box,

相关标签:
1条回答
  • 2021-01-16 13:26

    That is not very well defined as in this case color is made of RGB values. But here is a general solution that you could adapt. I 'flatten' these to a single channel by taking the average, and then test for the smallest value being below a threshold (white is 255, 255, 255 in RGB, black is 0,0,0) at the 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; which is now gone)

    0 讨论(0)
提交回复
热议问题