首页 > 其他分享 >基于已知点云数据的最小外接圆matlab函数

基于已知点云数据的最小外接圆matlab函数

时间:2023-09-20 09:33:05浏览次数:41  
标签:function pmin end yc 外接圆 matlab 点云 y2 pmax

基于已知点云数据的最小外接圆matlab函数 – MATLAB中文论坛 (ilovematlab.cn)

 

%该函数是在其他网站看到的,以此共享。有两种方法(函数)实现。
%第一种比较费时:
function [xc,yc,r] = smallestcircle(x,y)

% This finds the circle of smallest area containing all
% the points of vectors x and y.  The center is (xc,yc)
% and the radius is r.   RAS - 2/28/05

if any(size(x)~=size(y)) | min(size(x))>1
error('Args must be vectors of equal length.')
end
n = length(x);
if n == 1, xc = x(1); yc = y(1); r = 0;
else
  bestr2 = inf;
  for i = 1:n-1
    x1 = x(i); y1 = y(i);
    for j = i+1:n
      x2 = x(j); y2 = y(j); x0 = (x1+x2)/2; y0 = (y1+y2)/2;
      w = sqrt((x2-x1)^2+(y2-y1)^2);
      if w==0, break, end
      A = (x2-x1)/w; B = (y2-y1)/w; C = (x1*y2-y1*x2)/w;
      h = w/2; h2 = h^2;
      pmax = -inf; pmin = inf;
      for k = [1:i-1,i+1:j-1,j+1:n]
        x3 = x(k); y3 = y(k);
        o = A*y3-B*x3+C;
        if o==0, o = 2^(-1074); end
        p = ((x3-x0)^2+(y3-y0)^2-h2)/(2*o);
        if o > 0, if p > pmax, pmax = p; uk = k; end
        else if p < pmin, pmin = p; lk = k; end
        end
        if pmax > pmin, break, end
      end
      if pmax <= pmin
        if pmax > 0, p = pmax; k = uk;
        elseif pmin < 0, p = pmin; k = lk;
        else xc = x0; yc = y0; r = h; return
        end
        r2 = p^2+h2;
        if r2 < bestr2, bestr2 = r2; si = i; sj = j; sk = k; end
      end
    end
  end
  a = x(si); b = x(sj); c = x(sk);
  d = y(si); e = y(sj); f = y(sk);
  t = 2*(a*e+b*f+c*d-b*d-c*e-a*f);
  xc = ((a^2+d^2)*(e-f)+(b^2+e^2)*(f-d)+(c^2+f^2)*(d-e))/t;
  yc = ((a^2+d^2)*(c-b)+(b^2+e^2)*(a-c)+(c^2+f^2)*(b-a))/t;
  r = sqrt(((b-a)^2+(e-d)^2)*((a-c)^2+(d-f)^2)*((c-b)^2+(f-e)^2))/abs(t);
end

第二种比较优化:
function testcircumcircle
%% Test the CIRCUMCIRCLE function
% Generate random data
x = randn(20, 1);
y = randn(20, 1);
% Call CIRCUMCIRCLE
[c, r] = circumcircle(x, y);
% Plot the data as red + symbols
plot(x, y, 'r+');
% Wait for the next plot ...
hold on;
% Create a vector of theta values
dtheta = 0.1;
theta = 0:dtheta:(2*pi+dtheta);
% Plot the circle whose center is [c(1), c(2)] and whose radius is r
plot(r*cos(theta)+c(1), r*sin(theta)+c(2), 'g-')


function [c, r] = circumcircle(x, y)
%% The CIRCUMCIRCLE function
% Generate a feasible random guess
% The last elemeent of p0 is the maximum distance from the origin to a
% point in our data set. All points must be within that distance from the
% origin, so we're guaranteed that the nonlinear constraint in TestIfInside
% will be satisfied
p0 = [0; 0; sqrt(max(x.^2+y.^2))];
% Creating an options structure
options = optimset('Display', 'iter');
% Call FMINCON
P = fmincon(@(p) p(3), p0, [], [], [], [], [-Inf;-Inf;0], [], ...
   @(p) TestIfInside(p, x, y), options);
