contour extraction from the image

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);

 Respuesta aceptada

Star Strider
Star Strider el 1 de Jun. de 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 comentarios

Turbulence Analysis
Turbulence Analysis el 2 de Jun. de 2021
Hi,
Many thanks for this.. It's amazing.
By chance, if I need to extract inner boundary, may i know how to do that ??
As always, my pleasure!
Sure!
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) & (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)
figure
k = idx2(1)
plot(x{k}, y{k})
grid
axis('equal')
title('Outer Boundary')
figure
k = idx2(2)
plot(x{k}, y{k})
grid
axis('equal')
title('Inner Boundary')
figure
hold on
for k = 1:numel(idx2)
plot(x{idx2(k)}, y{idx2(k)})
end
hold off
grid
axis('equal')
legend('Outer Boundary','Inner Boundary', 'Location','best', 'FontSize',8)
With the last figure —
It turns out that the two largest contours are the complete inner and outer boundaries. (Also, there was a slight error in the earllier code (corrected here). I thought the previous version was working correctly, since all the cells were the correct size, however when I ran it just now, I discovered an error, and I may have posted the wrong version earlier, because of a computer crash for a software update, while I was working on it. This version is correct.)
.
Turbulence Analysis
Turbulence Analysis el 2 de Jun. de 2021
Thanks a lot.
Now, I have to implement this on 10000 images in a loop..
If I encounter any difficulties, I will get back to you...
Cheers !!!
Star Strider
Star Strider el 2 de Jun. de 2021
As always, my pleasure!
I may be necessary to adjust the contour level (‘lvl’ in my code) depending on the image characteristics. The objective is to avoid noise while getting representative values. Fortunately, filtering by the sizes of the contours may help in that respect. It may also be necessary to increase the nuimber of the largest contours identified to more than 2 (the last argument in the maxk call) in order to provide reasonably contiguous contours.
.
Turbulence Analysis
Turbulence Analysis el 2 de Jun. de 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
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.)
.

Iniciar sesión para comentar.

Más respuestas (1)

Turbulence Analysis
Turbulence Analysis el 2 de Jun. de 2021

0 votos

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 comentarios

Star Strider
Star Strider el 2 de Jun. de 2021
I would save the vectors to a .mat file in each iteration. Give the vectors for each iteration a distinct name, perhaps creating a table for them with cell2table with a new name for the table in each iteration, then add them to the file as described in Append Variable to MAT-File to have a permenent record of them that is easily accessable. Creating the table and writing to the file would slow the code slightly, however the permanent record of all of them would likely be worth the effort.
Turbulence Analysis
Turbulence Analysis el 2 de Jun. de 2021
Yes, now it seems I am getting closer to it.. I will update the status...
Star Strider
Star Strider el 2 de Jun. de 2021
Noted.

Iniciar sesión para comentar.

Preguntada:

el 1 de Jun. de 2021

Comentada:

el 2 de Jun. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by