机器视觉_联合编程(二)

链接相机,加载tb,检测

FrameGrabber链接相机拍照

using System;
using System.Collections;
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;
using Cognex.VisionPro;


namespace _02_FrameGrabber链接相机拍照
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        ICogFrameGrabber mGrabber=null;
        ICogAcqFifo mFifo = null;
        private void Form1_Load(object sender, EventArgs e)
        {
            Inital();
        }


        //定义加载相机的方法
        private void Inital()
        {
            //获取到所有的已经连接的相机(集合)
            CogFrameGrabbers cogFrame = new CogFrameGrabbers();
            if (cogFrame.Count<1)
            {
                MessageBox.Show("连接失败");
            }
            //遍历集合 取出相机
            foreach (ICogFrameGrabber item in cogFrame)
            {
               
                mGrabber =item;
                //创建采集图像接口
                //参数1:图像类型
                //参数2:像素类型
                mFifo= item.CreateAcqFifo("Generic GigEVision (Mono)",CogAcqFifoPixelFormatConstants.Format8Grey,0,true);
                //采图完成  之后 自动调用一个事件处理函数
                mFifo.Complete += mAcq_com;
            }
        }
        private void mAcq_com(object sender,CogCompleteEventArgs e )
        {

            int NumReady, numPending;

            bool busy;
            try
            {
                //定义 图像类型  和  接口
                CogImage8Grey image =new CogImage8Grey();
                CogAcqInfo info = new CogAcqInfo();
                //获取采集到的信息
                mFifo.GetFifoState(out NumReady,out numPending,out busy);

                //判断是否采集到图像信息
                if (numPending>0)
                {
                    //获取采集到的信息
                    image = (CogImage8Grey)mFifo.CompleteAcquireEx(info);
                    //显示图像信息
                    cogRecordDisplay1.Image=image;
                    cogRecordDisplay1.Fit();

                }



            }
            catch (Exception)
            {

   
            }

        }

        //拍照
        private void button1_Click(object sender, EventArgs e)
        {
            //调用接口拍照
            mFifo.StartAcquire();
        }
    }
}






设置曝光

  //设置曝光
  private void button3_Click(object sender, EventArgs e)
  {
      //手动设置的曝光的值
      int exps = Convert.ToInt32(textBox1.Text);
      //设置曝光
      mFifo.OwnedExposureParams.Exposure = exps;

      MessageBox.Show("曝光设置成功");

  }

释放相机

  //释放相机
  private void release()
  {
      CogFrameGrabbers cogFrame = new CogFrameGrabbers();
      foreach (ICogFrameGrabber item in cogFrame)
      {
          item.Disconnect(false);
      }

  }
  private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  {
      release();
  }

读取图片

方式一: CogImageFileTool

 public ICogImage ReadImage1()
 {
     string path = @"E:\机器视觉16班\VisionPro\课件\15_ConnectCamera\01_联合编程\02_连接相机\bin\Debug\Image1\wuyifan.bmp";

     //实例化工具
     CogImageFileTool imgFileTool= new CogImageFileTool();
     //打开图像
     //参数1:图像的路径
     //参数2:图像的操作
     imgFileTool.Operator.Open(path, CogImageFileModeConstants.Read);
     //运行工具
     imgFileTool.Run();
     //返回工具输入的图像信息
     return imgFileTool.OutputImage;
 }

方式二: bitmap

 public ICogImage ReadImage2()
 {
      string path = @"E:\机器视觉16班\VisionPro\课件\15_ConnectCamera\01_联合编程\02_连接相机\bin\Debug\Image1\20240507150110.bmp";
      Bitmap tmap = new Bitmap(path);
      ICogImage images = new CogImage24PlanarColor(tmap);
      return images;
  }

手动选择图片路径

 private void button5_Click(object sender, EventArgs e)
 {
     OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.Filter = "(*.jpg;*.jpeg;*.gif;*.bmp;*.png)|*.jpg;*.jpeg;*.gif;*.bmp;*.png";
     openFileDialog.InitialDirectory = @"E:\";

     if (openFileDialog.ShowDialog() == DialogResult.OK)
     {
         //获取手动选择的图像的路径
         string fileName =openFileDialog.FileName;
         Bitmap tmap = new Bitmap(fileName);
         ICogImage images = new CogImage24PlanarColor(tmap);
         cogRecordDisplay1.Image = images;
     }

 }

加载ToolBlock并运行工具

//1.
  private void LoadToolBock()
  {
      string path = Directory.GetCurrentDirectory() + @"\tb.vpp";

   tb =   (CogToolBlock) CogSerializer.LoadObjectFromFile(path);

      
  }

//2.
   private void button6_Click(object sender, EventArgs e)
   {
       FormTB formTB =  new FormTB(tb);
       formTB.Show();
   }
//3. 
private void FormTB_Load(object sender, EventArgs e)
  {
      cogToolBlockEditV21.Subject = mTB;
  }

