4

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
4

1 に答える 1

5

あなたはおそらく単に使いたいだけですfind()、例えば

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

CoG座標は、あなたが言ったように、行インデックスと列インデックスの平均であり、現在2つのベクトルにあります。を参照してくださいmean()

于 2013-09-02T21:58:07.617 に答える