I am currently working on writing a version of the RegionProps function for GNU Octave . I have implemented most of this, but I am still struggling with the implementation of several parts. Earlier, I asked about the second central points of the region.
It was theoretically useful, but I had problems with the implementation of the proposals. I get results wildly different from MATLAB (or common sense, for that matter), and really don't understand why.
Consider this test image:

We see that it is tilted 45 degrees along the X axis, with the small and large axes 30 and 100, respectively.
Running through the MATLAB RegionProps function confirms this:
MajorAxisLength: 101.3362 MinorAxisLength: 32.2961 Eccentricity: 0.9479 Orientation: -44.9480
Meanwhile, I didn't even get the axes to the right. I am trying to use these formulas from Wikipedia.
My code so far:
raw_moments.m:
function outmom = raw_moments(im,i,j) total = 0; total = int32(total); im = int32(im); [height,width] = size(im); for x = 1:width; for y = 1:height; amount = (x ** i) * (y ** j) * im(y,x); total = total + amount; end; end; outmom = total;
central_moments.m:
function cmom = central_moments(im,p,q); total = 0; total = double(total); im = int32(im); rawm00 = raw_moments(im,0,0); xbar = double(raw_moments(im,1,0)) / double(rawm00); ybar = double(raw_moments(im,0,1)) / double(rawm00); [height,width] = size(im); for x = 1:width; for y = 1:height; amount = ((x - xbar) ** p) * ((y - ybar) ** q) * double(im(y,x)); total = total + double(amount); end; end; cmom = double(total);
And so my code is trying to use them. I include comments for the values ββthat I get at each step:
inim = logical(imread('135deg100by30ell.png')); cm00 = central_moments(inim,0,0); % 2567 up20 = central_moments(inim,2,0) / cm00; % 353.94 up02 = central_moments(inim,0,2) / cm00; % 352.89 up11 = central_moments(inim,1,1) / cm00; % 288.31 covmat = [up20, up11; up11, up02]; %[ 353.94 288.31 % 288.31 352.89 ] eigvals = eig(covmat); % [65.106 641.730] minoraxislength = eigvals(1); % 65.106 majoraxislength = eigvals(2); % 641.730
I'm not sure what I'm doing wrong. I seem to be following these formulas correctly, but my results are stupid. I did not find any obvious errors in my instant functions, although, to be honest, my understanding of the moments is not the biggest start.
Can anyone see what I'm messing with? Thank you very much.