Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@RobBlackwell
Created May 2, 2018 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobBlackwell/20551558ff3b1491e62514dabbcc8500 to your computer and use it in GitHub Desktop.
Save RobBlackwell/20551558ff3b1491e62514dabbcc8500 to your computer and use it in GitHub Desktop.
"""
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