一、环境搭建
创建winfrom窗体应用,加入vp的cogRecordDisplay ,cogToolBlockEditV2和一个button按钮
2.右键点击项目属性,目标平台选择x64,并重新生成解决方案
3.导出visionpeo vpp项目到debug目录下(注意是CogToolBlock,并不是Cogjob项目)
二、代码结构
主要功能总结
该应用程序的主要功能如下:
- 加载图像路径:程序启动时,扫描指定目录下的所有
.bmp
图像文件,并将它们的路径存储在一个列表中。 - 显示图像:用户点击按钮时,程序加载并显示列表中的下一个图像。
- 图像处理:使用 Cognex VisionPro 的工具块对图像进行处理,识别图像中的缺陷。
- 结果分类与保存:根据工具块的处理结果,将图像分类为“OK”或“NG”,并保存到相应的文件夹中。
项目的主要代码结构如下:
using Cognex.VisionPro;
using Cognex.VisionPro.ImageFile;
using Cognex.VisionPro.ToolBlock;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
namespace NG和OK
{
public partial class Form1 : Form
{
ICogImage image;
List<string> imagePaths = new List<string>();
int currentIndex = 0;
CogToolBlock tb = null;
public Form1()
{
InitializeComponent();
}
string imagePath = @"C:\Users\15783\Desktop\NG和OK\NG和OK\bin\Debug\img";
private void button1_Click_1(object sender, EventArgs e)
{
if (currentIndex >= imagePaths.Count)
{
currentIndex = 0;
}
LoadLocalImage(imagePaths[currentIndex]);
currentIndex++;
tb.Inputs["OutputImage"].Value = image;
tb.Run();
SavetoImage();
}
private void SavetoImage()
{
string OKpath = Directory.GetCurrentDirectory() + @"\ImageOK";
if (!Directory.Exists(OKpath))
{
Directory.CreateDirectory(OKpath);
}
string NGpath = Directory.GetCurrentDirectory() + @"\ImageNG";
if (!Directory.Exists(NGpath))
{
Directory.CreateDirectory(NGpath);
}
string result = (string)tb.Outputs["res"].Value;
if (result == "NG")
{
string imageName = $"{"NG"}{DateTime.Now.ToString("HHmmss")}.bmp";
CogImageFileTool fileTool = new CogImageFileTool();
fileTool.InputImage = image;
fileTool.Operator.Open($"{NGpath}\\{imageName}", CogImageFileModeConstants.Write);
fileTool.Run();
}
else
{
string imageName = $"{"OK"}{DateTime.Now.ToString("HHmmss")}.bmp";
CogImageFileTool fileTool = new CogImageFileTool();
fileTool.InputImage = image;
fileTool.Operator.Open($"{OKpath}\\{imageName}", CogImageFileModeConstants.Write);
fileTool.Run();
}
}
private void LoadLocalImage(string imagePath)
{
CogImageFileTool fileTool = new CogImageFileTool();
fileTool.Operator.Open(imagePath, CogImageFileModeConstants.Read);
fileTool.Run();
image = fileTool.OutputImage;
cogRecordDisplay1.Image = image;
cogRecordDisplay1.Fit();
}
private void Form1_Load(object sender, EventArgs e)
{
LoadImagePaths(imagePath);
if (imagePaths.Count > 0)
{
LoadLocalImage(imagePaths[currentIndex]);
}
// 加载工具块
string path = Directory.GetCurrentDirectory() + "\\xiaci.vpp";
tb = (CogToolBlock)CogSerializer.LoadObjectFromFile(path);
if (tb != null)
{
cogToolBlockEditV21.Subject = tb;
}
}
private void LoadImagePaths(string imagePath)
{
if (Directory.Exists(imagePath))
{
imagePaths.AddRange(Directory.GetFiles(imagePath, "*.bmp"));
}
}
}
}
详细代码解释
1. 命名空间和引用
using Cognex.VisionPro;
using Cognex.VisionPro.ImageFile;
using Cognex.VisionPro.ToolBlock;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
- 引入 Cognex VisionPro 的核心命名空间,用于图像处理和工具块操作。
- 引入 .NET 标准库中的命名空间,用于文件操作和窗体控件。
2. 类和字段声明
namespace NG和OK
{
public partial class Form1 : Form
{
ICogImage image;
List<string> imagePaths = new List<string>();
int currentIndex = 0;
CogToolBlock tb = null;
public Form1()
{
InitializeComponent();
}
string imagePath = @"C:\Users\15783\Desktop\NG和OK\NG和OK\bin\Debug\img";
- 定义了几个字段:
image
: 用于存储当前处理的图像。imagePaths
: 存储所有.bmp
图像文件的路径列表。currentIndex
: 当前处理的图像在imagePaths
中的索引。tb
: 用于存储加载的工具块对象。
Form1
构造函数中调用InitializeComponent()
方法,初始化窗体组件。
3. 图像路径加载
private void LoadImagePaths(string imagePath)
{
if (Directory.Exists(imagePath))
{
imagePaths.AddRange(Directory.GetFiles(imagePath, "*.bmp"));
}
}
LoadImagePaths
方法用于加载指定目录下的所有.bmp
图像文件路径。Directory.Exists(imagePath)
: 检查目录是否存在。Directory.GetFiles(imagePath, "*.bmp")
: 获取该目录下所有.bmp
文件的路径,并将其添加到imagePaths
列表中。
4. 窗体加载事件
private void Form1_Load(object sender, EventArgs e)
{
LoadImagePaths(imagePath);
if (imagePaths.Count > 0)
{
LoadLocalImage(imagePaths[currentIndex]);
}
// 加载工具块
string path = Directory.GetCurrentDirectory() + "\\xiaci.vpp";
tb = (CogToolBlock)CogSerializer.LoadObjectFromFile(path);
if (tb != null)
{
cogToolBlockEditV21.Subject = tb;
}
}
Form1_Load
方法在窗体加载时执行。- 调用
LoadImagePaths(imagePath)
加载图像路径。 - 如果
imagePaths
列表中有图像路径,则调用LoadLocalImage(imagePaths[currentIndex])
加载第一张图像。 - 加载工具块文件
xiaci.vpp
,并将其赋值给tb
对象。 - 如果工具块加载成功,则将其设置为
cogToolBlockEditV21
控件的主体。
5. 图像加载
private void LoadLocalImage(string imagePath)
{
CogImageFileTool fileTool = new CogImageFileTool();
fileTool.Operator.Open(imagePath, CogImageFileModeConstants.Read);
fileTool.Run();
image = fileTool.OutputImage;
cogRecordDisplay1.Image = image;
cogRecordDisplay1.Fit();
}
LoadLocalImage
方法用于加载指定路径的图像文件。- 创建
CogImageFileTool
对象,并设置其操作模式为读取文件。 - 运行
CogImageFileTool
,获取输出图像并存储在image
字段中。 - 将加载的图像显示在
cogRecordDisplay1
控件中,并自动调整控件大小以适应图像。
6. 按钮点击事件
private void button1_Click_1(object sender, EventArgs e)
{
if (currentIndex >= imagePaths.Count)
{
currentIndex = 0;
}
LoadLocalImage(imagePaths[currentIndex]);
currentIndex++;
tb.Inputs["OutputImage"].Value = image;
tb.Run();
SavetoImage();
}
button1_Click_1
方法在按钮点击时执行。- 检查当前索引是否超出图像路径列表的范围,如果是则重置为 0。
- 加载当前索引的图像文件并将其显示在窗体中。
- 将当前图像设置为工具块的输入,并运行工具块进行图像处理。
- 处理完成后,调用
SavetoImage
方法保存处理结果。
7. 保存图像
private void SavetoImage()
{
string OKpath = Directory.GetCurrentDirectory() + @"\ImageOK";
if (!Directory.Exists(OKpath))
{
Directory.CreateDirectory(OKpath);
}
string NGpath = Directory.GetCurrentDirectory() + @"\ImageNG";
if (!Directory.Exists(NGpath))
{
Directory.CreateDirectory(NGpath);
}
string result = (string)tb.Outputs["res"].Value;
if (result == "NG")
{
string imageName = $"{"NG"}{DateTime.Now.ToString("HHmmss")}.bmp";
CogImageFileTool fileTool = new CogImageFileTool();
fileTool.InputImage = image;
fileTool.Operator.Open($"{NGpath}\\{imageName}", CogImageFileModeConstants.Write);
fileTool.Run();
}
else
{
string imageName = $"{"OK"}{DateTime.Now.ToString("HHmmss")}.bmp";
CogImageFileTool fileTool = new CogImageFileTool();
fileTool.InputImage = image;
fileTool.Operator.Open($"{OKpath}\\{imageName}", CogImageFileModeConstants.Write);
fileTool.Run();
}
}
SavetoImage
方法用于根据工具块的处理结果保存图像。- 定义了两个文件夹路径
OKpath
和NGpath
,分别用于存储“OK”和“NG”图像。 - 检查文件夹是否存在,如果不存在则创建。
- 从工具块的输出中获取处理结果
res
。 - 根据处理结果
res
,将图像保存到相应的文件夹中,并使用当前时间作为文件名的一部分。
三、效果展示
屏幕录制 2025-05-22 205255