# How to segment image without losing information at the edges for finding centroid?

4 views (last 30 days)
Max V on 16 Nov 2021
Commented: Bjorn Gustavsson on 17 Nov 2021
I have the following image and when I segment and fill it, I'm getting some error which ends up in the image having mutiple centroids.
How to I fix it make sure that I get only one centroid for the entire image. I have attached the code of my progress till now
clc;clear;close all;
imshow(I); title('Original Image')
level = graythresh(I);
imshow(BW,[]); title('Segmented Image')
BW2 = imfill(BW,'holes');
figure
imshow(BW2)
title('Filled Image')
Trace region boundaries in binary image
[B,L] = bwboundaries(BW,'noholes');
imshow(label2rgb(L, @jet, [.5 .5 .5]))
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end
Centroid calculation
s = regionprops(BW,'centroid');
centroids = cat(1,s.Centroid);
plot(centroids(:,1),centroids(:,2),'b*')

Bjorn Gustavsson on 16 Nov 2021
The only way I can imagine the meaning of centroid in a grayscale image like the one above is with the same definition as "centre of mass" has. For that you could do something like this:
[X,Y] = meshgrid(1:size(I,2),1:size(I,1));
Id = double(I)
r_centroid = [sum(X(:).*Id(:)),sum(Y(:).*Id(:))]/sum(Id(:));
This would properly give you a centre-of-brightness of the abdominal slice, which is not dependent on any arbitrary thresholding:
HTH
Bjorn Gustavsson on 17 Nov 2021
@Max V: In the example image you've presented the only "Local Binary Pattern"s I can see are the regions with 2-pixel-wide interference-like patterns (either from the CT-operation, too sparse number of projections or too high noise-level), or from a horrendously rough jpeg-compression. Here you'll have to settle for size, orientation, shape and widths of the different regions, you could perhaps take brightness-profiles of the different regions at the narrowest point.

yanqi liu on 16 Nov 2021
sir,may be use imclose to one block area
clc;clear all;close all;
if ndims(I)==3
I=rgb2gray(I);
end
imshow(I); title('Original Image')
level = graythresh(I);
imshow(BW,[]); title('Segmented Image')
BW2 = imfill(BW,'holes');
figure
imshow(BW2); hold on;
title('Filled Image')
s = regionprops(BW2,'centroid');
centroids = cat(1,s.Centroid);
plot(centroids(:,1),centroids(:,2),'b*')
% close bw
BW3 = imclose(BW2, strel('disk', round(mean(size(I)/2))));
figure
imshow(BW3); hold on;
title('Closed Image')
s = regionprops(BW3,'centroid');
centroids3 = cat(1,s.Centroid);
plot(centroids3(:,1),centroids3(:,2),'r*')
% distance
dis = centroids - repmat(centroids3(1,:), size(centroids,1), 1);
dis = dis(:,1).^2 + dis(:,2).^2;
[~,ind] = min(dis);
% display
figure;
imshow(BW2); hold on;
title('Filled Image')
s = regionprops(BW2,'centroid');
plot(centroids(ind,1),centroids(ind,2),'r*')
Max V on 16 Nov 2021
Thank you for taking time to reply @yanqi liu!
I want to also perform Shape Signature analysis by calculating the distance and angle from the centroid to the edge of the image.
Can you help/guide me on how to do it?

R2021a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!