I am trying to do the next things:
Objective: Find centroid and major axis of a set of x,y coordinates by using bwboundaries and image processing to finally plot the resulting boundary+centroid+major axis that should fit the original
Initial data: - x,y coordinates of a plane cut (image1.png)
- Get the outer boundaries by using matlab function: k = boundary(x,y)
- Use poly2mask to convert the boundary into a binary image (image2.png)
- Get the bwboundaries of the binary image
- Find centroid and major axis using regionprops (image2.png)
- Plot bwboundaries, major axis and centroid (image3.png)
- Move bwboundaries (x,y) to origin (0,0), move centroid, move major axis , and apply a scale factor to bwboundaries in order to match the original x,y coordinates scale (image4.png)
In image 4 the blue plot is the original, and the red plot is the bwboundaries shifted and scaled to fit the original. I found the centroid of the blue original plot using polygeom function from file exchange. And the circular markers just indicate the origin of each plot.
The way I have move the bwboundaries points so the origin is (0,0) is by subtracting the mean of x and y coordinates.
bwboundary(:,2) = bwboundary(:,2)-x_mean;
bwboundary(:,1) = bwboundary(:,1)-y_mean;
centroid(1) = centroid(1) - x_mean;
centroid(2) = centroid(2) - y_mean;
majoraxis(:,1) = majoraxis(:,1)-xx_mean + centroid(1);
majoraxis(:,2) = majoraxis(:,2)-yy_mean + centroid(2);
In order to get the scale factor, as you can see in images 1(original) and 3(bwboundaries), the plots are with axis tight, so i got the x and y axis limits for the two plots with [xl = xlim] and [yl = ylim] and calculated the distance from xmin to xmax and ymin to ymax for each plot. I calculated it by...
x_factor = distance_x_original/distance_x_bwboundaries;
y_factor = distance_y_original/distance_y_bwboundaries;
Finally just multiply the factors to bwboundaries coordinates, the centroid coordinates and the major axis coordinates.
As you can see the result, red plot, (image4.png) is slightly shifted from the original blue and the centroids are different where they would have to be the same, but I don't really know why is this happening. I have checked, x and y limits, distances and scale factor results and seems correct, aswell as the code used to shift and scale the bwboundaries coordinates. Maybe I am misusing some function.
Thanks for any help.