Ejemplo n.º 1
0
        private void Callback_PreviewUpdated(object sender, PreviewUpdatedEventArgs e)
        {
            CvProfiler.End("Captured");
            CvProfiler.Start("Captured");
            CvProfiler.Count("CapturedFPS");

            if (FrameReady == null)
            {
                return;
            }

            frameCount++;
            if (MultiThread)
            {
                if (e.Buffer != null && LimitedTaskScheduler.QueuedTaskCount < LimitedTaskScheduler.MaxTaskCount)
                {
                    LimitedTaskScheduler.Factory.StartNew(() => CaptureCvtProc(e.Buffer, frameCount, LimitedTaskScheduler.QueuedTaskCount));
                }
            }
            else
            {
                CaptureCvtProc(e.Buffer, 0, 0);
            }

            CvProfiler.Capture("TaskCount", LimitedTaskScheduler.QueuedTaskCount);
        }
Ejemplo n.º 2
0
        private void CaptureCvtProc(byte[] Buffer, long frameIndex, int threadindex)
        {
            CvProfiler.Start("CaptureCvt" + threadindex);
            Mat mat = null;

            CvProfiler.Start("CaptureCvt.CvtColor" + threadindex);
            switch (cameraType)
            {
            case Graphics.ImageFormatType.Nv16:
                mat = new Mat((int)Math.Round(height * 1.5), width, MatType.CV_8UC1, Buffer);
                Cv2.CvtColor(mat, mat, ColorConversionCodes.YUV2BGR_NV12);
                break;

            case Graphics.ImageFormatType.Nv21:
                mat = new Mat((int)Math.Round(height * 1.5), width, MatType.CV_8UC1, Buffer);
                Cv2.CvtColor(mat, mat, ColorConversionCodes.YUV2BGR_NV21);
                break;

            case Graphics.ImageFormatType.Rgb565:
                mat = new Mat(width, height, MatType.CV_16UC1, Buffer);
                Cv2.CvtColor(mat, mat, ColorConversionCodes.BGR5652BGR);
                break;

            case Graphics.ImageFormatType.Yuv420888:
            default:
                throw new NotImplementedException("Unknown Camera Format");
            }
            CvProfiler.End("CaptureCvt.CvtColor" + threadindex);

            CvProfiler.Start("CaptureCvt.Tp" + threadindex);
            Cv2.Transpose(mat, mat);
            CvProfiler.End("CaptureCvt.Tp" + threadindex);

            CvProfiler.Start("CaptureCvt.Flip" + threadindex);
            if (cameraIndex == 1)
            {
                Cv2.Flip(mat, mat, FlipMode.XY);
            }
            else
            {
                Cv2.Flip(mat, mat, FlipMode.Y);
            }
            CvProfiler.End("CaptureCvt.Flip" + threadindex);

            CvProfiler.End("CaptureCvt" + threadindex);
            capturedBuffer = mat;

            var k    = Cv2.WaitKey(1);
            var args = new FrameArgs(mat, (char)k);

            if (MultiThread)
            {
                lock (capturedBufferLocker)
                {
                    if (lastFrame > frameIndex)
                    {
                        if (mat != null)
                        {
                            mat.Dispose();
                        }
                        mat = null;
                        CvProfiler.Count("CaptureSkipped");
                        return;
                    }

                    lastFrame = frameIndex;
                    FrameReady?.Invoke(this, args);
                }
            }
            else
            {
                FrameReady?.Invoke(this, args);
            }

            if (args.MatDispose)
            {
                mat.Release();
                mat.Dispose();
                mat = null;
            }

            if (args.Break)
            {
                Dispose();
                Stop();
                return;
            }
        }