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

namespace CvImageTool
{
    public partial class FindCircleForm : Form
    {
        private int ProcessIndex { set; get; }

        private int ImageOptIndex { set; get; }

        private string Description { set; get; }

        private List<BaseProcess> processes;
        private CWindows[] cCWindows;

        private FindCircleTool findCircleTool;
        public FindCircleForm()
        {
            this.StartPosition = FormStartPosition.CenterParent;
            InitializeComponent();
        }

        public FindCircleForm(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;
            findCircleTool = new FindCircleTool(processIndex, imageOptIndex, cProcessSettingFilePath, cImageOptSettingFilePath, processes, cCWindows);
           
        }

        private void FindCircleForm_Load(object sender, EventArgs e)
        {
            this.Text = processes[ProcessIndex].Name + "#" + Description;
            Image_OptNameCMBox.Items.Clear();
            Image_OptNameCMBox.Items.Add("相机");
            Matrix_OptNameCMBox.Items.Clear();
            for (int i = 0; i < ImageOptIndex; i++)
            {
                string cDes = processes[ProcessIndex].BaseImageOpts[i].Description;
                Image_OptNameCMBox.Items.Add(cDes);
                Matrix_OptNameCMBox.Items.Add(cDes);
            }

            if (findCircleTool.ReadFile(Description))
            {
                string[] values = findCircleTool.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];
                }
                
                values = findCircleTool.affinMatrixSelect.Split('_');
                if (values.Length == 2)
                {
                    Matrix_OptNameCMBox.Text = values[0];
                    Matrix_ParamNameCMBox.Text = values[1];
                }
                else if (values.Length == 1)
                {
                    Matrix_OptNameCMBox.Text = values[0];
                }
            }
            propertyGrid1.SelectedObject = findCircleTool.findCircleParam;
            ShowCenterCBox.Checked = findCircleTool.isShowCircleCenter;
            ShowMeasuresCBox.Checked = findCircleTool.isShowCircleMeasuresCountour;
            ShowMeasuresResultCBox.Checked = findCircleTool.isShowCircleMeasuresResultCountour;
            ShowResultCBox.Checked = findCircleTool.isShowCircleResultCountour;
            if (findCircleTool.findCircleParam.radius != 0)
            {
                cWindows1.ADrawROI2d.VR._Radius = findCircleTool.findCircleParam.radius;
                cWindows1.ADrawROI2d.VR._X = findCircleTool.findCircleParam.centerColumn;
                cWindows1.ADrawROI2d.VR._Y = findCircleTool.findCircleParam.centerRow;
            }
            if (cCWindows[ProcessIndex].AImage2d != null)
            {
                //HOperatorSet.CopyObj(cCWindows[ProcessIndex].AImage2d, out HObject objectsSelected, 1, 1);
                //cWindows1.AImage2d = objectsSelected;
                cWindows1.AImage2d = cCWindows[ProcessIndex].AImage2d;
            }
            InitCWImage();
            cWindows1.ADisplay();
        }
        private void InitCWImage()
        {
            #region 界面图像初始化
            string[] imageSelects = findCircleTool.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 Matrix_OptNameCMBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Matrix_OptNameCMBox.SelectedIndex >= 0)
            {
                Matrix_ParamNameCMBox.Items.Clear();
                foreach (string value in processes[ProcessIndex].BaseImageOutParams[Matrix_OptNameCMBox.SelectedIndex].OutputHTupleNames)
                    Matrix_ParamNameCMBox.Items.Add(value);
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            cWindows1.ADrawROI2d.ROIValue();
            findCircleTool.findCircleParam.centerColumn = cWindows1.ADrawROI2d.VR._X;
            findCircleTool.findCircleParam.centerRow = cWindows1.ADrawROI2d.VR._Y;
            findCircleTool.findCircleParam.radius = cWindows1.ADrawROI2d.VR._Radius;
            findCircleTool.imageSelect = Image_OptNameCMBox.Text + "_" + Image_ParamNameCMBox.Text;
            findCircleTool.affinMatrixSelect = Matrix_OptNameCMBox.Text + "_" + Matrix_ParamNameCMBox.Text;
            findCircleTool.isShowCircleResultCountour = ShowResultCBox.Checked;
            findCircleTool.isShowCircleCenter = ShowCenterCBox.Checked;
            findCircleTool.isShowCircleMeasuresCountour = ShowMeasuresCBox.Checked;
            findCircleTool.isShowCircleMeasuresResultCountour = ShowMeasuresResultCBox.Checked;
            findCircleTool.findCircleParam = propertyGrid1.SelectedObject as FindCircleParam;
            findCircleTool.WriteFile(Description);
        }

        private void TestBtn_Click(object sender, EventArgs e)
        {
            InitCWImage();
            findCircleTool.Image = cWindows1.AImage2d;
            cWindows1.ADispose();
            dataGridView1.Rows.Clear();
            dataGridView1.Rows.Add(1);
            Stopwatch sw = new Stopwatch();
            sw.Start();
            findCircleTool.Running(out HObject measuresContour, out HObject resultContour, out HTuple measuresRow, out HTuple measuresCol, out HTuple circle);
            sw.Stop();
            label3.Text = "总计运行时间:" + sw.ElapsedMilliseconds;
            if (ShowMeasuresCBox.Checked)
            {
                CObject cObject = new CObject();
                cObject._Color = "blue";
                cObject._Draw = false;
                cObject._Object = measuresContour;
                cWindows1.AObject.Add(cObject);
            }
            if (circle.Length == 3) 
            {
                if (ShowCenterCBox.Checked)
                {
                    CText cText01 = new CText();
                    cText01._Text = "X:" + circle[1].D.ToString() + "; Y:" + circle[0].D.ToString() + "; R:" + circle[2].D.ToString();
                    cText01._X = 100;
                    cText01._Y = 100;
                    cText01._Color = "blue";
                    cWindows1.AText.Add(cText01);
                }
                if (ShowMeasuresResultCBox.Checked)
                {
                    CObject cObject = new CObject();
                    cObject._Color = "green";
                    cObject._Draw = false;
                    HOperatorSet.GenCrossContourXld(out HObject cross, measuresRow, measuresCol, 30, 1);
                    cObject._Object = cross;
                    cWindows1.AObject.Add(cObject);
                }
                if (ShowResultCBox.Checked)
                {
                    CObject cObject = new CObject();
                    cObject._Color = "green";
                    cObject._Draw = false;
                    cObject._Object = resultContour;
                    cWindows1.AObject.Add(cObject);
                }
                dataGridView1.Rows[0].Cells[0].Value = circle[1].D;
                dataGridView1.Rows[0].Cells[1].Value = circle[0].D;
                dataGridView1.Rows[0].Cells[2].Value = circle[2].D;
            }
            
            cWindows1.ADisplay();
            
          
        }

        private void FindCircleForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //if (cWindows1.AImage2d != null) cWindows1.AImage2d.Dispose();
        }
    }
}
 

Logo

聚焦RDK与具身智能,汇集机器人开发教程、开源案例和技术交流