Пример #1
0
        /// <summary>
        /// Saves the specified image.
        /// </summary>
        /// <typeparam name="TColor">Image color.</typeparam>
        /// <param name="image">Image to save.</param>
        /// <param name="fileName">Image filename.</param>
        private unsafe static void Save <TColor>(this Image <TColor> image, string fileName)
            where TColor : struct, IColor
        {
            var iplImage = image.AsOpenCvImage();

            CvHighGuiInvoke.cvSaveImage(fileName, &iplImage, IntPtr.Zero);
        }
Пример #2
0
 /// <summary>
 /// Releases all resources allocated by capture.
 /// </summary>
 public override void Close()
 {
     if (capturePtr != IntPtr.Zero)
     {
         CvHighGuiInvoke.cvReleaseCapture(ref capturePtr);
     }
 }
Пример #3
0
 /// <summary>
 /// Closes video writer.
 /// <para>Use dispose method to remove any additional resources.</para>
 /// </summary>
 public override void Close()
 {
     if (videoObjPtr != IntPtr.Zero)
     {
         CvHighGuiInvoke.cvReleaseVideoWriter(ref videoObjPtr);
     }
 }
Пример #4
0
        /// <summary>
        /// Writes the provided image to the stream.
        /// </summary>
        /// <param name="image">Image to write.</param>
        /// <returns>True, if the operation was successful, false otherwise.</returns>
        protected unsafe override bool WriteInternal(IImage image)
        {
            bool isSuccessful;

            lock (syncObj)
            {
                if (image.ColorInfo.ChannelCount == 3 && !ColorFrames)
                {
                    throw new Exception("Image must be grayscale!");
                }

                if (image.ColorInfo.ChannelCount == 1 && ColorFrames)
                {
                    throw new Exception("Image must be color!");
                }

                if (!image.Size.Equals(FrameSize))
                {
                    throw new Exception("Input image must be the same size as defined frame size!");
                }

                this.Position++;

                var       iplImg    = image.AsOpenCvImage();
                IplImage *iplImgPtr = (IplImage *)&iplImg;

                isSuccessful = CvHighGuiInvoke.cvWriteFrame(videoObjPtr, (IntPtr)iplImgPtr);
            }

            return(isSuccessful);
        }
Пример #5
0
        /// <summary>
        /// Sets the position within the current stream.
        /// <para>Warning: the underlying OpenCV function seeks to nearest key-frame, therefore the seek operation may not be frame-accurate.</para>
        /// </summary>
        /// <param name="offset">A frame index offset relative to the origin parameter.</param>
        /// <param name="origin">A value of type System.IO.SeekOrigin indicating the reference point used to obtain the new position.</param>
        /// <returns>The new position within the current stream.</returns>
        public override long Seek(long offset, System.IO.SeekOrigin origin = SeekOrigin.Current)
        {
            var frameIndex = base.Seek(offset, origin);

            CvHighGuiInvoke.cvSetCaptureProperty(capturePtr, CaptureProperty.PosFrames, frameIndex);

            return(Position);
        }
 public static Size GetImageSize(IntPtr capturePtr)
 {
     return(new Size
     {
         Width = (int)CvHighGuiInvoke.cvGetCaptureProperty(capturePtr, CaptureProperty.FrameWidth),
         Height = (int)CvHighGuiInvoke.cvGetCaptureProperty(capturePtr, CaptureProperty.FrameHeight)
     });
 }
