当前位置: 首页 > article >正文

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)
            {
            }
        }
    }
}


http://www.kler.cn/a/465475.html

相关文章:

  • Scala 访问修饰符
  • CSS——5. 外部样式
  • JavaVue-Get请求 数组参数(qs格式化前端数据)
  • ‘元素.style.样式名‘获取不到样式,应该使用Window.getComputedStyle()获取正真的样式
  • Mac 安装 Flutter 提示 A network error occurred while checking
  • 《计算机网络A》单选题-复习题库
  • 啥是大模型
  • 【Golang 面试题】每日 3 题(十七)
  • Objective-C语言的软件开发工具
  • 热备份路由HSRP及配置案例
  • 大数据学习(33)-续集
  • 如何配置【Docker镜像】加速器+【Docker镜像】的使用
  • 【踩坑记录】uni-app 微信小程序调试不更新问题解决指南
  • GitHub的简单操作
  • 《 C++ 点滴漫谈: 十七 》编译器优化与 C++ volatile:看似简单却不容小觑
  • idea 的 springboot项目spring-boot-devtools 自动编译 配置热部署
  • 考试座位号(PTA)C语言
  • 【Rust 学习笔记】Rust 基础数据类型介绍——指针、元组和布尔类型
  • 3d扫描仪三维扫描构建牙齿模型
  • certificate verify failed: unable to get local issuer certificate (_ssl.c:10
  • python生成、操作svg图片
  • Couchbase 的索引
  • HTTP Scheme 通常指的是在 URL 中用于指定使用 HTTP 协议的方案(scheme)
  • 【C】​动态内存管理
  • ==和===的区别,被坑的一天
  • Unity学习笔记(六)使用状态机重构角色移动、跳跃、冲刺