public void CaptureThreadProc()
        {
            CameraSdkStatus eStatus;
            tSdkFrameHead   FrameHead;
            uint            uRawBuffer;//rawbuffer由SDK内部申请。应用层不要调用delete之类的释放函数

            while (m_bExitCaptureThread == false)
            {
                //500毫秒超时,图像没捕获到前,线程会被挂起,释放CPU,所以该线程中无需调用sleep
                eStatus = MvApi.CameraGetImageBuffer(m_hCamera, out FrameHead, out uRawBuffer, 500);

                if (eStatus == CameraSdkStatus.CAMERA_STATUS_SUCCESS)//如果是触发模式,则有可能超时
                {
                    //图像处理,将原始输出转换为RGB格式的位图数据,同时叠加白平衡、饱和度、LUT等ISP处理。
                    MvApi.CameraImageProcess(m_hCamera, uRawBuffer, m_ImageBuffer, ref FrameHead);
                    //叠加十字线、自动曝光窗口、白平衡窗口信息(仅叠加设置为可见状态的)。
                    MvApi.CameraImageOverlay(m_hCamera, m_ImageBuffer, ref FrameHead);
                    //调用SDK封装好的接口,显示预览图像
                    MvApi.CameraDisplayRGB24(m_hCamera, m_ImageBuffer, ref FrameHead);
                    //成功调用CameraGetImageBuffer后必须释放,下次才能继续调用CameraGetImageBuffer捕获图像。
                    MvApi.CameraReleaseImageBuffer(m_hCamera, uRawBuffer);

                    if (FrameHead.iWidth != m_tFrameHead.iWidth || FrameHead.iHeight != m_tFrameHead.iHeight)
                    {
                        m_bEraseBk   = true;
                        m_tFrameHead = FrameHead;
                    }
                    m_iDisplayedFrames++;
                }
            }
        }
Ejemplo n.º 2
0
 public void UpdateImage(ref tSdkFrameHead tFrameHead, IntPtr pRgbBuffer)
 {
     SnapshotBox.Width  = tFrameHead.iWidth;
     SnapshotBox.Height = tFrameHead.iHeight;
     SnapshotBox.Image  = MvApi.CSharpImageFromFrame(pRgbBuffer, ref tFrameHead);
     panel1.AutoScroll  = true;
 }
Ejemplo n.º 3
0
        private void CameraGrabberFrameCallback(IntPtr Grabber, IntPtr pFrameBuffer, ref tSdkFrameHead pFrameHead, IntPtr Context)
        {
            HObject hv_Image = null;

            GC.Collect();//强制进行即时垃圾回收,防止内存溢出

            int w = pFrameHead.iWidth;
            int h = pFrameHead.iHeight;

            //判断格式是否为黑白类型
            Boolean gray = (pFrameHead.uiMediaType ==
                            (uint)MVSDK.emImageFormat.CAMERA_MEDIA_TYPE_MONO8);

            Bitmap Image = new Bitmap(w, h,
                                      gray ? w : w * 3,
                                      gray ? PixelFormat.Format8bppIndexed : PixelFormat.Format24bppRgb,
                                      pFrameBuffer);

            if (gray)
            {
                Image.Palette = m_GrayPal;
            }
            HOperatorSet.GenImageInterleaved(out hv_Image, new HTuple(pFrameBuffer), "bgr", w, h, new HTuple(-1), "byte", pFrameHead.iWidth, pFrameHead.iHeight, 0, 0, -1, 0);
            //HDevelopExport(hv_Image);

            _image.Add(hv_Image);

            hv_Image.Dispose();
        }
Ejemplo n.º 4
0
        private void CameraGrabberFrameCallback(IntPtr Grabber, IntPtr pFrameBuffer, ref tSdkFrameHead pFrameHead, IntPtr Context)
        {
            int    w         = pFrameHead.iWidth;
            int    h         = pFrameHead.iHeight;
            bool   gray      = (pFrameHead.uiMediaType == (uint)emImageFormat.CAMERA_MEDIA_TYPE_MONO8);
            Bitmap ImageDisp = new Bitmap(w, h,
                                          gray ? w : w * 3,
                                          gray ? PixelFormat.Format8bppIndexed : PixelFormat.Format24bppRgb,
                                          pFrameBuffer);

            // 如果是灰度图要设置调色板
            if (gray)
            {
                ImageDisp.Palette = m_GrayPal;
            }

            using (var stream = new MemoryStream())
            {
                ImageDisp.Save(stream, ImageFormat.Bmp);
                App.Current.Dispatcher.InvokeAsync(new Action(() =>
                {
                    SetPreviewImage(stream);
                }));
            }

            if (DetectFlag)
            {
                Image.Bitmap = ImageDisp;
                ImageQueue.Enqueue(Image);
                CapCount++;
            }
        }
