Image Processing - Implementing Sobel Filter

后端 未结 8 1840
梦毁少年i
梦毁少年i 2020-12-13 02:46

I\'ve got a task to implement Sobel filter which is, as you know, an image processing filter for edge detection. But unfortunately, I\'ve got no experience in image processi

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 03:26

    You can do this in R with the raster package (oriented towards geographic data)

    library(raster)
    sobel <- function(r) {
        fy <- matrix(c(1,0,-1,2,0,-2,1,0,-1)/4, nrow=3)
        fx <- matrix(c(-1,-2,-1,0,0,0,1,2,1)/4 , nrow=3)
        rx <- focal(r, fx)
        ry <- focal(r, fy)
        sqrt(rx^2 + ry^2)
    }
    
    b <- brick("https://i.stack.imgur.com/Bnxa6.jpg")
    plotRGB(b)
    s <- stack(lapply(1:3, function(i) sobel(b[[i]])))
    plotRGB(s)
    
    # or
    ss <- mean(s)
    plot(ss, col=gray(1:10/10))
    
    # or
    bb <- mean(b)
    sss <- sobel(bb)
    plot(sss, col=gray(1:10/10))
    

提交回复
热议问题