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

    }
}

下载

源码下载

相关推荐
凤枭香6 小时前
Python OpenCV 傅里叶变换
开发语言·图像处理·python·opencv
ctrey_7 小时前
2024-11-4 学习人工智能的Day21 openCV(3)
人工智能·opencv·学习
可均可可8 小时前
C++之OpenCV入门到提高004:Mat 对象的使用
c++·opencv·mat·imread·imwrite
蒙娜丽宁8 小时前
《Python OpenCV从菜鸟到高手》——零基础进阶,开启图像处理与计算机视觉的大门!
python·opencv·计算机视觉
plmm烟酒僧9 小时前
Windows下QT调用MinGW编译的OpenCV
开发语言·windows·qt·opencv
AI小杨9 小时前
【车道线检测】一、传统车道线检测:基于霍夫变换的车道线检测史诗级详细教程
人工智能·opencv·计算机视觉·霍夫变换·车道线检测
冷凝女子10 小时前
【QT】海康视频及openCv抓拍正脸接口
qt·opencv·音视频·海康
撞南墙者12 小时前
OpenCV自学系列(1)——简介和GUI特征操作
人工智能·opencv·计算机视觉
柳鲲鹏13 小时前
OpenCV视频防抖源码及编译脚本
人工智能·opencv·计算机视觉
jndingxin13 小时前
OpenCV视觉分析之目标跟踪(8)目标跟踪函数CamShift()使用
人工智能·opencv·目标跟踪