目录
一、Basler软件安装
二、winform界面设计
三、程序代码
一、Basler软件安装
机器视觉软件及工业相机软件下载 - pylon, ToF 等 | Basler (baslerweb.com)![]()
讯享网https://www.baslerweb.com/cn/sales-support/downloads/software-downloads/1)根据操作系统选择对应的pylon软件下载
2)打开安装包,选择Developer,Next
3)选择相机连接方式 ,本次开发过程使用网线连接,所以选用GigE
4)安装完成后,在你的安装路径下找到.\Basler\pylon 6\Development\Assemblies\Basler.Pylon,其中有32位和64位的动态链接库,在创建项目后引用Basler.dll
PS:在安装中遇到了一个错误代码,下载Pylon5,Pylon6都是同样的错误代码,最终解决办法是重装系统。QAQ
如欲安装失败,参考该文章
为什么出现 Baslerpylon安装错误如何解决?(8页)-原创力文档 (book118.com)
https://max.book118.com/html/2020/0703/8044045033002122.shtm二、winform界面设计


需求
1)显示相机基本信息
2)调节曝光和增益
3)设置软触发和硬触发
4)单张采集和连续采集
5)显示采集图像的数量
6)保存采集到的图像
根据需求分别使用了以下控件
| Label | 显示文本信息 |
| Button | 控制 |
| PictureBox | 显示图像 |
| TextBox | 显示获取的信息 |
| GroupBox | 将控件归为一类 |
| TrackBar | 滑动调节数值 |
| RadioButton | 单选按钮 |
| CheckBox | 复选框 |
| StatusStrip | 状态栏控件 |
三、程序代码
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; using Matrox.MatroxImagingLibrary; namespace BaslerAndMil { public partial class Form1 : Form { BaslerCam mCamera1; long count1 = 0; private static readonly int CONTOUR_DRAW_COLOR = MIL.M_COLOR_GREEN; private static readonly int CONTOUR_LABEL_COLOR = MIL.M_COLOR_RED; private MIL_ID MilApplication = MIL.M_NULL; private MIL_ID MilSystem = MIL.M_NULL; private MIL_ID MilDisp1 = MIL.M_NULL; private MIL_ID MilImageBuf1 = MIL.M_NULL; private MIL_ID GraphicList = MIL.M_NULL; private MIL_ID MilEdgeContext = MIL.M_NULL; private MIL_ID MilEdgeResult = MIL.M_NULL; double EdgeDrawColor = CONTOUR_DRAW_COLOR; double LabelDrawColor = CONTOUR_LABEL_COLOR; /// <summary> /// 相机1显示图像自定义委托 /// </summary> /// <param name="time">采集图像时间</param> public delegate void delegateDisplayImage(); public Form1() { InitializeComponent(); // 分配MIL系统资源 MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL); MIL.MdispAlloc(MilSystem, MIL.M_DEFAULT, "M_DEFAULT", MIL.M_DEFAULT, ref MilDisp1); } private void Form1_Load(object sender, EventArgs e) { mCamera1 = new BaslerCam("c1"); // 实例化UserID为"c1"的相机 if (mCamera1.OpenCam() == true) // 打开并初始化相机 { mCamera1.SetHeartBeatTime(10000); // 设置相机1心跳时间 mCamera1.eventComputerGrabTime += computeGrabTime1; // 注册计算采集图像时间回调函数 mCamera1.eventProcessImage += processImage1; // 注册MIL显示回调函数 textCamModel.Text = mCamera1.strModelName; textSerialNumber.Text = mCamera1.strSerialNumber; textUserID.Text = mCamera1.strUserID; //textIP.Text = mCamera1.strModeIP; tkbExposure1.Minimum = (int)mCamera1.minExposureTime; tkbExposure1.Maximum = (int)mCamera1.maxExposureTime; tkbGain1.Minimum = (int)mCamera1.minGain; tkbGain1.Maximum = (int)mCamera1.maxGain; //rdbFreerun1.PerformClick(); // 默认按下Freerun按钮 } else { SetBottonEnable1(false, false, false); } timer1.Start(); } // 计算相机1采集图像时间 private void computeGrabTime1(long time) { ++count1; ImageNum.Text = "[ Count1 : " + count1 + " ]"; TIme.Text = "[ Time1 : " + time + " ]"; } //图像显示窗口 private void dispSelectWindow1() { MIL.MdispSelectWindow(MilDisp1, MilImageBuf1, this.panel1.Handle); MIL.MdispControl(MilDisp1, MIL.M_SCALE_DISPLAY, MIL.M_ENABLE); } // 相机1 MIL窗体显示图像 private void processImage1(Boolean isColor, int width, int height, byte[] imgData) { if (isColor == false) { // 分配MIL图像buffer if (MilImageBuf1 == MIL.M_NULL) { MIL.MbufAlloc2d(MilSystem, width, height, 8 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_PROC + MIL.M_DISP, ref MilImageBuf1); } // 转MIL黑白图像格式 MIL.MbufPut(MilImageBuf1, imgData); } else { // 分配MIL图像buffer if (MilImageBuf1 == MIL.M_NULL) { MIL.MbufAllocColor(MilSystem, 3, width, height, 8 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_PROC + MIL.M_DISP, ref MilImageBuf1); } // 转MIL彩色图像格式 MIL.MbufPutColor(MilImageBuf1, MIL.M_PACKED + MIL.M_RGB24, MIL.M_ALL_BANDS, imgData); } // 设置MIL图像显示的窗体 delegateDisplayImage dispCallback1 = new delegateDisplayImage(dispSelectWindow1); panel1.Invoke(dispCallback1); if (checkBoxSaveImg.Checked) { MIL.MbufSave("./Camera1/" + count1 + ".bmp", MilImageBuf1); //MIL.MbufExport("./Camera1/" + count1 + ".bmp", MIL.M_BMP, MilImageBuf1); //mCamera1.SaveImage("./Camera1/" + count1 + ".bmp", MilImageBuf1, 3840, 2748, false); } } // 相机1单张采集 private void button3_Click(object sender, EventArgs e) { int clickTimes; object tag = this.button3.Tag; if (tag == null) { clickTimes = 0; } else { clickTimes = Convert.ToInt32(tag); } this.button3.Tag = ++clickTimes; if (clickTimes == 1) { mCamera1.GrabOne(); Console.WriteLine(clickTimes); } else if (clickTimes > 1) { mCamera1.GrabOne(); EdgeFind(); Console.WriteLine(clickTimes); } } //相机1连续采集 private void button1_Click(object sender, EventArgs e) { if (mCamera1.StartGrabbing()) { count1 = 0; ImageNum.Text = "[ Count1 : " + count1 + " ]"; SetBottonEnable1(false, true, false); } } //相机1停止采集 private void button2_Click(object sender, EventArgs e) { mCamera1.StopGrabbing(); SetBottonEnable1(true, false, true); } // 设置相机1为Freerun模式 private void rdbFreerun1_CheckedChanged(object sender, EventArgs e) { mCamera1.SetFreerun(); button4.Enabled = false; } // 设置相机1为软触发模式 private void radioButton2_CheckedChanged(object sender, EventArgs e) { mCamera1.SetSoftwareTrigger(); button4.Enabled = true; } // 设置相机1为硬触发模式 private void radioButton3_CheckedChanged(object sender, EventArgs e) { mCamera1.SetExternTrigger(); button4.Enabled = false; } // 执行相机1软触发命令 private void button4_Click(object sender, EventArgs e) { mCamera1.SendSoftwareExecute(); } //调节曝光值 private void tkbExposure1_Scroll(object sender, EventArgs e) { int value = tkbExposure1.Value; mCamera1.SetExposureTime(value); textBox5.Text = Convert.ToString(value); } //调节增益值 private void tkbGain1_Scroll(object sender, EventArgs e) { int value = tkbGain1.Value; mCamera1.SetGain(value); textBox6.Text = Convert.ToString(value); } //改变曝光值 private void txtBExposure1_TextChanged(object sender, EventArgs e) { int value = 0; int.TryParse(textBox5.Text, out value); mCamera1.SetExposureTime(value); } //改变增益值 private void txtBGain1_TextChanged(object sender, EventArgs e) { int value = 0; int.TryParse(textBox6.Text, out value); mCamera1.SetGain(value); } private void SetBottonEnable1(bool startEnable, bool stopEnable, bool singleEnable) { button1.Enabled = startEnable; button2.Enabled = stopEnable; button3.Enabled = singleEnable; } //异常弹出窗口 private void ShowException(Exception exception) { MessageBox.Show("Exception caught:\n" + exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } //显示当前时间 private void timer1_Tick(object sender, EventArgs e) { statusStrip1.LayoutStyle = ToolStripLayoutStyle.HorizontalStackWithOverflow; toolStripStatusLabel1.Alignment = ToolStripItemAlignment.Right; toolStripStatusLabel1.Text = System.DateTime.Now.ToString(); } //窗口关闭时释放资源 private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (GraphicList == MIL.M_NULL) { mCamera1.StopGrabbing(); mCamera1.CloseCam(); ClearBufferNoEdge(); } else { mCamera1.StopGrabbing(); mCamera1.CloseCam(); ClearBufferEdge(); } } //窗口关闭强制退出程序 private void Form1_FormClosed(object sender, FormClosedEventArgs e) { Environment.Exit(0); } private void ClearBufferNoEdge() { //MIL.MgraFree(GraphicList); //MIL.MedgeFree(MilEdgeContext); //MIL.MedgeFree(MilEdgeResult); MIL.MbufFree(MilImageBuf1); MIL.MdispFree(MilDisp1); MIL.MsysFree(MilSystem); MIL.MappFreeDefault(MilApplication, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL); } private void ClearBufferEdge() { //MIL.MgraFree(GraphicList); //MIL.MedgeFree(MilEdgeContext); //MIL.MedgeFree(MilEdgeResult); //MIL.MbufFree(MilImageBuf1); //MIL.MdispFree(MilDisp1); //MIL.MsysFree(MilSystem); //MIL.MappFreeDefault(MilApplication, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL); //释放内存顺序不影响程序运行 //MIL.MgraFree(GraphicList); //MIL.MbufFree(MilImage); //MIL.MmodFree(MilSearchContext); //MIL.MmodFree(MilResult); MIL.MgraFree(GraphicList); MIL.MbufFree(MilImageBuf1); MIL.MedgeFree(MilEdgeContext); MIL.MedgeFree(MilEdgeResult); MIL.MappFreeDefault(MilApplication, MilSystem, MilDisp1, MIL.M_NULL, MIL.M_NULL); } private void EdgeFind() { MIL.MgraAllocList(MilSystem, MIL.M_DEFAULT, ref GraphicList); MIL.MdispControl(MilDisp1, MIL.M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList); MIL.MedgeAlloc(MilSystem, MIL.M_CONTOUR, MIL.M_DEFAULT, ref MilEdgeContext); MIL.MedgeAllocResult(MilSystem, MIL.M_DEFAULT, ref MilEdgeResult); MIL.MedgeControl(MilEdgeContext, MIL.M_CIRCLE_FIT, MIL.M_ENABLE); MIL.MedgeCalculate(MilEdgeContext, MilImageBuf1, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, MilEdgeResult, MIL.M_DEFAULT); MIL.MedgeSelect(MilEdgeResult, MIL.M_EXCLUDE, MIL.M_CIRCLE_FIT_RADIUS, MIL.M_LESS, 6000, MIL.M_NULL); MIL.MgraColor(MIL.M_DEFAULT, EdgeDrawColor); MIL.MedgeDraw(MIL.M_DEFAULT, MilEdgeResult, GraphicList, MIL.M_DRAW_CIRCLE_FIT, MIL.M_DEFAULT, MIL.M_DEFAULT); }
讯享网
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/125373.html