OpenCVSharp 4.5 图像加边框

OpenCVSharp 4.5 跑一遍OpenCV官方教程(全为手敲代码,如有雷同都是我的错)

图像加边框

OpenCV教程链接:https://docs.opencv.org/4.5.0/dc/da3/tutorial_copyMakeBorder.html

核心函数: copyMakeBorder()

 

using OpenCvSharp;
using System;

namespace ConsoleApp1
{
    class tutorial12 : ITutorial
    {
        public void Run()
        {
            // Declare the variables
            Mat src, dst;
            int top, bottom, left, right;
            BorderTypes borderType = BorderTypes.Constant;
            const string window_name = "copyMakeBorder Demo";
            RNG rng = new RNG(12345);

            const string imageName = "I:\\csharp\\images\\lena.jpg";
            // Loads an image
            src = Cv2.ImRead(imageName, ImreadModes.Color); // Load an image
                                                            // Check if image is loaded fine
            if (src.Empty())
            {
                Console.WriteLine(" Error opening image\n");
                return;
            }

            Cv2.NamedWindow(window_name, WindowFlags.AutoSize);
            // Initialize arguments for the filter
            top = (int)(0.05 * src.Rows); bottom = top;
            left = (int)(0.05 * src.Cols); right = left;
            dst = new Mat();
            for (; ; )
            {
                Scalar value = new Scalar(rng.Uniform(0, 255), rng.Uniform(0, 255), rng.Uniform(0, 255));
                Cv2.CopyMakeBorder(src, dst, top, bottom, left, right, borderType, value);
                Cv2.ImShow(window_name, dst);
                char c = (char)Cv2.WaitKey(500);
                if (c == 27)
                { break; }
                else if (c == 'c')
                { borderType = BorderTypes.Constant; }
                else if (c == 'r')
                { borderType = BorderTypes.Replicate; }
            }
        }
    }
}

OpenCVSharp 4.5 图像加边框_第1张图片

 

你可能感兴趣的:(OpenCVSharp,opencv)