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

C#Halcon跨窗口颜色识别

       机器视觉是一门让计算机模拟人类视觉功能的学科。颜色识别在其中扮演着重要的角色,它旨在通过对图像中的颜色信息进行分析,从而识别出图像中的目标物体或者区域。例如,在水果分拣系统中,可以根据水果的颜色(如苹果的红色、香蕉的黄色等)将不同种类的水果区分开来;在工业生产线上,可以检测产品表面颜色是否符合标准,从而实现质量控制。

demo运行效果

修改参数

继续缩小系数

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;
using System.Drawing.Text;
namespace DeepLearningTest1
{
    public partial class FormMain : Form
    {
        HalconView HW;
        HObject HIMage = new HObject();
        public FormMain()
        {
            InitializeComponent();
            HW = new HalconView();
            HW.HWindowControl.BackColor = Color.White;
            splitContainer1.Panel1.Controls.Add(HW);
            HW.Dock = DockStyle.Fill;
        }       
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "图片文件(*.bmp;*.jpg;*.gif;*.png;*.tiff;*.tif;*.ply)|*.bmp;*.jpg;*.gif;*.png;*.tiff;*.tif;*.ply";
                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());
            }
        }
        private void btn_DrawROI_Click(object sender, EventArgs e)
        {
            Form2 F2 = new Form2( HIMage);
            F2.Show();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            HOperatorSet.ReadClassGmm("./12.ggc", out var hv_GMMHandle1);
            HOperatorSet.CreateClassLutGmm(hv_GMMHandle1, new HTuple(), new HTuple(), out  var hv_ClassLUTHandle1);
            HOperatorSet.ClassifyImageClassLut(HIMage, out var ho_ClassRegions, hv_ClassLUTHandle1);
            HW.HalconWindow.ClearWindow();
            HW.HalconWindow.SetDraw("fill");
            HW.HalconWindow.SetColor("white");
            HW.DispObject(HIMage);
            HW.DispObject(ho_ClassRegions);
        }
    }
}
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 static System.Net.Mime.MediaTypeNames;
namespace DeepLearningTest1
{
    public partial class Form2 : Form
    {
        HalconView HW2;
        HObject HIMage2 = new HObject(), Circle = new HObject ();
        HTuple  hv_GMMHandle = new HTuple(), hv_ClassLUTHandle=new HTuple();
        public Form2(HObject HIMage)
        {
            InitializeComponent();
            HIMage2 = HIMage;
            HW2 = new HalconView();
            HW2.HWindowControl.BackColor = Color.White;
            splitContainer1.Panel2.Controls.Add(HW2);
            HW2.Dock = DockStyle.Fill;
        }
        private void btn_loadImage_Click(object sender, EventArgs e)
        {
            if (!HIMage2.IsInitialized()) { MessageBox.Show("图片为空"); return; }
            HW2.DispImage(HIMage2, true);
        }
        private void btn_save_Click(object sender, EventArgs e)
        {
            HOperatorSet.WriteClassGmm(hv_GMMHandle, "./12.ggc");
            this.Close();
        }
        private void btn_DrawROI_Click(object sender, EventArgs e)
        {
            try
            {
                if (!HIMage2.IsInitialized()) { MessageBox.Show("图片为空"); return; }
                HSystem.SetSystem("clip_region", "false");
                HW2.Focus();
                HW2.DrawCircleMod(out Circle, 578, 627, 20, out HTuple r1, out HTuple c1, out HTuple radio);
                HW2.HalconWindow.SetColor("blue");
                HW2.DispObject(Circle);
            }
            catch (Exception ex)
            {
                MessageBox.Show("设置区域失败  " + ex.ToString());
            }
        }
        private void btn_Reduce_Click(object sender, EventArgs e)
        {
            HOperatorSet.CreateClassGmm(Convert.ToInt32(tBx_NunDim.Text), 1, 1, "full", "none", 3, 42, out hv_GMMHandle);
            HOperatorSet.AddSamplesImageClassGmm(HIMage2, Circle, hv_GMMHandle,
                 Convert.ToInt32(tBx_Randomize.Text));
            HOperatorSet.TrainClassGmm(hv_GMMHandle, 100, 0.1, "training", 1, out 
var hv_Centers,out var hv_Iter);
            HOperatorSet.CreateClassLutGmm(hv_GMMHandle, new HTuple(), new HTuple(), out hv_ClassLUTHandle);
        }
        private void btn_DisResult_Click(object sender, EventArgs e)
        {
            HOperatorSet.ClassifyImageClassLut(HIMage2, out var ho_ClassRegions, hv_ClassLUTHandle);
            HW2.HalconWindow.SetDraw("fill");
            HW2.DispObject(ho_ClassRegions);
        }       
    }
}

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

相关文章:

  • (leetcode算法题)面试题 17.19. 消失的两个数字
  • Python自学 - 函数初步(内置函数、模块函数、自定义函数)
  • .NET 9.0 WebApi 发布到 IIS 详细步骤
  • 1-markdown转网页样式页面 --[制作网页模板] 【测试代码下载】
  • 百度贴吧的ip属地什么意思?怎么看ip属地
  • 通过爬虫方式实现视频号助手发布视频
  • 使用 Spring Boot 进行数据校验
  • 如何构建云原生时空大数据平台?
  • EasyExcel监听器详解
  • 【QED】暴食海獭
  • DDcGAN_多分辨率图像融合的双鉴别条件生成对抗网络_y译文马佳义
  • 若依中Feign调用的具体使用(若依微服务版自身已集成openfeign依赖,并在此基础上定义了自己的注解)
  • 4_TypeScript 条件语句 --[深入浅出 TypeScript 测试]
  • 强化学习常用库的版本对应关系
  • 【C语言】可移植性陷阱与缺陷(六): 内存位置0的访问
  • C++并发:在线程间共享数据
  • 鸿蒙day1-ArkTS-认识和存储数据
  • C语言:调试的概念和调试器的选择
  • Laravel操作ElasticSearch
  • FFMPEG 保存实时流到本地文件
  • 【JVM】总结篇-运行时内存篇
  • 我用AI学Android Jetpack Compose之开篇
  • opengauss安装指南
  • IDEA配置maven和git并如何使用maven打包和git推送到gitlab
  • 如何让大模型不再“已读乱回”——RAG技术助力生成更精确的答案
  • NLP期末复习