C#+opencv开发winfrom,列出常见的函数例子

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;   //为了使用opencv
using Point = OpenCvSharp.Point;   //为了确定我们使用的point是opencv的而不是draw的
 
namespace WindowsFormsApp9
{
    public partial class Form1 : Form
    {
        private readonly VideoCapture capture;

        public Form1()
        {
            InitializeComponent();
            capture = new VideoCapture(0);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //this.pictureBox1.Image = Image.FromFile("c:\\test.png", false);
        }

        /// <summary>
        /// 开启摄像头
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //此处参考网上的读取方法
            int sleepTime = (int)Math.Round(1000 / capture.Fps);
            // 声明实例 Mat类
            Mat image = new Mat();

            // 进入读取视频每镇的循环
            while (true)
            {
                capture.Read(image);
                //判断是否还有没有视频图像 
                if (image.Empty())
                    break;
                // 在picturebox中播放视频, 需要先转换成bitmap格式
                pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(image);
                Cv2.WaitKey(sleepTime);
            } 
        }

        /// <summary>
        /// 关闭摄像头
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            //  capture.Dispose();
            capture.Release();

        }

        /// <summary>
        /// 读取图片 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            
            //this.pictureBox1.Image = Image.FromFile("c:\\test.png", false);

            Mat src = new Mat("c:\\test.png");
           
            Cv2.ImShow("Demo", src); //显示图片" 
            Cv2.WaitKey(0);

        }

        /// <summary>
        /// 灰度图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button8_Click(object sender, EventArgs e)
        {
            Mat src = new Mat("c:\\test.png", ImreadModes.Grayscale);
            //pBkImage是已经赋值了的图片,将这个图片复制到pBkImage_copy中
            Mat pBkImage_copy = new Mat();
            src.CopyTo(pBkImage_copy);
            Cv2.ImShow("Demo", pBkImage_copy); //显示图片" 
            Cv2.WaitKey(0);
        }

       

        private void button4_Click(object sender, EventArgs e)
        {
            Mat img1 = Cv2.ImRead("c:\\test2.png");
            Mat img2 = Cv2.ImRead("c:\\test1.png");
            Mat img = new Mat() ;
            Cv2.Absdiff(img1, img2 ,img);

            Cv2.ImShow("img1", img1);
            Cv2.ImShow("img2", img2);
            Cv2.ImShow("img区别", img);


            Cv2.WaitKey(0);
            Cv2.DestroyAllWindows();

            //Cv2.ImShow("Demo", img); //显示图片" 
            //Cv2.WaitKey(0);
        }

        /// <summary>
        /// 高斯模糊
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            Mat img1 = Cv2.ImRead("c:\\test.png");
            Mat img = new Mat();
            Cv2.GaussianBlur(img1, img, new OpenCvSharp.Size(11, 11), 4, 4);
            Cv2.ImShow("Demo", img); //显示图片" 
            Cv2.WaitKey(0);
        }

        /// <summary>
        /// 二值化(灰度值设置为0或255)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
             Mat img1 = Cv2.ImRead("c:\\test.png");
            Mat img = new Mat();
            Cv2.Threshold(img1, img, 60, 255, ThresholdTypes.Binary);
            Cv2.ImShow("Demo", img); //显示图片" 
            Cv2.WaitKey(0);
        }

        /// <summary>
        /// 开闭运算 开(开运算:先腐蚀后膨胀,能够消除图像区域外的小白点(噪声)。)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button7_Click(object sender, EventArgs e)
        {
            Mat img1 = Cv2.ImRead("c:\\test.png");
            Mat img = new Mat();
            //自定义核,进行开、闭运算
            Mat element = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(5, 5));
            Cv2.MorphologyEx(img1, img, MorphTypes.Open, element);
            //Cv2.MorphologyEx(Image_morp, Image_morp, MorphTypes.Close, element);

            Cv2.ImShow("Demo", img); //显示图片" 
            Cv2.WaitKey(0);

        }

        /// <summary>
        /// 开闭运算-闭(    闭运算:先膨胀后腐蚀,能够消除图像区域内的小黑点(噪声))
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button9_Click(object sender, EventArgs e)
        {
            Mat img1 = Cv2.ImRead("c:\\test.png");
            Mat img = new Mat();
            //自定义核,进行开、闭运算
            Mat element = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(5, 5));
            Cv2.MorphologyEx(img1, img, MorphTypes.Close, element);

            Cv2.ImShow("Demo", img); //显示图片" 
            Cv2.WaitKey(0);

        }

        private void button10_Click(object sender, EventArgs e)
        {
            Point[][] contours;
            HierarchyIndex[] hierarchy;
            Mat Image_morp = Cv2.ImRead("c:\\test.png");
            Cv2.FindContours(Image_morp, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxSimple, new Point(0, 0));
            if (contours.Length == 0)
            {
                //System.Diagnostics.Debug.WriteLine("没有任何前景");
                //return 0;
                MessageBox.Show("没有任何前景");
                return;
            }
            double image_area = 0.0;
            for (int i = 0; i < contours.Length; i++)
            {
                image_area += Cv2.ContourArea(contours[i]);
            }


            Cv2.ImShow("Demo", Image_morp); //显示图片" 
            Cv2.WaitKey(0);

        }
        /// <summary>
        /// 调节大小
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button11_Click(object sender, EventArgs e)
        {
            //OpenCvSharp.Size up = new Size(1000, 500);
            //Rect rect = new Rect(x, y, width, height);
            //Rect rect = new OpenCvSharp.Rect(x, y, width, height);

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值