matlab图像识别椭圆的代码_画置信椭圆+MATLAB代码

本文介绍了使用MATLAB进行图像识别椭圆的代码实现,并展示了如何画出置信椭圆。通过提供的代码,读者可以学习到MATLAB在图像处理中的椭圆检测和可视化技巧。
摘要由CSDN通过智能技术生成

MATLAB代码

%plot confidence ellipse
%run in MATLAB2018b

t = [0.9 1.1 4.8 3.2 7.8 6.8 11.6 1.6 1.2 7.2 3.2]';
x1 = [67.3 111.3 173.0 80.8 199.7 139.4 368.2 95.7 109.6 196.2 102.2]';

% t = rand(100,1);
% x1 = rand(100,1);

A = [t,x1];
p = 0.95;
errorEllipse(A,p)

function errorEllipse(datamatrix,p)
%print 2-demension confidence ellipse
%In:n×2 matrix, confidence probability p

data = datamatrix;
covariance = cov(data);
[eigenvec,eigenval] = eig(covariance);

[sortEigenval,index] = sort(diag(eigenval),'descend');
sortEigenvec = eigenvec(:,index);

largestEigenval = sortEigenval(1);
smallestEigenval = sortEigenval(end); %find the minimum eigenvalue
largestEigenvec = sortEigenvec(:,1); %find the maximum eigenvector

angle = atan2(largestEigenvec(2), largestEigenvec(1)); %calculate the angle between x-axis and the maximum eigenvector, [-pi,pi]

if(angle < 0) 
    angle = angle + 2*pi;
end

avg = mean(data); %calculate the mean of two columns of data

%configure the parameters of the confidence ellipse
chisquareVal = sqrt(chi2inv(p,2)); %chi-square value
thetaGrid = linspace(0,2*pi); 
phi = angle; %rotation angle
X0=avg(1);
Y0=avg(2); 
a=chisquareVal*sqrt(largestEigenval); %the wheelbase length
b=chisquareVal*sqrt(smallestEigenval);

ellipseXR = a*cos( thetaGrid ); %onto rectangular axis
ellipseYR = b*sin( thetaGrid );

R = [ cos(phi) sin(phi); -sin(phi) cos(phi) ]; %rotation matrix

rEllipse = [ellipseXR;ellipseYR]' * R; %rotation

plot(rEllipse(:,1) + X0,rEllipse(:,2) + Y0,'-',data(:,1), data(:,2), '.') %print
axis square
end

运行结果:

9f9ca5bbd03e6aeec861d871afe1c8d0.png
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值