|
| 1 | +# --- |
| 2 | +# cover: assets/sedge.png |
| 3 | +# title: Edge detection using Sujoy Filter |
| 4 | +# author: Sujoy Kumar Goswami; Partha Ghosh |
| 5 | +# date: 2020-09-22 |
| 6 | +# --- |
| 7 | + |
| 8 | +using Images |
| 9 | +using Statistics |
| 10 | +using TestImages |
| 11 | +using ImageBinarization |
| 12 | + |
| 13 | +# Sujoy's Edge Detection Algorithm is a better & more generic approach (first derivative) for edge |
| 14 | +# detection than the other commonly used first-derivative methods (like Robert’s operator, Prewitt |
| 15 | +# operator, Sobel operator etc.). |
| 16 | + |
| 17 | +# [Paper Link](https://www.ijert.org/research/a-better-first-derivative-approach-for-edge-detection-IJERTV2IS110616.pdf) |
| 18 | + |
| 19 | +""" |
| 20 | + edges = sujoy(img; four_connectivity=true) |
| 21 | +
|
| 22 | +Compute edges of an image using the Sujoy algorithm. |
| 23 | +
|
| 24 | +## Parameters |
| 25 | +
|
| 26 | +* `img` (Required): any gray image |
| 27 | +* `four_connectivity=true`: if true, kernel is based on 4-neighborhood, else, kernel is based on |
| 28 | + 8-neighborhood, |
| 29 | +
|
| 30 | +## Returns |
| 31 | +
|
| 32 | +* `edges` : gray image |
| 33 | +""" |
| 34 | +function sujoy(img; four_connectivity=true) |
| 35 | + img_channel = Gray.(img) |
| 36 | + |
| 37 | + min_val = minimum(img_channel) |
| 38 | + img_channel = img_channel .- min_val |
| 39 | + max_val = maximum(img_channel) |
| 40 | + |
| 41 | + if max_val == 0 |
| 42 | + return img |
| 43 | + end |
| 44 | + |
| 45 | + img_channel = img_channel./max_val |
| 46 | + |
| 47 | + if four_connectivity |
| 48 | + krnl_h = centered(Gray{Float32}[0 -1 -1 -1 0; 0 -1 -1 -1 0; 0 0 0 0 0; 0 1 1 1 0; 0 1 1 1 0]./12) |
| 49 | + krnl_v = centered(Gray{Float32}[0 0 0 0 0; -1 -1 0 1 1;-1 -1 0 1 1;-1 -1 0 1 1;0 0 0 0 0 ]./12) |
| 50 | + else |
| 51 | + krnl_h = centered(Gray{Float32}[0 0 -1 0 0; 0 -1 -1 -1 0; 0 0 0 0 0; 0 1 1 1 0; 0 0 1 0 0]./8) |
| 52 | + krnl_v = centered(Gray{Float32}[0 0 0 0 0; 0 -1 0 1 0; -1 -1 0 1 1;0 -1 0 1 0; 0 0 0 0 0 ]./8) |
| 53 | + end |
| 54 | + |
| 55 | + grad_h = imfilter(img_channel, krnl_h') |
| 56 | + grad_v = imfilter(img_channel, krnl_v') |
| 57 | + |
| 58 | + grad = (grad_h.^2) .+ (grad_v.^2) |
| 59 | + |
| 60 | + return grad |
| 61 | +end |
| 62 | + |
| 63 | +img = testimage("cameraman") |
| 64 | +img_edge = sujoy(img, four_connectivity=true) |
| 65 | +img_edge₀₁ = binarize(img_edge, Otsu()) # or use other binarization methods provided in ImageBinarization |
| 66 | + |
| 67 | +mosaicview(img, img_edge, img_edge₀₁; nrow = 1) |
| 68 | + |
| 69 | + |
| 70 | +# save covers #src |
| 71 | +save("assets/sedge.png", img_edge) #src |
0 commit comments