Ejemplo n.º 5
0
        private void CameraGrabberFrameCallback(
            IntPtr Grabber,
            IntPtr pFrameBuffer,
            ref tSdkFrameHead pFrameHead,
            IntPtr Context)
        {
            // 数据处理回调

            // 由于黑白相机在相机打开后设置了ISP输出灰度图像
            // 因此此处pFrameBuffer=8位灰度数据
            // 否则会和彩色相机一样输出BGR24数据

            // 彩色相机ISP默认会输出BGR24图像
            // pFrameBuffer=BGR24数据
            int w = pFrameHead.iWidth;
            int h = pFrameHead.iHeight;

            HObject Image = null;

            try
            {
                if (pFrameHead.uiMediaType == (uint)MVSDK.emImageFormat.CAMERA_MEDIA_TYPE_MONO8)
                {
                    HOperatorSet.GenImage1(out Image, "byte", w, h, pFrameBuffer);
                }
                else if (pFrameHead.uiMediaType == (uint)MVSDK.emImageFormat.CAMERA_MEDIA_TYPE_BGR8)
                {
                    HOperatorSet.GenImageInterleaved(out Image,
                                                     pFrameBuffer,
                                                     "bgr",
                                                     w, h,
                                                     -1, "byte",
                                                     w, h,
                                                     0, 0, -1, 0);
                }
                else
                {
                    throw new HalconException("Image format is not supported!!");
                }

                HObject ImageRaw = Image;
                HOperatorSet.MirrorImage(ImageRaw, out Image, "row");
                ImageRaw.Dispose();

                m_HDevelopExport.action(Image);
            }
            catch (HalconException Exc)
            {
            }
            finally
            {
                if (Image != null)
                {
                    Image.Dispose();
                }
            }
        }
Ejemplo n.º 6
0
 private void CameraGrabberFrameCallback3(
     IntPtr Grabber,
     IntPtr pFrameBuffer,
     ref tSdkFrameHead pFrameHead,
     IntPtr Context)
 {
     if (m_bRecording)
     {
         MvApi.CameraPushFrame(m_hCamera[3], pFrameBuffer, ref pFrameHead);
     }
 }
