在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → Ransac算法拟合直线/圆

Ransac算法拟合直线/圆

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.14M
  • 下载次数:52
  • 浏览次数:1686
  • 发布时间:2019-04-24
  • 实例类别:C#语言基础
  • 发 布 人:xiaoyouxia7380
  • 文件格式:.rar
  • 所需积分:2
 相关标签: Ransac

实例介绍

【实例简介】

【实例截图】

from clipboard

【核心代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace Ransac
{
    public partial class FormMain : Form
    {
        //成员
        Bitmap bmpSample;
        List<Point> points;

        public FormMain()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormMain_Load(object sender, EventArgs e)
        {
            btnReset_Click(null, null);
        }

        /// <summary>
        /// 重置样本点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReset_Click(object sender, EventArgs e)
        {
            lblFormula.Text = "";
            txtRandomPoints.Text = "";
            pbSample.Image = null;
            bmpSample = new Bitmap(pbSample.Width, pbSample.Height);
        }

        /// <summary>
        /// 得到样本点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbSample_Click(object sender, EventArgs e)
        {
            MouseEventArgs me=(MouseEventArgs)e;
            txtRandomPoints.Text  = string.Format("({0},{1}),", me.X, me.Y);
            DrawPoint(new Point(me.X, me.Y));
        }

        /// <summary>
        /// 在图例上画点
        /// </summary>
        /// <param name="p"></param>
        private void DrawPoint(Point p)
        {
            Graphics g = Graphics.FromImage(bmpSample);
            Pen pen = new Pen(Color.Red, 2);
            g.DrawEllipse(pen, p.X - 1, p.Y - 1, 2, 2);
            g.Flush();
            pbSample.Image = bmpSample;
        }

        /// <summary>
        /// 在图例上画直线
        /// </summary>
        /// <param name="line"></param>
        private void DrawLine(Line line)
        {
            Graphics g = Graphics.FromImage(bmpSample);
            Pen pen = new Pen(Color.Blue, 1);
            double x1 = 0;
            double y1 = line.GetY(x1);
            double x2 = pbSample.Width;
            double y2 = line.GetY(x2);
            g.DrawLine(pen, (int)x1, (int)y1, (int)x2, (int)y2);
            g.Flush();
            pbSample.Image = bmpSample;
        }

        /// <summary>
        /// 在图例上画圆
        /// </summary>
        /// <param name="circle"></param>
        private void DrawCircle(Circle circle)
        {
            Graphics g = Graphics.FromImage(bmpSample);
            Pen pen = new Pen(Color.Blue, 1);
            int x = (int)(circle.A - circle.R);
            int y = (int)(circle.B - circle.R);
            g.DrawEllipse(pen, x, y, (int)circle.R * 2, (int)circle.R * 2);
            g.Flush();
            pbSample.Image = bmpSample;
        }

        /// <summary>
        /// 得到样本点列表
        /// </summary>
        /// <returns></returns>
        private List<Point> GetSamplePoints()
        {
            List<Point> points=new List<Point>();
            string samplePoints = txtRandomPoints.Text.Replace("(", "").Replace(")", "");
            string[] arrPoints=samplePoints.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            int x, y;
            for (int idx = 0; idx < arrPoints.Length;idx =2 )
            {
                if (int.TryParse(arrPoints[idx], out x) && int.TryParse(arrPoints[idx   1], out y))
                    points.Add(new Point(x, y));
            }
            return points;
        }

        /// <summary>
        /// 尝试获取直线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGetLine_Click(object sender, EventArgs e)
        {
            //用RANSAC方法获取最佳直线
            points = GetSamplePoints();
            Line bestLine = null;           //最佳直线
            double bestInliersCount = 0;    //最佳模型的局内点数目
            Random random = new Random();
            for (int idx = 0; idx < nudIterCount.Value; idx  )
            {
                int idx1, idx2;
                GetRandomInliersPoints(random, out idx1, out idx2);
                int inliersCount = 2;
                Line line = new Line(points[idx1], points[idx2]);
                for (int i = 0; i < points.Count; i  )
                {
                    if (i != idx1 && i != idx2)
                    {
                        if (line.GetDistance(points[i]) <= (double)nudMinDistance.Value)
                            inliersCount  ;
                    }
                }
                if (inliersCount >= nudMinPointCount.Value)
                {
                    if (inliersCount > bestInliersCount)
                    {
                        bestLine = line;
                        bestInliersCount = inliersCount;
                    }
                }
            }
            //显示最佳直线
            if (bestLine != null)
            {
                lblFormula.Text = string.Format("方程:{0}\r\nA:{1}\r\nB:{2}\r\nC:{3}\r\n局内点数目:{4}",
                    bestLine.ToString(), bestLine.A, bestLine.B, bestLine.C, bestInliersCount);
                DrawLine(bestLine);
            }
            else
                lblFormula.Text = "没有获取到最佳直线。";
        }

        /// <summary>
        /// 随机获取两个局内点所在的索引
        /// </summary>
        /// <param name="idx1"></param>
        /// <param name="idx2"></param>
        private void GetRandomInliersPoints(Random random, out int idx1, out int idx2)
        {
            while (true)
            {
                idx1 = random.Next(points.Count);
                idx2 = random.Next(points.Count);
                if (idx1 != idx2)
                    break;
            }
        }

        /// <summary>
        /// 随机获取三个局内点所在的索引
        /// </summary>
        /// <param name="random"></param>
        /// <param name="idx1"></param>
        /// <param name="idx2"></param>
        /// <param name="idx3"></param>
        private void GetRandomInliersPoints(Random random, out int idx1, out int idx2, out int idx3)
        {
            while (true)
            {
                idx1 = random.Next(points.Count);
                idx2 = random.Next(points.Count);
                idx3 = random.Next(points.Count);
                if (idx1 != idx2 && idx2 != idx3 && idx1 != idx3)
                    break;
            }
        }

        /// <summary>
        /// 尝试获取圆
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGetCircle_Click(object sender, EventArgs e)
        {
            //用RANSAC方法获取最佳直线
            points = GetSamplePoints();
            Circle bestCircle = null;       //最佳圆
            double bestInliersCount = 0;    //最佳模型的局内点数目
            Random random = new Random();
            for (int idx = 0; idx < nudIterCount.Value; idx  )
            {
                int idx1, idx2, idx3;
                GetRandomInliersPoints(random, out idx1, out idx2, out idx3);
                int inliersCount = 3;
                Circle circle;
                try
                {
                    circle = new Circle(points[idx1], points[idx2], points[idx3]);
                }
                catch
                {
                    continue;
                }
                for (int i = 0; i < points.Count; i  )
                {
                    if (i != idx1 && i != idx2 && i!=idx3)
                    {
                        if (circle.GetDistance(points[i]) <= (double)nudMinDistance.Value)
                            inliersCount  ;
                    }
                }
                if (inliersCount >= nudMinPointCount.Value)
                {
                    if (inliersCount > bestInliersCount)
                    {
                        bestCircle = circle;
                        bestInliersCount = inliersCount;
                    }
                }
            }
            //显示最佳圆
            if (bestCircle != null)
            {
                lblFormula.Text = string.Format("方程:{0}\r\nA:{1}\r\nB:{2}\r\nR:{3}\r\n局内点数目:{4}",
                    bestCircle.ToString(), bestCircle.A, bestCircle.B, bestCircle.R, bestInliersCount);
                DrawCircle(bestCircle);
            }
            else
                lblFormula.Text = "没有获取到最佳圆。";
        }
    }
}

标签: Ransac

实例下载地址

Ransac算法拟合直线/圆

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警