Steve on Image Processing

August 31st, 2007

Intensity-weighted centroids

One the measurements provided by regionprops is 'Centroid'. Here's an example of labeling binary objects, computing the centroid of each object, and plotting the centroid location on top of the image.

I = imread('text.png');
imshow(I)
L = bwlabel(I);
s = regionprops(L, 'Centroid');
hold on
for k = 1:numel(s)
    plot(s(k).Centroid(1), s(k).Centroid(2), 'r*')
end
hold off
xlim([0 60])
ylim([0 60])

Last week, reader Daphne asked how to compute the intensity-weighted centroid. That is, if each labeled region corresponds to a region in a gray scale image, how do you compute the centroid weighted by the gray scale pixel values? The easiest way, I think, is to use the both the PixelIdxList and PixelList properties from regionprops. Let's try it with a simple synthetic image (download the image from here):

I = imread('spheres.png');
imshow(I)

Now let's threshold and label it:

bw = I < 255;
bw = imfill(bw, 'holes');
L = bwlabel(bw);
imshow(label2rgb(L, @jet, [.7 .7 .7]))

Next, get the PixelIdxList and PixelList for each labeled region:

s = regionprops(L, 'PixelIdxList', 'PixelList');

Each row of the PixelList for a region contains the x and y coordinates of a pixel in that region.

s(1).PixelList(1:4, :)
ans =

    48    94
    48    95
    48    96
    48    97

We can get the grayscale value of pixels in a region by indexing into I with PixelIdxList. By weighting those values with the x and y coordinates, we can compute the centroid.

idx = s(1).PixelIdxList;
sum_region1 = sum(I(idx));
x = s(1).PixelList(:, 1);
y = s(1).PixelList(:, 2);

xbar = sum(x .* double(I(idx))) / sum_region1
xbar =

   74.6128

ybar = sum(y .* double(I(idx))) / sum_region1
ybar =

   92.5121

Let's do that in a loop and superimpose the centroid locations on the image.

imshow(I)
hold on
for k = 1:numel(s)
    idx = s(k).PixelIdxList;
    pixel_values = double(I(idx));
    sum_pixel_values = sum(pixel_values);
    x = s(k).PixelList(:, 1);
    y = s(k).PixelList(:, 2);
    xbar = sum(x .* pixel_values) / sum_pixel_values;
    ybar = sum(y .* pixel_values) / sum_pixel_values;

    plot(xbar, ybar, '*')
end
hold off


Get the MATLAB code

Published with MATLAB® 7.5

