纯C#调用PMAlignTool

using Cognex.VisionPro;
using Cognex.VisionPro.PMAlign;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VPPDemo
{
    public partial class Form1 : Form
    {
        public Bitmap img;//读入的普通图像
        public CogImage8Grey img_8;//用作处理的CogImage8Grey类型图像
        public CogPMAlignTool pma;//PMA工具全局变量
        public CogPMAlignPattern pmap;//PMA工具训练模板全局变量

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 读图片到内存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            img = new Bitmap(@"C:\Users\Administrator\Desktop\无标题.png");
        }

        /// <summary>
        /// 将图片转为8位图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            img_8 = new CogImage8Grey(img);
        }

        /// <summary>
        /// 训练PMA
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            //创建一个新的PMA工具,此工具可以不显示界面
            pma = new CogPMAlignTool();
            //工具的运行输入图像
            pma.InputImage = img_8;
            //将工具显示出来,不显示不赋值即可,可以在界面右侧显示效果,不需要可以屏蔽
            cogPMAlignEditV21.Subject = pma;
            //PMA工具的训练模板,这里申明的为全局变量,方便后面的调用
            pmap = pma.Pattern;
            //pma工具的训练图
            pmap.TrainImage = img_8;
            //创建训练区域
            CogRectangleAffine PatMaxTrainRegion = default(CogRectangleAffine);
            PatMaxTrainRegion = pmap.TrainRegion as CogRectangleAffine;
            //判断是不是使用全图,null表示全图训练
            if (PatMaxTrainRegion != null)
            {
                //训练区域大小及旋转
                PatMaxTrainRegion.SetCenterLengthsRotationSkew(224,134,150,150,0,0);
                //允许调整训练区域的大小
                PatMaxTrainRegion.GraphicDOFEnable = CogRectangleAffineDOFConstants.Position | CogRectangleAffineDOFConstants.Rotation | CogRectangleAffineDOFConstants.Size;
            }
            //创建区域
            CogRectangle PatMaxSearchRegion = new CogRectangle();
            //将其赋值给pma工具的搜索区域
            pma.SearchRegion = PatMaxSearchRegion;
            //设置搜索区域
            PatMaxSearchRegion.SetCenterWidthHeight(0, 0, 900, 900);
            //允许调整搜索区域大小
            // PatMaxSearchRegion.GraphicDOFEnable = CogRectangleDOFConstants.Position | CogRectangleDOFConstants.Size;
            //允许鼠标选择搜索区域
            PatMaxSearchRegion.Interactive = true;
            //训练模板
            pmap.Train();
        }

        /// <summary>
        /// 运行PMA
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            //提供PMA工具输入图像
            pma.InputImage = img_8;
            //运行PMA工具
            pma.Run();
            MessageBox.Show("共找到" + pma.Results.Count.ToString() + "个结果");
        }
    }
}

 

posted @ 2022-11-22 22:36  金雨CHN  阅读(849)  评论(0)    收藏  举报