C# halcon 工业产品尺寸测量

产品检测

这段代码是一个基于HalconDotNet的Windows窗体应用程序,主要用于图像处理和测量。以下是对代码的一些总结:
1. **图像显示与加载:**
   - 使用`HalconDotNet`库进行图像处理。
   - 通过`OpenFileDialog`实现图像文件的选择和加载。
   - 使用`HImage`对象存储和显示图像。
2. **图像处理方法:**
   - 实现了图像的灰度化、二值化、连接区域等处理。
   - 提供了一系列图像处理的参数,如二值化阈值、区域筛选参数等。
3. **测量功能:**
   - 实现了对图像中物体宽度、高度的测量。
   - 计算了内外圈直径,并显示在界面上。
   - 提供了上下高度、左右宽度等测量按钮。
4. **界面交互:**
   - 通过按钮触发不同的图像处理和测量操作。
   - 提供了清除窗口和退出程序的功能。
5. **界面设计:**
   - 使用Windows窗体(`Form`)设计应用程序界面。
   - 包含了图像显示窗口(`HalconWindow`)和多个按钮用于触发不同操作。
6. **代码结构:**
   - 使用了命名空间(`WindowsFormsApp1`)。
   - 包含了一个`Form`类,其中定义了图像处理的方法和界面上的各个控件事件处理方法。
7. **需要改进的地方:**
   - 部分注释是中文,但整体注释较少,可以进一步添加注释以提高代码可读性。
   - 代码中使用了一些硬编码的路径,可以考虑使用相对路径或配置文件来管理。
总体而言,这是一个用于图像处理和测量的简单Windows应用程序,可以根据具体需求进一步扩展和优化。

