contour extraction from the image

26 views (last 30 days)
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);

Accepted Answer

Star Strider
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')
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
Turbulence Analysis
Turbulence Analysis on 2 Jun 2021
Yes, I did played with the lv1 for some of my cases, it doing its job perfectly.. I have impleted the code to handle my 10000 images using for loop as shown below.
As you can see, the writing the values of x{k}, y{k} pertains to outer boundary of each image in F , F1 respectively. However, i got the below error..
Unable to perform assignment because the size of the left side is 1-by-3229 and the size of the right side is 1-by-3445.
I guess it's due to the change in size of the x{k}, y{k} for each image.. Could you please help on how to get rid of this..
for f = 1:1:10
if (f>=1) && (f<=9)
fname_strt = 'B0000' ;
elseif (f>=10) && (f<=99)
fname_strt='B000';
elseif (f>=100) && (f<=999)
fname_strt='B00';
else
fname_strt='B0';
end
fname_end = num2str(f);
fname = strcat(fname_strt,fname_end,'.im7');
I=loadvec(fname);
x1=(I.x);
y1=(I.y);
h = I.w';
lvl = 750;
[c,h] = contour(h, [1 1]*lvl);
idx = find((c(1,:) == lvl) & (rem(c(2,:),1) == 0));
for k = 1:numel(idx)
Q(k) = c(2,idx(k));
range = idx(k)+(1:c(2,idx(k)));
x{k} = c(1,range);
y{k} = c(2,range);
end
[cellnrs,idx2] = maxk(cellfun(@numel,x),2)
k = idx2(1);
Q1 = x{k};
Q2 = y{k};
t1 = 1;
skT = 1;
% Writing the values of x{k}, y{k} F , F1
F(:,:,f) = Q1;
F1(:,:,f) = Q2;
clear Q1 Q2
end
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
  3 Comments
Turbulence Analysis
Turbulence Analysis on 2 Jun 2021
Yes, now it seems I am getting closer to it.. I will update the status...

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!