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

    }
}

下载

源码下载

相关推荐
六bring个六21 分钟前
qtcreater配置opencv
c++·qt·opencv·计算机视觉·图形渲染·opengl
多巴胺与内啡肽.1 小时前
OpenCV进阶操作:光流估计
人工智能·opencv·计算机视觉
alpszero2 小时前
YOLO11解决方案之物体模糊探索
人工智能·python·opencv·计算机视觉·yolo11
多巴胺与内啡肽.2 小时前
OpenCV进阶操作:风格迁移以及DNN模块解析
人工智能·opencv·dnn
zm-v-159304339863 小时前
解锁遥感数据密码:DeepSeek、Python 与 OpenCV 的协同之力
开发语言·python·opencv
jndingxin4 小时前
OpenCV CUDA 模块中在 GPU 上对图像或矩阵进行 翻转(镜像)操作的一个函数 flip()
人工智能·opencv
码农新猿类5 小时前
初入OpenCV
qt·opencv·计算机视觉
程序小K8 小时前
OpenCV的CUDA模块进行图像处理
图像处理·人工智能·opencv
jndingxin8 小时前
OpenCVCUDA 模块中在 GPU 上对图像或矩阵进行 边界填充(padding)函数copyMakeBorder()
人工智能·opencv
jndingxin19 小时前
OpenCV 的 CUDA 模块中用于将多个单通道的 GpuMat 图像合并成一个多通道的图像 函数cv::cuda::merge
人工智能·opencv·计算机视觉