Nikhil
Nikhil

Reputation: 1309

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

This is my homework question:

Write HW3_func.m as follows:

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

Upvotes: 4

Views: 10335

Answers (1)

Emmet
Emmet

Reputation: 6401

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().

Upvotes: 5

Related Questions