【机械视觉】C#+VisionPro联合编程———【五、硬币检测小项目实现(C#+VisionPro联合编程和csv文件格式操作)】

【机械视觉】C#+VisionPro联合编程———【五、硬币检测小项目实现(C#+VisionPro联合编程和csv文件格式操作)】

项目介绍

总共有十二张检测的图片,当点击检测按钮时检测当前展示的图片并且将检测效果展示在表格中,当点击上一页或下一页时换检测图片,点击保存本地时通过csv文件格式将表格数据保存,当下一次运行时将数据读取出来并且展示在表格中。

此项目通过异步进行优化,在加载时改变了以往卡顿的情况,加载变得流畅。

此项目检测时的逻辑代码是在visionPro中完成的。

所用的控件:

  • cogRecordDisplay
  • dataGridView
  • button
  • label

【机械视觉】C#+VisionPro联合编程———【五、硬币检测小项目实现(C#+VisionPro联合编程和csv文件格式操作)】_第1张图片

实现项目

第一步、创建toolblock工具,并且在其中训练检测工具。(并且保存)

三个匹配工具分别匹配一元一角五角,然后将匹配数量赋值给结果分析工具,通过结果分析工具计算出总和。

【机械视觉】C#+VisionPro联合编程———【五、硬币检测小项目实现(C#+VisionPro联合编程和csv文件格式操作)】_第2张图片

其中用到了结果分析工具(CogResultsAnalysisTool1),一下是构造:

【机械视觉】C#+VisionPro联合编程———【五、硬币检测小项目实现(C#+VisionPro联合编程和csv文件格式操作)】_第3张图片

第二步、创建winform项目,并且搭建界面:

【机械视觉】C#+VisionPro联合编程———【五、硬币检测小项目实现(C#+VisionPro联合编程和csv文件格式操作)】_第4张图片

第三步、编写代码:(以下是源码)

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    CogToolBlock tb = new CogToolBlock(); 
    int numPage = 1; // 图片页数
    string fileName = "./硬币/"; // 路径

    /// 
    /// 加载
    /// 
    /// 
    /// 
    private void Form1_Load(object sender, EventArgs e)
    {
        Task.Run(() =>
        {
            tb = (CogToolBlock)CogSerializer.LoadObjectFromFile("./硬币检测.vpp");
        });
        this.Invoke(new Action(() => {
            ConvertImage();
            this.labelPageMaxNum.Text = "最大页数为: 12";
            string[][] strings = csvSave("./data.csv");
            if (strings!=null)
            {
                for (int i = 1; i < strings.Length; i++)
                {
                    this.dataGridView1.Rows.Add(strings[i]);
                }
            }
        }));
    }
    /// 
    /// 检测
    /// 
    /// 
    /// 
    private void buttonDetection_Click(object sender, EventArgs e)
    {
        tb.Inputs["OutputImage"].Value = this.cogRecordDisplay1.Image;
        tb.Run();
        this.cogRecordDisplay1.Record = tb.CreateLastRunRecord().SubRecords[0];
         CogPMAlignTool pma1 = tb.Tools[1] as CogPMAlignTool;
         CogPMAlignTool pma2 = tb.Tools[2] as CogPMAlignTool;
         CogPMAlignTool pma3 = tb.Tools[3] as CogPMAlignTool;
        this.dataGridView1.Rows.Add(new string[] { DateTime.Now.ToString(), "凹凸曼", pma1.Results.Count.ToString(),pma2.Results.Count.ToString(),pma3.Results.Count.ToString(), tb.Outputs["YuanCount"].Value.ToString() });
    }

    /// 
    /// 上一页
    /// 
    /// 
    /// 
    private void buttonTopPage_Click(object sender, EventArgs e)
    {
        if (numPage>1)
        {
            numPage -= 1;
            ConvertImage();
        }
        else
        {
            System.Windows.Forms.MessageBox.Show("当前为第一页");
        }
    }

    /// 
    /// 下一页
    /// 
    /// 
    /// 
    private void buttonBottomPage_Click(object sender, EventArgs e)
    {
        if (numPage < 12)
        {
            numPage += 1;
            ConvertImage();
        }
        else
        {
            System.Windows.Forms.MessageBox.Show("当前为最后一页");
        }
    }

    /// 
    /// 换图片
    /// 
    private void ConvertImage()
    {
        Bitmap bitmap = new Bitmap(fileName + numPage + ".bmp");
        this.cogRecordDisplay1.Image = new CogImage24PlanarColor(bitmap);
        this.cogRecordDisplay1.Fit();
        this.labelPageShow.Text = $"第{numPage}页";
    }

    /// 
    /// 保存本地
    /// 
    /// 
    /// 
    private void buttonSave_Click(object sender, EventArgs e)
    {
        if(this.dataGridView1.Rows.Count < 1){
            System.Windows.Forms.MessageBox.Show("没有数据");
            return;
        }
        using (FileStream fs = new FileStream("./data.csv", FileMode.Create))
        using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
        {
            sw.WriteLine("测量时间,管理员,一元个数,一角个数,五角个数,总和");
            for (int i = 0;i

你可能感兴趣的:(c#,开发语言)