Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a kernel for filter2d in EMGU CV

Tags:

emgucv

I would like to sharp an image by performing CvInvoke.Filter2d with a sharpening kernel: {[-1,-1,-1],[-1, 9,-1],[-1,-1,-1]}

How do I can define this kernel in EMGU under C#?

I'm using EMGU version 4.3.0.3890

This is my code:

        // Load image
        Image<Bgr, Byte> img = null;
        img = new Image<Bgr, byte>(@"D:\Sample.png");
        
        // Convert the image to grayscale
        Image<Gray, float> gray = img.Convert<Gray, float>()
        .ThresholdBinary(new Gray(150), new Gray(255));

        // Add use of Filter2D here
like image 973
Itay Krisher Avatar asked Sep 07 '25 02:09

Itay Krisher


1 Answers

        // Load image
        Image<Bgr, byte> img = new Image<Bgr, byte>(@"path/img.jpg");

        // Convert the image to grayscale
        Image<Gray, byte> gray = img.Convert<Gray, byte>().ThresholdBinary(new Gray(150), new Gray(255));

        // Add use of Filter2D here
        CvInvoke.Imshow("GrayImage", gray);

        float[,] matrix = new float[3, 3] {
                      { 1, 1, 1 },
                      {  1, -9, 1},
                      { 1, 1, 1 }
                    };
        ConvolutionKernelF matrixKernel = new ConvolutionKernelF(matrix);

        var dst = new Mat(gray.Size, DepthType.Cv8U, 1);
        CvInvoke.Filter2D(gray, dst, matrixKernel, new Point(0, 0));

        CvInvoke.Imshow("FiltredImage", dst);

        CvInvoke.WaitKey(0);

PS:

  • Preferably you have to use Mat and Matrix (if you need to access the pixels) and not image. --> source: https://www.emgu.com/wiki/index.php/Working_with_Images

  • By converting to gray level, it is necessary to take the type byte and not float

like image 135
RashidLadj_Winux Avatar answered Sep 11 '25 00:09

RashidLadj_Winux