콘솔프로젝트로 이미지 필터링하기

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            applyLinearFilter();
            testBuiltinFilters();
        }

        private static void testBuiltinFilters()
        {
            using (var src = new Mat(@"c:\lena512.jpg", ImreadModes.AnyDepth | ImreadModes.AnyColor))
            {
                using (var dst = new Mat())
                {
                    src.CopyTo(dst);

                    using (new Window("src", image: src))
                    {
                        Cv2.Erode(src, dst, new Mat());
                        using (new Window("Erode", image: dst))
                        {
                            Cv2.Dilate(src, dst, new Mat());
                            using (new Window("Dilate", image: dst))
                            {
                                Cv2.BitwiseNot(src, dst);
                                using (new Window("Invert", image: dst))
                                {
                                    Cv2.WaitKey();
                                }
                            }
                        }
                    }
                }
            }
        }

        private static void applyLinearFilter()
        {
            using (var src = new Mat(@"c:\lena512.jpg", ImreadModes.AnyDepth | ImreadModes.AnyColor))
            using (var dst = new Mat())
            {
                src.CopyTo(dst);

                float[] data = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
                var kernel = new Mat(rows: 1, cols: 21, type: MatType.CV_32FC1, data: data);

                Cv2.Normalize(src: kernel, dst: kernel, alpha: 1.0, beta: 0, normType: NormTypes.L1);

                double sum = 0;
                foreach (var item in data)
                {
                    sum += Math.Abs(item);
                }
                Console.WriteLine(sum); // => .999999970197678

                Cv2.Filter2D(src, dst, MatType.CV_32FC1, kernel, anchor: new Point(0, 0));

                using (new Window("src", image: src))
                using (new Window("dst", image: dst))
                {
                    Cv2.WaitKey();
                }
            }

        }


    }
}

댓글

이 블로그의 인기 게시물

Draw Circle on PictureBox when Button Click - Winform

2D FFT of Gray Image by AForge