Ejemplo n.º 7
0
        private void CameraGrabberFrameCallback(
            IntPtr Grabber,
            IntPtr pFrameBuffer,
            ref tSdkFrameHead pFrameHead,
            IntPtr Context)
        {
            // 数据处理回调

            // 由于黑白相机在相机打开后设置了ISP输出灰度图像
            // 因此此处pFrameBuffer=8位灰度数据
            // 否则会和彩色相机一样输出BGR24数据

            // 彩色相机ISP默认会输出BGR24图像
            // pFrameBuffer=BGR24数据
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 迈德威视相机采集到图像回调处理函数
        /// </summary>
        /// <param name="Grabber"></param>
        /// <param name="pFrameBuffer"></param>
        /// <param name="pFrameHead"></param>
        /// <param name="Context"></param>
        private void OnSDKImageGrabbed(IntPtr Grabber, IntPtr pFrameBuffer, ref tSdkFrameHead pFrameHead, IntPtr Context)
        {
            if (HoImage != null &&
                HoImage.IsInitialized())
            {
                HoImage = null;
                //System.Threading.Thread.Sleep(1);
                //GC.Collect();
                //GC.WaitForPendingFinalizers();

                IsImageGrabbed = false;
            }

            #region 迈德威视相机SDK内部像素格式转换
            // 由于黑白相机在相机打开后设置了ISP输出灰度图像
            // 因此此处pFrameBuffer=8位灰度数据
            // 否则会和彩色相机一样输出BGR24数据

            // 彩色相机ISP默认会输出BGR24图像
            // pFrameBuffer=BGR24数据

            int w = pFrameHead.iWidth;
            int h = pFrameHead.iHeight;

            if (pFrameHead.uiMediaType == (uint)MVSDK.emImageFormat.CAMERA_MEDIA_TYPE_MONO8)
            {
                HalconDotNet.HOperatorSet.GenImage1(out HoImage, "byte", w, h, pFrameBuffer);
            }
            else if (pFrameHead.uiMediaType == (uint)MVSDK.emImageFormat.CAMERA_MEDIA_TYPE_BGR8)
            {
                HalconDotNet.HOperatorSet.GenImageInterleaved(out HoImage, pFrameBuffer, "bgr", w, h, -1, "byte", w, h, 0, 0, -1, 0);
            }

            MvApi.CameraClearBuffer(_deviceRef);

            #endregion

            if (HoImage != null &&
                HoImage.IsInitialized())
            {
                System.Threading.Thread.Sleep(1);
                IsImageGrabbed = true;
                if (CameraImageGrabbedEvt != null)
                {
                    CameraImageGrabbedEvt(Camera, HoImage);
                }
            }
        }
Ejemplo n.º 9
0
        private void CameraGrabberFrameCallback(
            IntPtr Grabber,
            IntPtr pFrameBuffer,
            ref tSdkFrameHead pFrameHead,
            IntPtr Context)
        {
            HTuple Height, Width;

            //hv_image.Dispose();
            GC.Collect();//强制进行即时垃圾回收,防止内存溢出

            int w = pFrameHead.iWidth;
            int h = pFrameHead.iHeight;

            //判断格式是否为黑白类型
            Boolean gray = (pFrameHead.uiMediaType ==
                            (uint)MVSDK.emImageFormat.CAMERA_MEDIA_TYPE_MONO8);

            Bitmap Image = new Bitmap(w, h,
                                      gray ? w : w * 3,
                                      gray ? PixelFormat.Format8bppIndexed : PixelFormat.Format24bppRgb,
                                      pFrameBuffer);

            if (gray)
            {
                Image.Palette = m_GrayPal;
            }
            //hv_image = BitmapToHImage(Image);
            HOperatorSet.GenImageInterleaved(out hv_image, new HTuple(pFrameBuffer), "bgr", w, h, new HTuple(-1), "byte", w, h, 0, 0, -1, 0);

            HOperatorSet.GetImageSize(hv_image, out Width, out Height);

            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, Height, Width);
            }


            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.DispObj(hv_image, HDevWindowStack.GetActive());
            }
            //this.Invoke((EventHandler)delegate
            //{
            //    pictureBox_CalibrationCamera.Image = Image;
            //    pictureBox_CalibrationCamera.Refresh();
            //});
        }
Ejemplo n.º 10
0
        public void UpdateImage(ref tSdkFrameHead tFrameHead, IntPtr pRgbBuffer)
        {
            BITMAPINFOHEADER bmi;
            BITMAPFILEHEADER bmfi;

            bmfi.bfType      = ((int)'M' << 8) | ((int)'B');
            bmfi.bfOffBits   = 54;
            bmfi.bfSize      = (uint)(54 + tFrameHead.iWidth * tFrameHead.iHeight * 3);
            bmfi.bfReserved1 = 0;
            bmfi.bfReserved2 = 0;

            bmi.biBitCount      = 24;
            bmi.biClrImportant  = 0;
            bmi.biClrUsed       = 0;
            bmi.biCompression   = 0;
            bmi.biPlanes        = 1;
            bmi.biSize          = 40;
            bmi.biHeight        = tFrameHead.iHeight;
            bmi.biWidth         = tFrameHead.iWidth;
            bmi.biXPelsPerMeter = 0;
            bmi.biYPelsPerMeter = 0;
            bmi.biSizeImage     = 0;

            MemoryStream stream = new MemoryStream();
            BinaryWriter bw     = new BinaryWriter(stream);

            byte[] data = new byte[14];
            IntPtr ptr  = Marshal.AllocHGlobal(54);

            Marshal.StructureToPtr((object)bmfi, ptr, false);
            Marshal.Copy(ptr, data, 0, data.Length);
            bw.Write(data);
            data = new byte[40];
            Marshal.StructureToPtr((object)bmi, ptr, false);
            Marshal.Copy(ptr, data, 0, data.Length);
            bw.Write(data);
            data = new byte[tFrameHead.iWidth * tFrameHead.iHeight * 3];
            Marshal.Copy(pRgbBuffer, data, 0, data.Length);
            bw.Write(data);
            Marshal.FreeHGlobal(ptr);


            SnapshotBox.Width  = tFrameHead.iWidth;
            SnapshotBox.Height = tFrameHead.iHeight;
            SnapshotBox.Image  = Image.FromStream(stream);
            panel1.AutoScroll  = true;
            //SnapshotBox.Layout =
        }
