Problem with bwboundaries in image processing
11 views (last 30 days)
Show older comments
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:
- 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.
% 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.
0 Comments
Accepted Answer
Matt J
on 29 Apr 2018
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.
27 Comments
More Answers (2)
Image Analyst
on 30 Apr 2018
See Steve's 5 part series on Feret Diameters: https://blogs.mathworks.com/steve/2017/09/29/feret-diameter-introduction/
Also, regionprops computes centroid based on the whole blob. Taking the mean of boundary points does not. For example, what if you had a square and only two points on the left side of the square to define it but a thousand on the right side of the square? Taking the mean of those would not put the x centroid in the middle of the square - not even close.
Matt J
on 30 Apr 2018
Edited: Matt J
on 30 Apr 2018
This is not exactly what you asked for, but it might make sense to fit an ellipse directly to the blue point cloud (e.g., using this ) and use the major axis of the ellipse fit to define the major axis of the given shape.
This is asking you to change your problem requirements, but it might be a better axis definition for the data that you have. And, at least you wouldn't have to worry about further losses and inaccuracies creeping in as you try to map the blue cloud back to the full image that it came from.
5 Comments
Matt J
on 2 May 2018
The data doesn't look like the shape you posted... In any case, make sure the coordinate samples are arranged columnwise, not row-wise.
I had no problem getting output when the samples were put in the columnwise order described in the help text.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!