How can I find the centroid of an object in an image and crop a portion of it around that centre point

10 ビュー (過去 30 日間)
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!
  2 件のコメント
Image Analyst
Image Analyst 2019 年 3 月 9 日
What is the "marked object"? I guess it's not the entire white disk because that's not marked. So do you mean the region marked in red? If so, you can just extract that from indexing and use imresize() to force it into a 227x227 size and shape. Or is the marked area just the rough stuff inside the red box and you want to find the weighted centroid (this is different than the centroid) of the stuff inside the marked/red box? The weighted centroid takes into account the gray levels while the centroid does not and would just be the exact center of the box.
If you've marked it in red, then you can just crop that red box - why do you need to find the centroid? Just crop it without finding the centroid.
OR do you want to find the rough stuff, and then find the centroid of that, without there being a red/marked box placed in advance? If so, do a texture filter then threshold it to get a mask and find the centroid or weighted centroid of that, then make a 227x227 box centered about that point.
Overall, you've given a very imprecise description of what you want to do.
And for the staff answer you said you have an image that it didn't work for but you forgot to attach the image!
Ernest Choong Hoe Yoon
Ernest Choong Hoe Yoon 2019 年 3 月 9 日
First of all, sorry for the imprecise description and thanks for replying! What I wanted was to find the rough stuff and then find the centroid of that without there being a red/marked box placed in advance.

サインインしてコメントする。

採用された回答

KSSV
KSSV 2019 年 3 月 8 日
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

その他の回答 (1 件)

Shunichi Kusano
Shunichi Kusano 2019 年 3 月 8 日
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.
  2 件のコメント
Ernest Choong Hoe Yoon
Ernest Choong Hoe Yoon 2019 年 3 月 9 日
Hey there, thanks for the help! It does work but for certain images it couldn't detect the centroid of the object.
Ernest Choong Hoe Yoon
Ernest Choong Hoe Yoon 2019 年 3 月 9 日
Attached is the sample image in which the code doesn't workGM 4X 3.jpg

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by