ShowImage() публичный Метод

Shows the image in this window
public ShowImage ( OpenCvSharp.Mat img ) : void
img OpenCvSharp.Mat Image to be shown.
Результат void
Пример #1
6
        public void Run()
        {
            var capture = new VideoCapture();
            capture.Set(CaptureProperty.FrameWidth, 640);
            capture.Set(CaptureProperty.FrameHeight, 480);
            capture.Open(-1);
            if (!capture.IsOpened())
                throw new Exception("capture initialization failed");

            var fs = FrameSource.CreateCameraSource(-1);
            var sr = SuperResolution.CreateBTVL1();
            sr.SetInput(fs);

            using (var normalWindow = new Window("normal"))
            using (var srWindow = new Window("super resolution"))
            {
                var normalFrame = new Mat();
                var srFrame = new Mat();
                while (true)
                {
                    capture.Read(normalFrame);
                    sr.NextFrame(srFrame);
                    if (normalFrame.Empty() || srFrame.Empty())
                        break;
                    normalWindow.ShowImage(normalFrame);
                    srWindow.ShowImage(srFrame);
                    Cv2.WaitKey(100);
                }
            }
        }
Пример #2
0
        private void trackbarCallback(int pos, object data)
        {
            if (pos == 0)
            {
                return;
            }
            double factor = (double)pos / 100;

            srcImage = baseImage.Resize(new Size(0, 0), factor, factor);
            CroppingWindow.ShowImage(srcImage);
        }
Пример #3
0
        private void ShowFrame(ISourceable source)
        {
            FrameNumber = 0;

            if (source.HaveNewFrame())
            {
                mFrame = source.GetFrame();
                FrameNumber++;
                mWin.ShowImage(mFrame);
                Cv2.WaitKey(33);
            }
        }
        public MainWindow()
        {
            InitializeComponent();

            using var capture = new VideoCapture(0, VideoCaptureAPIs.DSHOW);
            if (!capture.IsOpened())
            {
                return;
            }

            capture.FrameWidth  = 1920;
            capture.FrameHeight = 1280;
            capture.AutoFocus   = true;

            const int sleepTime = 10;

            using var window = new OpenCvSharp.Window("capture");
            var image = new Mat();

            while (true)
            {
                capture.Read(image);
                if (image.Empty())
                {
                    break;
                }

                window.ShowImage(image);
                int c = Cv2.WaitKey(sleepTime);
                if (c >= 0)
                {
                    break;
                }
            }
        }
Пример #5
0
    public Task StartTakePicture(int intervalMSec, CancellationTokenSource cancelTokenSource)
    {
        if (string.IsNullOrEmpty(photoFileNamePrefix))
        {
            var css    = ConnectionString.Split(";");
            var devdef = css[1].Split("=");
            photoFileNamePrefix = devdef[1];
        }
        lock (this) {
            takePhotoIntervalMSec = intervalMSec;
        }
        var tmpFileName     = photoFileName + photoFileNameExt;
        var photoTakingTask = Task.Factory.StartNew(async() => {
            var capture = OpenCvSharp.VideoCapture.FromCamera(0);
            using (var win = new OpenCvSharp.Window())
                using (var mat = new OpenCvSharp.Mat()) {
                    while (true)
                    {
                        capture.Read(mat);
                        win.ShowImage(mat);
                        var now = DateTime.Now;
                        if (File.Exists(tmpFileName))
                        {
                            File.Delete(tmpFileName);
                        }
                        using (var fs = new FileStream(tmpFileName, FileMode.CreateNew)) {
                            mat.WriteToStream(fs, photoFileNameExt, null);
                        }
                        var fileName = photoFileNamePrefix + now.ToString("yyyyMMddHHmmss") + photoFileNameExt;
                        await UploadFile(fileName, new FileInfo(tmpFileName).FullName);
                        var interval = 0;
                        lock (this) {
                            interval = takePhotoIntervalMSec;
                        }
                        await Task.Delay(interval);
                        if (cancelTokenSource.IsCancellationRequested)
                        {
                            break;
                        }
                    }
                    throw new OperationCanceledException(cancelTokenSource.Token);
                }
        }, cancelTokenSource.Token);

        return(photoTakingTask);
    }
 private void Button_Click(object sender, RoutedEventArgs e)
 {
     using (var capture = new VideoCapture(0))
         using (var win = new OpenCvSharp.Window("capture"))
             using (var mat = new Mat())
             {
                 capture.Set(CaptureProperty.FrameWidth, 640);
                 capture.Set(CaptureProperty.FrameHeight, 360);
                 while (true)
                 {
                     capture.Read(mat);
                     win.ShowImage(mat);
                     if (Cv2.WaitKey(30) >= 0)
                     {
                         break;
                     }
                 }
             }
 }
