C#使用OpenCvSharp实现图像校正

 更新时间:2023年11月15日 09:53:48   作者:天天代码码天天  
这篇文章主要为大家详细介绍了C#如何使用OpenCvSharp实现图像校正功能,文中的示例代码简洁易懂,具有一定的学习价值,需要的小伙伴可以参考下

效果

实现代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Extensions;
 
namespace OpenCvSharp_图像校正
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        string img = "test.png";
 
        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.Image = new Bitmap(img);
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            Mat src = new Mat(img);
 
            //转化为灰度图
            //Cv2.CvtColor(src, src, ColorConversionCodes.RGB2GRAY);
 
            InputArray kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(3, 3));
            Cv2.MorphologyEx(src, src, MorphTypes.Close, kernel, new OpenCvSharp.Point(-1, -1), 3);
            //Cv2.ImShow("MorphologyEx", src);
 
            /*
                ksize,高斯内核大小,ksize.width和ksize.height必须是正奇数,两者可以不相同,值越大越模糊
                sigmaX,Y轴方向的标准差,值越大越模糊
                sigmaY,X轴方向的标准差,值越大越模糊
             */
            Cv2.GaussianBlur(src, src, new OpenCvSharp.Size(11, 11), 2, 2);
            //Cv2.ImShow("GaussianBlur", src);
 
            //Canny边缘检测
            Mat canny_Image = new Mat();
            Cv2.Canny(src, canny_Image, 10, 30, 3, false);
 
            OpenCvSharp.Point[][] contours;
            HierarchyIndex[] hierarchly;
            /*
	          findContours找到轮廓
	          第一个参数:单通道图像矩阵,可以是灰度图,但更常用的是二值图像,一般是经过Canny、拉普拉斯等边缘检测算子处理过的二值图像;
	          第二个参数:contours 
	          第三个参数:hierarchy
	          第四个参数:轮廓的检索模式
	  		        取值一:CV_RETR_EXTERNAL 只检测最外围轮廓,包含在外围轮廓内的内围轮廓被忽略
	  		        取值二:CV_RETR_LIST     检测所有的轮廓,包括内围、外围轮廓,但是检测到的轮廓不建立等级关系,彼此之间独立,没有等级关系,这就意味着这个检索模式下不存在父轮廓或内嵌轮廓,所以hierarchy向量内所有元素的第3、第4个分量都会被置为-1,具体下文会讲到
	  		        取值三:CV_RETR_CCOMP    检测所有的轮廓,但所有轮廓只建立两个等级关系,外围为顶层,若外围内的内围轮廓还包含了其他的轮廓信息,则内围内的所有轮廓均归属于顶层
	  		        取值四:CV_RETR_TREE     检测所有轮廓,所有轮廓建立一个等级树结构。外层轮廓包含内层轮廓,内层轮廓还可以继续包含内嵌轮廓。
	          第五个参数:轮廓的近似方法
	  		        取值一:CV_CHAIN_APPROX_NONE   保存物体边界上所有连续的轮廓点到contours向量内
	  		        取值二:CV_CHAIN_APPROX_SIMPLE 仅保存轮廓的拐点信息,把所有轮廓拐点处的点保存入contours向量内,拐点与拐点之间直线段上的信息点不予保留
	  		        取值三和四:CV_CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS使用teh-Chinl chain 近似算法
	          第六个参数:Point偏移量,所有的轮廓信息相对于原始图像对应点的偏移量,相当于在每一个检测出的轮廓点上加上该偏移量,且Point可以是负值。不填为默认不偏移Point()
	         */
            Cv2.FindContours(canny_Image, out contours, out hierarchly,
                RetrievalModes.External,
                ContourApproximationModes.ApproxSimple,
                new OpenCvSharp.Point(0, 0));
 
            if (contours.Length == 0)
            {
                MessageBox.Show("边缘检测失败");
                return;
            }
 
            Random rnd = new Random();
            Scalar color;
            color = new Scalar(0, 255, 0);
            for (int i = 0; i < contours.Length; i++)
            {
                color = new Scalar(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255));
                Cv2.DrawContours(src, contours, i, color, 2, LineTypes.Link4);
            }
            //Cv2.ImShow("contours", src);
 
            //求出面积最大的轮廓
            double max_area = 0.0;
            double currentArea = 0.0;
            OpenCvSharp.Point[] max_contour = null;
            for (int i = 0; i < contours.Length; i++)
            {
                currentArea = Cv2.ContourArea(contours[i]);
                if (currentArea > max_area)
                {
                    max_area = currentArea;
                    max_contour = contours[i];
                }
            }
 
            //多边形拟合凸包的四个顶点
            OpenCvSharp.Point[] hull = Cv2.ConvexHull(max_contour);
            double epsilon = 0.02 * Cv2.ArcLength(max_contour, true);
            OpenCvSharp.Point[] approx = Cv2.ApproxPolyDP(hull, epsilon, true);
 
            if (approx.Length != 4)
            {
                MessageBox.Show("拟合凸包的四个顶点失败");
                return;
            }
 
            Scalar scalar2 = new Scalar(0, 255, 255);
 
            Cv2.Line(src, approx[0], approx[1], scalar2, 1, LineTypes.Link4);
            Cv2.Line(src, approx[1], approx[2], scalar2, 1, LineTypes.Link4);
            Cv2.Line(src, approx[2], approx[3], scalar2, 1, LineTypes.Link4);
            Cv2.Line(src, approx[3], approx[0], scalar2, 1, LineTypes.Link4);
 
            //排序
            Array.Sort(approx, (cs1, cs2) =>
            {
                if (cs1 != null && cs1 != null)
                {
                    if (cs1.Y > cs2.Y)
                        return 1;
                    else if (cs1.Y == cs2.Y)
                    {
                        if (cs1.X < cs2.X)
                            return 1;
                        else return -1;
                    }
                    else
                        return -1;
                }
                return 0;
 
            });
 
            //算法找出的角点
            OpenCvSharp.Point2f[] srcPt = new OpenCvSharp.Point2f[4];
            srcPt[0] = approx[0];
            srcPt[1] = approx[1];
            srcPt[2] = approx[3];
            srcPt[3] = approx[2];
 
            //最小外接矩形
            RotatedRect rect = Cv2.MinAreaRect(srcPt);
            Rect box = rect.BoundingRect();
            OpenCvSharp.Point2f[] dstPt = new OpenCvSharp.Point2f[4];
 
            dstPt[0].X = box.X;
            dstPt[0].Y = box.Y;
 
            dstPt[1].X = box.X + box.Width;
            dstPt[1].Y = box.Y;
 
            dstPt[2].X = box.X + box.Width;
            dstPt[2].Y = box.Y + box.Height;
 
            dstPt[3].X = box.X;
            dstPt[3].Y = box.Y + box.Height;
 
            Mat src2 = new Mat(img);
            Mat final = new Mat();
            Mat warpmatrix = Cv2.GetPerspectiveTransform(srcPt, dstPt);//获得变换矩阵
            Cv2.WarpPerspective(src2, final, warpmatrix, src.Size());//投射变换,将结果赋给final
 
            Bitmap temp = BitmapConverter.ToBitmap(final);
 
            pictureBox2.Image = temp;
 
            DrawLine(srcPt, dstPt);
 
            //Application.DoEvents();
            //System.Threading.Thread.Sleep(1000);
            //pictureBox2.Image = CutImage(temp, (int)p2f[0].X, (int)p2f[0].Y, (int)p2f[2].X, (int)p2f[2].Y);
 
        }
 
        void DrawLine(OpenCvSharp.Point2f[] srcPt, OpenCvSharp.Point2f[] dstPt)
        {
            Bitmap bmp = new Bitmap(img);
            Graphics g = Graphics.FromImage(bmp);
 
            Pen pen = new Pen(Color.Red, 3);
            Pen pen2 = new Pen(Color.Blue, 3);
 
            g.DrawLine(pen, srcPt[0].X, srcPt[0].Y, srcPt[1].X, srcPt[1].Y);
            g.DrawLine(pen, srcPt[1].X, srcPt[1].Y, srcPt[2].X, srcPt[2].Y);
            g.DrawLine(pen, srcPt[2].X, srcPt[2].Y, srcPt[3].X, srcPt[3].Y);
            g.DrawLine(pen, srcPt[3].X, srcPt[3].Y, srcPt[0].X, srcPt[0].Y);
 
            g.DrawLine(pen2, dstPt[0].X, dstPt[0].Y, dstPt[1].X, dstPt[1].Y);
            g.DrawLine(pen2, dstPt[1].X, dstPt[1].Y, dstPt[2].X, dstPt[2].Y);
            g.DrawLine(pen2, dstPt[2].X, dstPt[2].Y, dstPt[3].X, dstPt[3].Y);
            g.DrawLine(pen2, dstPt[3].X, dstPt[3].Y, dstPt[0].X, dstPt[0].Y);
 
            pictureBox1.Image = bmp;
 
        }
 
        /// <summary>
        /// 剪裁图片
        /// </summary>
        /// <param name="src">原图片</param>
        /// <param name="left">左坐标</param>
        /// <param name="top">顶部坐标</param>
        /// <param name="right">右坐标</param>
        /// <param name="bottom">底部坐标</param>
        /// <returns>剪裁后的图片</returns>
        public Image CutImage(Image src, int left, int top, int right, int bottom)
        {
            Bitmap srcBitmap = new Bitmap(src);
            int width = right - left;
            int height = bottom - top;
            Bitmap destBitmap = new Bitmap(width, height);
            using (Graphics g = Graphics.FromImage(destBitmap))
            {
                g.Clear(Color.Transparent);
                //设置画布的描绘质量         
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(srcBitmap, new Rectangle(0, 0, width, height), left, top, width, height, GraphicsUnit.Pixel);
            }
            return destBitmap;
        }
    }
}

