contour extraction from the image

78 views (last 30 days)
I have the image from which I need to extrat the outer contour. My image is saved in the matrix name h..(also .mat file is attached here)
I have tried as shown below, while executing the edge () function I am getting two profiles i.e. outer and inner boundary as shown in the attached figure (contour.bmp). However, my intrest is to extract only the outer boundary as marked with black lines in raw image.jpg
Could somebody help me on how to extract only the outer boundary??
% convert raw to binary image
rgb1 = imbinarize(h, 700);
rgb2 = ind2rgb(rgb1,jet(2));
grayimage = rgb2gray(rgb2);
Edge = edge(grayimage);

Accepted Answer

Star Strider
Star Strider on 1 Jun 2021
Try this —
LD = load('h.mat');
h = LD.h;
surf(h, 'EdgeColor','none')
grid on
lvl = 750;
[c,h] = contour(h, [1 1]*lvl);
idx = find(c(1,:) == lvl);
for k = 1:numel(idx)
range = (idx(k)+1):c(2,idx(k));
x{k} = c(1,range);
y{k} = c(2,range);
hold on
for k = 1:1
plot(x{k}, y{k})
hold off
As luck would have it, the outer boundary is the entire first contour. The (x,y) coordinates are in ‘x{1}’ and ‘y{1}‘. That may vary with the level, so there may be more one solution to this, depending on what you want. .
There might also be other ways to do this, however this works. I checked it with the original contour plot. I left the analysis steps in so you can see what I did.
Star Strider
Star Strider on 2 Jun 2021
In the outer loop, it may be necessary to clear or preallocate ‘x’ and ‘y’ each time, just after the ‘idx’ assignment.
That would be either clearing:
clear x y
or preallocating:
x = cell(size(idx));
y = cell(size(idx));
The preallocation is preferable anyway because it creates more efficient, faster code.
(I wrote this thinking that it was only for the posted data, so did not specifically design it to be used in a loop. The preallocation would both clear existing values of ‘x’ and ‘y’, and create a cell array that can be filled faster than by not preallocating. Preallocating usually results in about a 20% decrease in execution time in the following loop as opposed to not preallocating.)

Sign in to comment.

More Answers (1)

Turbulence Analysis
Turbulence Analysis on 2 Jun 2021
Actually, the idea is to retain the values pertain to outer boundary for each image, the preallocation erases the previous the iteration result and retains only the latest iteration

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!