Ejemplo n.º 11
0
        private void CameraGrabberFrameCallback0(
            IntPtr Grabber,
            IntPtr pFrameBuffer,
            ref tSdkFrameHead pFrameHead,
            IntPtr Context)
        {
            MIplImage IpImg = (MIplImage)Marshal.PtrToStructure(pFrameBuffer, typeof(MIplImage));
            //Mat mat = MIpImg.
            Mat mat = CvInvoke.CvArrToMat(IpImg.ImageData);

            captureImageBox.Image = mat;


            if (m_bRecording)
            {
                MvApi.CameraPushFrame(m_hCamera[0], pFrameBuffer, ref pFrameHead);
            }
        }
Ejemplo n.º 12
0
        public void ImageCaptureCallback(CameraHandle hCamera, IntPtr pFrameBuffer, ref tSdkFrameHead pFrameHead, IntPtr pContext)
        {
            //图像处理,将原始输出转换为RGB格式的位图数据,同时叠加白平衡、饱和度、LUT等ISP处理。
            MvApi.CameraImageProcess(hCamera, pFrameBuffer, m_ImageBuffer, ref pFrameHead);
            //叠加十字线、自动曝光窗口、白平衡窗口信息(仅叠加设置为可见状态的)。
            MvApi.CameraImageOverlay(hCamera, m_ImageBuffer, ref pFrameHead);
            //调用SDK封装好的接口,显示预览图像
            MvApi.CameraDisplayRGB24(hCamera, m_ImageBuffer, ref pFrameHead);
            m_tFrameHead = pFrameHead;
            m_iDisplayedFrames++;

            if (pFrameHead.iWidth != m_tFrameHead.iWidth || pFrameHead.iHeight != m_tFrameHead.iHeight)
            {
                timer2.Enabled = true;
                timer2.Start();
                m_tFrameHead = pFrameHead;
            }
        }
Ejemplo n.º 13
0
        private void CameraGrabberFrameCallback(
            IntPtr Grabber,
            IntPtr pFrameBuffer,
            ref tSdkFrameHead pFrameHead,
            IntPtr Context)
        {
            // 数据处理回调

            // 由于黑白相机在相机打开后设置了ISP输出灰度图像
            // 因此此处pFrameBuffer=8位灰度数据
            // 否则会和彩色相机一样输出BGR24数据

            // 彩色相机ISP默认会输出BGR24图像
            // pFrameBuffer=BGR24数据
            if (m_bRecording)
            {
                MvApi.CameraPushFrame(m_hCamera, pFrameBuffer, ref pFrameHead);
            }
        }
