Error with bwtraceboundary function

Hi all,
I have a function where I have a set of x,y coordinates for points to a contour as input. I calculate its derivatives using spline interpolation. When I take ellipse or circle, i can manually calculate 8 points on contour and use this. But When I draw an arbitrary shape like this:
i need to get a point set first. It seems to me that bwtraceboundary is the function that i need to use, here is the code that i write; i check the beginning points with pixelinfo
I = imread('example3.png');
figure(1)
imshow(I)
BW = im2bw(I);
figure(2)
imshow(BW)
impixelinfo
contour = bwtraceboundary(BW,[r c],'W',8,Inf,'counterclockwise');
figure(3)
imshow(I)
hold on;
plot(contour(:,2),contour(:,1),'g','LineWidth',3);
it gives me an empty contour.
Actually as I will always have a two color picture and one boundary there might be an easier way.
Any suggestion?

1 comentario

ttopal
ttopal el 14 de Feb. de 2017
I am checking the pixel values of binary image, where i have the contour it shows 0 and empty area as 1. Cant i change it to be otherwise? Then
[rows, columns] = find(BW ~= 0)
should be all I need. Am I wrong?

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 14 de Feb. de 2017

1 voto

The [r, c] you supply needs to be a point that is on the boundary. You do not show how you set [r, c]
You can use find() to locate a pixel.

3 comentarios

ttopal
ttopal el 14 de Feb. de 2017
Editada: ttopal el 14 de Feb. de 2017
Thanks Walter,
I use impixelinfo to locate a point on the contour. For example [498 263] is one of them. But i get an error:
Index exceeds matrix dimensions.
Error in graphtest2 (line 12)
plot(contour(:,2),contour(:,1),'g','LineWidth',3);
When I try your answer to another question where;
[r, c] = find(I, 1, 'first')
it draws a contour which is the frame of the picture. I noticed that in my binary image the contour has pixel value 0 and the empty areas has pixel value 1. Maybe thats the reason why i get this errors? Another thing is that Matlab suggests to use imbinarize instead of im2bw but when i do that it gives error the image should be two dimensional.
Walter Roberson
Walter Roberson el 14 de Feb. de 2017
Yes you need to reverse your mask so that the boundary is 1 and the background is 0
ttopal
ttopal el 14 de Feb. de 2017
Thanks Walter, now I have the contour correct with this code :
I = imread('example3.png');
figure(1)
imshow(I)
IM = im2bw(I);
IM2 = imcomplement(IM);
[r, c] = find(IM2, 1, 'first');
mycontour = bwtraceboundary(IM2, [r c] ,'E',8,Inf,'counterclockwise') ;
figure(3)
imshow(I)
hold on;
plot(mycontour(:,2),mycontour(:,1),'g','LineWidth',1);

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Image Processing Toolbox en Centro de ayuda y File Exchange.

Preguntada:

el 14 de Feb. de 2017

Comentada:

el 14 de Feb. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by