Пример #7
0
        public void Run()
        {
            // Opens MP4 file (ffmpeg is probably needed)
            var capture = new VideoCapture(FilePath.Movie.Bach);

            int sleepTime = (int)Math.Round(1000 / capture.Fps);

            using (var window = new Window("capture"))
            {
                // Frame image buffer
                Mat image = new Mat();

                // When the movie playback reaches end, Mat.data becomes NULL.
                while (true)
                {
                    capture.Read(image); // same as cvQueryFrame
                    if(image.Empty())
                        break;

                    window.ShowImage(image);
                    Cv2.WaitKey(sleepTime);
                } 
            }
        }
Пример #8
0
        public static int Main(string[] args)
        {
            // Parse input arguments
            string videoPath, facePath, studyPath, output;
            int    parseResult = ParseArgs(args, out videoPath, out facePath, out studyPath, out output);

            if (parseResult != 2)
            {
                return(parseResult);
            }

            // Create a Factory object
            var factory = new Dfx.Sdk.Factory();

            Console.WriteLine($"Created DFX Factory: {factory.Version}");

            // Initialize a study
            if (!factory.InitializeStudyFromFile(studyPath))
            {
                Console.WriteLine($"DFX study initialization failed: {factory.LastErrorMessage}");
                return(1);
            }
            Console.WriteLine($"Created study from {studyPath}");

            // Create a collector
            var collector = factory.CreateCollector();

            if (collector.CurrentState == Dfx.Sdk.Collector.State.ERROR)
            {
                Console.WriteLine($"Collector creation failed: {collector.LastErrorMessage}");
                Console.ReadKey();
                return(1);
            }
            Console.WriteLine("Created collector");

            // Load the face tracking data
            var jsonFaces = LoadJsonFaces(facePath);

            // Load video file (or stream of images)
            var videocap      = Cv.VideoCapture.FromFile(videoPath);
            var videoFileName = Path.GetFileName(videoPath);

            // Set target FPS and chunk duration
            double    targetFps             = videocap.Get(Cv.CaptureProperty.Fps);
            double    videoFrameCount       = videocap.Get(Cv.CaptureProperty.FrameCount);
            const int chunkDuration_s       = 5;
            const int KLUDGE                = 1;
            double    chunkFrameCount       = Math.Ceiling(chunkDuration_s * targetFps + KLUDGE);
            ulong     numberChunks          = (ulong)Math.Ceiling(videoFrameCount / chunkFrameCount); // Ask more chunks then needed
            double    durationOfOneFrame_ns = 1000_000_000.0 / targetFps;

            collector.TargetFps     = (float)targetFps;
            collector.ChunkDuration = chunkDuration_s;
            collector.NumberChunks  = numberChunks;

            Console.WriteLine($"    mode: {factory.Mode}");
            Console.WriteLine($"    number chunks: {collector.NumberChunks}");
            Console.WriteLine($"    chunk duration: {collector.ChunkDuration}");
            foreach (var constraint in collector.GetEnabledConstraints())
            {
                Console.WriteLine($"    enabled constraint: {constraint}");
            }

            // Start collection
            collector.StartCollection();

            // Start reading frames and adding to collector
            uint frameNumber = 0;
            bool success     = false;

            using (var window = new Cv.Window("capture"))
            {
                Cv.Mat image = new Cv.Mat();
                while (true)
                {
                    bool ret = videocap.Read(image);
                    if (!ret || image.Empty())
                    {
                        // Video ended, so grab what should be the last, possibly truncated chunk
                        var chunkData = collector.ChunkData;
                        if (chunkData != null)
                        {
                            var chunkPayload = chunkData.Payload;
                            //if (output != null)
                            //    savePayload(chunkPayload, output);
                            Console.WriteLine($"Got chunk with {chunkPayload}");
                        }
                        else
                        {
                            Console.WriteLine("Got empty chunk");
                        }
                        success = true;
                        break;
                    }

                    // Create a Dfx VideoFrame
                    using (Dfx.Sdk.VideoFrame videoFrame = new Dfx.Sdk.VideoFrame((ushort)image.Rows,
                                                                                  (ushort)image.Cols,
                                                                                  Dfx.Sdk.PixelType.TYPE_8UC3,
                                                                                  image.Channels() * image.Cols,
                                                                                  image.Data,
                                                                                  Dfx.Sdk.ChannelOrder.BGR,
                                                                                  (ulong)(frameNumber * durationOfOneFrame_ns),
                                                                                  frameNumber))
                    {
                        frameNumber++;

                        // Create a Dfx Frame from the VideoFrame
                        var frame = collector.CreateFrame(videoFrame);

                        // Add the Dfx Face to the Dfx Frame
                        var jsonFace = jsonFaces[frameNumber.ToString()];
                        var face     = new Dfx.Sdk.Face((string)jsonFace["id"]);
                        face.PoseValid = (bool)jsonFace["poseValid"];
                        face.Detected  = (bool)jsonFace["detected"];
                        face.SetRect((ushort)jsonFace["rect.x"], (ushort)jsonFace["rect.y"], (ushort)jsonFace["rect.w"], (ushort)jsonFace["rect.h"]);
                        foreach (JProperty entry in jsonFace["points"])
                        {
                            face.AddPosePoint(entry.Name, new Dfx.Sdk.PosePoint((float)entry.Value["x"],
                                                                                (float)entry.Value["y"],
                                                                                0,
                                                                                (bool)entry.Value["valid"],
                                                                                (bool)entry.Value["estimated"],
                                                                                (float)entry.Value["quality"]));
                        }
                        frame.AddFace(face);

                        // Add a marker to the 1000th dfx_frame
                        if (frameNumber == 1000)
                        {
                            frame.AddMarker("This is the 1000th frame");
                        }

                        // Do the extraction
                        collector.DefineRegions(frame);
                        var result = collector.ExtractChannels(frame);

                        // Grab a chunk and check if we are finished
                        if (result == Dfx.Sdk.Collector.State.CHUNKREADY || result == Dfx.Sdk.Collector.State.COMPLETED)
                        {
                            var chunkData = collector.ChunkData;
                            if (chunkData != null)
                            {
                                var chunkPayload = chunkData.Payload;
                                //if (output != null)
                                //    savePayload(chunkPayload, output);
                                Console.WriteLine($"Got chunk with {chunkPayload}");
                            }
                            else
                            {
                                Console.WriteLine("Got empty chunk");
                            }
                            if (result == Dfx.Sdk.Collector.State.COMPLETED)
                            {
                                Console.WriteLine($"{nameof(Dfx.Sdk.Collector.State.COMPLETED)} at frame {frameNumber}");
                                success = true;
                                break;
                            }
                        }

                        // Render
                        if (true)
                        {
                            foreach (var faceID in frame.FaceIdentifiers)
                            {
                                foreach (var regionID in frame.GetRegionNames(faceID))
                                {
                                    if (frame.GetRegionIntProperty(faceID, regionID, "draw") != 0)
                                    {
                                        var dfxpolygon = frame.GetRegionPolygon(faceID, regionID);
                                        var cvpolygon  = new List <Cv.Point>();
                                        foreach (var point in dfxpolygon)
                                        {
                                            cvpolygon.Add(new Cv.Point(point.X, point.Y));
                                        }
                                        var cvpolygons = new List <List <Cv.Point> >();
                                        cvpolygons.Add(cvpolygon);
                                        Cv.Cv2.Polylines(image, cvpolygons, isClosed: true, color: Cv.Scalar.Cyan, thickness: 1, lineType: Cv.LineTypes.AntiAlias);
                                    }
                                }
                            }

                            string msg = $"Extracting from {videoFileName} - frame {frameNumber} of {videoFrameCount}";
                            Cv.Cv2.PutText(image, msg, org: new Cv.Point(10, 30), fontFace: Cv.HersheyFonts.HersheyPlain, fontScale: 1, color: Cv.Scalar.Black, thickness: 1, lineType: Cv.LineTypes.AntiAlias);

                            window.ShowImage(image);
                            if (Cv.Cv2.WaitKey(1) == 'q')
                            {
                                success = false;
                                break;
                            }
                        }
                    }
                }
            }

            if (success)
            {
                Console.WriteLine("Collection finished completely. Press any key to exit...");
            }
            else
            {
                Console.WriteLine("Collection interrupted or failed. Press any key to exit...");
            }

            // When everything done, release the capture
            videocap.Release();

            Console.ReadKey();

            return(0);
        }
