一、新建Windows类库
1、新建类库
2、类库里面添加类(CogTool)和用户控件(CogToolEditV2)
2.1、对类(CogTool)的代码编写。需要Cognex.VisionPro 和 Cognex.VisionPro.Core 的引用
2.2、代码理解如下:
(1)先定义,但是这不清楚是什么意思
[Serializable]
[Editor(typeof(CogToolEditV2), typeof(Control))] //CogToolEditV2为用户控件名称
[CogDefaultToolInputTerminal(0, "InputImage", "InputImage")]
[CogDefaultToolOutputTerminal(0, "OutputImage", "OutputImage")]
[System.ComponentModel.DesignerCategory("")]
(2)本类继承Cognex.VisionPro.Implementation.CogToolBase,应该是VisionPro的工具基类,并实现重写该基类的方法
public class CogTool : Cognex.VisionPro.Implementation.CogToolBase
(3)新定义私有变量,_InputImage、_OutputImage和_ImageProcessingType,其中_ImageProcessingType为图像算法类型-默认为0不处理图像
#region 私有变量
// 输入图像(可以不序列化输入图像)
[CogSerializationOptionsAttribute(CogSerializationOptionsConstants.InputImages)]
private ICogImage _InputImage;
// 输出图像(可以不序列化输出图像)
[CogSerializationOptionsAttribute(CogSerializationOptionsConstants.OutputImages)]
private ICogImage _OutputImage;
//------------新增私有变量-------------
/// <summary>
/// 图像算法类型-默认为0不处理图像
/// </summary>
private int _ImageProcessingType = 0;
#endregion
(4)构造函数和clone,其中构造函数中有三个无参、一个参数、两个参数,主要是一个参数的构造函数,其内容就是初始化,将输入的图片保持输入图片,输出的图片,如果参数的输入和参数的输出的图片相同则输出参数的输入图片,反之,输出参数的输出图片。
重写clone函数,主要就是实现构造函数(这里是一个参数的构造函数)
#region 构造函数和clone
//无参构造函数
public CogTool()
{
}
public CogTool(CogTool otherTool)
: base(otherTool)
{
// 输入图像
if (otherTool._InputImage != null)
_InputImage = otherTool._InputImage;
// 输出图像
if (otherTool._OutputImage != null)
{
//输入图像和输出图像相同
if (Object.ReferenceEquals(otherTool._InputImage,
otherTool._OutputImage))
_OutputImage = _InputImage;
//输入图像和输出图像不同
else
_OutputImage = otherTool._OutputImage;
}
_ImageProcessingType = otherTool._ImageProcessingType;
}
private CogTool(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
protected override object Clone() { return new CogTool(this); }
#endregion
(5)状态标志,标志图片输入、图片输出、图片加工(SfImageProcessing)和下一哥的标定。
#region 状态标志
// 为返回可变数据的每个属性或方法定义一个 64 位状态标志常量。
// 常量的名称必须是“Sf”,后跟属性或方法的名称。
// 第一个状态标志应设置为“CogToolBase.SfNextSf”。
private const long Sf0 = CogToolBase.SfNextSf;//CogToolBase.SfNextSf=32
public const long SfInputImage = Sf0 << 0;
public const long SfOutputImage = Sf0 << 1;
public const long SfImageProcessing = Sf0 << 2;
protected new const long SfNextSf = Sf0 << 3;
#endregion
(6)get和set,主要是由InputImage、OutputImage和ImageProcessingType构成的,其中InputImage是获取图片输入,并有值输入时,调用OnChanged更改SfCreateCurrentRecord的状态。OutputImage没有进行set操作,因为本次操作是有不同的输出图片,要进行分类输出。ImageProcessingType,与之后的通过控件转换对应的状态有关,获取值后调用OnChanged更改状态。
#region GetSet
public ICogImage InputImage
{
get { return _InputImage; }
set
{
if (!Object.ReferenceEquals(_InputImage, value))
{
_InputImage = value;
// 有值输入时,调用OnChanged更改状态
OnChanged(SfInputImage | SfCreateCurrentRecord);
}
}
}
public ICogImage OutputImage
{
get { return _OutputImage; }
}
public int ImageProcessingType
{
get { return _ImageProcessingType; }
set
{
if (_ImageProcessingType != value)
{
_ImageProcessingType = value;
OnChanged(SfImageProcessing);
}
}
}
#endregion
(7)对InternalRun重写,这个是以编程方式或从编辑控件调用工具时执行的。首先对输入的图片进行检查,在通过之前的_ImageProcessingType的值对图片进行对应的操作。比如值为0时,是输出默认图,将_OutputImage的值等于输入的图片值。值为1时,输出的是灰度图,将_OutputImage的值等于灰度后的图片等等。每次结束后都要将SfCreateLastRunRecord的状态进行更新。最后返回工具成功的状态。
protected override CogToolResultConstants InternalRun(ref string message)
{
// 输入图像为空抛异常
if (_InputImage == null)
throw new Cognex.VisionPro.Exceptions.CogOperatorNoInputImageException();
ICogImage cogImage = _InputImage;
Bitmap bitmap = cogImage.ToBitmap();
switch (_ImageProcessingType)
{
case 0:
_OutputImage = cogImage; //将输入图片给到输出
message = "默认";
break;
case 1: //灰度化图像
try
{
for (int i = 0; i < bitmap.Width; i++)
{
for (int j = 0; j < bitmap.Height; j++)
{
Color color = bitmap.GetPixel(i, j); //在此获取指定的像素的颜色
int gray = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
Color newColor = Color.FromArgb(gray, gray, gray);
//设置指定像素的颜色 参数:坐标x,坐标y,颜色
bitmap.SetPixel(i, j, newColor);
}
}
//将Bitmap转为8位灰度
cogImage = new CogImage8Grey(bitmap);
_OutputImage = cogImage;
message = "图像灰度化成功";
}
catch (Exception e)
{
message = "图像处理失败&