C# OpenCvSharp Demo - 最大内接圆

C# OpenCvSharp Demo - 最大内接圆

码农世界 2024-05-15 前端 95 次浏览 0个评论

C# OpenCvSharp Demo - 最大内接圆

目录

效果

项目

代码

下载


效果

项目

代码

using OpenCvSharp;

using System;

using System.Diagnostics;

using System.Drawing;

using System.Drawing.Imaging;

using System.Linq;

using System.Windows.Forms;

namespace OpenCvSharp_Demo

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";

        string startupPath;

        string image_path;

        Stopwatch stopwatch = new Stopwatch();

        Mat image;

        Mat result_image;

        private void Form1_Load(object sender, EventArgs e)

        {

            startupPath = System.Windows.Forms.Application.StartupPath;

            image_path = "1.jpg";

            pictureBox1.Image = new Bitmap(image_path);

            image = new Mat(image_path);

        }

        private void button1_Click(object sender, EventArgs e)

        {

            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = fileFilter;

            if (ofd.ShowDialog() != DialogResult.OK) return;

            pictureBox1.Image = null;

            pictureBox2.Image = null;

            textBox1.Text = "";

            image_path = ofd.FileName;

            pictureBox1.Image = new Bitmap(image_path);

            image = new Mat(image_path);

        }

        private void button2_Click(object sender, EventArgs e)

        {

            stopwatch.Restart();

            result_image = image.Clone();

            Mat gray = new Mat();

            Mat binary = new Mat();

            Cv2.CvtColor(image, gray, ColorConversionCodes.BGR2GRAY);

            Cv2.Threshold(gray, binary, 1, 255, ThresholdTypes.Binary);

            //轮廓

            ConnectedComponents cc = Cv2.ConnectedComponentsEx(binary);

            foreach (var b in cc.Blobs.Skip(1))

            {

                Mat m = new Mat(binary, b.Rect);

                Cv2.FindContours(m, out OpenCvSharp.Point[][] contours, out _, RetrievalModes.External, ContourApproximationModes.ApproxSimple);

                double dist;

                double maxdist;

                OpenCvSharp.Point center = new OpenCvSharp.Point();

                foreach (var VPResult in contours)

                {

                    maxdist = 0d;

                    for (int i = 0; i < m.Cols; i++)

                    {

                        for (int j = 0; j < m.Rows; j++)

                        {

                            //点到轮廓的最大距离

                            dist = Cv2.PointPolygonTest(VPResult, new OpenCvSharp.Point(i, j), true);

                            if (dist > maxdist)

                            {

                                maxdist = dist;

                                center = new OpenCvSharp.Point(i, j);

                            }

                        }

                    }

                    Cv2.Circle(result_image, b.Left + center.X, b.Top + center.Y, (int)maxdist, Scalar.Red,1);

                }

            }

            double costTime = stopwatch.Elapsed.TotalMilliseconds;

            textBox1.Text = $"耗时:{costTime:F2}ms";

            pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());

        }

        private void button3_Click(object sender, EventArgs e)

        {

            if (pictureBox2.Image == null)

            {

                return;

            }

            Bitmap output = new Bitmap(pictureBox2.Image);

            var sdf = new SaveFileDialog();

            sdf.Title = "保存";

            sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";

            if (sdf.ShowDialog() == DialogResult.OK)

            {

                switch (sdf.FilterIndex)

                {

                    case 1:

                        {

                            output.Save(sdf.FileName, ImageFormat.Jpeg);

                            break;

                        }

                    case 2:

                        {

                            output.Save(sdf.FileName, ImageFormat.Png);

                            break;

                        }

                    case 3:

                        {

                            output.Save(sdf.FileName, ImageFormat.Bmp);

                            break;

                        }

                    case 4:

                        {

                            output.Save(sdf.FileName, ImageFormat.Emf);

                            break;

                        }

                    case 5:

                        {

                            output.Save(sdf.FileName, ImageFormat.Exif);

                            break;

                        }

                    case 6:

                        {

                            output.Save(sdf.FileName, ImageFormat.Gif);

                            break;

                        }

                    case 7:

                        {

                            output.Save(sdf.FileName, ImageFormat.Icon);

                            break;

                        }

                    case 8:

                        {

                            output.Save(sdf.FileName, ImageFormat.Tiff);

                            break;

                        }

                    case 9:

                        {

                            output.Save(sdf.FileName, ImageFormat.Wmf);

                            break;

                        }

                }

                MessageBox.Show("保存成功,位置:" + sdf.FileName);

            }

        }

    }

}

