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
        public override void ImShow(string name, Mat m)
        {
            if (ImShowTarget != null)
            {
                CvProfiler.Start("imshow");
                lock (imShowLocker)
                {
                    if (imShowBitmap == null)
                    {
                        imShowBitmap = Bitmap.CreateBitmap(m.Width, m.Height, Bitmap.Config.Argb8888);
                    }
                    else if (imShowBitmap.Width != m.Width && imShowBitmap.Height != m.Height)
                    {
                        //imShowBitmap.Recycle();
                        //imShowBitmap.Dispose();
                        imShowBitmap = Bitmap.CreateBitmap(m.Width, m.Height, Bitmap.Config.Argb8888);
                    }

                    using (Mat mat = new Mat())
                    {
                        Cv2.CvtColor(m, mat, ColorConversionCodes.BGR2RGBA);

                        var bufLen = mat.Channel * mat.Total();
                        if (imShowBuffer == null || imShowBuffer.Length != bufLen)
                        {
                            imShowBuffer = new byte[bufLen];
                        }
                        mat.GetArray(0, 0, imShowBuffer);

                        using (var raw = ByteBuffer.Wrap(imShowBuffer))
                        {
                            imShowBitmap.CopyPixelsFromBuffer(raw);
                        }

                        MainActivity.RunOnUiThread(() =>
                        {
                            ImShowTarget.SetImageBitmap(imShowBitmap);
                        });
                    }
                }
                CvProfiler.End("imshow");
            }
        }
Ejemplo n.º 3
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;
            }
        }