129 Responses to “Intensity-weighted centroids”

  1. Daphne replied on :

    Hi Steve,
    Thanks for the input. This works nicely and pretty quickly. However, it does require a binary image – i.e., a threshold value is needed. This is not so straightforward with an image that has less than perfect/clean background.

    Will try to incorporate this, perhaps with the brightest pixel cutoff I had mentioned in my previous comment. That is, only blobs with a pixel > thresh will be considered at all.

    A more general question, my original images just appear as nothing (plain gray) with imshow, but are beautiful with imagesc. Why is that ?
    I can email a sample figure if you like.

    Daphne

  2. Steve replied on :

    Daphne—The binary image is just a way to represent the set of pixels associated with each region. If you don’t know which pixels belong to each object, then I can’t think of a way to calculate the centroids. It’s like being asked to compute a definite integral without being told the limits of integration.

    imshow assumes certain black-to-white dynamic ranges for gray scale images. For example, if the input image is uint8, then 0 is displayed as black and 255 is displayed as white. If the input image is uint16, then 65535 is displayed as white. For double and single images, 1 is displayed as white. imagesc, on the other hand, displayes the image in image-dependent fashion. The minimum pixel is displayed using the first colormap color, and the maximum pixel is displayed using the last colormap color. To get a similar effect using imshow, use this syntax:

    imshow(I, [])
    

    See the imshow doc for an explanation. You might also want to look at my “All about pixel colors” posts between January and April 2006.

  3. suwanna replied on :

    Hello Steve,

    I wonder how i can find the proper threshold value of the picture.Could you please tell me how the theshold come and how i can get the proper threshold value ?
    please explian to me.
    I am new here and i’ve just started to study about Image Processing,so i do not know much about it but i want to learn about it.
    Regards
    suwanna

  4. Steve replied on :

    Suwanna—You might try using the function graythresh.

  5. Chaitanya replied on :

    Hi Steve,
    thanks for the nice solution to the gravity-center question. Just a minor issue: I noticed I cannot get the following line to work:

    >> bw=I > bw = im2bw(I,0.9);
    >> bw=1-bw;
    sorry the line-breaks were messed up in the last post- feel free to delete it.
    Regards,
    Chaitanya.

  6. Steve replied on :

    Chaitanya—Where did that code come from? It doesn’t resemble any code in this post. Your first line has a syntax error (two equals signs).

  7. suwanna replied on :

    Hello Steve
    Thanks very much .
    i will try to read it.
    Regards
    suwanna

  8. Tariq replied on :

    Hi Steve,

    Great tutorial. I wanted to ask you – the objects that I am trying to find the centroid of are not perfect spheres – is there anyway to manipulate a roughly spherical object into being represented as one. I’ve already applied a threshold to isolate the objects of interest, and i’ve also reduced noise by removing small objects [bw = bwareaopen(bw,30);]. However, due to the nature of the areas left, your code returns multiple centroids.

    So in a nutshell is there anyway of approximating crude blobs as spheres?

    Many thanks in advance.

  9. Steve replied on :

    Tariq—You might want to try a morphological closing step (imclose) prior to calling bwareaopen.

  10. Vipul Gupta replied on :

    Thanks Steve. It works great.

  11. k chaitanya replied on :

    Hi Steve
    i doing project on “automatic signature verification system” .for that i am reading IEEE papers.in some papers authors are takeing “signature centroid” as feature.using above method i computed “Intensity-weighted centroids”.it is giving so many centroid points.but in IEEE papers they are showing only one centroid point.please tell me the difference between “signature centroid “, and “Intensity-weighted centroids”.
    is their any difference between them.

  12. Steve replied on :

    K—The intensity-weighted centroids is giving you one centroid per connected component. You’ll need to preprocess the signature block to turn it into a single component. For the unweighted centroid, which is probably what you want, take the mean of the row coordinates and the mean of the columns coordinates for all the pixels belonging to the signature.

  13. k chaitaya replied on :

    THANK YOU
    STEVE

  14. Ana replied on :

    Hello Steve,
    I tried your method with an image with regularly spaced spheres, but the centroids were not marked in an orderly manner (for example, centroids are not marked columnwise from left to right of image). If the spheres were arranged regularly in rows and columns, how can we ensure that the values in ‘xbar’ and ‘ybar’ are in an orderly manner?

  15. Steve replied on :

    Ana—The connected-components labeling functions (bwlabel and bwlabeln) do scan the image in columnwise order, from left to right. The particular object ordering that results, though, depends on where the scan first encountered a pixel belonging to each object. For example, an object below another object will get labeled first if the lower object sticks out even a tiny bit to the left. You can use functions such as sort and sortrows to rearrange the measurements however you wish.

  16. sylvia replied on :

    hello sir,

    i want to find centroid for my ultrasound breast image so as to find out the seed points to grow a region of interest..
    Actually in the ultrasound image all lesion parts lye on the centre of the image only.
    i calculated the feature values and got the seed points..
    but some seed points are lying on the outside of the region of interest also..
    so i thought of calculating the centroid and then find out the seed points in that particular area..
    is my thought of doing this is write r not sir…
    please give suggestions and send the centroid coding.
    thank u..

  17. Lakshmi replied on :

    Hi Steve,

    I am working on hand written characters.

    I am trying to import a 35*35 image and wanted to find the centroid of the character not for the whole image but according to the character aligment and its pixel intensity I wanted to calculate the cenroid based on the x-coordinate and y-coordinate distance using the formula:

    Hence, Cx =(∑ xi) /N and Cy = (∑ yi)/N

    for all 1 to N pixels. Since, Centroid (Cx, Cy) of a character is :
    For a given character the summation of product of each pixels x-coordinate distance and its intensity value, divided by total no. of character pixels (N) gives x-coordinate of the centroid.

    How to find the x-coordinate distance from a pixel and like-wise the y-cordinate distance?

    Steve, Can you please let me know abt this…as this would be a great help to me.

    Thanks in Advance,
    Lakshmi

  18. Steve replied on :

    Sylvia—This post contains code and explanations for computing centroids.

  19. Steve replied on :

    Lakshmi—The centroid calculation involves the x-coordinate and y-coordinate of the pixel, not the distance from the pixel. This post shows how to do the calculation.

  20. Lakshmi replied on :

    Thank you Steve.

    My question was to find the pixel’s distance from x-axis.

    I used
    a=imread(‘img.bmp’)
    [y,x]=find(a)

    and got the ans for tht!

    Thanks once again

  21. sylvia replied on :

    Thank u Steve..
    Now i am working in ultrasound breast image.. my aim is to segment the leion area.. for this i used region growing technique.. i have found out seed point using texture features and grown the region by fixing the threshold(global thresholding).. but it works for only 2 images.. for the other images, other areas including the lesion area is segmented out..what i can i do for this type of problem. please clear my doubt and send me the coding to segment my lesion area alone.. waiting for your reply steve.. thank u.

  22. Steve replied on :

    Sylvia—I’m sorry, but image segmentation usually involves application- or data-set-specific algorithm development. It’s not something I can do for you or send you code for.

  23. Shahnawaz Hussain replied on :

    Dear Steve

    I want to achieve the same results as Daphne, except in my case, I am using a real life actual image of crystal lattices showing many atoms in rows and columns. Your synthetic image was good quality; but on my image I can’t use the simple thresholding you used, in my image the atoms are all of different intensities, thus I have used multi-level thresholding using the grayslice function. Can one achieve the same results and obtain the centroide for each atom.

  24. Mj replied on :

    Hello Steve,

    How could we use the MajorAxis and MinorAxis length to determine the location of an object in an image? I know that using the Centroid we could easily determine the location by using the x and y coordinates of the centroid.
    But how could that be done using Major and Minor axis?

  25. Steve replied on :

    Shahnawaz—You will need to find a way to solve the segmentation problem somehow. If you can’t identify which pixels belong to each atom, then I don’t see how you can compute the centroids.

  26. Steve replied on :

    Mj—MajorAxisLength and MinorAxisLength give you no information about the location of an object in an image.

  27. Mj replied on :

    Then what are the properties (other than centroid) that could give me information about the location of an object in an image?

  28. Steve replied on :

    Mj—See the documentation for regionprops.

  29. Shahnawaz replied on :

    Dear Steve

    How can I extract all the x and y coordinates in any image; then create separate images of Y and Y coordinates. I can at present extract certain X and Y coordinates but not all of them automatically.

    Regards Shahnawaz

  30. Steve replied on :

    Shahnawaz—Extract the x and y coordinates of what? And what is an image of X and Y coordinates?

  31. Shahnawaz replied on :

    Dear Steve

    Apologize for the lack of clarity, I meant extracting all the X and Y pixel coordinates from any image automatically; and create a pictorial representation (image) of all the X and Y pixel coordinates seperately. At present I can extract only certain pixel coordinates not all of them at once.

    Regards Shahnawaz

  32. Steve replied on :

    Shahnawaz—You just repeated your question. I still don’t understand it.

  33. Shahnawaz replied on :

    Dear Steve

    Sorry about this I understand but have not got it across well. I believe (imtool) in matlab does something similar but only one location and pixel value at a time using the mouse;

    I want to do it as a global operation and extract all the coordinate locations and pixel values in my case for each atom; I think it should work for any image. I want to store x and y location information separately as an image of location values one for x and one for y. I hope this makes more sense.

    P.S Can you delete the two which did not make sense.
    I have managed to extract some of them x and y coordinates and pixel values I am dealing with a graylevel image.

  34. Steve replied on :

    Shahnawaz—Try meshgrid.

  35. Suzie replied on :

    Dear steve

    How can I find MaxIntensity,MeanIntensity from regionprops for grayscale image?

  36. Steve replied on :

    Suzie—MaxIntensity and MeanIntensity measurements are provided by regionprops in the latest release, R2008a. If you have an earlier version, see the techniques used in this post.

  37. Josh replied on :

    Steve, I have used regionprops to find centroids of objects in binary images for a class, and I am inquiring as to the algorithm used to do this because we have to explain all methods used. Is it safe to assume that groups of white pixels are found based on sharing common sides (no corner contact, that would result in 2 objects) then the common formula for finding the centroid is applied to each white pixel object? I have found reference to this on other sites but was wondering if you could offer me more insight to the algorithm this function uses.
    Thanks again,
    Josh

  38. Steve replied on :

    Josh—Presumably, you used bwlabel before you called regionprops. bwlabel computes connected components. By default, it uses “8-connectivity,” meaning that pixels connected at the corners are assumed to belong to the same object. You can also get bwlabel to use “4-connectivity,” meaning that pixels connected only at the corners belong to different objects. The reference page for bwlabel describes the algorithm used and gives a reference. And yes, regionprops uses the “common formula” for the centroid. You could look at the ComputeCentroid subfunction of regionprops to see the actual implementation.

  39. patil rajendra V. replied on :

    hello sir
    I want to calculate the centroid of edges/ Lines. I am working on image segmentation Project. I have obtained edge map of image by using Phase Congruency(Peter Kovesi). After getting binary image of edge map. I have used bwlabel to label each edge/ line in the image. Now I want to find the centroid of each edge. Then want to use K means clustering for clustering long edges. I am facing problem in finding the centroid of edge/ Line. How it could be done?

    Thanks in advance
    Patil Rajendra

  40. Steve replied on :

    Patil—Use regionprops.

  41. shah replied on :

    Dear Steve

    How can I display all the x coordinates per row and y coordinates per row individually if I have many objects in a binary image in rows and columns using pixellist ???

    regards shah

    I have tried something like this below were ZW is a binary image.All I get is one x row and one y row

    % Computing X Y Coordinates

    LABEL= bwlabel(ZW)
    figure, imshow(LABEL)
    LABEL2=regionprops(LABEL,’PixelList’)
    x = LABEL2(1).PixelList(:, 1)
    y = LABEL2(2).PixelList(:, 2)

  42. shah replied on :

    Dear Steve

    How can I display the xy coordinates on the binary objects ????

  43. Steve replied on :

    Shah—Your code for extracting x and y coordinates looks correct. If LABEL2(1).PixelList has only one row, that means the first object labeled has only one pixel.

  44. shaz replied on :

    Dear Steve,

    I have found a bug in region groups operators, in my binary image I have many objects which represent atomic rows and columns etc. It turns out the first data value does not represent the first top left row object. It seems the first data value represents the 6th atom in the row which is object 1 and the first object in the row is represents the 1oth object. Surely this is a design fault in the function. Is there a way to overcome this so the first measurement value represents the first top left hand atom ?? Has this bug been fixed ???

  45. Steve replied on :

    Shaz—The behavior you describe is not a bug. There is no single correct way to order labeled objects. The Image Processing Toolbox function bwlabel searches along columns first because this is the way MATLAB orders elements in memory. See my post on bwlabel search order. If you want a different labeling order, you can postprocess the output of bwlabel, and I describe how to do this in my post on relabeling a label matrix.

  46. DP replied on :

    Hi steve;

    i have two binary images, one is measured and another is computed. in both images there are several small structure (i.e. rice grains). i want to measure 2D distance between each rice grains in each image with respect to the other, (i. e. the distance between measured and computed).
    could you please give me some ideas?

    Thank you.
    DP

  47. Steve replied on :

    DP—Use regionprops to compute the centroids, and then use hypot to compute the distances between the centroids.

  48. DP replied on :

    Hi Steve,

    Thanks for your email. i already apply regionprops and hypot function according to your example demo, but i have 81 centroids in computed binary image and 81 centroids on measured binary image, i added these 2 images and now, i want to find the distance between nearest neighbour between each computed and measured centroids on image. my image size is 256×256 and pixel size is 0.388mm x0.388mm. is this still possible to measure the distance between nearest-neighbour between centroids?
    sincerely yours,DP

  49. DP replied on :

    Hi steve,

    could you please look at the last few line of the code, am i doing the correct for loop? i am not getting expected answer for centroid distance.

    Thankz.
    DP

    I1 = imread(‘PP5_recon_01.tif’);
    info1 = imfinfo(‘PP5_recon_01.tif’)

    info1.XResolution;
    info1.YResolution;
    info1.ResolutionUnit;

    L = bwlabel(I1);
    s = regionprops(L, ‘Centroid’)

    I2 = imread(‘PP5_Measured_0_0.tif’);
    info2 = imfinfo(‘PP5_Measured_0_0.tif’)

    info2.XResolution;
    info2.YResolution;
    info2.ResolutionUnit;

    L1 = bwlabel(I2);
    s1 = regionprops(L1, ‘Centroid’)

    for k = 1:77 % number of centroids
    delta_x(k) = s(k).Centroid(1) – s1(k).Centroid(1);
    delta_y(k)= s(k).Centroid(2) – s1(k).Centroid(2);
    end;
    pixel_distance = hypot(delta_x, delta_y)

  50. Steve replied on :

    DP—Debugging user code is a bit beyond the scope of this blog, but I did notice one possible issue. If objects in the two images have different sizes and extents, then there’s no guarantee that bwlabel will label them in the same order. You’ll have to write additional code that somehow matches up corresponding objects in the two images. Also, I don’t really understand your question in comment #48.

  51. DP replied on :

    Thanks steve.

  52. Yuanming replied on :

    Dea Steve,
    You’ve introduced the intensity weighted centroid, what about squared intensity weighted centroid? Do you know any comparison between these two? Say, for example, which gives a better location under what condition? Thanks

  53. Steve replied on :

    Yuanming—Squared-intensity weighted centroid can be computed using the same basic procedure given in this post. You get to decide whether that measurement is meaningful and useful to you. :-) Just from a few moments thought (pun intended), I imagine it would bias the measured location toward the location of the highest-valued pixel.

  54. shahn replied on :

    hello Steve,
    I am trying to create an intensity plot from an atomic image. The image contains silicon and a higher silicon germanium concentration, which means that part produces more intensity. Currently I have tried to remove the backhround image from the atomic image by using imsubtract and strel ?? Would that have an affect on the resultant intensity plot ???

    [I deleted the code for space reasons. The code looked like a copy of the nonuniform illumination compensation demo in the Image Processing Toolbox. -Steve]

  55. Steve replied on :

    Shahn—If you subtract one signal from another, then it will of course affect the resulting plot. Or did I misunderstand the intent of your question?

  56. shahn replied on :

    The background intensity ofcourse would affect the resultant histogram intensity plot, thus it would not tally with the real elemental concentrations in the image. By removing the background intensity image I am assuming one would be left with the actual element concentrations in the atomic image. Thus any imhist plot would give precise concentration measurements.

    Do the functions imsubtract and strel have an affect on the resulting intensity and subsequently chanage the atomic image composition. ??????
    I am finding some discrepencies in my imhist plot of the atomic image with the background removed???
    Regards

  57. shahn replied on :

    You have above in your example how to find the x and y coordinates of your binary objects using pixellist ??? what if you had 100 columns and 100 rows what would be the best procedure to extract the x and y coordinates ???

  58. Steve replied on :

    Shahn—Comment 56: I do not understand enough about your application and data to give you a good answer, or really to even understand your question. Image subtraction and dilation/erosion would of course change the image intensity. What purpose is there in an image processing operation that has no effect on pixel values?

    Comment 57: I would still use PixelList. Why not?

  59. Charlie replied on :

    How much more coding would be required to the first example to output the x and y cordinates and the respective area for each letter?

  60. Steve replied on :

    Charlie—The following line computes the x- and y-coordinates and respective area for each connected component:

    s = regionprops(L, 'PixelList', 'Area');
    

    Note that some letters (for example, “i”) may have more than one connected component.

  61. shahn replied on :

    Hello Steve

    Instead of superimposing a star on the image to show the centroide. How would you superimpose a numerical value of the centroide on the binary object ??? In my work this is more useful.

    Regards Shahnawaz

  62. Steve replied on :

    Shahn—My November 17, 2006 post shows you how to do it.

  63. murat replied on :

    Hi Steve,
    I have an rgb image of a kind of cream and it contains some small black particles (black dots). In colormap I can see that black particles composed of 10-15 pixels (according to its size) and in grayscale usually they have a pixel value smaller than 100 (in 0-255). So first I want to label all particles in the image which have a average pixel value under 100, count them (how many dots in my image) and finally measure the approximate diameter of these dots. Thank you in advance Steve..
    regards

  64. Steve replied on :

    Murat—Use rgb2gray, then threshold using the MATLAB < operator, and then label the objects using bwlabel. Use regionprops to compute measurements that might be useful in estimating the diameter (bounding box, area, perimeter, ellipse parameters).

  65. Travis replied on :

    I found this post useful for finding the intensity-weighted centroid of a gray scale image. However, I need to go one step further… is there a way to compute the intensity-weighted orientation of an object (or region) in a gray scale image?

    Thanks,
    Travis

  66. Steve replied on :

    Travis—What is your definition of intensity-weighted orientation? For “flat” (nonweighted) regions, regionprops defines object orientation as the orientation of the major axis of an ellipse that has the same second-order moments as the region. It’s not obvious to me how to extend this definition to a nonflat region.

  67. Travis replied on :

    I believe I found a solution… By using the gray scale pixel values for intensity, the calculations for uxx, uyy, and uxy are weighted. These parameters are then used to calculate orientation the same way regionprops does for binary images.

    The part of the code in regionprops for the non-weighted case is:

    uxx = sum(x.^2)/N + 1/12;
    uyy = sum(y.^2)/N + 1/12;
    uxy = sum(x.*y)/N;
    

    I changed this to:

    uxx = sum((x.^2.*PixelWeights)/sum_region)-xbar^2;
    uyy = sum((y.^2.*PixelWeights)/sum_region)-ybar^2;
    uxy = sum((y.*x.*PixelWeights)/sum_region)-xbar*ybar;
    

    Where xbar and ybar are the intensity-weighted centroids (now available as an output from regionprops as “WeightedCentroid”) and sum_region is the sum of the gray level area. Hopefully my approach is correct… it seems to make sense on some test images.

    It would be nice to have regionprops expanded to include more properties for grayscale images. Thanks for the input.

  68. Steve replied on :

    Travis—Your method sounds promising. Are there any specific grayscale properties you are particularly interested in? Thanks for your suggestions.

  69. Travis replied on :

    Just that intensity-weighted second moment (orientation)… for now! Thanks.

  70. Matt replied on :

    I have some separated blobs to detect. Each one should have a Gaussian shape. Usually the bwlabel works well but sometimes two blobs are found as 1 object when they should really be two objects.
    What do you think to do about this? Could I detect that one feature has 2 peaks?
    Thanks Matt

  71. Steve replied on :

    Matt—You might try looking at the marker-controlled watershed segmentation demo.

  72. tony replied on :

    this is really good

  73. shahnawaz replied on :

    Hi Steve

    After one has found the center points of two binary objects as above in the example of sphere.png.
    Could one also find the distance between the two sheres and the x and y coordinates of the spheres ?????

    How can it be done within matlab ???

  74. fotis replied on :

    hi Steve

    i would like to ask, how is it possible to locate the center of the white spots, on the spheres surface (spheres.png).
    It is not the centroid as it can be seen above, (and at least in my case) is not the max or mean intensity (on the edge of the sphere e.g. you can have similar intensity).
    it can be considered as the source-center.

    can you help me with this??

  75. Steve replied on :

    Fotis—What does “center” mean to you, if not centroid?

  76. fotis replied on :

    hi again,

    i would say that in the spheres image,
    the “center” is the local min. inside the spot,
    but not the global min. that is on the edge of the spot.
    (the background is 0 of course)

    thanks for your help,
    Fotis

  77. shahnawaz replied on :

    Hi steve how would you find measure the distance between each sphere from center to center of mass etc. Is there a built in mthod in Matlab ???

  78. Steve replied on :

    Shahnawaz—Use the distance formula or hypot.

  79. Steve replied on :

    Fotis—Use imfill to fill in the dark spots. Then take that result and subtract from it the original image. Then threshold and use regionprops to compute the weighted centroids.

  80. shahnawaz replied on :

    Hi Steve I already have centroid values worked out so I guess I could just take the x,y coordinates and work out the distances using hypot ?? how would the distance values be stored ???????

    Regards Shahnawaz

  81. shahnawaz replied on :

    Hi steve or use square root (x^2 + y^2)

  82. Steve replied on :

    Shahnawaz—Yes, that’s the distance formula I was referring to, although to be more accurate I would write it as:

    sqrt((x1-x2).^2 + (y1-y2).^2)
    

    And that’s what the hypot function computes.

    As for how to store the values, you can store them however you wish.

  83. shahnawaz replied on :

    Hi Steve

    In your example can you actually indicate the numerical values instead of the stars on the spheres ???

    Are the values for the four stars which indicate center of mass on the spheres stored some where ???

    In my case the actual values are more useful ??

    Regards Shahnawaz

  84. shahnawaz replied on :

    Hi Steve
    Another question based on hypot usage; will hypot work on binary and uint8 images as fare as you now ??? I am dealing with objects contained within binary and graylevel images ??

    Regards Shahnawaz

  85. Steve replied on :

    Shahnawaz—Use the text function.

  86. Steve replied on :

    Shahnawaz—hypot takes two floating-point numbers and returns a floating-point answer. It’s up to you to determine what to pass it.

  87. fotis replied on :

    thanks a lot!!

  88. Shahnawaz replied on :

    Hi there Steve
    I have used the text function in a script I have programmed
    I just get numbers such as 1,2,4 etc. Not actual centroid values ??? My script is below. Would appreciate any insight into the problem. I have used your spheres example to perfect the script before I apply it to my own.

    S = regionprops(L, ‘Centroid’);
    figure, imshow(I); title(['Numerical value indicates Cenetr Of Mass ',char(10),'For each Atom']);
    hold on
    for k = 1:numel(S)
    CC = S(k).Centroid;
    text(CC(1), CC(2), sprintf(‘%d’, k),’HorizontalAlignment’,'center’,'VerticalAlignment’, ‘middle’);

    end
    hold off

  89. Steve replied on :

    Shahnawaz—Hint: the function sprintf is doing exactly what you’re asking it to do.

  90. eva replied on :

    Steve,
    I have started to play a little with images in Matlab and was surprised by regionprops function output. I was expecting that the one pixel bw image should have:
    Centroid: [0.5 0.5] and BoundingBox: [0 0 1 1],
    but Matlab output was:
    Centroid [1 1] and BoundingBox: [0.5 0.5 1 1].
    Looks a little illogical for me… could you explain shortly?
    Eva

  91. Steve replied on :

    Eva—In the default spatial coordinate system for images, a one-pixel image extends from (x,y) = (0.5,0.5) to (x,y) = (1.5,1.5). The centroid of that square is located at (1.0,1.0). The first two elements of BoundingBox give the upper-left corner, and the second two give the width and height of the box.

  92. Aymen replied on :

    salut steve;
    je me demande comment je peux calculer la précision sur les coordonnées des centroides obtenues
    Merci

  93. Aymen replied on :

    Bonjour Steve;
    j’aimerai savoir comment je peux calculer les erreurs sur les coordonnées des centroides trouvées
    Merci
    Aymen

  94. Steve replied on :

    Aymen—I don’t really know, but I’m pretty sure that you’ll need to start with characterizing the errors in the pixel-value measurements. Otherwise you have no information at all about the error in a measurement derived from those values.

  95. sanj replied on :

    Hi Steve,

    After finding centroid how to find sum of pixels present in 4 neigbors from the centroid point

    Thanks,

  96. Steve replied on :

    Sanj—I assume you are looking for some answer other than “add them up”? Can you clarify your question?

  97. zarri replied on :

    hi Steve

    i want to detect the black blobs from a binary image having white background and black blobs. how could i find these blobs and their centroids??? please help me. i tried regionprops but could not get the way.

  98. Steve replied on :

    Zarri—Turn black pixels into white and vice versa:

    bw2 = ~bw;
    
  99. Siddharth Magazine replied on :

    Hi there. My problem is similar to the above described problem. Basically, i need to identify [x,y] location of the black spots without human intervention in the image below, which means, i have all the codes in a matlab (.m) file, and when i run the program (F5), it should be able to scan the (rgb) image, look for the two black dots in the image (http://img195.imageshack.us/f/twopoints1.jpg/), get their pixel values if required to find the distance value (in pixels) between the two dots. This method needs to be like a universal one, which means, no matter where the two black dots in the image are, the program should be able to scan those images, find the two black dots, and give me a distance value (in pixels)on the Matlab workspace. How can i solve this problem? Please help me on this topic asap, as i need to get this problem solved. Thanks.

  100. arni replied on :

    Hi Steve

    terrific post! In fact, all your regionprops are. Find them perfect to explore teach myself some of the ideas in image processing/analysis.

    A question: how would you go about saving these centroids to a binary image to use for further work – for instance as “seeds” to create objects of a certain shape (different from original).
    Thanks a lot.

    Arni

  101. Steve replied on :

    Arni—Good question. I’ll do a follow-up blog on it, maybe next week.

  102. Arni replied on :

    Hello Steve

    That’s great, I look forward to reading it.

  103. sundar replied on :

    Hi Steve,
    1) I have an image which contains some spots that moves over time. I am using regionprops to find the centroid of these spots. Now, I would like to find the distance between the centroid of a spot and its nearest neighbor. Is there an easy way of doing this?
    2) Is it possible to copy the x-coordinates of the centroids of all the spots to a different variable?

    Thanks

  104. sundar replied on :

    Hi Steve,
    In addition to the previous comment (103):
    What I wanted to know was whether I have to open a for loop and then use hypot command between each pair of centroids to find the nearest neighbor, or is there a more direct way?

    Thanks

  105. Steve replied on :

    Sundar—Try searching on the File Exchange for code for searching for nearest neighbors. I vaguely recall that there have been some contributions on this topic. For grabbing all x-coordinates of the centroids:

    s = regionprops(bw, 'Centroid');
    centroids = cat(1, s.Centroid);
    x = centroids(:, 1);
    
  106. sundar replied on :

    Thanks.

    For getting the x-coordinates I did the following:

    centroids = [s.Centroid];
    centroid_x = centroids(1:2:end);

  107. Mark Jones replied on :

    Hi Steve,
    Do you have any info on detecting the centroid of an object with sub-pixel estimation?

    Thanks!

  108. Steve replied on :

    Mark—Assuming you already have a segmentation, you can use regionprops.

  109. Shazza replied on :

    Hi Steve

    I am interested in calculating the mean intensity of objects in a binary mask

    I have used

    I64=I5(8:13,3:7) object coordinates etc
    I70=I5(26:31,3:7)
    mean(mean(I64))
    mean(mean(I70))

    and is this other method the same thing using region groups

    bw = I5 ~= 0;
    L = bwlabel(bw);
    s = regionprops(L, I5, ‘MeanIntensity’);
    a = [s.MeanIntensity];

    but I get different values for the meanintensity why using either method ???? not sure why ???

    any help appreciated

    regards shazza

  110. Steve replied on :

    Shazza—I don’t know. It seems likely to me that you’ll need to do some debugging of your code. Are you sure your object subscripts are correct, for example? Do hand calculations to check the output and your assumptions. Simplify the image, if necessary, in order to make checking your work easier.

  111. Shazza replied on :

    Hi Steve I extracted one object from my image and created a new image matrix which included zero cells and I tested bother versions of the code both produced different answers.

    Without the inclusion of zeros in the matrix each method gave a same answer ???

    Using I64=I5(8:13,3:7) object coordinates etc
    I70=I5(26:31,3:7)

    mean(mean(I64))
    mean(mean(I70))

    I got a completely different answer

    compared to using

    bw = I5 ~= 0;
    L = bwlabel(bw);
    s = regionprops(L, I5, ‘MeanIntensity’);
    a = [s.MeanIntensity];

    but they should give the same answer surely whether there are zeros or not in the object matrix

    Shazza

  112. Steve replied on :

    Shazza—I’m not going to be able to provide you with detailed help on debugging your code here. I can only expand on my previous suggestion: start with extremely simple test cases, step through your code, and double-check all your assumptions about values at each step. Here’s what I get with the two methods for a simple test case:

    >> I = magic(5);
    >> L = ones(5,5);
    >> mean(mean(I(1:5,1:5)))
    
    ans =
    
        13
    
    >> s = regionprops(L,I,'MeanIntensity')
    
    s = 
    
        MeanIntensity: 13
    
  113. shazza replied on :

    After some rigorous testing with simulated matrices I have found the problem. If the objects in the image
    are square or rectangular with different widths or heights I got the correct answers with both methods.

    In my case the objects I am dealing with are round and speherical (oblong etc) I think the meanIntensity
    function fails to provide a correct answer. Bwlabel labels (tags) the objects in the binary image correctly
    so that can’t be the issue. I think the regionprops meaninetnsity may be the issue ??? I would have thought it would
    work for all shapes of objects; is there some other function for none square objects ???

  114. Steve replied on :

    Shazza—For a nonrectangular object, the mean(mean(I(a:b, c:d)) method you are comparing with is not correct. That’s because you are throwing extra zero-valued pixels into the call to mean, which changes the result.

  115. shazza replied on :

    Hello again Steve

    so the regions prop method will be giving the correct mean intensity value ??? and it works for any shape as opposed to the

    I80=square(8:13,1:7)
    mean(mean(I80))

    method which you are saying is only useful for rectangular or square objects ???

  116. Steve replied on :

    Shazza—I’m saying that mean(mean(I80)) computing the mean of a rectangular region of pixels. If that’s not what you want, then mean(mean(I80)) is incorrect.

  117. Bhaskar replied on :

    Hi Steve,

    Could u let me know how does the ‘regionprops.m’ calculate the centroid of an image. I assumed it would be the mean of X and Y axis which is incorrect. Please let me know this.

  118. jatan shah replied on :

    hey,can u please tell me that how can i find center of gravity of an image without converting into binary image?with help of edge detection we can find object and find its center of gravity.

  119. rore replied on :

    hi steve thx for a great code …
    i want to ask u for helped … if i have the same image but there is a one ball in it not 4 balls … and i want to centroid that ball what shall i change in ur code … plz i need ur help … plz plz plz …

  120. Steve replied on :

    Rore—Nothing needs to change.

  121. rore replied on :

    am try to crop 3 of that balls and test the code … it give me an error ..

  122. Steve replied on :

    Rore—Sounds like you’ve a bug in your code then.

  123. rore replied on :

    dear steve …
    am a student i cant write a good code … plz help me .. really i need ur help …
    what about the numbers in the code like [.7 .7 .7]
    and I<255 … and PixelList(1:4, :) ???
    plz give me the informatoin about it …

  124. Coaxen replied on :

    This article was extremely helpful, sir. Many thanks, Steve!

  125. Marcus replied on :

    Hi Steve,
    This might have already been asked but, using your intensity-weighted centroid example, is it possible to identify and plot only the two inferior most spheres/centroids?
    Many thanks,
    Marcus

  126. Steve replied on :

    Marcus—Sure. But can you clarify exactly what you mean by “inferior”?

  127. Marcus replied on :

    Hi Steve,
    Thanks for the reply. I was referring to the two lowest spheres in the image, but no worries, I’ve sorted it! I simply rotated image so the max and max-1 index of L referred to the lowest and second lowest sphere in the image.
    Many thanks,
    Marcus

  128. Jeremy replied on :

    Hi Steve,

    Great, useful posts, thanks.

    I have a question about centroids and user input: is there a way to choose a particular centroid by knowing it’s general region?

    I have an image with four spots on it. Is there a way to have a user click on one of the spots and compute the centroid for just that spot? I can figure out the code (using your helpful blog) for finding centroids and getting user input data, I just don’t know how to combine the two.

    Thanks!

  129. Steve replied on :

    Jeremy—Take a look at the function bwselect; you might find it useful.


MathWorks
Steve Eddins is a software development manager in the MATLAB and image processing areas at MathWorks. Steve coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

These postings are the author's and don't necessarily represent the opinions of The MathWorks.