% Note that for versions of MATLAB prior to MATLAB 7.0 (R14), you would
% need to replace the anonymous functions @(p) p(3) and
% @(p) TestIfInside(p, x, y) with inline functions or regular function
% handles
% Extracting the elements of the P vector; the first two are the center of
% the circle, the third is the radius
c = P(1:2);
r = P(3);


function [c, ceq] = TestIfInside(p, x, y)
%% Nonlinear constraint function for CIRCUMCIRCLE
% The squared distance between our points and the center of the circle is
% less than the radius squared
c = (p(1)-x).^2 + (p(2)-y).^2 - p(3).^2;
% No nonlinear equality constraint
ceq = [];

%最主要的就是后两个函数了,第一个只是测试。

  

  

标签:function,pmin,end,yc,外接圆,matlab,点云,y2,pmax
From: https://www.cnblogs.com/yibeimingyue/p/17716466.html

相关文章

  • Matlab决策树对空气质量和天气温度及天气数据做交通出行推荐预测|附代码数据
    全文链接:http://tecdat.cn/?p=31784原文出处:拓端数据部落公众号最近我们被客户要求撰写关于决策树的研究报告,包括一些图形和统计输出。为解决城市交通拥堵问题,本文提出了一种基于Matlab决策树的交通预测方法,我们通过采集上海地区的空气质量数据和温度数据,帮助客户在Matlab中......
  • m基于码率兼容打孔LDPC码BP译码算法的matlab误码率仿真
    1.算法仿真效果matlab2022a仿真结果如下:2.算法涉及理论知识概要码率兼容打孔LDPC码BP译码算法是一种改进的LDPC译码算法,能够在不同码率下实现更好的译码性能。该算法通过在LDPC码中引入打孔操作,使得码率可以灵活地调整,同时利用BP(BeliefPropagation)译码算法进行迭代译码,提高了......
  • m基于码率兼容打孔LDPC码BP译码算法的matlab误码率仿真
    1.算法仿真效果matlab2022a仿真结果如下:   2.算法涉及理论知识概要       码率兼容打孔LDPC码BP译码算法是一种改进的LDPC译码算法,能够在不同码率下实现更好的译码性能。该算法通过在LDPC码中引入打孔操作,使得码率可以灵活地调整,同时利用BP(BeliefPropagation)译......
  • 安装matlab少量版
    安装matlab完整版本往往很大,其中很多工具箱也不一定总会用到,下面是一个参考链接,推荐参考:https://www.tiger2doudou.com/doku.php/coding:matlab:a_clean_install_list PS:2017b以上有MappingToolbox按照需求安装可以大大减少体积~附上:MATLAB2017b安装教程参考https://mp......
  • 图像去噪及其Matlab实现
    ✅作者简介:热爱科研的算法开发者,Python、Matlab项目可交流、沟通、学习。......
  • Matlab-resample
    1.resample步骤1:低通滤波首先,resample函数会应用一个低通滤波器,以滤除输入信号中高于重采样频率的频率成分。这是为了防止混叠现象,即重采样后出现在新采样率频带之外的频率成分。在MATLAB中,默认使用一个FIR滤波器进行低通滤波,滤波器的设计根据重采样因子和滤波器长度来确定。此外......
  • 基于DNN深度学习网络的OFDM+QPSK信号检测算法matlab仿真
    1.算法运行效果图预览   2.算法运行软件版本matlab2022a  3.算法理论概述        正交频分复用(OFDM)是一种多载波调制技术,已经广泛应用于数字通信领域。OFDM信号检测是接收端的关键问题之一,目的是将接收到的OFDM信号恢复为原始数据。由于OFDM信号具有高带......
  • Matlab : 数据类型与文件读写
    学习一门技术的最佳方式之一是阅读官方文档。MATLAB官方文档提供了关于各种主要数据类型的详细介绍。数值类型(numeric)在MATLAB中,数值类型的变量默认为双精度浮点数(double),但可以使用类型转换将其转换为其他数值类型。以下是MATLAB支持的数值类型:double:双精度浮点数single:单精度......
  • 【语音压缩】基于adpcm实现语音信号压缩与解压缩附Matlab实现
    ✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。......
  • 【配色优化】基于遗传算法进行图形着色优化附matlab代码
    ✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。......