Example #1
0
 /// <summary>
 /// Returns Gaussian filter coefficients.
 /// </summary>
 /// <param name="ksize">Aperture size. It should be odd and positive.</param>
 /// <param name="sigma">Gaussian standard deviation.
 /// If it is non-positive, it is computed from ksize as `sigma = 0.3*((ksize-1)*0.5 - 1) + 0.8`.</param>
 /// <param name="ktype">Type of filter coefficients. It can be CV_32F or CV_64F.</param>
 /// <returns></returns>
 public static Mat GetGaussianKernel(int ksize, double sigma, MatType? ktype = null)
 {
     var ktype0 = ktype.GetValueOrDefault(MatType.CV_64F);
     var ret = NativeMethods.imgproc_getGaussianKernel(ksize, sigma, ktype0);
     if (ret == IntPtr.Zero)
         return null;
     return new Mat(ret);
 }
Example #2
0
        /// <summary>
        /// Returns filter coefficients for computing spatial image derivatives.
        /// </summary>
        /// <param name="kx">Output matrix of row filter coefficients. It has the type ktype.</param>
        /// <param name="ky">Output matrix of column filter coefficients. It has the type ktype.</param>
        /// <param name="dx">Derivative order in respect of x.</param>
        /// <param name="dy">Derivative order in respect of y.</param>
        /// <param name="ksize">Aperture size. It can be CV_SCHARR, 1, 3, 5, or 7.</param>
        /// <param name="normalize">Flag indicating whether to normalize (scale down) the filter coefficients or not.
        /// Theoretically, the coefficients should have the denominator \f$=2^{ksize*2-dx-dy-2}\f$. 
        /// If you are going to filter floating-point images, you are likely to use the normalized kernels.
        /// But if you compute derivatives of an 8-bit image, store the results in a 16-bit image, 
        /// and wish to preserve all the fractional bits, you may want to set normalize = false.</param>
        /// <param name="ktype">Type of filter coefficients. It can be CV_32f or CV_64F.</param>
        public static void GetDerivKernels(
            OutputArray kx, OutputArray ky, int dx, int dy, int ksize,
            bool normalize = false, MatType? ktype = null)
        {
            if (kx == null)
                throw new ArgumentNullException(nameof(kx));
            if (ky == null)
                throw new ArgumentNullException(nameof(ky));
            kx.ThrowIfNotReady();
            ky.ThrowIfNotReady();

            var ktype0 = ktype.GetValueOrDefault(MatType.CV_32F);
            NativeMethods.imgproc_getDerivKernels(
                kx.CvPtr, ky.CvPtr, dx, dy, ksize, normalize ? 1 : 0, ktype0);

            kx.Fix();
            ky.Fix();
        }
Example #3
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="src"></param>
 /// <param name="dst"></param>
 /// <param name="ddepth"></param>
 /// <param name="kernel"></param>
 public static void Filter2D(InputArray src, OutputArray dst, MatType ddepth, InputArray kernel)
 {
     Filter2D(src, dst, ddepth, kernel, new Point(-1, -1));
 }
Example #4
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="src"></param>
 /// <param name="dst"></param>
 /// <param name="ddepth"></param>
 /// <param name="kernelX"></param>
 /// <param name="kernelY"></param>
 public static void SepFilter2D(InputArray src, OutputArray dst, MatType ddepth, InputArray kernelX, InputArray kernelY)
 {
     SepFilter2D(src, dst, ddepth, kernelX, kernelY, new Point(-1, -1));
 }
Example #5
0
 /// <summary>
 /// Creates continuous GPU matrix
 /// </summary>
 /// <param name="size">Number of rows and columns in a 2D array.</param>
 /// <param name="type">Array type.</param>
 /// <returns></returns>
 public static GpuMat CreateContinuous(Size size, MatType type)
 {
     ThrowIfGpuNotAvailable();
     return CreateContinuous(size.Height, size.Width, type);
 }
Example #6
0
 /// <summary>
 /// Ensures that size of the given matrix is not less than (rows, cols) size
 /// and matrix type is match specified one too
 /// </summary>
 /// <param name="size">Number of rows and columns in a 2D array.</param>
 /// <param name="type">Array type.</param>
 /// <param name="m"></param>
 public static void EnsureSizeIsEnough(Size size, MatType type, GpuMat m)
 {
     ThrowIfGpuNotAvailable();
     EnsureSizeIsEnough(size.Height, size.Width, type, m);
 }
Example #7
0
 /// <summary>
 /// Computes a Hanning window coefficients in two dimensions.
 /// </summary>
 /// <param name="winSize">The window size specifications</param>
 /// <param name="type">Created array type</param>
 public void CreateHanningWindow(Size winSize, MatType type)
 {
     Cv2.CreateHanningWindow(this, winSize, type);
 }
Example #8
0
 public static Mat New(Size size, MatType type)
 {
     return(new Mat(size.ToCvSize(), type));
 }
Example #9
0
 internal static extern void core_Mat_type(IntPtr mat, out MatType output);