Ejemplo n.º 14
0
        public void CaptureThreadProc()
        {
            CameraSdkStatus eStatus;
            tSdkFrameHead   FrameHead;
            IntPtr          uRawBuffer;//rawbuffer由SDK内部申请。应用层不要调用delete之类的释放函数

            while (m_bExitCaptureThread == false)
            {
                //500毫秒超时,图像没捕获到前,线程会被挂起,释放CPU,所以该线程中无需调用sleep
                eStatus = MvApi.CameraGetImageBuffer(m_hCamera, out FrameHead, out uRawBuffer, 500);

                if (eStatus == CameraSdkStatus.CAMERA_STATUS_SUCCESS)//如果是触发模式,则有可能超时
                {
                    //图像处理,将原始输出转换为RGB格式的位图数据,同时叠加白平衡、饱和度、LUT等ISP处理。
                    MvApi.CameraImageProcess(m_hCamera, uRawBuffer, m_ImageBuffer, ref FrameHead);
                    //叠加十字线、自动曝光窗口、白平衡窗口信息(仅叠加设置为可见状态的)。
                    MvApi.CameraImageOverlay(m_hCamera, m_ImageBuffer, ref FrameHead);
                    //调用SDK封装好的接口,显示预览图像
                    MvApi.CameraDisplayRGB24(m_hCamera, m_ImageBuffer, ref FrameHead);
                    //成功调用CameraGetImageBuffer后必须释放,下次才能继续调用CameraGetImageBuffer捕获图像。
                    MvApi.CameraReleaseImageBuffer(m_hCamera, uRawBuffer);

                    if (FrameHead.iWidth != m_tFrameHead.iWidth || FrameHead.iHeight != m_tFrameHead.iHeight)
                    {
                        m_bEraseBk   = true;
                        m_tFrameHead = FrameHead;
                    }

                    m_iDisplayedFrames++;

                    if (m_bSaveImage)
                    {
                        string file_path;
                        file_path = "c:\\test.bmp";
                        byte[] file_path_bytes = Encoding.Default.GetBytes(file_path);
                        MvApi.CameraSaveImage(m_hCamera, file_path_bytes, m_ImageBuffer, ref FrameHead, emSdkFileType.FILE_BMP, 100);
                        m_bSaveImage = false;
                    }
                }
            }
        }
Ejemplo n.º 15
0
        public void ImageCaptureCallback(CameraHandle hCamera, IntPtr pFrameBuffer, ref tSdkFrameHead pFrameHead, IntPtr pContext)
        {
            MvApi.CameraImageProcess(hCamera, pFrameBuffer, m_ImageBuffer, ref pFrameHead);
            int bytewidth  = (pFrameHead.iWidth * 3 + 3) / 4 * 4; //要设置的RGB图 的宽度  保证其是4的倍数
            int bytewidthg = (pFrameHead.iWidth + 3) / 4 * 4;     //要设置分量图

            //实例化申请内存空间
            byte[] m_pImageData  = new byte[bytewidth * pFrameHead.iHeight];
            byte[] m_pImageDataR = new byte[pFrameHead.iWidth * pFrameHead.iHeight];
            byte[] m_pImageDataG = new byte[pFrameHead.iWidth * pFrameHead.iHeight];
            byte[] m_pImageDataB = new byte[pFrameHead.iWidth * pFrameHead.iHeight];
            //复制一张包含RGB的图像缓存(将RGB图像转换成Byte图像缓存)  RGB图像缓存,图像byte的图像类型,相机的索引,图像的字节数大小
            Marshal.Copy(m_ImageBuffer, m_pImageData, 0, pFrameHead.iWidth * pFrameHead.iHeight * 3);
            for (int j = 0; j < pFrameHead.iHeight; j++)
            {
                for (int i = 0; i < pFrameHead.iWidth; i++)
                {
                    //将图像的RGB分量 分别分给R分量图,G分量图,B分量图
                    m_pImageDataB[j * bytewidthg + i] = m_pImageData[j * bytewidth + i * 3 + 0];
                    m_pImageDataG[j * bytewidthg + i] = m_pImageData[j * bytewidth + i * 3 + 1];
                    m_pImageDataR[j * bytewidthg + i] = m_pImageData[j * bytewidth + i * 3 + 2];

                    // m_pImageDatagray[j * bytewidthg + i] = (byte)(0.11 * m_pImageData[j * bytewidth + i * 3 + 0] + 0.59 * m_pImageData[j * bytewidth + i * 3 + 1] + 0.30 * m_pImageData[j * bytewidth + i * 3 + 2]);
                }
            }
            unsafe
            {
                fixed(byte *pR = m_pImageDataR, pB = m_pImageDataB, pG = m_pImageDataG)
                {
                    HOperatorSet.GenImage3(out halcon_image, "byte", pFrameHead.iWidth, pFrameHead.iHeight, new IntPtr(pR), new IntPtr(pB), new IntPtr(pG));
                }
            }
            //获得图像宽高,然后设置显示图像的宽高
            HOperatorSet.GetImageSize(halcon_image, out hv_Width, out hv_Height);
            HOperatorSet.SetPart(hv_WindowHandle, 0, 0, hv_Height, hv_Width);
            //如果图像反向可以用halcon镜像图像的函数 将图像镜像反转一下
            //HOperatorSet.MirrorImage(halcon_image, out halcon_image, "row");
            HOperatorSet.DispObj(halcon_image, hv_WindowHandle);
            halcon_image.Dispose();
        }
