![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/638745/image.png)
contour extraction from the image
9 views (last 30 days)
Show older comments
Turbulence Analysis
on 1 Jun 2021
Commented: Star Strider
on 2 Jun 2021
Hi,
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);
imagesc(rgb1);
rgb2 = ind2rgb(rgb1,jet(2));
imagesc(rgb2);
grayimage = rgb2gray(rgb2);
imagesc(grayimage);
Edge = edge(grayimage);
imagesc(Edge);
0 Comments
Accepted Answer
Star Strider
on 1 Jun 2021
Try this —
LD = load('h.mat');
h = LD.h;
figure
surf(h, 'EdgeColor','none')
grid on
figure
lvl = 750;
[c,h] = contour(h, [1 1]*lvl);
grid
axis('equal')
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);
end
figure
hold on
for k = 1:1
plot(x{k}, y{k})
end
hold off
grid
axis('equal')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/638745/image.png)
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.
.
6 Comments
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.)
.
More Answers (1)
See Also
Categories
Find more on Graphics Object Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!