当前位置: 首页>>代码示例>>C#>>正文


C# Mat.Reshape方法代码示例

本文整理汇总了C#中Mat.Reshape方法的典型用法代码示例。如果您正苦于以下问题:C# Mat.Reshape方法的具体用法?C# Mat.Reshape怎么用?C# Mat.Reshape使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Mat的用法示例。


在下文中一共展示了Mat.Reshape方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DoOCR

        public void DoOCR(CvKNearest kNearest, string path)
        {
            var src = Cv2.ImRead(path);
            Cv2.ImShow("Source", src);

            var gray = new Mat();
            Cv2.CvtColor(src, gray, ColorConversion.BgrToGray);

            var threshImage = new Mat();
            Cv2.Threshold(gray, threshImage, Thresh, ThresholdMaxVal, ThresholdType.BinaryInv); // Threshold to find contour

            Point[][] contours;
            HiearchyIndex[] hierarchyIndexes;
            Cv2.FindContours(
                threshImage,
                out contours,
                out hierarchyIndexes,
                mode: ContourRetrieval.CComp,
                method: ContourChain.ApproxSimple);

            if (contours.Length == 0)
            {
                throw new NotSupportedException("Couldn't find any object in the image.");
            }

            //Create input sample by contour finding and cropping
            var dst = new Mat(src.Rows, src.Cols, MatType.CV_8UC3, Scalar.All(0));

            var contourIndex = 0;
            while ((contourIndex >= 0))
            {
                var contour = contours[contourIndex];

                var boundingRect = Cv2.BoundingRect(contour); //Find bounding rect for each contour

                Cv2.Rectangle(src,
                    new Point(boundingRect.X, boundingRect.Y),
                    new Point(boundingRect.X + boundingRect.Width, boundingRect.Y + boundingRect.Height),
                    new Scalar(0, 0, 255),
                    2);

                var roi = new Mat(threshImage, boundingRect); //Crop the image

                var resizedImage = new Mat();
                var resizedImageFloat = new Mat();
                Cv2.Resize(roi, resizedImage, new Size(10, 10)); //resize to 10X10
                resizedImage.ConvertTo(resizedImageFloat, MatType.CV_32FC1); //convert to float
                var result = resizedImageFloat.Reshape(1, 1);

                var results = new Mat();
                var neighborResponses = new Mat();
                var dists = new Mat();
                var detectedClass = (int)kNearest.FindNearest(result, 1, results, neighborResponses, dists);

                //Console.WriteLine("DetectedClass: {0}", detectedClass);
                //Cv2.ImShow("roi", roi);
                //Cv.WaitKey(0);

                //Cv2.ImWrite(string.Format("det_{0}_{1}.png",detectedClass, contourIndex), roi);

                Cv2.PutText(
                    dst,
                    detectedClass.ToString(CultureInfo.InvariantCulture),
                    new Point(boundingRect.X, boundingRect.Y + boundingRect.Height),
                    0,
                    1,
                    new Scalar(0, 255, 0),
                    2);

                contourIndex = hierarchyIndexes[contourIndex].Next;
            }

            Cv2.ImShow("Segmented Source", src);
            Cv2.ImShow("Detected", dst);

            Cv2.ImWrite("dest.jpg", dst);

            Cv2.WaitKey();
        }
开发者ID:kauser-cse-buet,项目名称:OpenCVSharp-Samples,代码行数:79,代码来源:SimpleOCR.cs

示例2: TrainData

        public CvKNearest TrainData(IList<ImageInfo> trainingImages)
        {
            var samples = new Mat();
            foreach (var trainingImage in trainingImages)
            {
                samples.PushBack(trainingImage.Image);
            }

            var labels = trainingImages.Select(x => x.ImageGroupId).ToArray();
            var responses = new Mat(labels.Length, 1, MatType.CV_32SC1, labels);
            var tmp = responses.Reshape(1, 1); //make continuous
            var responseFloat = new Mat();
            tmp.ConvertTo(responseFloat, MatType.CV_32FC1); // Convert  to float

            var kNearest = new CvKNearest();
            kNearest.Train(samples, responseFloat); // Train with sample and responses
            return kNearest;
        }
