C# OpenCvSharp Demo - 最大内接圆

news2024/10/6 6:47:38

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

    }
}

下载

源码下载

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1677077.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

YOLOv5独家改进:backbone改进 | 微软新作StarNet:超强轻量级Backbone | CVPR 2024

💡💡💡创新点:star operation(元素乘法)在无需加宽网络下,将输入映射到高维非线性特征空间的能力,这就是StarNet的核心创新,在紧凑的网络结构和较低的能耗下展示了令人印象深刻的性能和低延迟 💡💡💡如何跟YOLOv5结合:替代YOLOv5的backbone 收录 YOL…

分享一个基于Qt的Ymodem的上位机(GitHub开源)

文章目录 1.项目地址2.Ymodem 协议介绍3.文件传输过程4.使用5.SecureCRT 软件也支持Ymodem6.基于PyQt5的Ymodem界面实现案例 1.项目地址 https://github.com/XinLiGH/SerialPortYmodem 基于VS2019 Qt5.15.2 编译&#xff0c;Linux下编译也可以&#xff0c;这里不做说明。 2.…

ROS2+TurtleBot3+Cartographer+Nav2实现slam建图和导航

0 引言 入门机器人最常见的应用就是slam建图和导航&#xff0c;本文将详细介绍这一流程&#xff0c; 便于初学这快速上手。 首先对需要用到的软件包就行简单介绍。 turtlebot3: 是一个小型的&#xff0c;基于ros的移动机器人。 学习机器人的很多示例程序都是基于turtlebot3。 …

51 单片机[2-1]:点亮一个LED

一、在 Keil5 中新建项目 打开 Keil5 &#xff0c;点击 Project —— new μVision Project 新建文件夹 KeilProject &#xff0c;以后的项目都在这个文件夹下&#xff0c;再建一个文件夹 2-1 点亮一个LED。在该文件夹下创建名为 Project 的文件&#xff0c;并保存。推荐起这…

金万维动态域名小助手怎么用?

金万维动态域名小助手是一个域名检测工具&#xff0c;使用此工具可以进行检测域名解析是否正确、清除DNS缓存、修改DNS服务器地址及寻找在线客服&#xff08;仅支持付费用户&#xff09;等操作。对不懂网络的用户是一个很好的检测域名的工具&#xff0c;下面我就讲解一下金万维…

TimesFM: 预训练的时间序列基础模型

大模型技术论文不断&#xff0c;每个月总会新增上千篇。本专栏精选论文重点解读&#xff0c;主题还是围绕着行业实践和工程量产。若在阅读过程中有些知识点存在盲区&#xff0c;可以回到如何优雅的谈论大模型重新阅读。另外斯坦福2024人工智能报告解读为通识性读物。若对于如果…

根据Word文档用剪映批量自动生成视频发布抖音

手头有大量word文档&#xff0c;想通过剪映的AI图文成片功能批量生成视频&#xff0c;发布到抖音平台&#xff0c;简单3步即可&#xff1a; 第一步&#xff1a;把word文档或者PDF等文档转成txt文本&#xff0c;可以用一些软件&#xff0c;也可以用AI工具&#xff0c;具体常见文…

Windows下编译RTTR

虽然C11引入了RTTI、Metaprogramming 等技术&#xff0c;但C在Reflection编程方面依旧功能有限。在社区上&#xff0c;RTTR则提供了一套C编写的反射库&#xff0c;补充了C在Reflection方面的缺陷。 零、环境 操作系统Windows 11Visual StudioVisual Studio Community 2022 CMa…

Qt---Socket通信

一、TCP/IP通信 在Qt中实现TCP/IP服务器端通信的流程&#xff1a; 1. 创建套接字 2. 将套接字设置为监听模式 3. 等待并接受客户端请求 可以通过QTcpServer提供的void newConnection()信号来检测是否有连接请求&#xff0c;如果有可以在对应的槽函数中调用nextPendingCon…

【现代C++】范围库的应用

C20引入了范围库&#xff08;Ranges library&#xff09;&#xff0c;它是标准模板库&#xff08;STL&#xff09;的一个扩展&#xff0c;提供了一种新的方式来处理序列和范围。这个库允许开发者以更声明式的方式编写代码&#xff0c;使得操作序列变得更简洁、更易读。以下是C范…

【web网页开发制作】Html+Css+Js游戏主题特效及轮播效果网页作业天涯明月刀(7页面附源码)

