AoiMethod.cs 3.2 KB
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AOI
{
    public abstract class AoiMethod
    {
        //public string FullTypeName { get; set; }

        //public string PathDatas { get; set; }

        public string MethodName { get; set; }
        /// <summary>
        /// 兴趣区域路径
        /// </summary>
        [Newtonsoft.Json.JsonIgnore()]
        public GraphicsPath RoiPath { get; set; }

        public PathData GetRoiPathData()
        {
            if (RoiPath != null)
            {
                return RoiPath.PathData;
            }
            return null;
        }


        public abstract ResultBean Check(Image standardImage, Image imageToCheck);
        
        /// <summary>
        /// 获取单通道的掩模图片
        /// </summary>
        /// <param name="img"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public Image GetRoiMask(Image img, GraphicsPath path)
        {
            if (path != null)
            {
                var bounds = path.GetBounds();
                if (bounds.Width > 0 && bounds.Height > 0)
                {
                    try
                    {
                        Bitmap mask = new Bitmap(img.Width, img.Height);
                        using (Graphics g = Graphics.FromImage(mask))
                        {
                            var br = new TextureBrush(img);
                            g.FillPath(br, path);
                        }
                        return mask;
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }
            }
            return null;
        }

        public Image GetRoiImage(Image image, GraphicsPath path, bool needCut)
        {
            Image maskImg = GetRoiMask(image, path);
            if (maskImg != null)
            {
                var bounds = path.GetBounds();
                int resultWidth = (int)bounds.Width;
                int resultHeight = (int)bounds.Height;
                var srcLocation = bounds.Location;
                if (!needCut)
                {
                    //获取带原图位置的大图
                    resultWidth = image.Width;
                    resultHeight = image.Height;
                    srcLocation = PointF.Empty;
                }

                Bitmap result = new Bitmap(resultWidth, resultHeight);
                var dstRect = new RectangleF(0, 0, resultWidth, resultHeight);
                var srcRect = new RectangleF(srcLocation.X, srcLocation.Y, resultWidth, resultHeight);
                using (Graphics g = Graphics.FromImage(result))
                {
                    g.DrawImage(maskImg, dstRect, srcRect, GraphicsUnit.Pixel);
                }
                return result;
            }
            return null;
        }

        public Image GetRoiImage(Image image, bool needCut)
        {
            return GetRoiImage(image, RoiPath, needCut);
        }
    }
}