Skip to content

Commit 84e7b2d

Browse files
authored
sujoy edge demo (#158)
Edge detection using Sujoy Filter
1 parent 96e6703 commit 84e7b2d

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
99
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
1010
FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
1111
ImageAxes = "2803e5a7-5153-5ecf-9a86-9b4c37f5f5ac"
12+
ImageBinarization = "cbc4b850-ae4b-5111-9e64-df94c024a13d"
1213
ImageContrastAdjustment = "f332f351-ec65-5f6a-b3d1-319c6670881a"
1314
ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
1415
ImageDraw = "4381153b-2b60-58ae-a1ba-fd683676385f"
@@ -40,6 +41,7 @@ Documenter = "0.25"
4041
FileIO = "1"
4142
FixedPointNumbers = "0"
4243
ImageAxes = "0"
44+
ImageBinarization = "0.2"
4345
ImageContrastAdjustment = "0.3"
4446
ImageCore = "0"
4547
ImageDraw = "0"
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

Comments
 (0)