Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insertshape draw FilledPolygon in wrong place - Matlab

Tags:

matlab

Why does the insertshape function draw the FilledPolygon in the wrong place?

Code:

I = imread('coins.png');
BW = im2bw(I, graythresh(I));
[B,L] = bwboundaries(BW,'noholes');
boundary = B{1};
boundary1 = reshape(boundary.',1,[])
newI = insertShape(I,'FilledPolygon',boundary1);
imshow(newI);
like image 560
lroca Avatar asked Feb 12 '26 04:02

lroca


1 Answers

In MATLAB there often is confusion about matrix indices (i,j), where i is the row number, and coordinates (x,y), where x is horizontal.

It is important to pay close attention to the documentation, to see if it refers to i and j or row and column, or it it refers to x and y.

In this case, bwboundaries returns

Row and column coordinates of boundary pixels

and insertShape expects x and y coordinates.

Thus, to put the output of the one into the other you need to swap the two columns of B{1}:

I = imread('coins.png');
BW = im2bw(I, graythresh(I));
[B,L] = bwboundaries(BW,'noholes');
boundary = B{1};
boundary = boundary(:,[2,1]);  % <<< swap columns
boundary1 = reshape(boundary.',1,[]);
newI = insertShape(I,'FilledPolygon',boundary1);
imshow(newI);
like image 128
Cris Luengo Avatar answered Feb 14 '26 18:02

Cris Luengo