在C#中我们应用CogPMAlignTool类很多,主要用于零件的尺寸角度,中心点,角度确认(VISIONPRO9.0)
1.这个工具控件中我们需提供的是InputImage 为CogImage8Grey的灰图。
图片我们可以从相机来也可以文档的图片来,由于我这边没有相机,我采用网上下载的图片进行测试。
1 Bitmap A1 = (Bitmap)Bitmap.FromFile(@"D:AVI13.JPG");
2 CogImage8Grey ima1 = new CogImage8Grey(A1);
2.可以直接利用其控件cogPMAlignEditV2进行编辑,教示我们需确认的
具体教示请参考我之前发的控件学习。
3.教示完成后进行保存。
1 SaveFileDialog dig=new SaveFileDialog();
2 if (dig.ShowDialog() == DialogResult.OK)
3 {
4 CogSerializer.SaveObjectToFile(cogPMAlignEditV21.Subject ,dig.FileName);
5 }
4.进行测试确认,再输出数据:
1 Bitmap A1 = (Bitmap)Bitmap.FromFile(@"D:AVI13.JPG");
2 CogImage8Grey ima1 = new CogImage8Grey(A1);
3 tool2 = CogSerializer.LoadObjectFromFile(@"D:AVIpm1.vpp") as CogPMAlignTool;
4 tool2.InputImage = ima1;
5 tool2.Run();
6 cogRecordDisplay2.Record = tool2.CreateLastRunRecord().SubRecords[0];
7 CogTransform2DLinear aline= tool2.Results[0].GetPose();
8 label1.Text = string.Format("数据:角度:{0},
X:{1},Y:
{2},
得分{3}", aline.Rotation, aline.TranslationX, aline.TranslationY, tool2.Results[0].Score * 100);
5.代码:
form2的代码:
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using Cognex.VisionPro;
10 using Cognex.VisionPro.ToolBlock;
11 using Cognex.VisionPro.ImageFile;
12 using System.IO;
13 using Cognex.VisionPro.OCRMax;
14 using Cognex.VisionPro.ID;
15 using System.Diagnostics;
16 using Cognex.VisionPro.PMAlign;
17 using Cognex.VisionPro.Blob;
18
19 namespace qj
20 {
21 public partial class Form2 : Form
22 {
23 public Form2()
24 {
25 InitializeComponent();
26 }
27 CogImage8Grey ima1 = null;
28 private void button1_Click(object sender, EventArgs e) //导进新图片进行编辑
29 {
30 Bitmap A1 = (Bitmap)Bitmap.FromFile(@"D:AVI13.JPG");
31 ima1 = new CogImage8Grey(A1);
32 cogPMAlignEditV21.Subject.InputImage = ima1;
33 }
34
35 private void button3_Click(object sender, EventArgs e) //打开编辑
36 {
37 OpenFileDialog dig = new OpenFileDialog();
38 dig.Filter = "(*.vpp)|*.vpp";
39 if (dig.ShowDialog() == DialogResult.OK)
40 {
41 cogPMAlignEditV21.Subject = CogSerializer.LoadObjectFromFile(dig.FileName) as CogPMAlignTool;
42 }
43 }
44
45 private void button2_Click(object sender, EventArgs e) //保存
46 {
47 SaveFileDialog dig=new SaveFileDialog();
48 if (dig.ShowDialog() == DialogResult.OK)
49 {
50 CogSerializer.SaveObjectToFile(cogPMAlignEditV21.Subject ,dig.FileName);
51 }
52 }
53
54 private void button4_Click(object sender, EventArgs e) //测试运行
55 {
56 cogPMAlignEditV21.Subject.Run();
57 }
58 }
59 }
form1的原码:
1 private void button1_Click_1(object sender, EventArgs e) //定位测试
2 {
3 Bitmap A1 = (Bitmap)Bitmap.FromFile(@"D:AVI13.JPG");
4 CogImage8Grey ima1 = new CogImage8Grey(A1);
5
6 tool2 = CogSerializer.LoadObjectFromFile(@"D:AVIpm1.vpp") as CogPMAlignTool;
7 tool2.InputImage = ima1;
8 tool2.Run();
9 cogRecordDisplay2.Record = tool2.CreateLastRunRecord().SubRecords[0];
10 CogTransform2DLinear aline= tool2.Results[0].GetPose();
11 label1.Text = string.Format("数据:角度:{0},
X:{1},Y:
{2},
得分{3}", aline.Rotation, aline.TranslationX, aline.TranslationY, tool2.Results[0].Score * 100);
12 }
13
14 private void button2_Click(object sender, EventArgs e)
15 {
16
17 }
18
19 private void button3_Click(object sender, EventArgs e) //CogPMAlignTool mark编辑
20 {
21 Form2 gi = new Form2();
22 gi.ShowDialog();
23 }