This is my homework question:
Write HW3_func.m as follows:
function [cogR, cogC] = HW3_func ( f, i )f: input grayscale imagei : intensity level to checkf 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:
X with the same dimension as the image. Initialize it with all zerosX 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
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().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With