Пример #9
-12
        public void Run()
        {
            const string OutVideoFile = "out.avi";

            // Opens MP4 file (ffmpeg is probably needed)
            VideoCapture capture = new VideoCapture(FilePath.Movie.Bach);

            // Read movie frames and write them to VideoWriter 
            Size dsize = new Size(640, 480);
            using (VideoWriter writer = new VideoWriter(OutVideoFile, -1, capture.Fps, dsize))
            {
                Console.WriteLine("Converting each movie frames...");
                Mat frame = new Mat();
                while(true)
                {
                    // Read image
                    capture.Read(frame);
                    if(frame.Empty())
                        break;

                    Console.CursorLeft = 0;
                    Console.Write("{0} / {1}", capture.PosFrames, capture.FrameCount);

                    // grayscale -> canny -> resize
                    Mat gray = new Mat();
                    Mat canny = new Mat();
                    Mat dst = new Mat();
                    Cv2.CvtColor(frame, gray, ColorConversionCodes.BGR2GRAY);
                    Cv2.Canny(gray, canny, 100, 180);
                    Cv2.Resize(canny, dst, dsize, 0, 0, InterpolationFlags.Linear);
                    // Write mat to VideoWriter
                    writer.Write(dst);
                } 
                Console.WriteLine();
            }

            // Watch result movie
            using (VideoCapture capture2 = new VideoCapture(OutVideoFile))
            using (Window window = new Window("result"))
            {
                int sleepTime = (int)(1000 / capture.Fps);

                Mat frame = new Mat();
                while (true)
                {
                    capture2.Read(frame);
                    if(frame.Empty())
                        break;

                    window.ShowImage(frame);
                    Cv2.WaitKey(sleepTime);
                }
            }
        }