首页 > 编程语言 >VisionPro C#混合编程实现工业相机实时图像采集

VisionPro C#混合编程实现工业相机实时图像采集

时间:2023-08-25 09:55:06浏览次数:40  
标签:hDev VisionPro isAvail 编程 System private C# Pylon using

  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.ImageFile;
 11 using System.IO;
 12 using PylonC.NET;
 13 using System.Runtime.InteropServices;
 14 using System.Threading;
 15 
 16 namespace Demo
 17 {
 18     public partial class Form1 : Form
 19     {
 20         private CogImageFileTool m_ImageFileTool;
 21         private Thread ThreadObject;                //线程
 22         private bool   ThreadStop = false;
 23 
 24         public Form1()
 25         {
 26             InitializeComponent();
 27 
 28             //线程对象实例化
 29             ThreadObject = new Thread(new ThreadStart(ThreadFunction));
 30 
 31             //打开相机
 32             OpenCameraSoftTrigger();
 33 
 34 
 35         }
 36 
 37         private void DisplayImage(object sender, EventArgs e)
 38         {
 39 
 40             if (ThreadObject.ThreadState == System.Threading.ThreadState.Unstarted)
 41             {
 42                 ThreadObject.Start();
 43 
 44             }
 45 
 46         }
 47 
 48 
 49         private void StopPlay_button_Click(object sender, EventArgs e)
 50         {
 51             ThreadStop = true;
 52 
 53             //停止采集图像
 54             Pylon.DeviceClose(hDev);
 55             Pylon.DestroyDevice(hDev);
 56         }
 57 
 58 
 59         //线程回调函数
 60         public void ThreadFunction()
 61         {
 62             int ImageWidth = 1280;
 63             int ImageHeight = 1024;
 64             CogImage8Grey Image = new CogImage8Grey();
 65             var cogRoot = new CogImage8Root();
 66             IntPtr ImageBufferPtr = Marshal.AllocHGlobal(ImageWidth * ImageHeight);
 67             byte[] ImageBuffer = new byte[ImageWidth * ImageHeight];
 68             while (!ThreadStop)
 69             {
 70                 //采集单张图像
 71                 SnapAcquisitionSoftTrigger(ref ImageBuffer);
 72 
 73                 //将图像数据从托管区拷贝到非托管区
 74                 Marshal.Copy(ImageBuffer, 0, ImageBufferPtr, ImageWidth * ImageHeight);
 75 
 76                 //初始化
 77                 cogRoot.Initialize(ImageWidth, ImageHeight, ImageBufferPtr, ImageWidth, null);
 78 
 79                 //指定Image图像数据为cogRoot
 80                 Image.SetRoot(cogRoot);
 81 
 82                 //将图像数据传给cogRecordDisplay1控件
 83                 cogRecordDisplay1.Image = Image as CogImage8Grey;
 84 
 85                 //显示图像
 86                 cogRecordDisplay1.Fit(true);
 87 
 88             }
 89         }
 90 
 91 
 92         ///balser  sdk/
 93         private PYLON_DEVICE_HANDLE hDev = new PYLON_DEVICE_HANDLE();   /* Handle for the pylon device. */
 94         private uint numDevices;                                        /* Number of available devices. */
 95         private const int numGrabs = 1000;                              /* Number of images to grab. */
 96         private PylonBuffer<Byte> imgBuf = null;                        /* Buffer used for grabbing. */
 97 
 98         //打开相机
 99         public void OpenCameraSoftTrigger()
100         {
101             bool isAvail;
102 
103             Pylon.Initialize();
104 
105             /* Enumerate all camera devices. You must call PylonEnumerateDevices() before creating a device. */
106             numDevices = Pylon.EnumerateDevices();
107 
108             if (0 == numDevices)
109             {
110                 MessageBox.Show("没有找到相机");
111                 return;
112             }
113             else
114             {
115                 /* Get a handle for the first device found.  */
116                 hDev = Pylon.CreateDeviceByIndex(0);
117             }
118 
119             /* Before using the device, it must be opened. Open it for configuring parameters and for grabbing images. */
120             Pylon.DeviceOpen(hDev, Pylon.cPylonAccessModeControl | Pylon.cPylonAccessModeStream);
121 
122             /* Set the pixel format to Mono8, where gray values will be output as 8 bit values for each pixel. */
123             /* ... Check first to see if the device supports the Mono8 format. */
124             isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_PixelFormat_Mono8");
125 
126             if (!isAvail)
127             {
128                 /* Feature is not available. */
129                 MessageBox.Show("设备不支持8位灰度图像");
130             }
131 
132             /* ... Set the pixel format to Mono8. */
133             Pylon.DeviceFeatureFromString(hDev, "PixelFormat", "Mono8");
134 
135             /* Disable acquisition start trigger if available. */
136             isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_AcquisitionStart");
137             if (isAvail)
138             {
139                 Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "AcquisitionStart");
140                 Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
141             }
142 
143 
144             /* Disable frame burst start trigger if available */
145             isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameBurstStart");
146             if (isAvail)
147             {
148                 Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameBurstStart");
149                 Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
150             }
151 
152             /* Disable frame start trigger if available */
153             isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameStart");
154             if (isAvail)
155             {
156                 Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameStart");
157                 Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
158             }
159 
160             /* For GigE cameras, we recommend increasing the packet size for better
161             performance. If the network adapter supports jumbo frames, set the packet
162             size to a value > 1500, e.g., to 8192. In this sample, we only set the packet size
163             to 1500. */
164             /* ... Check first to see if the GigE camera packet size parameter is supported and if it is writable. */
165             isAvail = Pylon.DeviceFeatureIsWritable(hDev, "GevSCPSPacketSize");
166 
167             if (isAvail)
168             {
169                 /* ... The device supports the packet size feature. Set a value. */
170                 Pylon.DeviceSetIntegerFeature(hDev, "GevSCPSPacketSize", 1500);
171             }
172 
173         }
174 
175 
176         //采集单张图像
177         public void SnapAcquisitionSoftTrigger(ref byte[] ImageBufferPtr)
178         {
179 
180             PylonGrabResult_t grabResult;
181 
182             /* Grab one single frame from stream channel 0. The
183             camera is set to "single frame" acquisition mode.
184             Wait up to 500 ms for the image to be grabbed.
185             If imgBuf is null a buffer is automatically created with the right size.*/
186             Pylon.DeviceGrabSingleFrame(hDev, 0, ref imgBuf, out grabResult, 500);
187             IntPtr dataAddress = imgBuf.Pointer;
188 
189             /* Check to see if the image was grabbed successfully. */
190             if (grabResult.Status == EPylonGrabStatus.Grabbed)
191             {
192                 Marshal.Copy(dataAddress, ImageBufferPtr, 0, 1280 * 1024 - 1);
193 
194             }
195             else
196             {
197                 Console.WriteLine("图像抓取失败!n");
198             }
199 
200         }
201 
202     }
203 }

 

