Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update edge.py #112

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 62 additions & 3 deletions mahotas/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
[ 1, 2, 1]])/8.

__all__ = [
'sobel',
'dog',
]
'sobel',
'sujoy',
'dog',
]

def sobel(img, just_filter=False):
'''
Expand Down Expand Up @@ -69,6 +70,64 @@ def sobel(img, just_filter=False):
thresh = 2*np.sqrt(filtered.mean())
return mh.regmax(filtered) * (np.sqrt(filtered) > thresh)

def sujoy(img, kernel_nhood=0, just_filter=False):

'''
edges = sujoy(img, kernel_nhood=0, just_filter=False)

Compute edges using Sujoy's algorithm

`edges` is a binary image of edges computed according to Sujoy's algorithm.
Paper link: https://www.ijert.org/research/a-better-first-derivative-approach-for-edge-detection-IJERTV2IS110616.pdf

Parameters
----------
img : Any 2D-ndarray
kernel_nhood : 0(default) or 1
if 0, kernel is based on 4-neighborhood
else , kernel is based on 8-neighborhood
just_filter : boolean, optional
If true, then return the result of filtering the image with the Sujoy's
filters, but do not threshold (default is False).

Returns
-------
edges : ndarray
Binary image of edges, unless `just_filter`, in which case it will be
an array of floating point values.
'''
# This is similar to 'sobel" implementation above
img = np.array(img, dtype=np.float)
if img.ndim != 2:
raise ValueError('mahotas.sujoy: Only available for 2-dimensional images')
img -= img.min()
ptp = img.ptp()
if ptp == 0:
return img
img /= ptp

if kernel_nhood:
krnl_h = np.array([[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.
krnl_v = np.array([[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.
else:
krnl_h = np.array([[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.
krnl_v = np.array([[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.

grad_h = convolve(img, krnl_h, mode='nearest')
grad_v = convolve(img, krnl_v, mode='nearest')

grad_h **=2
grad_v **=2

grad = grad_h
grad += grad_v
if just_filter:
return grad
t = np.sqrt(grad.mean())

return mh.regmax(grad)*(np.sqrt(grad)>t)


def dog(img, sigma1 = 2, multiplier = 1.001, just_filter = False):
'''
edges = dog(img, sigma1 = 2, thresh= None, just_filter = False)
Expand Down