Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6856165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:51:02+00:00 2026-05-27T01:51:02+00:00

I am trying to find centroid of objects. I have already implemented connected components

  • 0

I am trying to find centroid of objects. I have already implemented connected components labeling and I have developed following code for centroid, it does give result but does not gives correct result:
I have following output matrix i.e matrix_img:

 0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
 0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
 0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
 0     0     0     0     0     0     0     0     0     0     1     1     1     1     0     0
 2     2     2     2     0     0     0     0     0     1     1     1     1     1     1     0
 2     2     2     2     0     0     0     0     1     1     1     1     1     1     1     1
 2     2     2     2     0     0     0     0     1     1     1     1     1     1     1     1
 2     2     2     2     0     0     0     0     1     1     1     1     1     1     1     1
 2     2     2     2     0     0     0     0     0     1     1     1     1     1     1     0
 2     2     2     2     0     0     0     0     0     0     1     1     1     1     0     0
 2     2     2     2     0     0     0     0     0     0     0     0     0     0     0     0
 2     2     2     2     0     0     0     0     0     0     0     0     0     0     0     0
 2     2     2     2     0     0     5     5     5     0     0     0     0     0     0     0
 2     2     2     2     0     0     5     5     5     0     0     0     0     0     0     0
 2     2     2     2     0     0     5     5     5     0     0     0     0     0     0     0
 2     2     2     2     0     0     0     0     0     0     0     0     0     0     0     0

and following is the code

n= max(max(matrix_img));
for k=1:n
a(k)=length(find(matrix_img==k)); 



 sx(k)=0;
    sy(k)=0;
    cx=0;
    cy=0;

for i=1:1:r
    for j=1:1:c
        if(matrix_img(i,j)==k)

           sx(k)=sx(k)+i;
           sy(k)=sy(k)+j;
           cx=sx(k)/a(k);
           cy=sy(k)/a(k);

       end
    end 
end
  fprintf('Centroid of Object %d is %d and %d \n', k, cx, cy);   
end

It gives result like :

Centroid of Object 1 is 7 and 1.250000e+001 
Centroid of Object 2 is 1.050000e+001 and 2.500000e+000 
Centroid of Object 3 is 0 and 0 
Centroid of Object 4 is 0 and 0 
Centroid of Object 5 is 14 and 8 

Object 5 result is correct, object 2 is completely wrong and object 1 is partially wrong.. what shall I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:51:03+00:00Added an answer on May 27, 2026 at 1:51 am

    The values you obtain are the exact centroids for those objects. So you might want to define what you’d expect to get as results.

    To make this more clear, I’ve colored the objects in your matrix. By the symmetry on your cartesian grid, there should be an equal number of points to the left and right of your centroid and the same for above/below the centroid. I’ve drawn a figure with your objects colored in, together with lines to represent the horizontal and vertical center lines. Those are lines for which, we have an equal number of points to the left/right (or above/below) of them that are part of a certain object.

    Their intersection is the centroid, so you can see for object 5 (blue one) that the centroid is at (8, 14). For the other two objects, these center lines do not lie on the integer grid you have: the red object (1) has its centroid at (12.5, 7) which is also the outcome of your code and the green object (2) is centered around (2.5, 10.5).

    You will either have to live with inaccuracy introduced by rounding your centroids (e.g. round(cx)) or you will have to live with the non-integer coordinates of the centroids.

    Graphical representation

    Next to that, I also recommend you vectorize your code as oli showed: this allows you to run your code faster and it is easier to understand when you are somewhat familiar with MATLAB than for loops.

    Perhaps a little note with regard to your string representation: don’t use %d for non-integers, as you see that will cause real numbers to be displayed in scientific notation. I think it’s clearer if you use something like %0.2f as your format string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to find the sqlserver adapter for rails on windows. I have tried getting
I am trying to find the centroid of a contour but am having trouble
I'm trying find a way to have PHP to indicate to the browser that
I'm trying find a way to create mysql users with passwords using php code,
Trying to find code to basically stream audio and visual streams over a network
Trying to find duplicate users by email, who have an item count less than
Trying to find: Show the top-20 PIs who have the largest total amount of
Trying to find the best way to write this SQL statement. I have a
I'm trying find an explanation to the following question, I looked around and haven't
Trying to find the leak in my code I removed all the thing that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.