以上就是C#使用OpenCvSharp实现图像校正的详细内容,更多关于C# OpenCvSharp图像校正的资料请关注脚本之家其它相关文章!

相关文章

  • C#基于Sockets类实现TCP通讯

    C#基于Sockets类实现TCP通讯

    这篇文章主要为大家详细介绍了C#基于Sockets类实现TCP通讯,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • 详解Unity中的ShaderGraph入门使用教程

    详解Unity中的ShaderGraph入门使用教程

    Unity2018版本之后推出了一个可编程渲染管线工具ShaderGraph,让我们可以通过可视化界面拖拽来实现着色器的创建和编辑,今天重点给大家介绍Unity中的ShaderGraph入门使用教程,需要的朋友参考下吧
    2021-07-07
  • C#实现骑士飞行棋

    C#实现骑士飞行棋

    这篇文章主要为大家详细介绍了C#实现骑士飞行棋,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • c# 获取数据库中所有表名称的方法

    c# 获取数据库中所有表名称的方法

    在很多情况下我们需要将指定的数据库中的所有表都列出来。在使用c#进行软件开发时,我们有哪些方法可是实现这个目的呢?本人对此进行概要的总结,有以下6中方式可以实现这个目的。
    2010-02-02
  • 自己编写sqlhelper类示例分享

    自己编写sqlhelper类示例分享

    这篇文章主要介绍了自己编写sqlhlper类示例,需要的朋友可以参考下
    2014-04-04
  • C# winform实现自动更新

    C# winform实现自动更新

    这篇文章主要为大家详细介绍了C# winform实现自动更新的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-10-10
  • .net文件上传时实现通过文件头确认文件类型的方法

    .net文件上传时实现通过文件头确认文件类型的方法

    这篇文章主要介绍了.net文件上传时实现通过文件头确认文件类型的方法,很实用的功能,需要的朋友可以参考下
    2014-07-07
  • 在C# WPF下自定义滚动条ScrollViewer样式的操作

    在C# WPF下自定义滚动条ScrollViewer样式的操作

    这篇文章主要介绍了在C# WPF下自定义滚动条ScrollViewer样式的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • C# Winform实现自定义分页控件

    C# Winform实现自定义分页控件

    一些第三方的分页控件要么就是界面不够美观大方,要么就是使用起来感觉很麻烦,所以本文就为大家介绍一下如何利用Winform自定义分页控件,需要的可以参考一下
    2023-07-07
  • c#判断代码是否执行超时的几种方式总结

    c#判断代码是否执行超时的几种方式总结

    这篇文章主要介绍了c#判断代码是否执行超时的几种方式总结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01

最新评论