From 43c8858338fb1c1f8664b8ef1bc562ad74493dc1 Mon Sep 17 00:00:00 2001 From: Umesh Date: Sun, 21 Apr 2013 17:18:48 -0700 Subject: [PATCH] Updated Implementation of Roberts Algorithm --- doc/examples/plot_edge_filter.py | 23 +++++++++ skimage/filter/__init__.py | 2 +- skimage/filter/edges.py | 88 ++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 doc/examples/plot_edge_filter.py diff --git a/doc/examples/plot_edge_filter.py b/doc/examples/plot_edge_filter.py new file mode 100644 index 00000000..bbc79ff8 --- /dev/null +++ b/doc/examples/plot_edge_filter.py @@ -0,0 +1,23 @@ +import matplotlib +import matplotlib.pyplot as plt + +from skimage.data import camera +from skimage.filter import roberts,sobel + +image = camera() +edge_roberts = roberts(image) +edge_sobel = sobel(image) + +plt.figure(figsize=(8, 2.5)) +plt.subplot(1, 2, 1) +plt.imshow(edge_roberts, cmap=plt.cm.gray) +plt.title('Roberts Edge Detection') +plt.axis('off') + +plt.subplot(1, 2, 2) +plt.imshow(edge_sobel, cmap=plt.cm.gray) +plt.title('Sobel Edge Detection') +plt.axis('off') + + +plt.show() diff --git a/skimage/filter/__init__.py b/skimage/filter/__init__.py index 15eec3da..e98583ff 100644 --- a/skimage/filter/__init__.py +++ b/skimage/filter/__init__.py @@ -2,7 +2,7 @@ from .lpi_filter import * from .ctmf import median_filter from ._canny import canny from .edges import (sobel, hsobel, vsobel, scharr, hscharr, vscharr, prewitt, - hprewitt, vprewitt) + hprewitt, vprewitt, roberts , pdroberts, ndroberts) from ._denoise import denoise_tv_chambolle, tv_denoise from ._denoise_cy import denoise_bilateral, denoise_tv_bregman from ._rank_order import rank_order diff --git a/skimage/filter/edges.py b/skimage/filter/edges.py index fcd9f548..ef0a37a5 100644 --- a/skimage/filter/edges.py +++ b/skimage/filter/edges.py @@ -338,3 +338,91 @@ def vprewitt(image, mask=None): [1, 0, -1], [1, 0, -1]]).astype(float) / 3.0)) return _mask_filter_result(result, mask) + + +def roberts(image, mask=None): + """Find the edge magnitude using Roberts' Cross Operator. + + Parameters + ---------- + image : 2-D array + Image to process. + mask : 2-D array, optional + An optional mask to limit the application to a certain area. + Note that pixels surrounding masked regions are also masked to + prevent masked regions from affecting the result. + + Returns + ------- + output : ndarray + The Roberts' Cross edge map. + """ + return np.sqrt(pdroberts(image, mask)**2 + ndroberts(image, mask)**2) + + +def pdroberts(image, mask=None): + """Find the cross edges of an image using the Roberts' Cross operator. + The kernel is applied to the input image, to produce separate measurements + of the gradient component one orientation. + Parameters + ---------- + image : 2-D array + Image to process. + mask : 2-D array, optional + An optional mask to limit the application to a certain area. + Note that pixels surrounding masked regions are also masked to + prevent masked regions from affecting the result. + + Returns + ------- + output : ndarray + The Robert edge map. + + Notes + ----- + We use the following kernel and return the absolute value of the + result at each point:: + + 1 0 + 0 -1 + + """ + image = img_as_float(image) + result = np.abs(convolve(image, + np.array([[ 1, 0], + [ 0, -1]]).astype(float) / 1.0 )) + return _mask_filter_result(result, mask) + + +def ndroberts(image, mask=None): + """Find the cross edges of an image using the Roberts' Cross operator. + The kernel is applied to the input image, to produce separate measurements + of the gradient component one orientation. + Parameters + ---------- + image : 2-D array + Image to process. + mask : 2-D array, optional + An optional mask to limit the application to a certain area. + Note that pixels surrounding masked regions are also masked to + prevent masked regions from affecting the result. + + Returns + ------- + output : ndarray + The Robert edge map. + + Notes + ----- + We use the following kernel and return the absolute value of the + result at each point:: + + 0 1 + -1 0 + + """ + image = img_as_float(image) + result = np.abs(convolve(image, + np.array([[0, 1], + [-1, 0]]).astype(float) / 1.0)) + return _mask_filter_result(result, mask)