# center of mass of binary image

조회 수: 54 (최근 30일)
Yeshudas Muttu 2014년 9월 20일
답변: gwoo 2019년 3월 21일
hi , i want to know how to calculate center of mass of the binary image(silhouette).below is the image where i have crop the image basedon it boundary box

댓글을 달려면 로그인하십시오.

### 채택된 답변

Image Analyst 2014년 9월 20일
##### 댓글 수: 2없음 표시없음 숨기기
Yeshudas Muttu 2014년 9월 24일
thks for the help
Basically i have got centroid and bounding box using regionprop.. next i wanted to crop the image based on the bounding box ie
subImage =imcrop(BW,s.BoundingBox); figure(3); imshow(subImage) title('Cropped Image')
i get the crop part but without centroid marker on it.how do it get thr croppd image with the centroid marker on it?
<<
>>
Image Analyst 2014년 9월 24일
Yep, my code will do that. Did you see how if found each coin and cropped it out. I was hoping you could adapt it yourself after seeing how I used regionprops to ask for hte bounding box and then used this code to crop out all the blobs:
message = sprintf('Would you like to crop out each coin to individual images?');
reply = questdlg(message, 'Extract Individual Images?', 'Yes', 'No', 'Yes');
% Note: reply will = '' for Upper right X, 'Yes' for Yes, and 'No' for No.
figure;
% Maximize the figure window.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
for k = 1 : numberOfBlobs % Loop through all blobs.
% Find the bounding box of each blob.
thisBlobsBoundingBox = blobMeasurements(k).BoundingBox; % Get list of pixels in current blob.
% Extract out this coin into it's own image.
subImage = imcrop(originalImage, thisBlobsBoundingBox);
% Determine if it's a dime (small) or a nickel (large coin).
if blobMeasurements(k).Area > 2200
coinType = 'nickel';
else
coinType = 'dime';
end
% Display the image with informative caption.
subplot(3, 4, k);
imshow(subImage);
caption = sprintf('Coin #%d is a %s.\nDiameter = %.1f pixels\nArea = %d pixels', ...
k, coinType, blobECD(k), blobMeasurements(k).Area);
title(caption, 'FontSize', 14);
end
Did you actually download and run my demo? Do you think you'll be able to transfer that code to your program?

댓글을 달려면 로그인하십시오.

### 추가 답변 (2개)

gwoo 2019년 3월 21일
This is the fastest simpliest way I've seen to do it without regionprops:
[r, c] = find(binaryImage == 1);
rowcolCoordinates = [mean(r), mean(c)];
##### 댓글 수: 0이전 댓글 -2개 표시이전 댓글 -2개 숨기기

댓글을 달려면 로그인하십시오.

Guillaume 2014년 9월 20일
편집: Guillaume 2014년 9월 20일
You haven't attached any image.
If your image is a single connected blob, regionprops can give you the centre of mass with the centroid property.
If you want the centre of mass for the whole image, regardless of how many blobs are in it, it's fairly trivial:
[x, y] = meshgrid(1:size(img, 2), 1:size(img, 1));
weightedx = x .* img;
weightedy = y .* img;
xcentre = sum(weightedx(:)) / sum(img(:));
ycentre = sum(weightedy(:)) / sum(img(:));

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by