检测获取结果

 private void button7_Click(object sender, EventArgs e)
 {

     if (tb != null)
     {
         //把采集到的图像传入进tb
         tb.Inputs["OutputImage"].Value = mImage;
         tb.Run();
         //显示测量之后的图像
         cogRecordDisplay1.Record = tb.CreateLastRunRecord().SubRecords[0];
         cogRecordDisplay1.Fit();
         double MWidth =(double) tb.Outputs["MyWidth"].Value;
         label1.Text=MWidth.ToString();

     }

 }

总结

相机工具 :			cogAcqFifoEditV21  
想办法 把我们用 vispro 做好的 Vpp 赋值给 相机工具 
从文件里面 加载对象  :		CogSerializer.LoadObjectFromFile( 文件地址 );
声明一个相机类:  			CogAcqFifoTool
往winform 窗口里面的控件赋值  : 	winform里面的工具名.Subject = 文件里面加载出来的 对象

加载相机 双击 winform 主窗口  来添加事件 直接加载相机  

拍照  点击时 用一个控件显示 
没有的话就是空 :			文件里面加载的对象.Operator   
运行工具:			.Run();
图像类型:			ICogImage 
展示的工具:			cogRecordDisplay.Image  是展示工具要展示的图片

释放相机 添加一个事件 
窗口关闭事件:			FormClosing   当关闭窗口时会触发的事件

释放相机:
  //也要判断一下相机是不是空的
            if (mAcq.Operator != null) 
             {
                //.FrameGrabber  找到硬件 释放相机
                mAcq.Operator.FrameGrabber.Disconnect(false);
            }
        }

//需要背的东西 
工具:  
相机工具 :			cogAcqFifoEditV21 
展示的工具:			cogRecordDisplay.Image  是展示工具要展示的图片

事件:
加载相机 双击 winform 主窗口  来添加事件 直接加载相机  
点击事件
窗口关闭事件:			FormClosing   当关闭窗口时会触发的事件

函数:

往winform 窗口里面的控件赋值  : 	winform里面的工具名.Subject = 文件里面加载出来的 对象
运行工具:			.Run();
展示的工具:			cogRecordDisplay.Image  是展示工具要展示的图片
图片百分比显示:			放图片的工具.Fit();
弹出提示框:			MessageBox.Show("提示语句");
创建采集接口:			相机类型的接口.CreateAcqFifo("Generic GigEVision (Mono)", CogAcqFifoPixelFormatConstants.Format8Grey,0,true);
拿相机获取到的图像信息:		相机接口.CompleteAcquireEx(info) as CogImage8Grey;
加载文件中的vpp对象:		(工具类型)CogSerializer.LoadObjectFromFile(文件地址);
释放相机:			相机接口.Disconnect(false);
接口拍照的方法:			相机类型接口.StartAcquire();
设置曝光:			相机类型接口.OwnedExposureParams.Exposure = 曝光数;
获取当前程序运行路径:		Directory.GetCurrentDirectory()
判断文件夹是否存在:		Directory.Exists(文件地址)
创建文件夹:			Directory.CreateDirectory(文件夹地址);
bitmap类型保存图片:		Bitmap bmp = 图片 as Bitmap;
保存图像:			bmp.Save(文件地址,System.Drawing.Imaging.ImageFormat.图片类型(jpg,png....));
读取图片:  (浅记)
CogImageFileTool imageFile = new CogImageFileTool();
            //.Operator.Open()  工具里面的方法  第一个是图片路径  第二个是图片格式
            //imageFile.Operator.Open(path,CogImageFileModeConstants.Read);
            //imageFile.Run();
            //return imageFile.OutputImage;
bitmap类读取图片:		Bitmap bmp = new Bitmap(图片路径);
保存图像的格式:			(需要一个图像类型) = new CogImage24PlanarColor(btm储存好的);
百分比显示适应图像:		 显示图像的控件.Fit();
弹出窗口:			需要弹出的窗口.ShowDialog();
输入图片:			工具名字.Inputs["OutputImage"].Value = 图像;
把当前文件打包覆盖指定文件: 	CogSerializer.SaveObjectToFile(mTB,文件的完整路径);(需要手动创建路径)
添加事件完成时可直接跳转到事件中:	相机彩图完成后的相机接口.Complete +=事件名(自己起);
过滤文件类型:			文件对话框类.Filter
显示对话框:			.ShowDialog()

通过bitmap 类实现读取图片

            Bitmap bmp = new Bitmap(path);
            ICogImage image = new CogImage24PlanarColor(bmp);   
            return image;
通过 工具类读取图片
	 CogImageFileTool imageFile = new CogImageFileTool();
            //.Operator.Open()  工具里面的方法  第一个是图片路径  第二个是图片格式
            //imageFile.Operator.Open(path,CogImageFileModeConstants.Read);
            //imageFile.Run();
            //return imageFile.OutputImage;
			



用到的类:
声明一个相机类:  		  CogAcqFifoTool
图像类型:		     	ICogImage 
TookBlick类:			  CogToolBlick
连接的硬件设备类:		 ICogFrameGrabber
相机接口类:			   ICogAcqFifo
所有已连接的相机类		 CogFrameGrabbers 
文件对话框类:			   OpenFileDialog

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值