C# OpenCvSharp Demo - 最大内接圆

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);
            }
        }

    }
}

下载

源码下载

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-12 07:52:10       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-12 07:52:10       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-12 07:52:10       82 阅读
  4. Python语言-面向对象

    2024-05-12 07:52:10       91 阅读

热门阅读

  1. leetcode 1749.任意子数组和的绝对值的最大值

    2024-05-12 07:52:10       28 阅读
  2. PHP类和对象概念及用法

    2024-05-12 07:52:10       29 阅读
  3. C++Primer Plus第三章编程练习4

    2024-05-12 07:52:10       28 阅读
  4. Node.js -- 会话控制

    2024-05-12 07:52:10       32 阅读
  5. iOS 如何让超出父视图的部分响应事件

    2024-05-12 07:52:10       34 阅读
  6. 电商平台遭遇DDOS、CC攻击有什么防护方案

    2024-05-12 07:52:10       29 阅读
  7. Lucene 英文词根处理

    2024-05-12 07:52:10       32 阅读
  8. 十分钟“手撕”内部类+static在内部类的使用

    2024-05-12 07:52:10       35 阅读
  9. react经验15:拖拽排序组件dnd-kit的使用经验

    2024-05-12 07:52:10       34 阅读
  10. vue.js 介绍

    2024-05-12 07:52:10       31 阅读
  11. React Router DOM BrowserRouter

    2024-05-12 07:52:10       36 阅读
  12. Pycharm所有快捷键的使用

    2024-05-12 07:52:10       25 阅读
  13. 【DevOps】Linux 网络命令netstat详解

    2024-05-12 07:52:10       30 阅读