Home > Mobile >  X,Y coordinates and centroid of an object after rotation - Matlab
X,Y coordinates and centroid of an object after rotation - Matlab

Time:09-17

I am using this script to extract the X,Y coordinates and centroid of an object after rotation. The outline coordinates are saved in a table but when I scatter plot them in JMP I get another rotation (as shown in the image). Furthermore, I do not get the centroid. Any idea why and how can I solve it?

enter image description here

Code:

clc;
clear;
close all;
 
url='http://clipart-library.com/newimages/fish-clip-art-32.png';
I = rgb2gray(imread(url));
imshow(I);
rotAngle = 55;
I = imrotate(I, rotAngle,'nearest','loose');
imshow(I); 
           
[B,L] = bwboundaries(I,'noholes');
             
k=1;
stat = regionprops(I,'Centroid');
b = B{k};
xBoundary = b(:,2);
yBoundary = b(:,1);
centroidObject = stat(k).Centroid;
xCentre = centroidObject(:,2)
yCentre = centroidObject(:,1)
dataTable = table(xBoundary,yBoundary);            
writetable(dataTable,'E:/dataTable.csv')
hold on;  
plot(yCentre,xCentre,'Marker','x','Color','r');
plot(xBoundary,yBoundary,'Color','g');

CodePudding user response:

You have a typo in your code:

stat = regionprops(I,'Centroid');

The input should be L, the labeled image, not I. If you fix that, you'll see that stat has only 1 output, rather than 255, and stat(1) will have meaningful values for your object, rather than NaN because I didn't have any pixels with a value of 1. The centroid being NaN caused no marker to be plotted.

Next, your plot is upside down because by default the image axes have y increasing downward, whereas the normal plot axes have y increasing upward. With

set(gca,'YDir','reverse')

after creating the plot, you can reverse the y axis of the plot.

  • Related