30 views (last 30 days)

Hi there,

I want to find the centroid for the marked object. After finding the centroid, I need to crop the image around the centroid with a size of 227x227.

Can someone please help by guilding me or writing the code in which I will be able to find the centroid and crop the object from the image? Thank you in advance!

KSSV
on 8 Mar 2019

I = imread('image.jpeg') ;

[nx,ny,nt] = size(I) ;

x = 1:ny ; y = 1:nx ;

C = round([mean(x) mean(y)]) ;

idx = C(2)-227:C(2)+227 ;

idy = C(1)-227:C(1)+227 ;

I = I(idx,idy,:) ;

imshow(I)

YOu may play around with idx and idy

Shunichi Kusano
on 8 Mar 2019

This is a rough code to detect the centroid of the object. I interpreted that the "object" is the largest one seen in the center.

%% read image

img = imread('image.jpeg');

figure, imshow(img);

%% region filter to reject smooth field

Ifilt = rangefilt(double(rgb2gray(img)), ones(5,5));

figure, imshow(Ifilt, []);

BW = Ifilt > 50;

figure, imshow(BW);

%% processing binary image

BW = imfill(BW, 'holes');

BW = bwareaopen(BW, 10000); % small blobs are removed

figure, imshow(BW);

%% centroid calculation

[c,r] = find(BW);

centroid = [mean(c),mean(r)]; % or you can use regionprops

figure

imshow(img);

hold on;plot(centroid(2), centroid(1), 'r*');

hope this helps.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/448956-how-can-i-find-the-centroid-of-an-object-in-an-image-and-crop-a-portion-of-it-around-that-centre-poi#comment_679114

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/448956-how-can-i-find-the-centroid-of-an-object-in-an-image-and-crop-a-portion-of-it-around-that-centre-poi#comment_679114

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/448956-how-can-i-find-the-centroid-of-an-object-in-an-image-and-crop-a-portion-of-it-around-that-centre-poi#comment_679131

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/448956-how-can-i-find-the-centroid-of-an-object-in-an-image-and-crop-a-portion-of-it-around-that-centre-poi#comment_679131

Sign in to comment.