学习VISIONPRO各控件的C#中类库--应用(CogPMAlignTool类)
在C#中我们应用CogPMAlignTool类很多,主要用于零件的尺寸角度,中心点,角度确认(VISIONPRO9.0)
1.这个工具控件中我们需提供的是InputImage 为CogImage8Grey的灰图。
图片我们可以从相机来也可以文档的图片来,由于我这边没有相机,我采用网上下载的图片进行测试。
Bitmap A1 = (Bitmap)Bitmap.FromFile(@"D:\AVI\13.JPG");
CogImage8Grey ima1 = new CogImage8Grey(A1);
2.可以直接利用其控件cogPMAlignEditV2进行编辑,教示我们需确认的
具体教示请参考我之前发的控件学习。
3.教示完成后进行保存。
SaveFileDialog dig=new SaveFileDialog();
if (dig.ShowDialog() == DialogResult.OK)
{
CogSerializer.SaveObjectToFile(cogPMAlignEditV21.Subject ,dig.FileName);
}
4.进行测试确认,再输出数据:
Bitmap A1 = (Bitmap)Bitmap.FromFile(@"D:\AVI\13.JPG");
CogImage8Grey ima1 = new CogImage8Grey(A1);
tool2 = CogSerializer.LoadObjectFromFile(@"D:\AVI\pm1.vpp") as CogPMAlignTool;
tool2.InputImage = ima1;
tool2.Run();
cogRecordDisplay2.Record = tool2.CreateLastRunRecord().SubRecords[0];
CogTransform2DLinear aline= tool2.Results[0].GetPose();
label1.Text = string.Format("数据:角度:{0},\n X:{1},Y:\n{2},\n得分{3}", aline.Rotation, aline.TranslationX, aline.TranslationY, tool2.Results[0].Score * 100);
5.代码:
form2的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.ToolBlock;
using Cognex.VisionPro.ImageFile;
using System.IO;
using Cognex.VisionPro.OCRMax;
using Cognex.VisionPro.ID;
using System.Diagnostics;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.Blob;
namespace qj
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
CogImage8Grey ima1 = null;
private void button1_Click(object sender, EventArgs e) //导进新图片进行编辑
{
Bitmap A1 = (Bitmap)Bitmap.FromFile(@"D:\AVI\13.JPG");
ima1 = new CogImage8Grey(A1);
cogPMAlignEditV21.Subject.InputImage = ima1;
}
private void button3_Click(object sender, EventArgs e) //打开编辑
{
OpenFileDialog dig = new OpenFileDialog();
dig.Filter = "(*.vpp)|*.vpp";
if (dig.ShowDialog() == DialogResult.OK)
{
cogPMAlignEditV21.Subject = CogSerializer.LoadObjectFromFile(dig.FileName) as CogPMAlignTool;
}
}
private void button2_Click(object sender, EventArgs e) //保存
{
SaveFileDialog dig=new SaveFileDialog();
if (dig.ShowDialog() == DialogResult.OK)
{
CogSerializer.SaveObjectToFile(cogPMAlignEditV21.Subject ,dig.FileName);
}
}
private void button4_Click(object sender, EventArgs e) //测试运行
{
cogPMAlignEditV21.Subject.Run();
}
}
}
form1的原码:
private void button1_Click_1(object sender, EventArgs e) //定位测试
{
Bitmap A1 = (Bitmap)Bitmap.FromFile(@"D:\AVI\13.JPG");
CogImage8Grey ima1 = new CogImage8Grey(A1);
tool2 = CogSerializer.LoadObjectFromFile(@"D:\AVI\pm1.vpp") as CogPMAlignTool;
tool2.InputImage = ima1;
tool2.Run();
cogRecordDisplay2.Record = tool2.CreateLastRunRecord().SubRecords[0];
CogTransform2DLinear aline= tool2.Results[0].GetPose();
label1.Text = string.Format("数据:角度:{0},\n X:{1},Y:\n{2},\n得分{3}", aline.Rotation, aline.TranslationX, aline.TranslationY, tool2.Results[0].Score * 100);
}
private void button2_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e) //CogPMAlignTool mark编辑
{
Form2 gi = new Form2();
gi.ShowDialog();
}