using OpenCvSharp;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Windows.Forms;
namespace OpenCvSharp_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string startupPath;
        string image_path;
        Stopwatch stopwatch = new Stopwatch();
        Mat image;
        Mat result_image;
        private void Form1_Load(object sender, EventArgs e)
        {
            startupPath = System.Windows.Forms.Application.StartupPath;
            image_path = "1.jpg";
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox1.Image = null;
            pictureBox2.Image = null;
            textBox1.Text = "";
            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            stopwatch.Restart();
            result_image = image.Clone();
            Mat gray = new Mat();
            Mat binary = new Mat();
            Cv2.CvtColor(image, gray, ColorConversionCodes.BGR2GRAY);
            Cv2.Threshold(gray, binary, 1, 255, ThresholdTypes.Binary);
            //轮廓
            ConnectedComponents cc = Cv2.ConnectedComponentsEx(binary);
            foreach (var b in cc.Blobs.Skip(1))
            {
                Mat m = new Mat(binary, b.Rect);
                Cv2.FindContours(m, out OpenCvSharp.Point[][] contours, out _, RetrievalModes.External, ContourApproximationModes.ApproxSimple);
                double dist;
                double maxdist;
                OpenCvSharp.Point center = new OpenCvSharp.Point();
                foreach (var VPResult in contours)
                {
                    maxdist = 0d;
                    for (int i = 0; i < m.Cols; i++)
                    {
                        for (int j = 0; j < m.Rows; j++)
                        {
                            //点到轮廓的最大距离
                            dist = Cv2.PointPolygonTest(VPResult, new OpenCvSharp.Point(i, j), true);
                            if (dist > maxdist)
                            {
                                maxdist = dist;
                                center = new OpenCvSharp.Point(i, j);
                            }
                        }
                    }
                    Cv2.Circle(result_image, b.Left + center.X, b.Top + center.Y, (int)maxdist, Scalar.Red,1);
                }
            }
            double costTime = stopwatch.Elapsed.TotalMilliseconds;
            textBox1.Text = $"耗时:{costTime:F2}ms";
            pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (pictureBox2.Image == null)
            {
                return;
            }
            Bitmap output = new Bitmap(pictureBox2.Image);
            var sdf = new SaveFileDialog();
            sdf.Title = "保存";
            sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";
            if (sdf.ShowDialog() == DialogResult.OK)
            {
                switch (sdf.FilterIndex)
                {
                    case 1:
                        {
                            output.Save(sdf.FileName, ImageFormat.Jpeg);
                            break;
                        }
                    case 2:
                        {
                            output.Save(sdf.FileName, ImageFormat.Png);
                            break;
                        }
                    case 3:
                        {
                            output.Save(sdf.FileName, ImageFormat.Bmp);
                            break;
                        }
                    case 4:
                        {
                            output.Save(sdf.FileName, ImageFormat.Emf);
                            break;
                        }
                    case 5:
                        {
                            output.Save(sdf.FileName, ImageFormat.Exif);
                            break;
                        }
                    case 6:
                        {
                            output.Save(sdf.FileName, ImageFormat.Gif);
                            break;
                        }
                    case 7:
                        {
                            output.Save(sdf.FileName, ImageFormat.Icon);
                            break;
                        }
                    case 8:
                        {
                            output.Save(sdf.FileName, ImageFormat.Tiff);
                            break;
                        }
                    case 9:
                        {
                            output.Save(sdf.FileName, ImageFormat.Wmf);
                            break;
                        }
                }
                MessageBox.Show("保存成功,位置:" + sdf.FileName);
            }
        }
    }
}

下载

源码下载

转载请注明来自码农世界,本文标题:《C# OpenCvSharp Demo - 最大内接圆》

百度分享代码,如果开启HTTPS请参考李洋个人博客
每一天,每一秒,你所做的决定都会改变你的人生!

发表评论

快捷回复:

评论列表 (暂无评论,95人围观)参与讨论

还没有评论,来说两句吧...

Top