# Problem with bwboundaries in image processing

24 views (last 30 days)
Alfonso on 27 Apr 2018
Commented: Matt J on 2 May 2018
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.

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.

Matt J on 2 May 2018
My question would be, is the information obtained from the polygon more consistent than the one obtained to bwboundaries in comparison with the original plane cut(blue)?
Yes, because polyshape doesn't involve any pixelation of the object. Once you pixelate the object, you have introduced approximations, and any calculation deriving from the pixelation (e.g., bwboundaries) inherits some error from that.
Alfonso on 2 May 2018
Okay, this final result seems to be good enough in terms of error. Thank you for all your help, information and time Matt.
Matt J on 2 May 2018
You're quite welcome.

Image Analyst on 30 Apr 2018
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.

#### 1 Comment

Alfonso on 30 Apr 2018
I have been trying Steve´s part series on Feret diameters where he uses square pixels, circular pixels on corners.
The next image is the binary image with circular pixels in corners. The gray background is the grid (which is only appreciable if you zoom in). The next image is a comparison of the original with the boundary from the binary img using circular pixels. I have used the output x,y coordinates of the boundary but as it does not cover the whole image the output boundary has blank spaces, I am not sure how to include the remaining points aswell to the ones obtained.
Finally the comparison between original and output is logically wrong, which makes me think that if I don't get the exactly original boundary the match will not be 100% (which coincides to what I understood about your comment; nº of points in both clouds of points should be practically in the same place ). As the k = boundary(x,y) Matlab function does not give a faithful boundary of the original it will not be possible to obtain a match at the end of the process. By the fact that fixing s=1 as I answered previously to Matt J gives me a wrong output boundary.
As the objective of all this is to find the major axis of the original(blue) cloud of points, is there any effective way to obtain it (as my initial data (blue plot) is not an image) without using image processing which I could give a try?

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.

Matt J on 1 May 2018
Please attach the point cloud data in a .mat file, so that I can examine it.
Alfonso on 1 May 2018
Here is the original plane cut data.
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.