标签:hDev,VisionPro,isAvail,编程,System,private,C#,Pylon,using
From: https://www.cnblogs.com/ybqjymy/p/17656104.html

相关文章

  • [LeetCode][198]house-robber
    ContentYouareaprofessionalrobberplanningtorobhousesalongastreet.Eachhousehasacertainamountofmoneystashed,theonlyconstraintstoppingyoufromrobbingeachofthemisthatadjacenthouseshavesecuritysystemsconnectedanditwilla......
  • VisionPro 如何通过向导生成项目应用程序
    最终结果Job1:识别二维码Job2:变形匹配......
  • SAP GUI Scripting VBA Code Snippet to Detect all IDs of the UI Elements
    '-Begin-----------------------------------------------------------------OptionExplicitDimgColl()AsStringDimjAsIntegerSubGetAll(ObjAsObject)'---------------------------------------------'-'-Recursivelycalledsubro......
  • VisionPro 控制工业相机实时图像采集
    原谅我,我的相机是国产的,连不上。我就不下什么驱动了。1.参数设置连上相机后,会有相机的序列号(LH54282651)、曝光、增益、对比度、亮度等相机参数设置。2.闪光灯和触发器软触发:软件内部触发(VisionPro)硬触发:外部硬件触发(上位机)3.图像属性4.GigE传输超时、延时等......
  • tomcat9中设置jks证书和pfx证书
    jks证书设置server.xml<Connectorport="443"protocol="org.apache.coyote.http11.Http11NioProtocol"maxThreads="1000"connectionTimeout="30000"maxPostSize="-1"enabl......
  • ASP.NET Core实时库SignalR简单应用
    一、什么是SignalR:SignalR是用于构建需要实时用户交互或实时数据更新的Web应用程序的一个开放源代码.NET库。不仅仅用在Web应用中,后面会讲到它的应用范围。它简化了简化了构建实时应用程序的过程,包括ASP.NETServer库和JavaScriptClient库,以便管理Client与Server连接并将内容......
  • 公司来了个大佬,把 FullGC 40 次/天优化为 10 天 1 次,太秀了~!
    来源:https://heapdump.cn/article/1859160通过这一个多月的努力,将FullGC从40次/天优化到近10天才触发一次,而且YoungGC的时间也减少了一半以上,这么大的优化,有必要记录一下中间的调优过程。对于JVM垃圾回收,之前一直都是处于理论阶段,就知道新生代,老年代的晋升关系,这些知......
  • VS插件DevExpress CodeRush v23.1 - 支持Visual Studio ARM
    DevExpress CodeRush是一个强大的VisualStudio.NET插件,它利用整合技术,通过促进开发者和团队效率来提升开发者体验。CodeRush能帮助你以极高的效率创建和维护源代码。Consume-first申明,强大的模板,智能的选择工具,智能代码分析和创新的导航以及一个无与伦比的重构集,在它们的帮助......
  • VisionPro 软件介绍
    VisionPro软件介绍一VisionPro的四种开发模式1.Quickbuild视觉+向导生成的操作界面Quickbuild视觉 向导生成的操作界面2Quickbuild视觉+修改的操作界面使用QuickBuild互动开发视觉、输入和输出和工作控件使用应用程序向导生成操作界面(VB或者C#)使用VisualStudio定......
  • eslint-config-prettier和eslint-plugin-prettier有什么关系
    `eslint-config-prettier`和`eslint-plugin-prettier`是用于与ESLint和Prettier集成的两个不同的包。1.`eslint-config-prettier`:这是一个ESLint配置规则的包,它将禁用与Prettier冲突的ESLint规则。使用`eslint-config-prettier`可以确保ESLint规则与Prettier......