简体   繁体   English

识别黑色光栅图像的R光栅

[英]R raster recognizing black color raster image

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, there is a black color (or a similar color such as grey) pixel then don't proceed. 我想提出一个条件,如果沿着盒子的边缘,有黑色(或类似的颜色,如灰色)像素,则不要继续。 How can i specify such condition? 如何指定这种条件?

In below example, in the case of the red square I don't want to proceed further as it has black pixels at the top right hand corner. 在下面的示例中,在红色正方形的情况下,由于它在右上角具有黑色像素,因此我不想进一步进行操作。 While I would like to proceed in the case of green square as it doesn't have a 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 is not very well defined as in this case color is made of RGB values. 定义不是很好,因为在这种情况下颜色是由RGB值组成的。 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 我通过取平均值将它们“展平”到单个通道,然后在边界处测试最小值是否低于阈值(白色是255、255、255,RGB是0、0、0)

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) (在看到jbaums的解决方案之后有了很大的改进;现在已经消失了)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM