C#Halcon交互绘制ROI

画圆

确定后效果

矩形2

矩形1

画线

用简单的方式初步实现ROI交互绘制,方便初学者熟悉原理

UI界面

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 ViewControl;
using HalconDotNet;
using System.Reflection.Emit;
using static System.Net.Mime.MediaTypeNames;
namespace DeepLearningTest1
{
    public partial class Form1 : Form
    {
        HalconView HW;
        HObject HIMage = new HObject();
        public Form1()
        {
            InitializeComponent();
            HW = new HalconView();
            HW.HWindowControl.BackColor = Color.White;
            splitContainer1.Panel1.Controls.Add(HW);
            HW.Dock = DockStyle.Fill;
        }       
        /// <summary>
        /// 加载图像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "图片文件(*.bmp;*.jpg;*.gif;*.png;*.tiff;*.tif)|*.bmp;*.jpg;*.gif;*.png;*.tiff;*.tif";
                openFileDialog.RestoreDirectory = true;
                openFileDialog.FilterIndex = 1;
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    label3.Text = openFileDialog.FileName;
                    HOperatorSet.ReadImage(out HIMage, label3.Text);
                    
                    HW.DispImage(HIMage, true);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载图片失败  " + ex.ToString());
            }
        }
        /// <summary>
        /// 画圆
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DrawCircle(object sender, EventArgs e)
        {
            try
            {
                if (!HIMage.IsInitialized()) { MessageBox.Show("图片为空"); return; }
                HSystem.SetSystem("clip_region", "false");
                HW.Focus();
                HW.DrawCircleMod(out HObject Circle11,  2000,  2000,  100, out HTuple r1, out HTuple c1, out HTuple ra);
                HW.HalconWindow.SetColor("yellow");
                HW.DispObject(Circle11);
            }
            catch (Exception ex)
            {
                MessageBox.Show("设置区域失败  " + ex.ToString());
            }
        }
        /// <summary>
        /// 画线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DrawLine(object sender, EventArgs e)
        {
            try
            {
                if (!HIMage.IsInitialized()) { MessageBox.Show("图片为空"); return; }
                HSystem.SetSystem("clip_region", "false");
                HW.Focus();
                HW.DrawLineMod(out HObject Line, 2000, 20, 2000,2000, out HTuple r1, out HTuple c1, out HTuple r2, out HTuple c2);
                HW.HalconWindow.SetColor("green");
                HW.DispObject(Line);
            }
            catch (Exception ex)
            {
                MessageBox.Show("设置区域失败  " + ex.ToString());
            }
        }
        /// <summary>
        /// 画矩形1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DrawRectangle(object sender, EventArgs e)
        {
            try
            {
                if (!HIMage.IsInitialized()) { MessageBox.Show("图片为空"); return; }
                HSystem.SetSystem("clip_region", "false");
                HW.Focus();
                HW.DrawRectangle1Mod(out HObject Rectangle, 200, 200, 2000, 2000, out HTuple r1, out HTuple c1, out HTuple r2, out HTuple c2);
                HW.HalconWindow.SetColor("spring green");
                HW.DispObject(Rectangle);
            }
            catch (Exception ex)
            {
                MessageBox.Show("设置区域失败  " + ex.ToString());
            }
        }
        /// <summary>
        /// 画矩形2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DrawRectangle2(object sender, EventArgs e)
        {
            try
            {
                if (!HIMage.IsInitialized()) { MessageBox.Show("图片为空"); return; }
                HSystem.SetSystem("clip_region", "false");
                HW.Focus();
                HW.DrawRectangle2Mod(out HObject Rectangle2,2000,2000,0,200,100, out HTuple r1, out HTuple c1, out HTuple phi, out HTuple l1, out HTuple l2);
                HW.HalconWindow.SetColor("blue");
                HW.DispObject(Rectangle2);
            }
            catch (Exception ex)
            {
                MessageBox.Show("设置区域失败  " + ex.ToString());
            }
        }
        /// <summary>
        /// 绘制ROI
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_DrawROI_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "画圆")
            {
                DrawCircle(null, null);
            }
            if (comboBox1.Text == "画线")
            {
                DrawLine(null, null);
            }
            if (comboBox1.Text == "矩形1")
            {
                DrawRectangle(null, null);
            }
            if (comboBox1.Text == "矩形2")
            {
                DrawRectangle2(null, null);
            }
        }
        /// <summary>
        /// 清空界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, EventArgs e)
        {
            try
            {
                if (!HIMage.IsInitialized()) { MessageBox.Show("图片为空"); return; }
                HW.HalconWindow.ClearWindow();
                HW.DispObject(HIMage);
            }
            catch (Exception ex)
            {
            }
        }
    }
}
相关推荐
Blossom.11810 分钟前
低代码开发:开启软件开发的新篇章
人工智能·深度学习·安全·低代码·机器学习·计算机视觉·数据挖掘
IM1GENIUS21 分钟前
.NET高频技术点(持续更新中)
c#·.net
serve the people25 分钟前
解决osx-arm64平台上conda默认源没有提供 python=3.7 的官方编译版本的问题
开发语言·python·conda
red-fly1 小时前
c#修改ComboBox当前选中项的文本
c#·combobox
柒七爱吃麻辣烫1 小时前
在Linux中安装JDK并且搭建Java环境
java·linux·开发语言
极小狐1 小时前
如何构建容器镜像并将其推送到极狐GitLab容器镜像库?
开发语言·数据库·机器学习·gitlab·ruby
RK_Dangerous1 小时前
【深度学习】计算机视觉(18)——从应用到设计
人工智能·深度学习·计算机视觉
小虎卫远程打卡app1 小时前
视频编解码学习10之成像技术原理
学习·计算机视觉·视频编解码
多多*2 小时前
Java反射 八股版
java·开发语言·hive·python·sql·log4j·mybatis
正在走向自律2 小时前
从0到1:Python机器学习实战全攻略(8/10)
开发语言·python·机器学习