using HalconDotNet;
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;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static HImage hImage = new HImage();
        public HRegion hRegion = new HRegion();

        public void HImageShow(HImage hImage, string filePath)
        {
            hImage.ReadImage(filePath);
            hImage.GetImageSize(out HTuple width, out HTuple height);
            HOperatorSet.CountChannels(hImage, out HTuple channels);
            HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, height + 1, width + 1);
            //hImage.DispImage(hWindowControl1.HalconWindow);  //灰度图像
            hImage.DispObj(hWindowControl1.HalconWindow);

        }

        //二值化参数
        HTuple MinValue = 4;
        HTuple MaxValue = 88;

        //ROI筛选1
        HTuple feature1 = "area";
        string operation = "and";
        HTuple min1 = 150.0;
        HTuple max1 = 99999.0;

        // 照片位深
        int dpi = 72;
        public HRegion hImage1Process(HImage hImage)
        {
            HImage grayImage = hImage.Rgb1ToGray();
            HRegion Region1 = grayImage.Threshold(MinValue, MaxValue);
            HRegion connectionRegion = Region1.Connection();
            HRegion selectRegion = connectionRegion.SelectShape(feature1, operation, min1, max1);
            HRegion regionUnion = selectRegion.Union1();
            HRegion closingRegion = regionUnion.ClosingCircle(7.5);
            HRegion opeingRegion = closingRegion.OpeningCircle(3.5);
            return opeingRegion;
        }


        //显示图片
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog2 = new OpenFileDialog();
            openFileDialog2.Multiselect = true;  //该值确定是否可以选择多个文件
            //openFileDialog2.Title = "请选择文件";
            openFileDialog2.Filter = "(*.PNG)|*.PNG|(*.jpg)|*.jpg|(*.txt)|*.txt";
            //openFileDialog2.Filter = "文档(*.doc;*.docx)|*.doc;*.docx";
            string filePath = "";
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                if (openFileDialog2.FileName != "")
                {
                    filePath = openFileDialog2.FileName;
                    HImageShow(hImage, filePath);
                }
            }
        }

        //清除窗口
        private void button2_Click(object sender, EventArgs e)
        {
            hWindowControl1.HalconWindow.ClearWindow();
            hImage.Dispose();
        }

        //退出程序
        private void button9_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);
        }

        //宽度计算
        public void objWidth(HImage hImage)
        {
            hRegion = hImage1Process(hImage);
            hRegion.SmallestRectangle2(out HTuple row, out HTuple column, out HTuple Phi, out HTuple Length1, out HTuple Length2);
            hRegion.SmallestCircle(out HTuple Row, out HTuple Column, out HTuple Radius);
            HTuple MaxLength1 = Length1.TupleMax();

            textBox2.Text = (MaxLength1 / dpi * 25.4).ToString() + "mm\n";
            hWindowControl1.HalconWindow.SetDraw("margin");
            hRegion.GenRegionLine(row, column - Length1, row, column + Length1);

            hRegion.DispObj(hWindowControl1.HalconWindow);
            hRegion.Dispose();
        }
        //高度计算
        public void objHeigth(HImage hImage)
        {
            hRegion = hImage1Process(hImage);
            hRegion.SmallestRectangle2(out HTuple row, out HTuple column, out HTuple Phi, out HTuple Length1, out HTuple Length2);
            hRegion.SmallestCircle(out HTuple Row, out HTuple Column, out HTuple Radius);
            double MaxLength2 = 0;
            HTuple MaxLength1 = Length1.TupleMax();

            textBox1.Text = (Length1[0] == MaxLength1[0]).ToString();
            for (int Index = 0; Index < Length1.Length; Index++)
            {
                if (Length1[Index] - MaxLength1[Index] <= 0.000001)
                {
                    MaxLength2 = Length2[Index];
                }
            }

            textBox1.Text = (MaxLength2 / dpi * 25.4).ToString() + "mm\n";
            hWindowControl1.HalconWindow.SetDraw("margin");
            hRegion.GenRectangle2(row, column, Phi, Length1, Length2);
            hRegion.GenRegionLine(row - Length2, column, row + Length2, column);

            hRegion.DispObj(hWindowControl1.HalconWindow);
            hRegion.Dispose();
        }


        //上下高度
        private void button3_Click(object sender, EventArgs e)
        {
            objHeigth(hImage);
        }

        //左右宽度
        private void button4_Click(object sender, EventArgs e)
        {
            objWidth(hImage);
        }

        //内圈直径
        private void button5_Click(object sender, EventArgs e)
        {
            objCicle1(hImage);
        }

        //内圈直径计算
        public void objCicle1(HImage hImage)
        {
            hRegion = hImage1Process(hImage);
            hRegion.SmallestRectangle2(out HTuple row, out HTuple column, out HTuple Phi, out HTuple Length1, out HTuple Length2);
            hRegion.SmallestCircle(out HTuple Row, out HTuple Column, out HTuple Radius);
            hRegion.GenRectangle2(row, column, Phi, Length1, Length2);
            HImage grayImage = hImage.Rgb1ToGray();
            HImage ImageReduced = grayImage.ReduceDomain(hRegion);
            HRegion Regions = ImageReduced.Threshold(94.0, 119.0);
            HRegion Fillup = Regions.OpeningCircle(5.5).Connection().SelectShape("area", "and", 30000.0, 999999).FillUp();
            hWindowControl1.HalconWindow.SetDraw("margin");

            Fillup.SmallestCircle(out double row2, out double column2, out double radius2);
            hRegion.GenCircle(row2, column2, radius2);
            hRegion.DispObj(hWindowControl1.HalconWindow);
            textBox3.Text = (radius2 / dpi * 25.4).ToString() + "mm\n";

        }


        //外圈直径计算
        public void objCicle2(HImage hImage)
        {
            hRegion = hImage1Process(hImage);
            hRegion.SmallestRectangle2(out HTuple row, out HTuple column, out HTuple Phi, out HTuple Length1, out HTuple Length2);
            hRegion.SmallestCircle(out HTuple Row, out HTuple Column, out HTuple Radius);
            hRegion.GenRectangle2(row, column, Phi, Length1, Length2);
            HImage grayImage = hImage.Rgb1ToGray();
            HImage ImageReduced = grayImage.ReduceDomain(hRegion);
            HRegion Regions = ImageReduced.Threshold(94.0, 119.0);
            HRegion Fillup = Regions.OpeningCircle(5.5).Connection().SelectShape("area", "and", 30000.0, 999999).FillUp();
            hWindowControl1.HalconWindow.SetDraw("margin");

            Fillup.SmallestCircle(out double row2, out double column2, out double radius2);

            double radius3 = Length2;
            hRegion.GenCircle(row2, column2, radius3);
            hRegion.DispObj(hWindowControl1.HalconWindow);

            textBox4.Text = (radius3 / dpi * 25.4).ToString() + "mm\n";

        }

        private void button6_Click(object sender, EventArgs e)
        {
            objCicle2(hImage);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            hRegion = hImage1Process(hImage);
            hRegion.SmallestRectangle2(out HTuple row, out HTuple column, out HTuple Phi, out HTuple Length1, out HTuple Length2);
            hRegion.SmallestCircle(out HTuple Row, out HTuple Column, out HTuple Radius);
            double MaxLength2 = 0;
            double MaxRow = 0;
            double MaxColumn = 0;
            double MaxPhi = 0;
            HTuple MaxLength1 = Length1.TupleMax();

            textBox1.Text = (Length1[0] == MaxLength1[0]).ToString();
            for (int Index = 0; Index < Length1.Length; Index++)
            {
                if (Length1[Index] - MaxLength1[Index] <= 0.000001)
                {

                    MaxLength2 = Length2[Index];
                    MaxRow = Row[Index];
                    MaxColumn = Column[Index];
                    MaxPhi = Phi[Index];
                }
            }

            textBox5.Text = (MaxLength2 / dpi * 25.4).ToString() + "mm\n";
            hWindowControl1.HalconWindow.SetDraw("margin");
            hRegion.GenRectangle2(new HTuple(MaxRow), new HTuple(MaxColumn), new HTuple(MaxPhi), new HTuple(MaxLength1), new HTuple(MaxLength2));

            hRegion.DispObj(hWindowControl1.HalconWindow);
            hRegion.Dispose();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            hImage.ReadImage("C:\\Users\\1\\Desktop\\表盘imgaes\\1-1.png");
            hImage.GetImageSize(out HTuple width, out HTuple height);
            HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, height + 1, width + 1);
            //hImage.DispImage(hWindowControl1.HalconWindow);  //灰度图像
            hImage.DispObj(hWindowControl1.HalconWindow);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            hImage.ReadImage("C:\\Users\\1\\Desktop\\表盘imgaes\\1-2.png");
            hImage.GetImageSize(out HTuple width, out HTuple height);
            HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, height + 1, width + 1);
            //hImage.DispImage(hWindowControl1.HalconWindow);  //灰度图像
            hImage.DispObj(hWindowControl1.HalconWindow);
        }
    }
}

