纯C#使用Visionpro工具2 操作斑点工具

结果图

通过斑点工具中非圆性找取圆特征

纯C#使用Visionpro工具2 操作斑点工具_第1张图片

代码 

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 Cognex.VisionPro.ImageFile;
using Cognex.VisionPro;
using Cognex.VisionPro.Blob;
using Cognex.VisionPro.Exceptions;

namespace myapp4
{
    public partial class Form1 : Form
    {
        //成员声明
        CogBlobTool mTool;
        CogImageFileTool mImageFileTool;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            mTool = new CogBlobTool();
            mImageFileTool = new CogImageFileTool();

            mImageFileTool.Operator.Open("bracket_std.idb", CogImageFileModeConstants.Read);
            mImageFileTool.Run();

            CogDisplay1.Image = mImageFileTool.OutputImage;//设置输出图像
            CogDisplay1.Fit();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            CogDisplay1.StaticGraphics.Clear();//清空静态图形
            //运行blob工具
            mTool.InputImage = mImageFileTool.OutputImage;
            mTool.Run();
            //获取斑点的轮廓 并显示
            CogDisplay1.Image = mImageFileTool.OutputImage;


            int r;
            for (r = 0; (r<= (mTool.Results.GetBlobs().Count - 1)); r++)
            {
                if (optCircular.Checked)//如果选取圆性
                {
                    //  display circular objects here
                    if (((mTool.Results.GetBlobs()[r].Acircularity > 0.9)
                                && (mTool.Results.GetBlobs()[r].Acircularity < 1.1)))
                    {
                        CogDisplay1.StaticGraphics.Add(mTool.Results.GetBlobs()[r].CreateResultGraphics(CogBlobResultGraphicConstants.Boundary), "test");
                    }

                }
                else//非圆性
                {
                    //  display non-circular objects here
                    if (((mTool.Results.GetBlobs()[r].Acircularity <= 0.9)
                                || (mTool.Results.GetBlobs()[r].Acircularity >= 1.1)))
                    {
                        CogDisplay1.StaticGraphics.Add(mTool.Results.GetBlobs()[r].CreateResultGraphics(CogBlobResultGraphicConstants.Boundary), "test");
                    }

                }

            }

        }

    }
}

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