Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        public override void AssignResult()
        {
            if (!IsReady())
            {
                throw new NotSupportedException();
            }

            // Matで結果取得
            using (var vectorOfMat = new VectorOfMat())
            {
                NativeMethods.HandleException(
                    NativeMethods.core_OutputArray_getVectorOfMat(ptr, vectorOfMat.CvPtr));
                GC.KeepAlive(this);
                list.Clear();
                list.AddRange(vectorOfMat.ToArray());
            }
        }
        /// <summary>
        /// Saves an image to a specified file.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="img">Image to be saved.</param>
        /// <param name="prms">Format-specific save parameters encoded as pairs</param>
        /// <returns></returns>
        public static bool ImWrite(string fileName, IEnumerable <Mat> img, int[]?prms = null)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException(nameof(fileName));
            }
            if (img == null)
            {
                throw new ArgumentNullException(nameof(img));
            }
            prms ??= Array.Empty <int>();

            using var imgVec = new VectorOfMat(img);
            NativeMethods.HandleException(
                NativeMethods.imgcodecs_imwrite_multi(fileName, imgVec.CvPtr, prms, prms.Length, out var ret));
            GC.KeepAlive(img);
            return(ret != 0);
        }
        /// <summary>
        /// Decodes QR codes in image once it's found by the detect() method.
        /// Returns UTF8-encoded output string or empty string if the code cannot be decoded.
        /// </summary>
        /// <param name="img">grayscale or color (BGR) image containing QR code.</param>
        /// <param name="points">Quadrangle vertices found by detect() method (or some other algorithm).</param>
        /// <param name="decodedInfo">UTF8-encoded output vector of string or empty vector of string if the codes cannot be decoded. </param>
        /// <param name="straightQrCode">The optional output image containing rectified and binarized QR code</param>
        /// <param name="isOutputStraightQrCode"><see langword="true"/> to output <paramref name="straightQrCode"/></param>
        /// <returns></returns>
        protected bool DecodeMulti(InputArray img, IEnumerable <Point2f> points, out string?[] decodedInfo, out Mat[] straightQrCode, bool isOutputStraightQrCode)
        {
            if (img == null)
            {
                throw new ArgumentNullException(nameof(img));
            }
            if (points == null)
            {
                throw new ArgumentNullException(nameof(points));
            }

            img.ThrowIfDisposed();

            using var decodedInfoVec = new VectorOfString();
            using var pointsVec      = new VectorOfPoint2f(points);

            int ret;

            if (isOutputStraightQrCode)
            {
                NativeMethods.HandleException(
                    NativeMethods.objdetect_QRCodeDetector_decodeMulti(
                        ptr, img.CvPtr, pointsVec.CvPtr, decodedInfoVec.CvPtr, out var straightQrCodePtr, out ret));

                using var straightQrCodeVec = new VectorOfMat(straightQrCodePtr);
                straightQrCode = straightQrCodeVec.ToArray();
            }
            else
            {
                NativeMethods.HandleException(
                    NativeMethods.objdetect_QRCodeDetector_decodeMulti_NoStraightQrCode(
                        ptr, img.CvPtr, pointsVec.CvPtr, decodedInfoVec.CvPtr, out ret));
                straightQrCode = Array.Empty <Mat>();
            }

            // decode utf-8 bytes.
            decodedInfo = decodedInfoVec.ToArray();

            GC.KeepAlive(img);
            GC.KeepAlive(points);
            GC.KeepAlive(this);

            return(ret != 0);
        }
        /// <summary>
        /// Both detects and decodes QR code.
        /// To simplify the usage, there is a only API: detectAndDecode
        /// </summary>
        /// <param name="inputImage">supports grayscale or color(BGR) image.</param>
        /// <param name="bbox">optional output array of vertices of the found QR code quadrangle.Will be empty if not found.</param>
        /// <param name="results">list of decoded string.</param>
        public void DetectAndDecode(InputArray inputImage, out Mat[] bbox, out string[] results)
        {
            if (inputImage == null)
            {
                throw new ArgumentNullException(nameof(inputImage));
            }
            inputImage.ThrowIfDisposed();

            using var bboxVec = new VectorOfMat();
            using var texts   = new VectorOfString();
            NativeMethods.HandleException(
                NativeMethods.wechat_qrcode_WeChatQRCode_detectAndDecode(
                    ptr, inputImage.CvPtr, bboxVec.CvPtr, texts.CvPtr));

            bbox    = bboxVec.ToArray();
            results = texts.ToArray();
            GC.KeepAlive(this);
            GC.KeepAlive(inputImage);
        }
Exemple #5
0
        /// <summary>
        /// Saves an image to a specified file.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="img">Image to be saved.</param>
        /// <param name="prms">Format-specific save parameters encoded as pairs</param>
        /// <returns></returns>
        public static bool ImWrite(string fileName, IEnumerable <Mat> img, int[]?prms = null)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException(nameof(fileName));
            }
            if (img == null)
            {
                throw new ArgumentNullException(nameof(img));
            }
            if (prms == null)
            {
                prms = new int[0];
            }

            using (var imgVec = new VectorOfMat(img))
            {
                var res = NativeMethods.imgcodecs_imwrite_multi(fileName, imgVec.CvPtr, prms, prms.Length) != 0;
                GC.KeepAlive(img);
                return(res);
            }
        }
Exemple #6
0
        /// <summary>
        /// Constructs a pyramid which can be used as input for calcOpticalFlowPyrLK
        /// </summary>
        /// <param name="img">8-bit input image.</param>
        /// <param name="pyramid">output pyramid.</param>
        /// <param name="winSize">window size of optical flow algorithm.
        /// Must be not less than winSize argument of calcOpticalFlowPyrLK().
        /// It is needed to calculate required padding for pyramid levels.</param>
        /// <param name="maxLevel">0-based maximal pyramid level number.</param>
        /// <param name="withDerivatives">set to precompute gradients for the every pyramid level.
        /// If pyramid is constructed without the gradients then calcOpticalFlowPyrLK() will
        /// calculate them internally.</param>
        /// <param name="pyrBorder">the border mode for pyramid layers.</param>
        /// <param name="derivBorder">the border mode for gradients.</param>
        /// <param name="tryReuseInputImage">put ROI of input image into the pyramid if possible.
        /// You can pass false to force data copying.</param>
        /// <returns>number of levels in constructed pyramid. Can be less than maxLevel.</returns>
        public static int BuildOpticalFlowPyramid(
            InputArray img, out Mat[] pyramid,
            Size winSize, int maxLevel,
            bool withDerivatives    = true,
            BorderTypes pyrBorder   = BorderTypes.Reflect101,
            BorderTypes derivBorder = BorderTypes.Constant,
            bool tryReuseInputImage = true)
        {
            if (img == null)
            {
                throw new ArgumentNullException(nameof(img));
            }
            img.ThrowIfDisposed();

            using var pyramidVec = new VectorOfMat();
            NativeMethods.HandleException(
                NativeMethods.video_buildOpticalFlowPyramid2(
                    img.CvPtr, pyramidVec.CvPtr, winSize, maxLevel, withDerivatives ? 1 : 0,
                    (int)pyrBorder, (int)derivBorder, tryReuseInputImage ? 1 : 0, out var ret));
            GC.KeepAlive(img);
            pyramid = pyramidVec.ToArray();
            return(ret);
        }