Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate center of gravity of pixels in an image?

Tags:

matlab

This is my homework question:

Write HW3_func.m as follows:

  • function [cogR, cogC] = HW3_func ( f, i )
  • f: input grayscale image
  • i : intensity level to check
  • Function should find all the pixels in f with intensity of i. Then, return the center of gravity of those pixels as [cogR, cogC]. Center of gravity is computed as the average of the row and average of column. If no pixel == i, then return [0,0]

I don't understand how to calculate center of gravity. What I have done is:

  1. Declare a matrix X with the same dimension as the image. Initialize it with all zeros
  2. Find the position of the pixels with the given intensity in the input image and replace those positions in X with 1.

Am I on the right path?

This is what I have right now:

function [ cogR,cogC ] = HW3_func(f,i)

    [r,c] = size(f)
    X = zeros(r,c)
    for k = 1:r
        for j = 1:c
            if f(k,j)==i
               X(k,j)=1;
            end        
        end
    end

    %disp(X)

    cogR=centroid(X);
    cogC=centroid(X,2);

    disp(cogR)
    disp(cogC)

end
like image 848
Nikhil Avatar asked Sep 11 '25 02:09

Nikhil


1 Answers

You probably just want to use find(), e.g.

[row_indices, col_indices, values] = find(f==i)

The CoG coordinates are then, as you said, just the average of the row and column indices, which you now have in two vectors. See mean().

like image 137
Emmet Avatar answered Sep 13 '25 14:09

Emmet