Пример #7
0
 /// <summary>
 /// Saves the specified image.
 /// </summary>
 /// <typeparam name="TColor">Image color.</typeparam>
 /// <param name="image">Image to save.</param>
 /// <param name="fileName">Image filename.</param>
 private unsafe static void Save <TColor>(this TColor[,] image, string fileName)
     where TColor : struct, IColor
 {
     using (var img = image.Lock())
     {
         var iplImage = img.AsOpenCvImage();
         CvHighGuiInvoke.cvSaveImage(fileName, &iplImage, IntPtr.Zero);
     }
 }
        public static bool SetImageSize(IntPtr capturePtr, Size newSize)
        {
            bool success;

            success  = CvHighGuiInvoke.cvSetCaptureProperty(capturePtr, CaptureProperty.FrameWidth, newSize.Width);
            success &= CvHighGuiInvoke.cvSetCaptureProperty(capturePtr, CaptureProperty.FrameHeight, newSize.Height);

            return(success);
        }
Пример #9
0
        /// <summary>
        /// Opens the video file stream.
        /// </summary>
        public override void Open()
        {
            if (videoObjPtr != IntPtr.Zero)
            {
                return;
            }

            videoObjPtr = CvHighGuiInvoke.cvCreateVideoWriter(OutputFileName, (int)Codec, FrameRate, FrameSize, ColorFrames);
            if (videoObjPtr == IntPtr.Zero)
            {
                throw new Exception(String.Format("Cannot open FileStream! Please check that the selected codec ({0}) is supported.", Codec));
            }
        }
Пример #10
0
        /// <summary>
        /// Opens the video file stream.
        /// </summary>
        public override void Open()
        {
            if (capturePtr != IntPtr.Zero)
            {
                return;
            }

            capturePtr = CvHighGuiInvoke.cvCreateFileCapture(fileName);
            if (capturePtr == IntPtr.Zero)
            {
                throw new Exception("Cannot open FileStream!");
            }
        }
Пример #11
0
        /// <summary>
        /// Opens the camera stream.
        /// </summary>
        public override void Open()
        {
            if (capturePtr != IntPtr.Zero)
            {
                return;
            }

            capturePtr = CvHighGuiInvoke.cvCreateCameraCapture(cameraIdx);
            if (capturePtr == IntPtr.Zero)
            {
                throw new Exception("Cannot open camera stream! It seems that camera device can not be found.");
            }
        }
Пример #12
0
        //TODO: Load overloads for float, double... ??
        //TODO: add imDecode, imEncode (requires CvMat implementation)

        private unsafe static IImage load(string fileName, ImageLoadType imageLoadType)
        {
            var iplImagePtr = CvHighGuiInvoke.cvLoadImage(fileName, imageLoadType);
            var image       = (*iplImagePtr).AsImage((_) =>
            {
                if (iplImagePtr == null)
                {
                    return;
                }
                CvHighGuiInvoke.cvReleaseImage(ref iplImagePtr);
            });

            return(image);
        }
Пример #13
0
        /// <summary>
        /// Saves the provided image. If the image has non-supported color or depth false value is returned.
        /// </summary>
        /// <param name="image">Image to save.</param>
        /// <param name="fileName">Filename.</param>
        /// <returns>True if the image is saved, false otherwise.</returns>
        public unsafe static bool TrySave(IImage image, string fileName)
        {
            IplImage iplImage = default(IplImage);

            try
            {
                iplImage = image.AsOpenCvImage();
            }
            catch
            {
                return(false);
            }

            CvHighGuiInvoke.cvSaveImage(fileName, &iplImage, IntPtr.Zero);
            return(true);
        }
Пример #14
0
        /// <summary>
        /// Reads the next image in the stream and advances the position by one.
        /// </summary>
        /// <param name="image">Read image.</param>
        /// <returns>True if the reading operation was successful, false otherwise.</returns>
        protected override bool ReadInternal(out IImage image)
        {
            bool status = false;

            image = default(IImage);

            lock (syncObj)
            {
                IntPtr cvFramePtr;
                cvFramePtr = CvHighGuiInvoke.cvQueryFrame(capturePtr);

                if (cvFramePtr != IntPtr.Zero)
                {
                    image = IplImage.FromPointer(cvFramePtr).AsImage();
                    this.Position++;
                    status = true;
                }
            }

            return(status);
        }