Example #10
0
 /// <summary>
 /// Smoothes image using box filter
 /// </summary>
 /// <param name="ddepth"></param>
 /// <param name="ksize">The smoothing kernel size</param>
 /// <param name="anchor">The anchor point. The default value Point(-1,-1) means that the anchor is at the kernel center</param>
 /// <param name="normalize">Indicates, whether the kernel is normalized by its area or not</param>
 /// <param name="borderType">The border mode used to extrapolate pixels outside of the image</param>
 /// <returns>The destination image; will have the same size and the same type as src</returns>
 public Mat BoxFilter(MatType ddepth, Size ksize, Point? anchor = null, 
     bool normalize = true, BorderType borderType = BorderType.Default)
 {
     var dst = new Mat();
     Cv2.BoxFilter(this, dst, ddepth, ksize, anchor, normalize, borderType);
     return dst;
 }
Example #11
0
        /// <summary>
        ///  sobel 法
        /// </summary>
        /// <param name="fileName"> </param>
        private void SobelMethod()
        {
            if (this.srcImg == null)
            {
                return;
            }
            if (this.srcImg.Empty())
            {
                return;
            }

            Mat blur = null;
            Mat gary = null;

            int blurSize = int.Parse(this.blurSize.Text);

            blur = this.srcImg.GaussianBlur(new OpenCvSharp.Size(blurSize, blurSize), 0);
            gary = blur.CvtColor(ColorConversionCodes.BGR2GRAY);

            // sobel 运算
            MatType ddepth = MatType.CV_16S;

            int sobel_Scale       = 1;
            int sobel_Delta       = 0;
            int sobel_X_Weight    = 1;
            int sobel_Y_Weight    = 0;
            int morph_Size_Width  = 17;
            int morph_Size_Height = 3;

            Mat grad_x = gary.Sobel(ddepth, 1, 0, 3, sobel_Scale, sobel_Delta, BorderTypes.Default);

            Mat abs_grad_x = grad_x.ConvertScaleAbs();

            Mat grad_y = gary.Sobel(ddepth, 0, 1, 3, sobel_Scale, sobel_Delta, BorderTypes.Default);

            Mat abs_grad_y = grad_y.ConvertScaleAbs();

            Mat grad = new Mat();

            Cv2.AddWeighted(abs_grad_x, sobel_X_Weight, abs_grad_y, sobel_Y_Weight, 0, grad);

            this.picGrade.Image = grad.ToBitmap();

            //二值化
            Mat threshold = gary.Threshold(0, 255, ThresholdTypes.Otsu | ThresholdTypes.Binary);

            this.picTh_sobel.Image = threshold.ToBitmap();

            //形态学操作
            Mat element = Cv2.GetStructuringElement(MorphShapes.Rect,
                                                    new OpenCvSharp.Size(morph_Size_Width, morph_Size_Height));
            Mat threshold_Close = threshold.MorphologyEx(MorphTypes.Close, element);

            this.picmorphology_sobel.Image = threshold_Close.ToBitmap();

            //腐蚀
            Mat element_Erode = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(3, 3));

            threshold_Erode       = threshold_Close.Erode(element_Erode);
            this.picCorrode.Image = threshold_Erode.ToBitmap();
            //释放内存  --- 不够及时
            GC.Collect();
        }
Example #12
0
 internal static extern void core_Mat_new8(int rows, int cols, MatType type, IntPtr data, IntPtr step, out IntPtr returnValue);
        public static SSIMResult getMSSIM(Mat i1, Mat i2)
        {
            const double C1 = 6.5025, C2 = 58.5225;
            /***************************** INITS **********************************/
            MatType d = MatType.CV_32F;

            Mat I1 = new Mat(), I2 = new Mat();

            i1.ConvertTo(I1, d);           // cannot calculate on one byte large values
            i2.ConvertTo(I2, d);

            Mat I2_2  = I2.Mul(I2);       // I2^2
            Mat I1_2  = I1.Mul(I1);       // I1^2
            Mat I1_I2 = I1.Mul(I2);       // I1 * I2

            /***********************PRELIMINARY COMPUTING ******************************/

            Mat mu1 = new Mat(), mu2 = new Mat();   //

            Cv2.GaussianBlur(I1, mu1, new OpenCvSharp.Size(11, 11), 1.5);
            Cv2.GaussianBlur(I2, mu2, new OpenCvSharp.Size(11, 11), 1.5);

            Mat mu1_2   = mu1.Mul(mu1);
            Mat mu2_2   = mu2.Mul(mu2);
            Mat mu1_mu2 = mu1.Mul(mu2);

            Mat sigma1_2 = new Mat(), sigma2_2 = new Mat(), sigma12 = new Mat();

            Cv2.GaussianBlur(I1_2, sigma1_2, new OpenCvSharp.Size(11, 11), 1.5);
            sigma1_2 -= mu1_2;

            Cv2.GaussianBlur(I2_2, sigma2_2, new OpenCvSharp.Size(11, 11), 1.5);
            sigma2_2 -= mu2_2;

            Cv2.GaussianBlur(I1_I2, sigma12, new OpenCvSharp.Size(11, 11), 1.5);
            sigma12 -= mu1_mu2;

            ///////////////////////////////// FORMULA ////////////////////////////////
            Mat t1, t2, t3;

            t1 = 2 * mu1_mu2 + C1;
            t2 = 2 * sigma12 + C2;
            t3 = t1.Mul(t2);              // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))

            t1 = mu1_2 + mu2_2 + C1;
            t2 = sigma1_2 + sigma2_2 + C2;
            t1 = t1.Mul(t2);               // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))

            Mat ssim_map = new Mat();

            Cv2.Divide(t3, t1, ssim_map);      // ssim_map =  t3./t1;

            Scalar mssim = Cv2.Mean(ssim_map); // mssim = average of ssim map



            SSIMResult result = new SSIMResult();

            result.diff  = ssim_map;
            result.mssim = mssim;


            return(result);
        }
Example #14
0
 /// <summary>
 /// Ensures that size of the given matrix is not less than (rows, cols) size
 /// and matrix type is match specified one too
 /// </summary>
 /// <param name="size">Number of rows and columns in a 2D array.</param>
 /// <param name="type">Array type.</param>
 /// <param name="m"></param>
 public static void EnsureSizeIsEnough(Size size, MatType type, GpuMat m)
 {
     ThrowIfGpuNotAvailable();
     EnsureSizeIsEnough(size.Height, size.Width, type, m);
 }
Example #15
0
 /// <summary>
 /// Creates continuous GPU matrix
 /// </summary>
 /// <param name="size">Number of rows and columns in a 2D array.</param>
 /// <param name="type">Array type.</param>
 /// <returns></returns>
 public static GpuMat CreateContinuous(Size size, MatType type)
 {
     ThrowIfGpuNotAvailable();
     return(CreateContinuous(size.Height, size.Width, type));
 }
 /// <summary>
 /// initializes maps for cv::remap() to correct lens distortion and optionally rectify the image
 /// </summary>
 /// <param name="cameraMatrix"></param>
 /// <param name="distCoeffs"></param>
 /// <param name="r"></param>
 /// <param name="newCameraMatrix"></param>
 /// <param name="size"></param>
 /// <param name="m1Type"></param>
 /// <param name="map1"></param>
 /// <param name="map2"></param>
 public static void InitUndistortRectifyMap(InputArray cameraMatrix, InputArray distCoeffs,
     InputArray r, InputArray newCameraMatrix,
     Size size, MatType m1Type, OutputArray map1, OutputArray map2)
 {
     if (cameraMatrix == null)
         throw new ArgumentNullException("cameraMatrix");
     if (distCoeffs == null)
         throw new ArgumentNullException("distCoeffs");
     if (r == null)
         throw new ArgumentNullException("r");
     if (newCameraMatrix == null)
         throw new ArgumentNullException("newCameraMatrix");
     if (map1 == null)
         throw new ArgumentNullException("map1");
     if (map2 == null)
         throw new ArgumentNullException("map2");
     cameraMatrix.ThrowIfDisposed();
     distCoeffs.ThrowIfDisposed();
     r.ThrowIfDisposed();
     newCameraMatrix.ThrowIfDisposed();
     map1.ThrowIfNotReady();
     map2.ThrowIfNotReady();
     NativeMethods.imgproc_initUndistortRectifyMap(
         cameraMatrix.CvPtr, distCoeffs.CvPtr, r.CvPtr, newCameraMatrix.CvPtr, size, m1Type, map1.CvPtr, map2.CvPtr);
     map1.Fix();
     map2.Fix();
 }
Example #17
0
 /// <summary>
 /// allocates new matrix data unless the matrix already has specified size and type.
 /// previous data is unreferenced if needed.
 /// </summary>
 /// <param name="rows">Number of rows in a 2D array.</param>
 /// <param name="cols">Number of columns in a 2D array.</param>
 /// <param name="type">Array type. </param>
 public void Create(int rows, int cols, MatType type)
 {
     ThrowIfDisposed();
     NativeMethods.gpu_GpuMat_create1(ptr, rows, cols, type);
 }
Example #18
0
 /// <summary>
 /// Creates continuous GPU matrix
 /// </summary>
 /// <param name="size">Number of rows and columns in a 2D array.</param>
 /// <param name="type">Array type.</param>
 /// <param name="m"></param>
 public static void CreateContinuous(Size size, MatType type, GpuMat m)
 {
     ThrowIfGpuNotAvailable();
     CreateContinuous(size.Height, size.Width, type, m);
 }
Example #19
0
 /// <summary>
 /// allocates new matrix data unless the matrix already has specified size and type.
 /// previous data is unreferenced if needed.
 /// </summary>
 /// <param name="size">2D array size: Size(cols, rows) </param>
 /// <param name="type">Array type. </param>
 public void Create(Size size, MatType type)
 {
     ThrowIfDisposed();
     NativeMethods.gpu_GpuMat_create2(ptr, size, type);
 }
Example #20
0
 /// <summary>
 /// Calculates the first x- or y- image derivative using Scharr operator
 /// </summary>
 /// <param name="ddepth">The destination image depth</param>
 /// <param name="xorder">Order of the derivative x</param>
 /// <param name="yorder">Order of the derivative y</param>
 /// <param name="scale">The optional scale factor for the computed derivative values (by default, no scaling is applie</param>
 /// <param name="delta">The optional delta value, added to the results prior to storing them in dst</param>
 /// <param name="borderType">The pixel extrapolation method</param>
 /// <returns>The destination image; will have the same size and the same number of channels as src</returns>
 public Mat Scharr(MatType ddepth, int xorder, int yorder,
     double scale = 1, double delta = 0, BorderType borderType = BorderType.Default)
 {
     var dst = new Mat();
     Cv2.Scharr(this, dst, ddepth, xorder, yorder, scale, delta, borderType);
     return dst;
 }
Example #21
0
        private static void Sobel(string path)
        {
            using (Mat src = new Mat(path, ImreadModes.AnyColor | ImreadModes.AnyDepth))
            {
                //1:高斯模糊平滑
                Mat dst = new Mat();
                Cv2.GaussianBlur(src, dst, new OpenCvSharp.Size(3, 3), 0, 0, BorderTypes.Default);
                //转为灰度
                //Mat gray = new Mat();
                //Cv2.CvtColor(dst, gray, ColorConversionCodes.BGR2GRAY);

                MatType m = src.Type();

                //求 X 和 Y 方向的梯度  Sobel  and scharr
                Mat xgrad = new Mat();
                Mat ygrad = new Mat();
                Cv2.Sobel(src, xgrad, MatType.CV_16S, 1, 0, 3);
                Cv2.Sobel(src, ygrad, MatType.CV_16S, 0, 1, 3);

                Cv2.ConvertScaleAbs(xgrad, xgrad);//缩放、计算绝对值并将结果转换为8位。不做转换的化显示不了,显示图相只能是8U类型
                Cv2.ConvertScaleAbs(ygrad, ygrad);

                //加强边缘检测
                //Cv2.Scharr(gray, xgrad, -1, 1, 0, 3);
                //Cv2.Scharr(gray, ygrad, -1, 0, 1, 3);

                Mat output = new Mat(xgrad.Size(), xgrad.Type());
                //图像混合相加(基于权重 0.5)不精确
                //Cv2.AddWeighted(xgrad, 0.5, ygrad, 0.5, 0, output);

                //基于 算法 G=|Gx|+|Gy|
                int width = xgrad.Cols;
                int hight = xgrad.Rows;

                //基于 G= (Gx*Gx +Gy*Gy)的开方根
                for (int x = 0; x < hight; x++)
                {
                    for (int y = 0; y < width; y++)
                    {
                        int xg = xgrad.At <byte>(x, y);
                        int yg = ygrad.At <byte>(x, y);
                        //byte xy =(byte) (xg + yg);
                        double v1  = Math.Pow(xg, 2);
                        double v2  = Math.Pow(yg, 2);
                        int    val = (int)Math.Sqrt(v1 + v2);
                        if (val > 255) //确保像素值在 0 -- 255 之间
                        {
                            val = 255;
                        }
                        if (val < 0)
                        {
                            val = 0;
                        }
                        byte xy = (byte)val;
                        output.Set <byte>(x, y, xy);
                    }
                }
                using (new OpenCvSharp.Window("X Image", WindowMode.Normal, xgrad));
                //using (new Window("Y Image", WindowMode.Normal, ygrad))
                //using (new Window("OUTPUT Image", WindowMode.Normal, output))
                //using (new Window("SRC", WindowMode.Normal, src))
            }
        }
Example #22
0
 /// <summary>
 /// computes covariation matrix of a set of samples
 /// </summary>
 /// <param name="samples"></param>
 /// <param name="covar"></param>
 /// <param name="mean"></param>
 /// <param name="flags"></param>
 /// <param name="ctype"></param>
 public static void CalcCovarMatrix(InputArray samples, OutputArray covar,
     InputOutputArray mean, CovarFlags flags, MatType ctype)
 {
     if (samples == null)
         throw new ArgumentNullException("samples");
     if (covar == null)
         throw new ArgumentNullException("covar");
     if (mean == null)
         throw new ArgumentNullException("mean");
     samples.ThrowIfDisposed();
     covar.ThrowIfNotReady();
     mean.ThrowIfNotReady();
     NativeMethods.core_calcCovarMatrix_InputArray(samples.CvPtr, covar.CvPtr, mean.CvPtr, (int)flags, ctype);
     GC.KeepAlive(samples); 
     covar.Fix();
     mean.Fix();
 }
Example #23
0
 internal static extern void imgproc_filter2D(IntPtr src, IntPtr dst, MatType ddepth, IntPtr kernel, Point anchor,
                                              double delta, BorderTypes borderType);
Example #24
0
 /// <summary>
 /// Creates continuous GPU matrix
 /// </summary>
 /// <param name="size">Number of rows and columns in a 2D array.</param>
 /// <param name="type">Array type.</param>
 /// <param name="m"></param>
 public static void CreateContinuous(Size size, MatType type, GpuMat m)
 {
     ThrowIfGpuNotAvailable();
     CreateContinuous(size.Height, size.Width, type, m);
 }
Example #25
0
 /// <summary>
 /// allocates new matrix data unless the matrix already has specified size and type.
 /// previous data is unreferenced if needed.
 /// </summary>
 /// <param name="rows">Number of rows in a 2D array.</param>
 /// <param name="cols">Number of columns in a 2D array.</param>
 /// <param name="type">Array type. </param>
 public void Create(int rows, int cols, MatType type)
 {
     ThrowIfDisposed();
     NativeMethods.cuda_GpuMat_create1(ptr, rows, cols, type);
     GC.KeepAlive(this);
 }
Example #26
0
 /// <summary>
 /// Ensures that size of the given matrix is not less than (rows, cols) size
 /// and matrix type is match specified one too
 /// </summary>
 /// <param name="rows">Number of rows in a 2D array.</param>
 /// <param name="cols">Number of columns in a 2D array.</param>
 /// <param name="type">Array type.</param>
 /// <param name="m"></param>
 public static void EnsureSizeIsEnough(int rows, int cols, MatType type, GpuMat m)
 {
     ThrowIfGpuNotAvailable();
     if (m == null)
         throw new ArgumentNullException(nameof(m));
     NativeMethods.cuda_ensureSizeIsEnough(rows, cols, type, m.CvPtr);
 }
Example #27
0
 /// <summary>
 /// allocates new matrix data unless the matrix already has specified size and type.
 /// previous data is unreferenced if needed.
 /// </summary>
 /// <param name="size">2D array size: Size(cols, rows) </param>
 /// <param name="type">Array type. </param>
 public void Create(Size size, MatType type)
 {
     ThrowIfDisposed();
     NativeMethods.cuda_GpuMat_create2(ptr, size, type);
     GC.KeepAlive(this);
 }
Example #28
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="src"></param>
 /// <param name="dst"></param>
 /// <param name="ddepth"></param>
 /// <param name="ksize"></param>
 public static void BoxFilter(InputArray src, OutputArray dst, MatType ddepth, Size ksize)
 {
     BoxFilter(src, dst, ddepth, ksize, new Point(-1, -1));
 }
Example #29
0
 /// <summary>
 ///  Create a new Mat instance, and trace it
 /// </summary>
 /// <param name="size">size</param>
 /// <param name="matType">matType</param>
 /// <param name="scalar">scalar</param>
 /// <returns></returns>
 public Mat NewMat(Size size, MatType matType, Scalar scalar)
 {
     return(T(new Mat(size, matType, scalar)));
 }
Example #30
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="src"></param>
        /// <param name="dst"></param>
        /// <param name="ddepth"></param>
        /// <param name="kernel"></param>
        /// <param name="anchor"></param>
        /// <param name="delta"></param>
        /// <param name="borderType"></param>
        public static void Filter2D(InputArray src, OutputArray dst, MatType ddepth,
	        InputArray kernel, Point anchor, double delta = 0, BorderType borderType = BorderType.Default)
        {
            if (src == null)
                throw new ArgumentNullException("src");
            if (dst == null)
                throw new ArgumentNullException("dst");
            if (kernel == null)
                throw new ArgumentNullException("kernel");
            src.ThrowIfDisposed();
            dst.ThrowIfNotReady();
            kernel.ThrowIfDisposed();
            NativeMethods.imgproc_filter2D(src.CvPtr, dst.CvPtr, ddepth, kernel.CvPtr, anchor, delta, (int)borderType);
            dst.Fix();
        }
 /// <summary>
 /// Smoothes image using box filter
 /// </summary>
 /// <param name="src">The source image</param>
 /// <param name="dst">The destination image; will have the same size and the same type as src</param>
 /// <param name="ddepth"></param>
 /// <param name="ksize">The smoothing kernel size</param>
 /// <param name="anchor">The anchor point. The default value Point(-1,-1) means that the anchor is at the kernel center</param>
 /// <param name="normalize">Indicates, whether the kernel is normalized by its area or not</param>
 /// <param name="borderType">The border mode used to extrapolate pixels outside of the image</param>
 public static void BoxFilter(InputArray src, OutputArray dst, MatType ddepth, 
     Size ksize, Point? anchor = null, bool normalize = true, BorderType borderType = BorderType.Default)
 {
     if (src == null)
         throw new ArgumentNullException("src");
     if (dst == null)
         throw new ArgumentNullException("dst");
     src.ThrowIfDisposed();
     dst.ThrowIfNotReady();
     Point anchor0 = anchor.GetValueOrDefault(new Point(-1, -1));
     NativeMethods.imgproc_boxFilter(src.CvPtr, dst.CvPtr, ddepth, ksize, anchor0, normalize ? 1 : 0, (int)borderType);
     dst.Fix();
 }
 /// <summary>
 /// Computes a Hanning window coefficients in two dimensions.
 /// </summary>
 /// <param name="dst">Destination array to place Hann coefficients in</param>
 /// <param name="winSize">The window size specifications</param>
 /// <param name="type">Created array type</param>
 public static void CreateHanningWindow(InputOutputArray dst, Size winSize, MatType type)
 {
     if (dst == null)
         throw new ArgumentNullException("dst");
     dst.ThrowIfNotReady();
     NativeMethods.imgproc_createHanningWindow(dst.CvPtr, winSize, type);
     dst.Fix();
 }
 /// <summary>
 /// Calculates the Laplacian of an image
 /// </summary>
 /// <param name="src">Source image</param>
 /// <param name="dst">Destination image; will have the same size and the same number of channels as src</param>
 /// <param name="ddepth">The desired depth of the destination image</param>
 /// <param name="ksize">The aperture size used to compute the second-derivative filters</param>
 /// <param name="scale">The optional scale factor for the computed Laplacian values (by default, no scaling is applied</param>
 /// <param name="delta">The optional delta value, added to the results prior to storing them in dst</param>
 /// <param name="borderType">The pixel extrapolation method</param>
 public static void Laplacian(InputArray src, OutputArray dst, MatType ddepth,
     int ksize = 1, double scale = 1, double delta = 0, BorderType borderType = BorderType.Default)
 {
     if (src == null)
         throw new ArgumentNullException("src");
     if (dst == null)
         throw new ArgumentNullException("dst");
     src.ThrowIfDisposed();
     dst.ThrowIfNotReady();
     NativeMethods.imgproc_Laplacian(src.CvPtr, dst.CvPtr, ddepth, ksize, scale, delta, (int)borderType);
     dst.Fix();
 }
 /// <summary>
 /// initializes maps for cv::remap() for wide-angle
 /// </summary>
 /// <param name="cameraMatrix"></param>
 /// <param name="distCoeffs"></param>
 /// <param name="imageSize"></param>
 /// <param name="destImageWidth"></param>
 /// <param name="m1Type"></param>
 /// <param name="map1"></param>
 /// <param name="map2"></param>
 /// <param name="projType"></param>
 /// <param name="alpha"></param>
 /// <returns></returns>
 public static float InitWideAngleProjMap(InputArray cameraMatrix, InputArray distCoeffs,
     Size imageSize, int destImageWidth, MatType m1Type, OutputArray map1, OutputArray map2,
     ProjectionType projType, double alpha = 0)
 {
     if (cameraMatrix == null)
         throw new ArgumentNullException("cameraMatrix");
     if (distCoeffs == null)
         throw new ArgumentNullException("distCoeffs");
     if (map1 == null)
         throw new ArgumentNullException("map1");
     if (map2 == null)
         throw new ArgumentNullException("map2");
     cameraMatrix.ThrowIfDisposed();
     distCoeffs.ThrowIfDisposed();
     map1.ThrowIfNotReady();
     map2.ThrowIfNotReady();
     float ret = NativeMethods.imgproc_initWideAngleProjMap(cameraMatrix.CvPtr, distCoeffs.CvPtr, imageSize,
         destImageWidth, m1Type, map1.CvPtr, map2.CvPtr, (int)projType, alpha);
     map1.Fix();
     map2.Fix();
     return ret;
 }
Example #35
0
 public static Mat New(Size size, MatType type, Array buffer)
 {
     return(new Mat((int)size.Width, (int)size.Height, type, buffer));
 }
 /// <summary>
 /// Applies separable linear filter to an image
 /// </summary>
 /// <param name="src">The source image</param>
 /// <param name="dst">The destination image; will have the same size and the same number of channels as src</param>
 /// <param name="ddepth">The destination image depth</param>
 /// <param name="kernelX">The coefficients for filtering each row</param>
 /// <param name="kernelY">The coefficients for filtering each column</param>
 /// <param name="anchor">The anchor position within the kernel; The default value (-1, 1) means that the anchor is at the kernel center</param>
 /// <param name="delta">The value added to the filtered results before storing them</param>
 /// <param name="borderType">The pixel extrapolation method</param>
 public static void SepFilter2D(InputArray src, OutputArray dst, MatType ddepth, InputArray kernelX, InputArray kernelY,
     Point? anchor = null, double delta = 0, BorderType borderType = BorderType.Default)
 {
     if (src == null)
         throw new ArgumentNullException("src");
     if (dst == null)
         throw new ArgumentNullException("dst");
     if (kernelX == null)
         throw new ArgumentNullException("kernelX");
     if (kernelY == null)
         throw new ArgumentNullException("kernelY");
     src.ThrowIfDisposed();
     dst.ThrowIfNotReady();
     kernelX.ThrowIfDisposed();
     kernelY.ThrowIfDisposed();
     Point anchor0 = anchor.GetValueOrDefault(new Point(-1, -1));
     NativeMethods.imgproc_sepFilter2D(src.CvPtr, dst.CvPtr, ddepth, 
         kernelX.CvPtr, kernelY.CvPtr, anchor0, delta, (int)borderType);
     dst.Fix();
 }
Example #37
0
        /// <summary>
        /// computes the connected components labeled image of boolean image. 
        /// image with 4 or 8 way connectivity - returns N, the total number of labels [0, N-1] where 0 
        /// represents the background label. ltype specifies the output label image type, an important 
        /// consideration based on the total number of labels or alternatively the total number of 
        /// pixels in the source image.
        /// </summary>
        /// <param name="image">the image to be labeled</param>
        /// <param name="labels">destination labeled image</param>
        /// <param name="stats">statistics output for each label, including the background label, 
        /// see below for available statistics. Statistics are accessed via stats(label, COLUMN) 
        /// where COLUMN is one of cv::ConnectedComponentsTypes</param>
        /// <param name="centroids">floating point centroid (x,y) output for each label, 
        /// including the background label</param>
        /// <param name="connectivity">8 or 4 for 8-way or 4-way connectivity respectively</param>
        /// <param name="ltype">output image label type. Currently CV_32S and CV_16U are supported.</param>
        /// <returns></returns>
        public static int ConnectedComponentsWithStats(
            InputArray image, OutputArray labels,
            OutputArray stats, OutputArray centroids,
            PixelConnectivity connectivity,
            MatType ltype)
        {
            if (image == null)
                throw new ArgumentNullException(nameof(image));
            if (labels == null)
                throw new ArgumentNullException(nameof(labels));
            if (stats == null)
                throw new ArgumentNullException(nameof(stats));
            if (centroids == null)
                throw new ArgumentNullException(nameof(centroids));
            image.ThrowIfDisposed();
            labels.ThrowIfNotReady();
            stats.ThrowIfNotReady();
            centroids.ThrowIfNotReady();

            int result = NativeMethods.imgproc_connectedComponentsWithStats(
                image.CvPtr, labels.CvPtr, stats.CvPtr, centroids.CvPtr, (int) connectivity, ltype);

            GC.KeepAlive(image);
            labels.Fix();
            stats.Fix();
            centroids.Fix();
            return result;
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="map1"></param>
 /// <param name="map2"></param>
 /// <param name="dstmap1"></param>
 /// <param name="dstmap2"></param>
 /// <param name="dstmap1Type"></param>
 /// <param name="nnInterpolation"></param>
 public static void ConvertMaps(InputArray map1, InputArray map2, OutputArray dstmap1, OutputArray dstmap2, MatType dstmap1Type, bool nnInterpolation = false)
 {
     if (map1 == null)
         throw new ArgumentNullException("map1");
     if (map2 == null)
         throw new ArgumentNullException("map2");
     if (dstmap1 == null)
         throw new ArgumentNullException("dstmap1");
     if (dstmap2 == null)
         throw new ArgumentNullException("dstmap2");
     map1.ThrowIfDisposed();
     map2.ThrowIfDisposed();
     dstmap1.ThrowIfDisposed();
     dstmap2.ThrowIfDisposed();
     NativeMethods.imgproc_convertMaps(map1.CvPtr, map2.CvPtr, dstmap1.CvPtr, dstmap2.CvPtr, dstmap1Type, nnInterpolation ? 1 : 0);
     dstmap1.Fix();
     dstmap2.Fix();
 }
Example #39
0
        /// <summary>
        /// Convolves an image with the kernel
        /// </summary>
        /// <param name="src">The source image</param>
        /// <param name="dst">The destination image. It will have the same size and the same number of channels as src</param>
        /// <param name="ddepth">The desired depth of the destination image. If it is negative, it will be the same as src.depth()</param>
        /// <param name="kernel">Convolution kernel (or rather a correlation kernel), 
        /// a single-channel floating point matrix. If you want to apply different kernels to 
        /// different channels, split the image into separate color planes using split() and process them individually</param>
        /// <param name="anchor">The anchor of the kernel that indicates the relative position of 
        /// a filtered point within the kernel. The anchor should lie within the kernel. 
        /// The special default value (-1,-1) means that the anchor is at the kernel center</param>
        /// <param name="delta">The optional value added to the filtered pixels before storing them in dst</param>
        /// <param name="borderType">The pixel extrapolation method</param>
        public static void Filter2D(
            InputArray src, OutputArray dst, MatType ddepth,
	        InputArray kernel, Point? anchor = null, double delta = 0, 
            BorderTypes borderType = BorderTypes.Default)
        {
            if (src == null)
                throw new ArgumentNullException(nameof(src));
            if (dst == null)
                throw new ArgumentNullException(nameof(dst));
            if (kernel == null)
                throw new ArgumentNullException(nameof(kernel));
            src.ThrowIfDisposed();
            dst.ThrowIfNotReady();
            kernel.ThrowIfDisposed();
            Point anchor0 = anchor.GetValueOrDefault(new Point(-1, -1));
            NativeMethods.imgproc_filter2D(src.CvPtr, dst.CvPtr, ddepth, kernel.CvPtr, 
                anchor0, delta, (int)borderType);
            GC.KeepAlive(src);
            dst.Fix();
        }
Example #40
0
 /// <summary>
 /// Applies separable linear filter to an image
 /// </summary>
 /// <param name="ddepth">The destination image depth</param>
 /// <param name="kernelX">The coefficients for filtering each row</param>
 /// <param name="kernelY">The coefficients for filtering each column</param>
 /// <param name="anchor">The anchor position within the kernel; The default value (-1, 1) means that the anchor is at the kernel center</param>
 /// <param name="delta">The value added to the filtered results before storing them</param>
 /// <param name="borderType">The pixel extrapolation method</param>
 /// <returns>The destination image; will have the same size and the same number of channels as src</returns>
 public Mat SepFilter2D(MatType ddepth, InputArray kernelX, InputArray kernelY,
     Point? anchor = null, double delta = 0, BorderType borderType = BorderType.Default)
 {
     var dst = new Mat();
     Cv2.SepFilter2D(this, dst, ddepth, kernelX, kernelY, anchor, delta, borderType);
     return dst;
 }
Example #41
0
 /// <summary>
 /// Calculates the first x- or y- image derivative using Scharr operator
 /// </summary>
 /// <param name="src">The source image</param>
 /// <param name="dst">The destination image; will have the same size and the same number of channels as src</param>
 /// <param name="ddepth">The destination image depth</param>
 /// <param name="xorder">Order of the derivative x</param>
 /// <param name="yorder">Order of the derivative y</param>
 /// <param name="scale">The optional scale factor for the computed derivative values (by default, no scaling is applie</param>
 /// <param name="delta">The optional delta value, added to the results prior to storing them in dst</param>
 /// <param name="borderType">The pixel extrapolation method</param>
 public static void Scharr(
     InputArray src, OutputArray dst, MatType ddepth, int xorder, int yorder, 
     double scale = 1, double delta = 0, BorderTypes borderType = BorderTypes.Default)
 {
     if (src == null)
         throw new ArgumentNullException(nameof(src));
     if (dst == null)
         throw new ArgumentNullException(nameof(dst));
     src.ThrowIfDisposed();
     dst.ThrowIfNotReady();
     NativeMethods.imgproc_Scharr(src.CvPtr, dst.CvPtr, ddepth, xorder, yorder, 
         scale, delta, (int)borderType);
     GC.KeepAlive(src);
     dst.Fix();
 }
Example #42
0
 /// <summary>
 /// Calculates the Laplacian of an image
 /// </summary>
 /// <param name="ddepth">The desired depth of the destination image</param>
 /// <param name="ksize">The aperture size used to compute the second-derivative filters</param>
 /// <param name="scale">The optional scale factor for the computed Laplacian values (by default, no scaling is applied</param>
 /// <param name="delta">The optional delta value, added to the results prior to storing them in dst</param>
 /// <param name="borderType">The pixel extrapolation method</param>
 /// <returns>Destination image; will have the same size and the same number of channels as src</returns>
 public Mat Laplacian(MatType ddepth,
     int ksize = 1, double scale = 1, double delta = 0, BorderType borderType = BorderType.Default)
 {
     var dst = new Mat();
     Cv2.Laplacian(this, dst, ddepth, ksize, scale, delta, borderType);
     return dst;
 }
Example #43
0
 /// <summary>
 /// Creates continuous GPU matrix
 /// </summary>
 /// <param name="rows">Number of rows in a 2D array.</param>
 /// <param name="cols">Number of columns in a 2D array.</param>
 /// <param name="type">Array type.</param>
 /// <param name="m"></param>
 public static void CreateContinuous(int rows, int cols, MatType type, GpuMat m)
 {
     ThrowIfGpuNotAvailable();
     if (m == null)
         throw new ArgumentNullException(nameof(m));
     NativeMethods.cuda_createContinuous1(rows, cols, type, m.CvPtr);
 }
Example #44
0
 /// <summary>
 /// computes covariation matrix of a set of samples
 /// </summary>
 /// <param name="samples"></param>
 /// <param name="covar"></param>
 /// <param name="mean"></param>
 /// <param name="flags"></param>
 /// <param name="ctype"></param>
 public static void CalcCovarMatrix(Mat[] samples, Mat covar, Mat mean,
     CovarFlags flags, MatType ctype)
 {
     if (samples == null)
         throw new ArgumentNullException("samples");
     if (covar == null)
         throw new ArgumentNullException("covar");
     if (mean == null)
         throw new ArgumentNullException("mean");
     covar.ThrowIfDisposed();
     mean.ThrowIfDisposed();
     IntPtr[] samplesPtr = EnumerableEx.SelectPtrs(samples);
     NativeMethods.core_calcCovarMatrix_Mat(samplesPtr, samples.Length, covar.CvPtr, mean.CvPtr, (int)flags, ctype);
     GC.KeepAlive(samples);
     GC.KeepAlive(covar);
     GC.KeepAlive(mean);
 }
Example #45
0
 /// <summary>
 /// Creates continuous GPU matrix
 /// </summary>
 /// <param name="rows">Number of rows in a 2D array.</param>
 /// <param name="cols">Number of columns in a 2D array.</param>
 /// <param name="type">Array type.</param>
 /// <returns></returns>
 public static GpuMat CreateContinuous(int rows, int cols, MatType type)
 {
     ThrowIfGpuNotAvailable();
     IntPtr ret = NativeMethods.cuda_createContinuous2(rows, cols, type);
     return new GpuMat(ret);
 }
        /// <summary>
        /// 指定したIplImageのビット深度・チャンネル数に適合するPixelFormatを返す
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private static PixelFormat GetOptimumPixelFormats(MatType type)
        {
            if (type == MatType.CV_8UC1 || type == MatType.CV_8SC1)
                return PixelFormats.Gray8;
            if (type == MatType.CV_8UC3 || type == MatType.CV_8SC3)
                return PixelFormats.Bgr24;
            if (type == MatType.CV_8UC4 || type == MatType.CV_8SC4)
                return PixelFormats.Bgra32;

            if (type == MatType.CV_16UC1 || type == MatType.CV_16SC1)
                return PixelFormats.Gray16;
            if (type == MatType.CV_16UC3 || type == MatType.CV_16SC3)
                return PixelFormats.Rgb48;
            if (type == MatType.CV_16UC4 || type == MatType.CV_16SC4)
                return PixelFormats.Rgba64;

            if (type == MatType.CV_32SC4)
                return PixelFormats.Prgba64;

            if (type == MatType.CV_32FC1)
                return PixelFormats.Gray32Float;
            if (type == MatType.CV_32FC3)
                return PixelFormats.Rgb128Float;
            if (type == MatType.CV_32FC4)
                return PixelFormats.Rgba128Float;

            throw new ArgumentOutOfRangeException("type", "Not supported MatType");
        }
Example #47
0
 public static Mat GetMat(int rows, int columns, byte[] array, MatType matType)
 {
     return(new Mat(rows, columns, MatType.CV_8S, array));
 }