AoiBlobControl.cs 3.0 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AOI;
using OpenCvSharp.Blob;

namespace AccAOI.control
{
    public partial class AoiBlobControl : ABaseControl
    {
        public AoiBlobControl()
        {
            InitializeComponent();
        }

        private void trackBarThresh_ValueChanged(object sender, EventArgs e)
        {
            int value = trackBarThresh.Value;
            lblthresh.Text = value.ToString();
            UpdateImage();
        }
        private AoiBlobMethod MethodBlob = null;
        public override void ShowAoiInfo()
        {
            if(this.AoiInfo is AoiBlobMethod)
            {
                MethodBlob = (AoiBlobMethod)AoiInfo;
                if (MethodBlob.thresh < 0)
                {
                    trackBarThresh.Value = 0;
                }
                else
                {
                    trackBarThresh.Value = MethodBlob.thresh;
                }

                chkwhiteOnBlack.Checked = MethodBlob.whiteOnBlack;
                txtmaxArea.Text = MethodBlob.maxArea.ToString();
                txtminArea.Text = MethodBlob.minArea.ToString();
                txtmaxNum.Text = MethodBlob.maxNum.ToString();
                txtminNum.Text = MethodBlob.minNum.ToString();
            }

        }
        public override AoiMethod  GetAoiInfo()
        {
            if (this.AoiInfo is AoiBlobMethod)
            {
                MethodBlob = (AoiBlobMethod)AoiInfo;
                if (trackBarThresh.Value.Equals(0))
                {
                    MethodBlob.thresh = -1;
                }
                else
                {
                    MethodBlob.thresh= trackBarThresh.Value;
                }

                MethodBlob.whiteOnBlack= chkwhiteOnBlack.Checked   ;
                MethodBlob.maxArea = FormUtil.GetIntValue(txtmaxArea);
                MethodBlob.minArea = FormUtil.GetIntValue(txtminArea);
                MethodBlob.maxNum = FormUtil.GetIntValue(txtmaxNum);
                MethodBlob.minNum = FormUtil.GetIntValue(txtminNum);
                if (currPath != null)
                {
                    MethodBlob.RoiPath = currPath;
                }
                AoiInfo = MethodBlob;
            }
            return AoiInfo;
        }
        public override void UpdateImage()
        {
            if (BaseImage == null)
            {
                return;
            }
            GetAoiInfo();
            Image outImage = null;
            List<CvBlob> list = new List<CvBlob>();
            float result = MethodBlob.GetBlobNum(BaseImage, out outImage, out list);

            txtNumResult.Text = result.ToString();
            this.aoiImage.Image = outImage;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            UpdateImage();
        }

        private void chkwhiteOnBlack_CheckedChanged(object sender)
        {
            UpdateImage();
        }
    }
}