Labelling edges in an image

6 views (last 30 days)
Adam
Adam on 6 Nov 2014
Edited: Adam on 7 Nov 2014
I am trying to do what should be a really basic image processing task, but don't seem to be able to find a particular neat solution so hopefully someone can suggest one.
I simply wish to find the edges between three coloured segments of the image below as single-pixel thick lines with 3 distinct values representing the 3 different boundary possibilities. I don't care what the 3 distinct values are so long as they are distinct.
This is the solution I want (ignore the colourmap, I was just trying to find colours that made it easy to distinguish) - here the 3 values happen to be 3, 4, 5.
My method for achieving it was:
f = @(x) ( x(2) ~= x(1) ) * ( x(2) + x(1) );
edgeRes = nlfilter( wedge, [1 2], f );
i.e. basically just looking at a 1*2 window and adding together the two values in the window if they differ.
I don't like this solution much though as nlfilter throws up a progress bar (plus it feels like there should be a much simpler method).
The input data and the edge result I have should be in the attached .mat file if anyone has any better idea for achieving this seemingly trivial task!

Accepted Answer

Ahmet Cecen
Ahmet Cecen on 6 Nov 2014
For this particular image and any image with a similar enough trivial nature, there is a very short solution: A is your image with 3 regions which are 1 2 and 4, use circshift to shift A left by 1 pixel, then add the shifted image with the original image. Now anywhere with a 3 is a 1 2 boundary, 5 is a 1 4 boundary 6 is a 2 4 boundary.
  1 Comment
Adam
Adam on 7 Nov 2014
Edited: Adam on 7 Nov 2014
Thanks. After managing to do every part of your solution incorrectly at first (wrong circshift dimension, forgetting to use 4 instead of 3 for my 3rd region, etc) this gives me exactly what I want:
wedge( wedge == 3 ) = 4;
edgeRes = wedge + circshift( wedge, 1, 2 );
edgeRes(:,1) = 0;
edgeRes( isIntegerValued( log2( edgeRes ) ) ) = 0;
(isIntegerValued is my own function and seemed the neatest way of removing all the other values which I wish to be 0).

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!