opencvSharp实验三则

来源:互联网 发布:淘宝贷款最多贷多少钱 编辑:程序博客网 时间:2024/06/07 15:32

实验环境:Visual Studio 2015



实验1:色彩替换

using OpenCvSharp;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;namespace opencvSharpTest2console{    class Program    {        static void Main(string[] args)        {           test();        }        static void test()        {            Mat mat = new Mat("pic4.jpg", ImreadModes.Color);            for (int y = 0; y < mat.Height; y++)            {                for (int x = 0; x < mat.Width; x++)                {                    Vec3b color = mat.Get<Vec3b>(y, x);//获取RGB向量                    byte temp = color.Item0;//Item012分别是BGR                    color.Item0 = color.Item2; // 将Red赋值给Blue                    color.Item2 = temp;        // 将Blue赋值给Red                    mat.Set<Vec3b>(y, x, color);                }            }            Cv2.NamedWindow("win1",WindowMode.AutoSize);//建立一个窗体,自动大小            Cv2.ImShow("win1", mat);//将矩阵显示在窗体win1            Cv2.WaitKey(0);//等待按键,如果没有这个函数图片不显示            //   Thread.Sleep(5600);        }    }}


原图和处理后:





实验2:利用霍夫方法追踪视频中的圆形


 static void cameraOpen()        {            VideoCapture cap = new VideoCapture(0);            int time;            int pos = 0;            CvTrackbarCallback setThreshold = new CvTrackbarCallback(func);            int thresholdwide = 0;            Cv2.NamedWindow("threshold");            CvTrackbar tk = new CvTrackbar("Threshold", "threshold", thresholdwide,255, setThreshold);                        double rate = 25.0;            Size videosize = new Size(320,240);            Mat frame,grayframe;            grayframe = new Mat();            List<Vec3f> aid = new List<Vec3f>();//用于霍夫圆形的位置和半径的记录,x,y,radio            OpenCvSharp.CircleSegment[] circles = new CircleSegment[200];            while (cap.IsOpened())            {                 frame  = cap.RetrieveMat();//获取视频帧                Cv2.CvtColor(frame, grayframe, ColorConversionCodes.RGB2GRAY);//转换成为灰度               // Cv2.ImShow("video", frame);//显示原始视频                Cv2.GaussianBlur(grayframe, grayframe,new Size(7, 7), 2, 2); //高斯模糊                Cv2.Threshold(grayframe, grayframe, tk.Pos, 255, ThresholdTypes.Binary);//二值化               circles = Cv2.HoughCircles(grayframe,HoughMethods.Gradient,2,10);//设置并执行霍夫找圆函数                Scalar circlecolor = new Scalar(255, 0, 0);//用于描绘边缘的颜色                for (int i = 0; i < circles.Count(); i++) //绘制获取到的圆形                {                    Point point = new Point((int)circles[i].Center.X, (int)circles[i].Center.Y);                    if (point.X > 0)                    {                        Cv2.Circle(frame, point,Convert.ToInt16( circles[i].Radius), circlecolor);//绘制圆                    }                }                Cv2.ImShow("process", frame);//显示处理后的视频                Cv2.ImShow("threshold", grayframe);                if (Cv2.WaitKey(20) == 27)//如果按下esc按键则退出                {                    break;                }            }        }







原创粉丝点击