OpenCVSharp基础系列

一、简介

官方地址:https://github.com/shimat/opencvsharp

API:http://shimat.github.io/opencvsharp/api/OpenCvSharp.html

二、Converting Image

            Mat mat = new Mat("fruits.jpg", ImreadModes.Unchanged);//将图片转换为Mat对象
            WriteableBitmap wb = mat.ToWriteableBitmap();//将Mat对象转换为WriteableBitmap
            BitmapSource bitmapSource = mat.ToBitmapSource();//将Mat对象转换为BitmapSource
            Bitmap bitmap = mat.ToBitmap();//将Mat对象转换为Bitmap

            byte[] source = mat.ToBytes();//将Mat对象转换为byte[]
            MemoryStream ms = mat.ToMemoryStream();//将Mat对象转换为MemoryStream
            mat.WriteToStream(new MemoryStream());//将Mat对象转换为Bitmap
            iamgeHost.Source = bitmapSource;

在这里插入图片描述

我们可以将一个Mat对象转换为多种数据类型,当然也可以将各种数据类型转换回Mat对象,这里就不举例了。

三、Accessing Pixel

            Mat mat = new Mat("fruits.jpg", ImreadModes.Unchanged);

            var mat3 = new Mat<Vec3b>(mat); // cv::Mat_<cv::Vec3b>
            var indexer = mat3.GetIndexer();

            for (int y = 0; y < mat.Height; y++)
            {
                for (int x = 0; x < mat.Width; x++)
                {
                    Vec3b color = indexer[y, x];
                    byte temp = color.Item0;
                    color.Item0 = color.Item2; // B <- R
                    color.Item2 = temp;        // R <- B
                    indexer[y, x] = color;
                }
            }

在这里插入图片描述

四、Solve Equation

            // x + y = 10
            // 2x + 3y = 26
            // (x=4, y=6)
            double[,] av = {{1, 1},{2, 3}};
            double[] yv = { 10, 26 };

            Mat a = new Mat(2, 2, MatType.CV_64FC1, av);
            Mat y = new Mat(2, 1, MatType.CV_64FC1, yv);
            Mat x = new Mat();

            Cv2.Solve(a, y, x, DecompTypes.LU);

            // => X1 = 4, X2 = 6
            Debug.WriteLine("X1 = {0}, X2 = {1}", x.At<double>(0), x.At<double>(1));

五、Histogram

            Mat src = Cv2.ImRead("lenna.png", ImreadModes.Grayscale);

            WriteableBitmap wb = src.ToWriteableBitmap();
            iamgeHost.Source = wb;

           

            // Histogram view
            const int Width = 260, Height = 200;
            Mat render = new Mat(new OpenCvSharp.Size(Width, Height), MatType.CV_8UC3, Scalar.All(255));

            // Calculate histogram
            Mat hist = new Mat();
            int[] hdims = { 256 }; // Histogram size for each dimension
            Rangef[] ranges = { new Rangef(0, 256), }; // min/max 
            Cv2.CalcHist(
                new Mat[] { src },
                new int[] { 0 },
                null,
                hist,
                1,
                hdims,
                ranges);

            // Get the max value of histogram
            double minVal, maxVal;
            Cv2.MinMaxLoc(hist, out minVal, out maxVal);

            Scalar color = Scalar.All(100);
            // Scales and draws histogram
            hist = hist * (maxVal != 0 ? Height / maxVal : 0.0);
            for (int j = 0; j < hdims[0]; ++j)
            {
                int binW = (int)((double)Width / hdims[0]);
                render.Rectangle(
                    new OpenCvSharp.Point(j * binW, render.Rows - (int)(hist.Get<float>(j))),
                    new OpenCvSharp.Point((j + 1) * binW, render.Rows),
                    color,
                    -1);
            }

            using (new OpenCvSharp.Window("Histogram",render, WindowFlags.AutoSize | WindowFlags.FreeRatio))
            {
                Cv2.WaitKey();
            }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值