how to calculate the center of an object in binary image

hellow dears i need support to calculate the center of an object in binary image, the binary image has only one object so that i need to find the center of that object in order to crop the object from the binary image as in attached image

 Risposta accettata

You do not need the center of the blob to crop it from the image. You need the bounding box.
measurements = regionprops(binaryImage, 'BoundingBox');
croppedImage = imcrop(binaryImage, measurements.BoundingBox);

8 Commenti

Dear Image Analyst first thing many thanks for your kind support and i need the center so that i will depend on it is value in crop the object and other calculation i need it in next steps of my project, so that i need your kind support to solve this issue? kind regards
If you need the centroid for other reasons not related to cropping, you can ask for it
measurements = regionprops(binaryImage, 'BoundingBox', 'Centroid');
Of course the centroid will be the row and column from the original full size image, not from the cropped image.
Dear Image Analyst i test your code and i have error in
measurements = regionprops(binaryImage, 'BoundingBox');
my issue how i can find the center of an object in binary image?? we assume that the binary image has only one object no more than one for simplicity. as you know the object in a binary image represent the white color and remaining binary image is black. is there a function in matlab to find center of object in binary image??
Kind Regards
The code is correct. What error message did you observe? You forgot to post it. Post all the red error text. Maybe you called your binary image something different than "binaryImage" like you called it BW or something. The code will give the center of the binary image. Please post your original gray scale image and the line of code you used to threshold it and produce a binary image from it.
Dear Image Analyst many thanks for your usual and kind support, below the code i use it to test your code:
I=imread('ddd.png');
figure(1);
imshow(I);
level=graythresh(I);
BW=im2bw(I,level);
figure(2);
imshow(BW)
measurements = regionprops(BW, 'BoundingBox');
croppedImage = imcrop(BW, measurements.BoundingBox);
figure(3);
imshow(croppedImage);
and the message error is:
Error in test (line 12)
croppedImage = imcrop(BW, measurements.BoundingBox);
and the input image used in code test(ddd.png) you can find it in attachment.
so please first thing focus on my issue: i want a matlab code to find the center of an object in a binary image not the center of the whole size of binary image, assuming that the binary image has only one object and the position of an object is variable between the different binary images.
kind regards
You forgot to give the rest of the error message. Anyway, here's a full demo for you, attached.
Dear Image Analyst: first thing i am very grateful for your kind and fast support i really appreciate your kindness and i test your code it is excellent and give me the right result, if you don't mind can you give me your email in case i need your kind support. Kind Regards
You're welcome. Thanks for accepting. You don't need my email because if you need help again you can post back here in Answers and possibly get more answers than from just me.

Accedi per commentare.

Più risposte (2)

Use regionprops() and ask for Centroid

7 Commenti

Mr. Walter Roberson can you please explain it in coding??
YourImage = imread('aaaaa.png');
bw = im2bw(YourImage);
info = regionprops(bw, 'Centroid');
Image_Center = info.Centroid;
%now display it visually
image(bw)
colormap(gray(2))
scatter(Image_Center(1), Image_Center(2), 'r*')
Dear Mr.Walter Roberson thanks a lot for your kind and fast support, i test your code and it gives in result a center point, the next issue how can i use this center point to crop the object in size of 480*160 pixels by imcrop function? Kind regards
x_left = Image_Center(1) - 160/2;
y_bottom = Image_Center(2) - 480/2;
pos = [x_left y_bottom 160 480];
cropped = imcrop(YourImage, pos);
Mr. Walter Roberson i really grateful for your kind support but i test your code of finding the center and it give me the center of full size of binary image not the center of the object in it? so Mr. Walter Roberson can you give me the code of finding the center of an object assuming that the binary image has only one object as the attached image?? Kind Regards
can we make the object in the binary image as an array which it is pixels has value of 1 and we find the center of this array which is the center of the object?? i mean we find the center of array of pixels which has value 1 ??
regionprops should already act that way.
Note: the page you posted, aaaaa.png, has two or more blobs, because it has the grayscale image at the left and the diagram at the right. You need to be operating on just the grayscale image.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by