View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Finds edges in a binary image

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

3.0 | 1 rating Rate this file 2 Downloads (last 30 days) File Size: 1.52 KB File ID: #41239 Version: 1.0
image thumbnail

Finds edges in a binary image



Function "EDGE2" is an alternative to function "EDGE". Function is compact and fast.

| Watch this File

File Information

Features of code:
1. Not difficult.
2. The minimum number of lines.
3. Using simple operations on matrices.
4. Fast.


function [OUT] = edge2(IN)
% EDGE2 finds edges in a binary image.
% IN - logic array (binary image).
% OUT - logic array (binary image) of the same size as IN.
% Binary image IN:
% (0) "false" - background,
% (1) "true" - objects.
% Binary image OUT:
% (1) "true" where the function finds edges,
% (0) "false" elsewhere.
% Remarks
% -------
% Edges can be internal and external. This function finds the EXTERNAL
% edges. But if you need to find the internal edges, you must invert
% a binary image IN.
% Example
% -------
% Find edges in the binary image IN:
% OUT = edge2(IN); external edges
% OUT = edge2(~IN); internal edges
% See also: EDGE
% Author: Nazar Petruk
% Address: TSTU, Ukraine
% email:
% Date: 2013, April
% Copyright (c) 2013, by Nazar Petruk

[r,c] = size(IN);
% Creating a logic array A. The dimension of the array A is equal to
% the dimension of the array IN plus 2(two).
A = false([r,c]+2);
% Fill the array A, using the operation "OR".
for i = 0:2,
    for j = 0:2,
        A((1:r)+i,(1:c)+j) = A((1:r)+i,(1:c)+j) | IN;
% Receiving array OUT, using the operation "exclusive-OR".
OUT = xor(A(2:r+1,2:c+1), IN);

Required Products MATLAB
MATLAB release MATLAB 8.0 (R2012b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (1)
11 Apr 2013 Anon

Anon (view profile)

No, it is not an alternativ since edge can handle grayscale images, too. However, I like the good documentation, the comments in the code and the H1 line.

Contact us