示例#1
0
        static int Main(string[] args)
        {
            // print usage
            Console.WriteLine("Usage: dotnet csharp_demo.dll [OPTIONS]");
            Console.WriteLine("Examples:");
            Console.WriteLine("    To specify license directory (for online license for example): dotnet csharp_demo.dll --license_dir license_dir");
            Console.WriteLine("");

            // parse arguments
            bool    error   = false;
            Options options = new Options();

            CommandLine.Parser.Default.ParseArguments <Options>(args)
            .WithParsed <Options>(opts => options  = opts)
            .WithNotParsed <Options>(errs => error = true);

            // exit if argument parsign error
            if (error)
            {
                return(1);
            }

            // print values of arguments
            Console.WriteLine("Arguments:");
            foreach (var opt in options.GetType().GetProperties())
            {
                Console.WriteLine("--{0} = {1}", opt.Name, opt.GetValue(options, null));
            }
            Console.WriteLine();

            try
            {
                OpenCvSharp.Window window = new OpenCvSharp.Window("demo");
                // create worker
                // (we are expecting run from bin folder)
                Worker worker = new Worker(
                    options.config_dir,
                    options.capturer_config,
                    options.license_dir);

                // here we trying camera open with opencv
                const int desiredFrameWidht  = 1280;
                const int desiredFrameHeight = 720;

                int camera_id = 0;

                // VideoCapture camera;
                OpenCvSharp.VideoCapture camera = new OpenCvSharp.VideoCapture();


                for (; camera_id < 10; ++camera_id)
                {
                    camera.Open(camera_id);
                    camera.Set(OpenCvSharp.CaptureProperty.FrameWidth, desiredFrameWidht);
                    camera.Set(OpenCvSharp.CaptureProperty.FrameHeight, desiredFrameHeight);
                    OpenCvSharp.Mat frame = new OpenCvSharp.Mat();
                    for (int i = 0; i < 10; i++)
                    {
                        frame = camera.RetrieveMat();
                    }

                    if (frame.Empty())
                    {
                        Console.WriteLine("webcam " + camera_id + " not opened");
                        camera.Release();
                    }
                    else
                    {
                        break;
                    }
                }

                for (;;)
                {
                    OpenCvSharp.Mat frame;

                    //camera.Grab();
                    frame = camera.RetrieveMat();

                    if (frame.Empty())
                    {
                        break;
                    }

                    // give a frame to the worker
                    worker.work(frame);

                    if (27 == (char)OpenCvSharp.Cv2.WaitKey(10))
                    {
                        camera.Release();
                        break;
                    }
                }

                // force free resources
                // otherwise licence error may occur
                // when create sdk object in next time
                worker.dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine("! exception catched: '" + e + "' ... exiting");
                return(1);
            }
            Console.WriteLine("close program");
            return(0);
        }
示例#2
0
        static void Main(string[] args)
        {
            var wb = OpenCvSharp.XPhoto.SimpleWB.Create();

            wb.P         = 0.2f;
            wb.OutputMin = 8.0f;

            var decodedFramesBlock = new TransformBlock <OpenCvSharp.Mat, OpenCvSharp.Mat>(
                mat =>
            {
                return(Task.Run(() => { wb.BalanceWhite(mat, mat); return mat; }));
            },
                new ExecutionDataflowBlockOptions
            {
                BoundedCapacity        = 10,
                MaxDegreeOfParallelism = 10,
                EnsureOrdered          = true,
            }
                );

            // var inputVideo = new OpenCvSharp.VideoCapture("2017_0928_105336_023.MOV");
            var inputVideo  = new OpenCvSharp.VideoCapture("YI004801.mp4");
            var outputVideo = new OpenCvSharp.VideoWriter("out_lina.mp4", "avc1", inputVideo.Fps, new OpenCvSharp.Size(inputVideo.FrameWidth, inputVideo.FrameHeight));

            ConcurrentQueue <OpenCvSharp.Mat> decodedFramesQueue = new ConcurrentQueue <OpenCvSharp.Mat>();

            // decoder
            Task decodeTask = Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    var mat = inputVideo.RetrieveMat();
                    if (mat.Height == 0)
                    {
                        Console.WriteLine("DECODED");
                        decodedFramesBlock.Complete();
                        break;
                    }
                    decodedFramesBlock.SendAsync(mat).Wait();
                }
            });

            Task writeTask = Task.Factory.StartNew(() =>
            {
                int c = 0;
                OpenCvSharp.Mat mat;
                while (!decodedFramesBlock.Completion.IsCompleted)
                {
                    ++c;
                    mat = decodedFramesBlock.Receive();
                    outputVideo.Write(mat);
                    mat.Dispose();
                    Console.WriteLine("Frame " + c + " of " + inputVideo.FrameCount);
                }
            });

            decodeTask.Wait();
            writeTask.Wait();

            Console.WriteLine("ready!");
            outputVideo.Release();

            /*
             * var channels = img.Split();
             * channels = channels.Select(c => c.EqualizeHist()).ToArray();
             * foreach(var channel in channels)
             * {
             *  channel.ConvertTo(channel, channel.Type(), 0.9, 0);
             * }
             * OpenCvSharp.Cv2.Merge(channels, img);
             */
        }