function [ centroid, orientation ] = locate( segment_file )
%LOCATE Summary of this function goes here
% [ loc, angle ] = locate( segment_file )

im = imread(segment_file,'bmp');
im = double(im);
M00 = moment(im,0,0);
M01 = moment(im,0,1);
M10 = moment(im,1,0);

centroid = [M10/M00, M01/M00];
M11 = moment(im,1,1) - centroid(1)*centroid(2)*M00;
M02 = moment(im,0,2) - centroid(2)*centroid(2)*M00;
M20 = moment(im,2,0) - centroid(1)*centroid(1)*M00;

%M11 = moment2(im,1,1,centroid(1),centroid(2));
%M20 = moment2(im,2,0,centroid(1),centroid(2));
%M02 = moment2(im,0,2,centroid(1),centroid(2));

orientation = atan2(2*M11,M20-M02)/2;



function [ m ] = moment( im,ox,oy )
%MOMENT find the moment of the im
%  m = moment(im,ox,oy);
m = 0;
for y = 1:size(im,1)
    for x=1:size(im,2)
        m = m + x^ox*y^oy*im(y,x);
    end
end

function [ m ] = moment2( im,ox,oy,cx,cy )
%MOMENT find the moment of the im
%  m = moment(im,ox,oy);
m = 0;
for y = 1:size(im,1)
    for x=1:size(im,2)
        m = m + (x-cx)^ox*(y-cy)^oy*im(y,x);
    end
end
