element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Experimenting with Gesture Sensors
  • Challenges & Projects
  • Design Challenges
  • Experimenting with Gesture Sensors
  • More
  • Cancel
Experimenting with Gesture Sensors
Forum Trying to understand this center of mass methodology?
  • Challenge Blog
  • Forum
  • Documents
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Experimenting with Gesture Sensors to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 3 replies
  • Subscribers 42 subscribers
  • Views 1029 views
  • Users 0 members are here
  • gesture sensors
Related

Trying to understand this center of mass methodology?

BigG
BigG over 2 years ago

I am working through the Firmware Framework code and I came across this routine for center of mass calculation, which did not make sense at first glance.

I can't find anything online to confirm that this is the correct or maybe best approach. I did find this on stackoverflow but it's not really helpful in confirming if code methodology correct: https://stackoverflow.com/questions/66175912/center-of-mass-algorithm

void calcCenterOfMass(const int pixels[], const unsigned int xres, const unsigned int yres, float *cmx, float *cmy, int *totalmass)
{
  int cmx_numer=0, cmy_numer=0;
  for (unsigned int i = 0; i < xres*yres; i++) {
    cmx_numer += (i%xres)*pixels[i];
    cmy_numer += (i/xres)*pixels[i];
    *totalmass += pixels[i];
  }
  if (*totalmass == 0) {
    *totalmass = 1; // avoid NaN
  }
  *cmx = (float)cmx_numer/(float)(*totalmass);
  *cmy = (float)cmy_numer/(float)(*totalmass);
}



For example, when calculating cmx_numer and cmy_numer, one uses modulus of xres and the other divides by xres. Struggling to work out if correct.

I'm also wondering about the "for" routine too.

So I am seeking feedback.

  • Sign in to reply
  • Cancel

Top Replies

  • dougw
    dougw over 2 years ago +4
    I'm not sure what center of mass you are calculating so I don't know if this is relevant, but... The way I understand it, to calculate the centroid coordinates of a 2d black and white image: the…
  • misaz
    misaz over 2 years ago +4
    It makes sense. They flattened two-dimensional array of 10*6 pixels to one single-dimension 60 pixel wide array. For example, pixel at location (x=6, y=3) is at index 36. Pixels are mapped to signle dimensional…
  • BigG
    BigG over 2 years ago +1
    Thanks dougw and misaz your explanations have really helped.
  • dougw
    0 dougw over 2 years ago

    I'm not sure what center of mass you are calculating so I don't know if this is relevant, but...

    The way I understand it, to calculate the centroid coordinates of a 2d black and white image:

    • the x coordinate of the centroid is the sum of all the x coordinates of each white pixel in the image divided by the number of lines in the image to obtain the average x value.
    • the y coordinate of the centroid is the sum of all the y coordinates of each white pixel in the image divided by the number of columns in the image to obtain the average y value.

    I'm not sure how they get the coordinates of each pixel, but they seem to be dividing by the total number of pixels, which would give a normalized value (between 0 and 1).

    • Cancel
    • Vote Up +4 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • misaz
    0 misaz over 2 years ago

    It makes sense. They flattened two-dimensional array of 10*6 pixels to one single-dimension 60 pixel wide array. For example, pixel at location (x=6, y=3) is at index 36. Pixels are mapped to signle dimensional array as foolows:

    image

    Division and modulus are techniques for revers computing pixel x and y location from array index. For example, imagine you want to get coordinates of 47th pixel from array. If you pass 47 % 10 you get 7. If you divide 47 / 10 you get 4 and as you can see 7 and 4 are coordinates of pixel (X=7, Y=4). In their code:

    • i is index in flat single-dimensional array (for example 47)
    • cmx_numer is X coordinates of canter of mass. It is weighed multiplication of pixel coordinates
    • cmy_number is the same but for Y coordinate.
    • totalmass is sum of all

    Imagine simple row with three pixels and suppose that data from sensor are (10, 20, 30) from left to right. i iterates over 0, 1 and 2. In first iteration you add to cmx_nummer 0 * 10 = 0, in second you add 1 * 20 = 20 and in third you add 2 * 30 = 60. cmx_numer will be 0 + 20 + 60 = 80. Total is 10+20+30 = 60 and you compute X axis of center of mass as 80 / 60 = 1.33 and it makes sense if you take in account that values are accumulated more at right side, you center of mass between 2nd (1st when counted from zero) and 3rd pixels (2nd when counted from zero).

    Code is almost correct, I think. The only issue I see that it does not compute first row and column because they are indexed by 0 hence the value is always multiplied by 0. I think they should add + 1 to both coordinates when computing and then subtract 1 from computed coordinates. But this has minor impact on algorithm in overall.

    • Cancel
    • Vote Up +4 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • BigG
    0 BigG over 2 years ago

    Thanks dougw and misaz your explanations have really helped.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube