/// <summary>
        /// 模
        /// </summary>
        /// <param name="image">图片对象</param>
        /// <returns></returns>
        public static Image Model(this Bitmap image)
        {
            using (Mat src = image.ToMat())
            {
                using (Mat sobelX = new Mat())
                {
                    Cv2.Sobel(src, sobelX, MatType.CV_8U, 1, 0, 3, 0.4, 128);
                    using (Mat sobelY = new Mat())
                    {
                        Cv2.Sobel(src, sobelY, MatType.CV_8U, 0, 1, 3, 0.4, 128);

                        Cv2.Sobel(src, sobelX, MatType.CV_16S, 1, 0);
                        Cv2.Sobel(src, sobelY, MatType.CV_16S, 0, 1);

                        // 计算L1模
                        using (Mat sobel = sobelX.Abs() + sobelY.Abs())
                        {
                            double            sobmin, sobmax;
                            OpenCvSharp.Point minLoc, maxLoc;
                            Cv2.MinMaxLoc(sobel, out sobmin, out sobmax, out minLoc, out maxLoc);

                            using (Mat sobelL1Image = new Mat())
                            {
                                sobel.ConvertTo(sobelL1Image, MatType.CV_8U, -255.0 / sobmax, 255);
                                return(sobelL1Image.ToBitmap());
                            }
                        }
                    }
                }
            }
        }
 public Task<Mat> FilterAsync(Bitmap src)
 {
     using (var mat = src.ToMat())
     {
         return FilterAsync(src);
     }
 }
 public Mat Filter(Bitmap src)
 {
     using (var mat = src.ToMat())
     {
         return Filter(src);
     }
 }
 /// <summary>
 /// 黑帽
 /// </summary>
 /// <param name="image">图片对象</param>
 /// <returns></returns>
 public static Image BlackHat(this Bitmap image)
 {
     using (Mat src = image.ToMat())
     {
         using (Mat element5 = new Mat(5, 5, MatType.CV_8U, new Scalar(1)))
         {
             Cv2.MorphologyEx(src, src, MorphTypes.BlackHat, element5);
             return(src.ToBitmap());
         }
     }
 }
 /// <summary>
 /// 双边滤波
 /// </summary>
 /// <param name="image">图片对象</param>
 /// <returns></returns>
 public static Image BilateralFilter(this Bitmap bmp)
 {
     try
     {
         using (Mat src = bmp.ToMat())
         {
             using (Mat result = new Mat())
             {
                 Cv2.BilateralFilter(src, result, 25, 25 * 2, 25 / 2);
                 return(result.ToBitmap());
             }
         }
     }
     catch (OpenCVException)
     {
         return(bmp);
     }
 }
Exemple #6
0
        Boolean IsMatchWithTemplate(System.Drawing.Bitmap monsterRef, System.Drawing.Bitmap monsterTemplate)
        {
            Mat reference = monsterRef.ToMat();
            Mat template  = monsterTemplate.ToMat();
            Mat result    = new Mat(reference.Rows - template.Rows + 1, reference.Cols - template.Cols + 1, MatType.CV_32FC1);
            {
                //Convert input images to gray
                Mat gref = reference.CvtColor(ColorConversionCodes.BGR2GRAY);
                Mat gtpl = template.CvtColor(ColorConversionCodes.BGR2GRAY);

                double threshold = 0.7;
                Cv2.MatchTemplate(gref, gtpl, result, TemplateMatchModes.CCoeffNormed);
                Cv2.Threshold(result, result, threshold, 1.0, ThresholdTypes.Tozero);
                Cv2.MinMaxLoc(result, out _, out double maxval, out _, out _);

                if (maxval >= threshold)
                {
                    return(true);

                    #region Отрисовка найденого сходства (дебажная функция)

                    /*
                     * //Setup the rectangle to draw
                     * Rect r = new Rect(new Point(maxloc.X, maxloc.Y), new Size(template.Width, template.Height));
                     * Console.WriteLine($"MinVal={minval.ToString()} MaxVal={maxval.ToString()} MinLoc={minloc.ToString()} MaxLoc={maxloc.ToString()} Rect={r.ToString()}");
                     * //Draw a rectangle of the matching area
                     * Cv2.Rectangle(reference, r, Scalar.LimeGreen, 2);
                     *
                     * //Fill in the result Mat so you don't find the same area again in the MinMaxLoc
                     * //Rect outRect;
                     * //Cv2.FloodFill(result, maxloc, new Scalar(0), out outRect, new Scalar(0.1), new Scalar(1.0), FloodFillFlags.Link4);
                     *
                     * Cv2.ImShow("Matches", reference);
                     * Cv2.WaitKey();
                     */
                    #endregion
                }
                else
                {
                    return(false);
                }
            }
        }
 /// <summary>
 /// 灰度化
 /// </summary>
 /// <param name="image">图片对象</param>
 /// <returns></returns>
 public static Image Graying(this Bitmap image)
 {
     using (Mat src = image.ToMat())
         return(src.CvtColor(ColorConversionCodes.BGR2GRAY).ToBitmap());
 }
 public Bitmap FilterBitmap(Bitmap src)
 {
     using (var mat = src.ToMat())
     {
         return FilterBitmap(src);
     }
 }