这段代码有一些优点和一些需要改进的地方。
**优点:**
1. **功能丰富:** 代码实现了图像处理和测量的多个功能,包括灰度化、二值化、区域连接、测量物体的宽度、高度等。
2. **界面交互:** 通过Windows窗体设计,提供了用户友好的界面,通过按钮触发不同的操作,易于使用。
3. **代码结构清晰:** 代码使用了命名空间和类的结构,提高了代码的组织性和可维护性。
4. **使用HalconDotNet库:** 利用HalconDotNet库进行图像处理,该库功能强大,适用于工业视觉和图像处理领域。
**需要改进的地方:**
1. **注释不足:** 部分注释是中文,但整体注释较少。可以添加更多注释,特别是对于复杂逻辑和算法的部分,以提高代码的可读性。
2. **硬编码路径:** 代码中使用了一些硬编码的路径,建议使用相对路径或配置文件来管理路径,提高代码的灵活性。
3. **异常处理:** 缺少对异常的处理,例如文件加载失败、图像处理失败等情况应该进行适当的异常处理。
4. **代码复用性:** 一些功能可能可以抽象成可复用的方法,提高代码的重用性。
5. **界面布局:** 界面布局方面可以进一步优化,使界面更加美观和易于使用。
总体而言,这段代码是一个功能完善的图像处理应用程序,通过一些改进可以进一步提高代码的质量和可维护性。

  • 11
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

挽风起苍岚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值