开发者ID:kauser-cse-buet,项目名称:OpenCVSharp-Samples,代码行数:18,代码来源:SimpleOCR.cs

示例3: processTrainingImage

        private static Mat processTrainingImage(Mat gray)
        {
            var threshImage = new Mat();
            Cv2.Threshold(gray, threshImage, Thresh, ThresholdMaxVal, ThresholdType.BinaryInv); // Threshold to find contour

            Point[][] contours;
            HiearchyIndex[] hierarchyIndexes;
            Cv2.FindContours(
                threshImage,
                out contours,
                out hierarchyIndexes,
                mode: ContourRetrieval.CComp,
                method: ContourChain.ApproxSimple);

            if (contours.Length == 0)
            {
                return null;
            }

            Mat result = null;

            var contourIndex = 0;
            while ((contourIndex >= 0))
            {
                var contour = contours[contourIndex];

                var boundingRect = Cv2.BoundingRect(contour); //Find bounding rect for each contour
                var roi = new Mat(threshImage, boundingRect); //Crop the image

                //Cv2.ImShow("src", gray);
                //Cv2.ImShow("roi", roi);
                //Cv.WaitKey(0);

                var resizedImage = new Mat();
                var resizedImageFloat = new Mat();
                Cv2.Resize(roi, resizedImage, new Size(10, 10)); //resize to 10X10
                resizedImage.ConvertTo(resizedImageFloat, MatType.CV_32FC1); //convert to float
                result = resizedImageFloat.Reshape(1, 1);

                contourIndex = hierarchyIndexes[contourIndex].Next;
            }

            return result;
        }
开发者ID:kauser-cse-buet,项目名称:OpenCVSharp-Samples,代码行数:44,代码来源:SimpleOCR.cs

示例4: example02

        private static void example02()
        {
            var src = new Mat(@"..\..\Images\fruits.jpg", LoadMode.AnyDepth | LoadMode.AnyColor);
            Cv2.ImShow("Source", src);
            Cv2.WaitKey(1); // do events

            Cv2.Blur(src, src, new Size(15, 15));
            Cv2.ImShow("Blurred Image", src);
            Cv2.WaitKey(1); // do events

            // Converts the MxNx3 image into a Kx3 matrix where K=MxN and
            // each row is now a vector in the 3-D space of RGB.
            // change to a Mx3 column vector (M is number of pixels in image)
            var columnVector = src.Reshape(cn: 3, rows: src.Rows * src.Cols);

            // convert to floating point, it is a requirement of the k-means method of OpenCV.
            var samples = new Mat();
            columnVector.ConvertTo(samples, MatType.CV_32FC3);

            for (var clustersCount = 2; clustersCount <= 8; clustersCount += 2)
            {
                var bestLabels = new Mat();
                var centers = new Mat();
                Cv2.Kmeans(
                    data: samples,
                    k: clustersCount,
                    bestLabels: bestLabels,
                    criteria:
                        new TermCriteria(type: CriteriaType.Epsilon | CriteriaType.Iteration, maxCount: 10, epsilon: 1.0),
                    attempts: 3,
                    flags: KMeansFlag.PpCenters,
                    centers: centers);

                var clusteredImage = new Mat(src.Rows, src.Cols, src.Type());
                for (var size = 0; size < src.Cols * src.Rows; size++)
                {
                    var clusterIndex = bestLabels.At<int>(0, size);
                    var newPixel = new Vec3b
                    {
                        Item0 = (byte)(centers.At<float>(clusterIndex, 0)), // B
                        Item1 = (byte)(centers.At<float>(clusterIndex, 1)), // G
                        Item2 = (byte)(centers.At<float>(clusterIndex, 2)) // R
                    };
                    clusteredImage.Set(size / src.Cols, size % src.Cols, newPixel);
                }

                Cv2.ImShow(string.Format("Clustered Image [k:{0}]", clustersCount), clusteredImage);
                Cv2.WaitKey(1); // do events
            }

            Cv2.WaitKey();
            Cv2.DestroyAllWindows();
        }
开发者ID:kauser-cse-buet,项目名称:OpenCVSharp-Samples,代码行数:53,代码来源:Program.cs


注:本文中的Mat.Reshape方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。