MATLAB: Problem with bwboundaries in image processing

bwboundariescentroidimage processingmajor axispolyshaperegionprops

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)
Process:
  1. Get the outer boundaries by using matlab function: k = boundary(x,y)
  2. Use poly2mask to convert the boundary into a binary image (image2.png)
  3. Get the bwboundaries of the binary image
  4. Find centroid and major axis using regionprops (image2.png)
  5. Plot bwboundaries, major axis and centroid (image3.png)
  6. 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.
% x axis origin in 0
x_mean=mean(bwboundary(:,2));
bwboundary(:,2) = bwboundary(:,2)-x_mean;
% y axis origin in 0
y_mean=mean(bwboundary(:,1));
bwboundary(:,1) = bwboundary(:,1)-y_mean;
% Move the centroid
centroid(1) = centroid(1) - x_mean;
centroid(2) = centroid(2) - y_mean;
% major axis passes through centroid
xx_mean=mean(majoraxis(:,1));
majoraxis(:,1) = majoraxis(:,1)-xx_mean + centroid(1);
yy_mean=mean(majoraxis(:,2));
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.

Best Answer

Well, there's no reason to think the centroid according to regionprops will agree with the centroid according to polygeom. They derive results from different data. Also, the s-parameter in boundary(x,y,s) will affect agreement between the output of boundary and the original data. You might try using s=1 for a tighter wrapping.
Related Question