机器视觉通用平台之缺陷检测(区域差分)算法UI

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CvBase;
using HalconDotNet;
using CWindowTool;
using System.IO;
using System.Diagnostics;

namespace CvImageTool.RegionDiff
{
    public partial class RegionDiffForm : Form
    {
        private int ProcessIndex { set; get; }
        private int ImageOptIndex { set; get; }
        private string Description { set; get; }
        private RegionDiffTool regionDiffTool;
        private List<BaseProcess> processes;
        private CWindows[] cCWindows;
        public RegionDiffForm()
        {
            InitializeComponent();
        }

        public RegionDiffForm(int processIndex, int imageOptIndex,
        string cProcessSettingFilePath, string cImageOptSettingFilePath, string cDescription,
        List<BaseProcess> baseProcesses, CWindows[] cWindows)
        {
            this.StartPosition = FormStartPosition.CenterParent;
            InitializeComponent();
            this.ProcessIndex = processIndex;
            this.ImageOptIndex = imageOptIndex;
            this.Description = cDescription;
            this.processes = baseProcesses;
            this.cCWindows = cWindows;
            regionDiffTool = new RegionDiffTool(processIndex, imageOptIndex, cProcessSettingFilePath, cImageOptSettingFilePath, processes, cWindows);
        }

        private void RegionDiffForm_Load(object sender, EventArgs e)
        {
            //算法工具添加到资源控件
            this.Text = processes[ProcessIndex].Name + "#" + Description;
            Image_OptNameCMBox.Items.Clear();
            Image_OptNameCMBox.Items.Add("相机");
            Line1_OptNameCMBox.Items.Clear();
            Line2_OptNameCMBox.Items.Clear();
            for (int i = 0; i < ImageOptIndex; i++)
            {
                string cDes = processes[ProcessIndex].BaseImageOpts[i].Description;
                Image_OptNameCMBox.Items.Add(cDes);
                Line1_OptNameCMBox.Items.Add(cDes);
                Line2_OptNameCMBox.Items.Add(cDes);
            }

            if (regionDiffTool.ReadFile(Description))
            {
                //图像资源控件显示
                string[] values = regionDiffTool.imageSelect.Split('_');
                if (values.Length == 2)
                {
                    Image_OptNameCMBox.Text = values[0];
                    Image_ParamNameCMBox.Text = values[1];
                }
                else if (values.Length == 1)
                {
                    Image_OptNameCMBox.Text = values[0];
                }
                //直线1资源控件显示
                values = regionDiffTool.region1Select.Split('_');
                if (values.Length == 2)
                {
                    Line1_OptNameCMBox.Text = values[0];
                    Line1_ParamNameCMBox.Text = values[1];
                }
                else if (values.Length == 1)
                {
                    Line1_OptNameCMBox.Text = values[0];
                }
                //直线2资源控件显示
                values = regionDiffTool.region2Select.Split('_');
                if (values.Length == 2)
                {
                    Line2_OptNameCMBox.Text = values[0];
                    Line2_ParamNameCMBox.Text = values[1];
                }
                else if (values.Length == 1)
                {
                    Line2_OptNameCMBox.Text = values[0];
                }
            }

            propertyGrid1.SelectedObject = regionDiffTool.regionDiffParam;
            ShowResultRegionCBox.Checked = regionDiffTool.isShowResultRegion;
            ShowROICBox.Checked = regionDiffTool.isShowMeasureRegion;
            ShowResultCenterCBox.Checked = regionDiffTool.isShowResult;

            if (cCWindows[ProcessIndex].AImage2d != null)
            {
                cWindows1.AImage2d = cCWindows[ProcessIndex].AImage2d;
            }
            InitCWImage();
            cWindows1.ADisplay();
        }

        private void InitCWImage()
        {
            #region 界面显示图像初始化
            string[] imageSelects = regionDiffTool.imageSelect.Split('_');
            if (imageSelects.Length != 2) return;
            if (imageSelects[0] == "相机")
            {
                if (imageSelects[1] == "灰度图")
                    cWindows1.AImage2d = processes[ProcessIndex].ImageGray;
                else
                    cWindows1.AImage2d = processes[ProcessIndex].ImageHeight;
            }
            else
            {
                ProcessTool.Instance.FindImageOpt(processes[ProcessIndex], imageSelects[0], out BaseImageOpt paramByImageOpt1, out int paramByImageOptIndex1);
                if (paramByImageOptIndex1 >= 0)
                {
                    processes[ProcessIndex].BaseImageOutParams[paramByImageOptIndex1].GetHObjectValue(imageSelects[1], out HObject value);
                    cWindows1.AImage2d = value;
                }
            }
            #endregion
        }

        private void Image_OptNameCMBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Image_OptNameCMBox.SelectedIndex > 0)
            {
                Image_ParamNameCMBox.Items.Clear();
                foreach (string value in processes[ProcessIndex].BaseImageOutParams[Image_OptNameCMBox.SelectedIndex - 1].OutputHObjectNames)
                    Image_ParamNameCMBox.Items.Add(value);
            }
            else if (Image_OptNameCMBox.SelectedIndex == 0)
            {
                Image_ParamNameCMBox.Items.Clear();
                Image_ParamNameCMBox.Items.Add("灰度图");
                Image_ParamNameCMBox.Items.Add("深度图");
            }
            else
            {
                Image_ParamNameCMBox.Items.Clear();
            }
        }

        private void Line1_OptNameCMBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Line1_OptNameCMBox.SelectedIndex >= 0)
            {
                Line1_ParamNameCMBox.Items.Clear();
                foreach (string value in processes[ProcessIndex].BaseImageOutParams[Line1_OptNameCMBox.SelectedIndex].OutputHObjectNames)
                    Line1_ParamNameCMBox.Items.Add(value);
            }
        }

        private void Line2_OptNameCMBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Line2_OptNameCMBox.SelectedIndex >= 0)
            {
                Line2_ParamNameCMBox.Items.Clear();
                foreach (string value in processes[ProcessIndex].BaseImageOutParams[Line2_OptNameCMBox.SelectedIndex].OutputHObjectNames)
                    Line2_ParamNameCMBox.Items.Add(value);
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            regionDiffTool.imageSelect = Image_OptNameCMBox.Text + "_" + Image_ParamNameCMBox.Text;
            regionDiffTool.region1Select = Line1_OptNameCMBox.Text + "_" + Line1_ParamNameCMBox.Text;
            regionDiffTool.region2Select = Line2_OptNameCMBox.Text + "_" + Line2_ParamNameCMBox.Text;
            regionDiffTool.isShowResult = ShowResultCenterCBox.Checked;
            regionDiffTool.isShowResultRegion = ShowResultRegionCBox.Checked;
            regionDiffTool.isShowMeasureRegion = ShowROICBox.Checked;
            regionDiffTool.regionDiffParam = propertyGrid1.SelectedObject as RegionDiffParam;
            regionDiffTool.WriteFile(Description);
        }

        private void TestBtn_Click(object sender, EventArgs e)
        {
            InitCWImage();
            regionDiffTool.Image = cWindows1.AImage2d;
            cWindows1.ADispose();
            dataGridView1.Rows.Clear();
            Stopwatch sw = new Stopwatch();
            sw.Start();
            regionDiffTool.Running(out HObject MRegion, out HObject Region, out HTuple a, out HTuple x, out HTuple y);
            sw.Stop();
            label3.Text = "总计运行时间:" + sw.ElapsedMilliseconds;
            if (ShowROICBox.Checked)
            {
                CObject cObject = new CObject();
                cObject._Color = "blue";
                cObject._Draw = false;
                cObject._Object = MRegion;
                cWindows1.AObject.Add(cObject);
            }
            if (a.Length > 0)
            {
                if (ShowResultCenterCBox.Checked)
                {
                    CText cText01 = new CText();
                    cText01._Text = "D:" + a.TupleSum().D;
                    cText01._X = 100;
                    cText01._Y = 100;
                    cText01._Color = "blue";
                    cWindows1.AText.Add(cText01);
                }
                if (ShowResultRegionCBox.Checked)
                {
                    CObject cObject = new CObject();
                    cObject._Color = "green";
                    cObject._Draw = false;
                    cObject._Object = Region;
                    cWindows1.AObject.Add(cObject);
                }
                dataGridView1.Rows.Add(a.Length);
                for (int i = 0; i < a.Length; i++)
                {
                    dataGridView1.Rows[i].Cells[0].Value = (i + 1);
                    dataGridView1.Rows[i].Cells[1].Value = x[i].D.ToString("0.00");
                    dataGridView1.Rows[i].Cells[2].Value = y[i].D.ToString("0.00");
                    dataGridView1.Rows[i].Cells[3].Value = a[i].D.ToString("0.00");
                }
                
            }
            cWindows1.ADisplay();
        }
    }
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值