简单使用gige千兆网口basler工业相机

使用200万basler彩色相机

1,安装basler相机的驱动程序,选择安装pylonc.net

2,创建c#程序,项目添加引用pylonc.net.dll动态库

3,最简单使用gige千兆网口basler工业相机

a,using PylonC.NET;

b, //声明basler相机变量
        PYLON_DEVICE_HANDLE hDev = null;
        PylonBuffer<Byte> imgBuf = null;
        PylonGrabResult_t grabResult;

c,    /* This is a special debug setting needed only for GigE cameras.
                See 'Building Applications with pylon' in the Programmer's Guide. */

//初始化相机

#if DEBUG        
            Environment.SetEnvironmentVariable("PYLON_GIGE_HEARTBEAT", "300000" /*ms*/);
#endif
            Pylon.Initialize();//初始化相机
            Thread.Sleep(1000);
            //GigE basler相机
            hDev = new PYLON_DEVICE_HANDLE();
            #region Bsler相机             
                uint numDevices;    /*有效设备数 */
                numDevices = Pylon.EnumerateDevices();
                if (0 == numDevices)
                {
                    throw new Exception("No devices found.");//没有相机
                }
                else
                    try{ 

                       bool isAvail;
            //以下只使用一个相机,其index=0,只针对黑白相机 the Mono8 pixel format
                        hDev = Pylon.CreateDeviceByIndex(0);
                        Pylon.DeviceOpen(hDev, Pylon.cPylonAccessModeControl
                            | Pylon.cPylonAccessModeStream);
                        isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_PixelFormat_Mono8");
                        if (!isAvail)
                        {
                            /* Feature is not available. */
                            throw new Exception("Device doesn't support the Mono8 pixel format.");
                        }
                        Pylon.DeviceFeatureFromString(hDev, "PixelFormat", "Mono8");
                        isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_AcquisitionStart");
                        if (isAvail)
                        {
                            Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "AcquisitionStart");
                            Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
                        }
                        isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameBurstStart");
                        if (isAvail)
                        {
                            Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameBurstStart");
                            Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
                        }
                        isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameStart");
                        if (isAvail)
                        {
                            Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameStart");
                            Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
                        }
                        isAvail = Pylon.DeviceFeatureIsWritable(hDev, "GevSCPSPacketSize");

                        if (isAvail)
                        {
                            /* ... The device supports the packet size feature. Set a value. */
                            Pylon.DeviceSetIntegerFeature(hDev, "GevSCPSPacketSize", 1500);
                        }
                        Pylon.DeviceGrabSingleFrame(hDev, 0, ref imgBuf, out grabResult, 500);

                       //此处只针对80万,200万,500万相机
                        if (grabResult.SizeX <= 1074 && grabResult.SizeX >= 1024)
                        {//相机像素略大于80万情况,只取80万
                            pictureBox1.Size = new Size(512, 384);
                        }
                        else if (grabResult.SizeX <= 1650 && grabResult.SizeX >= 1600)
                        {//相机像素略大于200万情况,只取200万
                            pictureBox1.Size = new Size(400, 300);
                        }
                        else if (grabResult.SizeX <= 2498 && grabResult.SizeX >= 2448)
                        {//相机像素略大于500万情况,只取500万
                            pictureBox1.Size = new Size(612, 512);
                        } //另一种500万相机,未列出,分辨率1944*2592

                    }
                    catch (Exception)
                    {
                     
                    }
            
            #endregion Bsler相机

d,//开启相机,

 timer1.Start();

//取像并显示出来

   private void timer1_Tick(object sender, EventArgs e)
        {
            int ww = Convert.ToInt32(inputW.Text);
            int hh = Convert.ToInt32(inputH.Text);
            try
            {              
                Pylon.DeviceGrabSingleFrame(hDev, 0, ref imgBuf, out grabResult, 500);

                int bytes = ww * hh * 3;
                byte[] bufercopy = new byte[bytes];
                Bitmap cutPic24 = new Bitmap(ww, hh,
                    System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                BitmapData _cutPic = cutPic24.LockBits(new Rectangle(0, 0, ww, hh),
                    ImageLockMode.ReadWrite, cutPic24.PixelFormat);
                IntPtr ptr = _cutPic.Scan0;//得到首地址           

                int yy = (grabResult.SizeY - hh) / 2;//多余不取
                int xx = (grabResult.SizeX - ww) / 2;//多余不取
                for (int ii = yy; ii < yy + hh; ii++)//图像复原,即由8位图变成24位图像
                {
                    for (int j = xx; j < xx + ww; j++)
                    {
                        int n = ii * grabResult.SizeX + j;
                        int k = (ii - yy) * ww + j - xx;
                        int m = 3 * k;
                        bufercopy[m]  = imgBuf.Array[n];
                        bufercopy[m + 1] = imgBuf.Array[n];
                        bufercopy[m + 2] = imgBuf.Array[n];
                   
                    }
                }
                //把cutvalues数组给ptr
                System.Runtime.InteropServices.Marshal.Copy(bufercopy, 0, ptr, ww * hh * 3);
                cutPic24.UnlockBits(_cutPic);
                if (ww == 1600)
                {
                    pictureBox1.Size = new System.Drawing.Size(400, 300);
                }
                if (ww == 1024)
                {
                    pictureBox1.Size = new System.Drawing.Size(512, 384);
                }
            
                if (ww == 2448)
                {
                    pictureBox1.Size = new System.Drawing.Size(612, 512);
                }
               pictureBox1.Image = cutPic24;          
            }
            catch (Exception)
            {
            }
        }

e,关闭相机

  try {
                            if (hDev.IsValid)//异常,先释放句柄
                            {
                                /* ... Close and release the pylon device. */
                                if (Pylon.DeviceIsOpen(hDev))
                                {
                                    Pylon.DeviceClose(hDev);
                                }
                                Pylon.DestroyDevice(hDev);
                            }
                        }
                        catch (Exception)
                        {
                            /*No further handling here.*/
                        }
                        Pylon.Terminate();//其次,终结相机

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
使用Python调用Gige网口彩色工业相机,可以使用HK的MVS软件。首先,你需要下载并安装MVS软件。在安装文件中,你可以找到Python的开发案例。可以在HK的开发案例中进行修改,实现基于Python和OpenCV的Gige接口工业相机的图片采集。 在图像处理中,图片信息可以使用多种表示方式,不仅仅是我们熟知的RGB。还有HLS、HSV、YUV、Bayer等表示方式。但由于OpenCV等视觉处理库的广泛运用,RGB空间色彩模型是最常用的(实际上是BGR)。 具体的步骤如下: 1. 下载并安装HK的MVS软件。 2. 在MVS软件安装文件中找到Python的开发案例。 3. 根据需要修改开发案例,实现基于Python和OpenCV的Gige接口工业相机的图片采集。 4. 调用Gige网口彩色工业相机的步骤和函数可以在开发案例中找到。 在使用Python调用Gige网口彩色工业相机时,你可以创建一个Python文件,例如命名为GigeOpencv.py,在其中编写代码来实现图片采集和处理的功能。 另外,你也可以创建一个名为AllOpencv.py的文件,该文件用于整合所有的代码和功能,使其更加结构化和易于管理。 通过上述步骤和文件的创建,你可以实现使用Python调用Gige网口彩色工业相机使用OpenCV进行图像处理的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [python调用Gige网口工业相机 opencv](https://blog.csdn.net/mcuwangzai/article/details/122333013)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [OpenCV调用工业相机](https://blog.csdn.net/Emins/article/details/124863690)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值