Ejemplo n.º 16
0
        private void CameraGrabberFrameCallback(
            IntPtr Grabber,
            IntPtr pFrameBuffer,
            ref tSdkFrameHead pFrameHead,
            IntPtr Context)
        {
            // 数据处理回调

            // 由于黑白相机在相机打开后设置了ISP输出灰度图像
            // 因此此处pFrameBuffer=8位灰度数据
            // 否则会和彩色相机一样输出BGR24数据

            // 彩色相机ISP默认会输出BGR24图像
            // pFrameBuffer=BGR24数据

            // 执行一次GC,释放出内存
            GC.Collect();

            int     w     = pFrameHead.iWidth;
            int     h     = pFrameHead.iHeight;
            Boolean gray  = (pFrameHead.uiMediaType == (uint)MVSDK.emImageFormat.CAMERA_MEDIA_TYPE_MONO8);
            Bitmap  Image = new Bitmap(w, h,
                                       gray ? w : w * 3,
                                       gray ? PixelFormat.Format8bppIndexed : PixelFormat.Format24bppRgb,
                                       pFrameBuffer);

            // 如果是灰度图要设置调色板
            if (gray)
            {
                Image.Palette = m_GrayPal;
            }

            this.Invoke((EventHandler) delegate
            {
                DispWnd.Image = Image;
                DispWnd.Refresh();
            });
        }
Ejemplo n.º 17
0
        private int CameraGrabberFrameListener(IntPtr Grabber, int Phase, IntPtr pFrameBuffer, ref tSdkFrameHead pFrameHead, IntPtr Context)
        {
            if (Phase == 0)
            {
                // RAW数据处理,pFrameBuffer=Raw数据
            }
            else if (Phase == 1)
            {
                // 截图前处理,pFrameBuffer=RGB数据
            }
            else if (Phase == 2)
            {
                // 显示前处理,pFrameBuffer=RGB数据

                try
                {
                    if (ignoreImage)
                    {
                        return(1);
                    }
                    //HTuple startTime;
                    HOperatorSet.CountSeconds(out startTime);
                    // Reduce the number of displayed images to a reasonable amount if the camera is acquiring images very fast.
                    //if (!stopWatch.IsRunning || stopWatch.ElapsedMilliseconds > 33)
                    {
                        //stopWatch.Restart();

                        //if (hPylonImage != null && hPylonImage.IsInitialized())
                        //{
                        //    hPylonImage.Dispose();
                        //}
                        hPylonImage = new HImage();
                        emImageFormat currentFormat = (emImageFormat)pFrameHead.uiMediaType;
                        if (currentFormat == emImageFormat.CAMERA_MEDIA_TYPE_MONO8)
                        {
                            hPylonImage.GenImage1("byte", pFrameHead.iWidth, pFrameHead.iHeight, pFrameBuffer);
                        }
                        else if (currentFormat == emImageFormat.CAMERA_MEDIA_TYPE_RGB8)
                        {
                            hPylonImage.GenImageInterleaved(pFrameBuffer,
                                                            "rgb",
                                                            pFrameHead.iWidth, pFrameHead.iHeight,
                                                            -1, "byte",
                                                            pFrameHead.iWidth, pFrameHead.iHeight,
                                                            0, 0, -1, 0);
                        }
                        else
                        {
                            Util.Notify(string.Format("相机{0}编码格式不正确,当前格式{1}", cameraIndex, pFrameHead.uiMediaType));
                        }
                        TrigerImageEvent();
                    }
                }
                catch (System.ArgumentException ex)
                {
                    Util.WriteLog(this.GetType(), ex);
                    Util.Notify(string.Format("相机{0}图像数据包丢失", cameraIndex));
                }
                catch (Exception ex)
                {
                    Util.WriteLog(this.GetType(), ex);
                    Util.Notify(string.Format("相机{0}图像数据返回出现异常", cameraIndex));
                }
            }

            return(1);
        }