传奇服务器网站如何建设,劳动仲裁院内部网站建设,株洲做网站哪家好,东莞有哪家比较好的网站建设公司该示例演示了如何使用卡尺工具和夹具工具来固定 Blob 工具。示例代码将检测图像上部区域中小方块的存在。当点击“运行”按钮时#xff0c;将读取一张新图像。卡尺工具将被运行#xff0c;卡尺工具的输出 Y 信息将传递给夹具工具。夹具工具使用来自卡尺工具的 Y 信息和新图像…该示例演示了如何使用卡尺工具和夹具工具来固定 Blob 工具。示例代码将检测图像上部区域中小方块的存在。当点击“运行”按钮时将读取一张新图像。卡尺工具将被运行卡尺工具的输出 Y 信息将传递给夹具工具。夹具工具使用来自卡尺工具的 Y 信息和新图像并创建一个新的输出图像。夹具工具的输出图像随后将传递给 Blob 工具. 警告 该应用程序仅在图像仅在 Y 方向上变化时才能正常工作。这是因为卡尺工具仅测量图像的上边缘因此只能准确检测 Y 方向的变化。
这三个工具是在此应用程序之外创建的并在初始化Form_Load期间加载。可以使用 QuickStart 检查各个工具的设计它们位于上一级目录中。
1. 创建vpp文件
打开VisionPro QuickBuild,新建CogJob1,打开ImageSouce,选择文件C:\Program Files\Cognex\VisionPro\Images\square_images.idb作为输入图像源 依次添加CogCaliperTool,CogFixtureTool,CogBlobTool工具,并连线 卡尺工具的搜索区域设置为包含小白方块的矩形的上边缘。该工具设置为一个边缘并从浅到深进行查找,该应用程序仅在图像仅在 Y 方向上变化时才能正常工作。这是因为卡尺工具仅测量图像的上边缘因此只能准确检测 Y 方向的变化需要设置旋转90度 夹具工具从 Caliper 获取位置信息并从图像文件工具获取图像创建一个新图像然后传递给 Blob 工具。夹具工具的新图像已针对新位置进行了校正 Blob 工具设置为最小 10 像素。阈值设置为硬动态适用于浅色背景上的深色 Blob 运行工具后,小方块的存在被检测 确认无误后,分别保存CogCaliper,CogFixture,CogBlob工具的vpp到本地供后续程序开发使用 2. 添加引用
using Cognex.VisionPro.Blob;
using Cognex.VisionPro.CalibFix;
using Cognex.VisionPro.Caliper;
using Cognex.VisionPro.ImageFile;3. 界面设计
添加CogDisplay控件和Button按钮. 4. 声明变量 private CogImageFileTool fileTool;private CogCaliperTool caliperTool;private CogFixtureTool fixtureTool;private CogBlobTool blobTool;5. 加载工具 private void InitializeCogTool(){string ImageFileName Images\square_images.idb;string strBaseDir Environment.GetEnvironmentVariable(VPRO_ROOT);if (string.IsNullOrEmpty(strBaseDir)){throw new Exception(环境变量VPRO_ROOT未设置.);}fileTool new CogImageFileTool();fileTool.Operator.Open(Path.Combine(strBaseDir, ImageFileName), CogImageFileModeConstants.Read);string VPPFiles G:/VisonProStudy/UsingQB/vpp2/;caliperTool (CogCaliperTool)CogSerializer.LoadObjectFromFile(VPPFiles caliper_tool.vpp);fixtureTool (CogFixtureTool)CogSerializer.LoadObjectFromFile(VPPFiles fixture_tool.vpp);blobTool (CogBlobTool)CogSerializer.LoadObjectFromFile(VPPFiles blob_tool.vpp);}private void DisplayErrorAndExit(string ErrorMsg){MessageBox.Show(ErrorMsg \nPress OK to exit.);Application.Exit();}private void Form12_Load(object sender, EventArgs e){try{InitializeCogTool();}catch (CogException ex){DisplayErrorAndExit(Tool Load Error: ex.Message);}catch (Exception ex){DisplayErrorAndExit(Tool Load Error: ex.Message);}}6. 处理按钮点击事件
private void button1_Click(object sender, EventArgs e){CogTransform2DLinear linXform;CogImage8Grey tempImage;try{// 第一步清除静态图形cogDisplay1.StaticGraphics.Clear();// 第二步从图像数据库文件获取一张图像imageFileTool.Run();tempImage (CogImage8Grey)imageFileTool.OutputImage;cogDisplay1.Image tempImage;// 第三步运行卡尺工具并检查其结果绘制结果图形caliperTool.InputImage tempImage;caliperTool.Run();if (caliperTool.RunStatus.Result ! CogToolResultConstants.Accept){throw caliperTool.RunStatus.Exception;}if (caliperTool.Results.Count 0){throw new Exception(未找到边缘.);}cogDisplay1.StaticGraphics.Add(caliperTool.Results[0].CreateResultGraphics(CogCaliperResultGraphicConstants.All), );// 第四步运行夹具工具并检查其结果fixtureTool.InputImage tempImage;fixtureTool.Run();if (fixtureTool.RunStatus.Result ! CogToolResultConstants.Accept){throw fixtureTool.RunStatus.Exception;}// 仅设置 Y不设置 X、缩放或倾斜linXform (CogTransform2DLinear)fixtureTool.RunParams.UnfixturedFromFixturedTransform;linXform.TranslationY caliperTool.Results[0].Edge0.PositionY;fixtureTool.Run();if (fixtureTool.RunStatus.Result ! CogToolResultConstants.Accept){throw fixtureTool.RunStatus.Exception;}// 第五步运行 Blob 工具并检查其结果绘制结果图形blobTool.InputImage (CogImage8Grey)fixtureTool.OutputImage;blobTool.Run();if (blobTool.RunStatus.Result ! CogToolResultConstants.Accept){throw blobTool.RunStatus.Exception;}if (blobTool.Results.GetBlobs().Count 0){throw new Exception(未找到 blob 结果.);}cogDisplay1.StaticGraphics.Add(blobTool.Results.GetBlobs()[0].CreateResultGraphics(CogBlobResultGraphicConstants.Boundary | CogBlobResultGraphicConstants.CenterOfMass), );}catch (CogException ex){DisplayErrorAndExit(工具运行错误: ex.Message);}catch (Exception gex){DisplayErrorAndExit(工具运行错误: gex.Message);}}7. 界面效果