First, let's find x-y coordinates of all the dark pixels
bw = imread('http://i.imgur.com/0LxC6bd.png');
bw = min( bw, [], 3 ) < 50 ; % dark pixels - intensity lower than 50
[y x] = find( bw ); % note that find returns row-col coordinates.
Compute the covariance matrix of the text coordinates
mx = mean(x);
my = mean(y);
C = [ mean( (x-mx).^2 ), mean( (x-mx).*(y-my) );...
mean( (x-mx).*(y-my) ) mean( (y-my).^2 ) ];
You can get the orientation of the ellipse from the eigen vectors and eigen values of C
:
[V D] = eig( C );
figure; imshow( bw ); hold on;
quiver( mx([1 1]), my([1 1]), (V(1,:)*D), (V(2,:)*D), .05 );
Looking at the eigenvectors and eigen values:
V =
-0.9979 -0.0643
-0.0643 0.9979
D =
1.0e+003 *
0.1001 0
0 1.3652
You can see that the eigen-vectors (columns of V
) are approximately the pointing to the -X
direction (first column) and the Y
direction (second column). Examining the eigen values (diagonal of D
) you can see that the second eigen value is much larger than the first - this is the major axis of your ellipse. Now you can recover the orientation of the ellipse:
[~, mxi] = max(diag(D)); % find major axis index: largest eigen-value
Recover the angle from the corresponding eigen-vector
or = atan2( V(2,mxi), V(1,mxi) ) * 180/pi ; % convert to degrees for readability
or =
93.6869
As you can see the major axis of the ellipse is almost 90deg off the horizon.
You can rotate the image back
imrotate( bw, -or );
Drawing an ellipse given the covariance matrix:
th = linspace(0, 2*pi, 500 );
xy = [cos(th);sin(th)];
RR = chol( C ); % cholesky decomposition
exy = xy'*RR; %//'
figure;imshow( bw ); hold on;
plot( 2*exy(:,1)+mx, 2*exy(:,2)+my, 'r', 'LineWidth', 2 );
