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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With