HTMLCSSJS游戏主题轮播效果 &#x1f354;涉及知识&#x1f964;写在前面✨特效展示特效1、轮播幻灯效果特效2和3、鼠标悬浮及点击效果 &#x1f367;一、网页主题&#x1f333;二、网页效果Page1、首页Page2、游戏简介Page3、新闻中心Page4、互动专区Page5、视听盛宴Page6、用…

Kotlin协程实战指南:解锁Android开发高效能新时代

前言 在移动互联网的狂飙突进之中&#xff0c;Android开发领域如同站在风口的勇士&#xff0c;不断接受技术迭代与创新的双重洗礼。在这个快速变化的市场里&#xff0c;用户对应用性能和体验的期待水涨船高&#xff0c;开发者们面临的挑战也越来越大&#xff1a;如何在功能的丰…

Dart 3.4 发布:Wasm Native Macros(宏)

Google I/O 的结束&#xff0c;除了 Flutter 3.22 的发布 &#xff0c;Dart 3.4 也迎来了它是「史诗级」的更新&#xff0c;之所以这么说&#xff0c;就是因为 Wasm Native 的落地和 Macros 的实验性展示。 在此之前&#xff0c;其实我也提前整理过一些对应的内容&#xff0c;…

运维别卷系列 - 云原生监控平台 之 06.prometheus pushgateway 实践

文章目录 [toc]Pushgateway 简介Pushgateway 部署创建 svc创建 deployment Pushgateway 测试删除 Pushgateway 上对应 lable 的数据 Pushgateway 简介 WHEN TO USE THE PUSHGATEWAY Pushgateway 是一种中介服务&#xff0c;允许您从无法抓取的作业中推送指标。 The Pushgateway…

深入理解 npm、cnpm、npx、yarn 和 pnpm:JavaScript 包管理器的对比

在 JavaScript 的世界中&#xff0c;包管理器是一个重要的工具&#xff0c;它帮助我们管理、安装和升级项目的依赖。在这篇文章中&#xff0c;我们将深入探讨三个最流行的 JavaScript 包管理器&#xff1a;npm、yarn 和 pnpm。 npm&#xff08;Node Package Manager&#xff0…

未来IT行业的模块化、学习与跨界融合

随着技术的快速发展&#xff0c;IT行业已成为推动全球经济和社会发展的核心动力。从云计算和大数据到人工智能&#xff08;AI&#xff09;和物联网&#xff0c;这些创新技术正在彻底改变我们的生活方式和工作模式。而在AI领域&#xff0c;尤其是人工智能生成内容&#xff08;AI…

怎么识别数学公式?分享简单识别方法

怎么识别数学公式&#xff1f;在学术研究和日常工作中&#xff0c;数学公式无疑是一个常见且重要的元素。然而&#xff0c;手动输入复杂的数学公式往往既耗时又容易出错。幸运的是&#xff0c;随着科技的发展&#xff0c;现在我们有了一些高效的软件工具&#xff0c;可以帮助我…

奥维地图下载高清影像的两种方式!以及ArcGIS、QGIS、GlobalMapper、自编工具下载高清影像的方法推荐!

今天来介绍一下奥维互动地图是如何下载高清影像的&#xff0c;也不是多了不起的功能&#xff01;有朋友问&#xff0c;加上这个软件确实用的人多。 下载的高清数据在ArcGIS中打开的效果&#xff01; 开始介绍奥维之前我们也介绍一下我们之前介绍的几个方法&#xff0c;没有优劣…

IP代理网络协议介绍

在IP代理页面上&#xff0c;存在HTTP/HTTPS/Socks5三种协议。它们都是客户端与服务器之间交互的协议。 HTTP HTTP又称之为超文本传输协议&#xff0c;在因特网使用范围广泛。它是一种请求/响应模型&#xff0c;客户端向服务器发送请求&#xff0c;服务器解析请求后对客户端作出…

四信5G全连接工厂一站式解决方案上线,打造可持续发展工业未来

政策背景 2022年9月&#xff0c;为进一步加快“5G工业互联网”新技术新场景新模式向工业生产各领域各环节深度拓展&#xff0c;工信部印发《5G全连接工厂建设指南》&#xff0c;明确提出&#xff0c;推动万家企业开展5G全连接工厂建设&#xff0c;建成1000个分类分级、特色鲜明…