Created
May 2, 2018 12:41
-
-
Save RobBlackwell/20551558ff3b1491e62514dabbcc8500 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
bwselect(BW, c , r) | |
Select objects in a binary image. | |
Similar to the MATLAB function of the same name. | |
""" | |
function bwselect(BW, c, r) | |
# constants | |
north = CartesianIndex(-1, 0) | |
south = CartesianIndex( 1, 0) | |
east = CartesianIndex( 0, 1) | |
west = CartesianIndex( 0, -1) | |
queue = CartesianIndex.(r,c) | |
m,n = size(BW) | |
out = falses(m,n) | |
while !isempty(queue) | |
node = pop!(queue) | |
if BW[node] | |
wnode = node | |
enode = node + east | |
# Move west until node is false | |
while checkbounds(Bool, BW, wnode) && BW[wnode] | |
out[wnode] = true | |
if checkbounds(Bool, BW, wnode + north) && !out[wnode + north] | |
push!(queue, wnode + north) | |
end | |
if checkbounds(Bool, BW, wnode + south) && !out[wnode + south] | |
push!(queue, wnode + south) | |
end | |
wnode += west | |
end | |
# Move east until node is false | |
while checkbounds(Bool, BW, enode) && BW[enode] | |
out[enode] = true | |
if checkbounds(Bool, BW, enode + north) && !out[enode + north] | |
push!(queue, enode + north) | |
end | |
if checkbounds(Bool, BW, enode + south) && !out[enode + south] | |
push!(queue, enode + south) | |
end | |
enode += east | |
end | |
end | |
end | |
return out | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment