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


C# Mat.Rectangle方法代码示例

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


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

示例1: Update

    // Update is called once per frame
    void Update()
    {
        using ( Mat image = new Mat() ) {
            // Webカメラから画像を取得する
            video.Read( image );

            // 顔を検出する
            var faces = cascade.DetectMultiScale( image );
            if ( faces.Length > 0 ) {
                var face = faces[0];

                // 顔の矩形を描画する
                image.Rectangle( face, new Scalar( 255, 0, 0 ), 2 );

                // 中心の座標を計算する
                var x = face.TopLeft.X + (face.Size.Width / 2);
                var y = face.TopLeft.Y + (face.Size.Height / 2);

                // オブジェクトを移動する
                if ( Object !=null ) {
                    Object.transform.localPosition = Vector2ToVector3( new Vector2( x, y ) );
                }
            }

            // OpenCVのデータがBGRなのでRGBに変える
            // Bitmap形式に変えてテクスチャに流し込む
            using(var cvtImage = image.CvtColor( ColorConversion.BgrToRgb )){
                texture.LoadRawTextureData( cvtImage.ImEncode( ".bmp" ) );
                texture.Apply();
            }
        }
    }
开发者ID:kaorun55,项目名称:UnitySandbox,代码行数:33,代码来源:FaceDetect.cs

示例2: Run

        public void Run()
        {
            Mat src = Cv2.ImRead(FilePath.Lenna, LoadMode.GrayScale);

            // Histogram view
            const int Width = 260, Height = 200;
            Mat render = new Mat(new Size(Width, Height), MatType.CV_8UC3, Scalar.All(255));

            // Calculate histogram
            Mat hist = new Mat();
            int[] hdims = {256}; // Histogram size for each dimension
            Rangef[] ranges = { new Rangef(0,256), }; // min/max 
            Cv2.CalcHist(
                new Mat[]{src}, 
                new int[]{0}, 
                null,
                hist, 
                1, 
                hdims, 
                ranges);
  
            // Get the max value of histogram
            double minVal, maxVal;
            Cv2.MinMaxLoc(hist, out minVal, out maxVal);

            Scalar color = Scalar.All(100);
            // Scales and draws histogram
            hist = hist * (maxVal != 0 ? Height / maxVal : 0.0);
            for (int j = 0; j < hdims[0]; ++j)
            {
                int binW = (int)((double)Width / hdims[0]);
                render.Rectangle(
                    new Point(j * binW, render.Rows),
                    new Point((j + 1) * binW, render.Rows - (int)(hist.Get<float>(j))),
                    color, 
                    -1);
            }

            using (new Window("Image", WindowMode.AutoSize | WindowMode.FreeRatio, src))
            using (new Window("Histogram", WindowMode.AutoSize | WindowMode.FreeRatio, render))
            {
                Cv.WaitKey();
            }
        }
开发者ID:jorik041,项目名称:opencvsharp,代码行数:44,代码来源:HistSample.cs

示例3: calculateHistogram2

        private static void calculateHistogram2(Window histogramWindow, Mat src, Mat modifiedSrc)
        {
            const int histogramSize = 64;//from 0 to 63
            using (var histogram = new Mat())
            {
                int[] dimensions = { histogramSize }; // Histogram size for each dimension
                Rangef[] ranges = { new Rangef(0, histogramSize) }; // min/max
                Cv2.CalcHist(
                    images: new[] { modifiedSrc },
                    channels: new[] { 0 }, //The channel (dim) to be measured. In this case it is just the intensity (each array is single-channel) so we just write 0.
                    mask: null,
                    hist: histogram,
                    dims: 1,
                    histSize: dimensions,
                    ranges: ranges);

                // Get the max value of histogram
                double minVal, maxVal;
                Cv2.MinMaxLoc(histogram, out minVal, out maxVal);

                var color = Scalar.All(100);

                // Scales and draws histogram
                var scaledHistogram = (Mat)(histogram * (maxVal != 0 ? src.Rows / maxVal : 0.0));

                using (var histogramImage = new Mat(new Size(src.Cols, src.Rows), MatType.CV_8UC3, Scalar.All(255)))
                {
                    var binW = (int)((double)src.Cols / histogramSize);
                    for (var j = 0; j < histogramSize; j++)
                    {
                        histogramImage.Rectangle(
                            new Point(j * binW, histogramImage.Rows),
                            new Point((j + 1) * binW, histogramImage.Rows - (int)(scaledHistogram.Get<float>(j))),
                            color,
                            -1);
                    }

                    histogramWindow.Image = histogramImage;
                }
            }
        }
开发者ID:kauser-cse-buet,项目名称:OpenCVSharp-Samples,代码行数:41,代码来源:Program.cs

示例4: CreateHistogram

        public static Mat CreateHistogram(Mat grayScaleMat, int width = 260, int height = 200)
        {
            // Histogram view
            Mat render = new Mat(new OpenCvSharp.CPlusPlus.Size(width, height), MatType.CV_8UC3, Scalar.All(255));

            // Calculate histogram
            Mat hist = new Mat();
            int[] hdims = { 256 }; // Histogram size for each dimension
            Rangef[] ranges = { new Rangef(0, 256), }; // min/max
            Cv2.CalcHist(
                new Mat[] { grayScaleMat },
                new int[] { 0 },
                null,
                hist,
                1,
                hdims,
                ranges);

            // Get the max value of histogram
            double minVal, maxVal;
            Cv2.MinMaxLoc(hist, out minVal, out maxVal);

            Scalar color = Scalar.All(100);
            // Scales and draws histogram
            hist = hist * (maxVal != 0 ? height / maxVal : 0.0);
            for (int j = 0; j < hdims[0]; ++j)
            {
                int binW = (int)((double)width / hdims[0]);
                render.Rectangle(
                    new OpenCvSharp.CPlusPlus.Point(j * binW, render.Rows),
                    new OpenCvSharp.CPlusPlus.Point((j + 1) * binW, render.Rows - (int)(hist.Get<float>(j))),
                    color,
                    -1);
            }

            return render;
        }
开发者ID:Muraad,项目名称:DynamoOpenCV,代码行数:37,代码来源:OpenCv.cs

示例5: CopyTo

 public static void CopyTo(this Mat src, Mat dst, Rect rect)
 {
     var mask = new Mat(src.Rows, src.Cols, MatType.CV_8UC1);
     mask.Rectangle(rect, new Scalar(255), -1);
     src.CopyTo(dst, mask);
 }
开发者ID:DrReiz,项目名称:DrReiz.Robo-Gamer,代码行数:6,代码来源:Zuma.cs

示例6: MakeImagesForArticle


//.........这里部分代码省略.........
                channels[i] = src_channels[i];
                var dst = new Mat();
                Cv2.Merge(channels, dst);
                dst.Resize(resizeK).ImWrite(dir + string.Format("0 ch{0}.png", i));
                src_channels[i].ThresholdStairs().Resize(resizeK).ImWrite(dir + string.Format("0 ch{0} th.png", i));
            }

            if (true)
            {
                src.Resize(0.4).ImWrite(dir + "0.png");
                src_1.Resize(0.4).ImWrite(dir + "1.png");
                background.Resize(0.4).ImWrite(dir + "bg.png");

                var dst_01 = new Mat();
                Cv2.Absdiff(src, src_1, dst_01);
                dst_01.Resize(resizeK).ImWrite(dir + "01.png");
                dst_01.Cut(new Rect(50, src.Height * 4 / 5 - 50, src.Width / 5, src.Height / 5)).ImWrite(dir + "01 part.png");
                dst_01.Cut(new Rect(50, src.Height * 4 / 5 - 50, src.Width / 5, src.Height / 5)).CvtColor(ColorConversion.RgbToGray).ImWrite(dir + "01 g.png");
                dst_01.CvtColor(ColorConversion.RgbToGray).ThresholdStairs().Resize(resizeK).ImWrite(dir + "01 g th.png");

                var dst_01_g = new Mat();
                Cv2.Absdiff(src_g, src_1_g, dst_01_g);
                dst_01_g.Cut(new Rect(50, src.Height * 4 / 5 - 50, src.Width / 5, src.Height / 5)).ImWrite(dir + "0g1g.png");
                dst_01_g.ThresholdStairs().Resize(resizeK).ImWrite(dir + "0g1g th.png");
            }

            if (true)
            {
                var dst_0b = new Mat();
                Cv2.Absdiff(src, background, dst_0b);
                dst_0b.Resize(0.6).ImWrite(dir + "0b.png");

                var dst_0b_g = new Mat();
                Cv2.Absdiff(src_g, background_g, dst_0b_g);
                dst_0b_g.Resize(0.3).ImWrite(dir + "0b g.png");
                dst_0b_g.ThresholdStairs().Resize(0.3).ImWrite(dir + "0b g th.png");
            }
            if (true)
            {
                var hsv_src = new Mat();
                Cv2.CvtColor(src, hsv_src, ColorConversion.RgbToHsv);


                var hsv_background = new Mat();
                Cv2.CvtColor(background, hsv_background, ColorConversion.RgbToHsv);

                var hsv_background_channels = hsv_background.Split();

                var hsv_src_channels = hsv_src.Split();

                if (true)
                {
                    var all = new Mat(src.ToIplImage(), true);
                    for (var i = 0; i < hsv_src_channels.Length; ++i)
                    {
                        hsv_src_channels[i].CvtColor(ColorConversion.GrayToRgb).CopyTo(all, new Rect(i * src.Width / 3, src.Height / 2, src.Width / 3, src.Height / 2));
                    }
                    src_g.CvtColor(ColorConversion.GrayToRgb).CopyTo(all, new Rect(src.Width / 2, 0, src.Width / 2, src.Height / 2));
                    all.Resize(0.3).ImWrite(dir + "all.png");
                }

                foreach (var pair in new[] { "h", "s", "v" }.Select((channel, index) => new { channel, index }))
                {
                    var diff = new Mat();
                    Cv2.Absdiff(hsv_src_channels[pair.index], hsv_background_channels[pair.index], diff);
                    diff.Resize(0.3).With_Title(pair.channel).ImWrite(dir + string.Format("0b {0}.png", pair.channel));
                    diff.ThresholdStairs().Resize(0.3).ImWrite(dir + string.Format("0b {0} th.png", pair.channel));

                    hsv_src_channels[pair.index].Resize(resizeK).With_Title(pair.channel).ImWrite(dir + string.Format("0 {0}.png", pair.channel));

                    foreach (var d in new[] { -100, -50, 50, 100 })
                    {
                        var delta = new Mat(hsv_src_channels[pair.index].ToIplImage(), true);
                        delta.Rectangle(new Rect(0, 0, delta.Width, delta.Height), new Scalar(Math.Abs(d)), -1);

                        var new_channel = new Mat();
                        if (d >= 0)
                            Cv2.Add(hsv_src_channels[pair.index], delta, new_channel);
                        else
                            Cv2.Subtract(hsv_src_channels[pair.index], delta, new_channel);

                        //delta.ImWrite(dir + string.Format("d{0}{1}.png", pair.channel, d));
                        //new_channel.ImWrite(dir + string.Format("q{0}{1}.png", pair.channel, d));

                        var new_hsv = new Mat();
                        Cv2.Merge(hsv_src_channels.Select((channel, index) => index == pair.index ? new_channel : channel).ToArray(), new_hsv);

                        var res = new Mat();
                        Cv2.CvtColor(new_hsv, res, ColorConversion.HsvToRgb);
                        res.Resize(resizeK).With_Title(string.Format("{0} {1:+#;-#}", pair.channel, d)).ImWrite(dir + string.Format("0 {0}{1}.png", pair.channel, d));
                    }
                }
                //if (true)
                //{
                //  var mat = new Mat(src.ToIplImage(), true);
                //  mat.CopyTo(
                //}

            }
        }
开发者ID:DrReiz,项目名称:DrReiz.Robo-Gamer,代码行数:101,代码来源:Zuma.cs

示例7: With_Title

 public static Mat With_Title(this Mat mat, string text)
 {
     var res = new Mat(mat.ToIplImage(), true);
     res.Rectangle(new Rect(res.Width / 2 - 10, 30, 20 + text.Length * 15, 25), new Scalar(0), -1);
     res.PutText(text, new OpenCvSharp.CPlusPlus.Point(res.Width / 2, 50), FontFace.HersheyComplex, 0.7, new Scalar(150, 200, 150));
     return res;
 }
开发者ID:DrReiz,项目名称:DrReiz.Robo-Gamer,代码行数:7,代码来源:Zuma.cs

示例8: ThresholdStairs

        public static Mat ThresholdStairs(this Mat src)
        {
            var dst = new Mat(src.Rows, src.Cols, src.Type());

            var partCount = 10;
            var partWidth = src.Width / partCount;

            for (var i = 0; i < partCount; ++i)
            {
                var th_mat = new Mat();
                Cv2.Threshold(src, th_mat, 255 / 10 * (i + 1), 255, ThresholdType.Binary);
                th_mat.Rectangle(new Rect(0, 0, partWidth * i, src.Height), new Scalar(0), -1);
                th_mat.Rectangle(new Rect(partWidth * (i + 1), 0, src.Width - partWidth * (i + 1), src.Height), new Scalar(0), -1);

                Cv2.Add(dst, th_mat, dst);
            }
            var color_dst = new Mat();
            Cv2.CvtColor(dst, color_dst, ColorConversion.GrayToRgb);
            for (var i = 0; i < partCount; ++i)
            {
                color_dst.Line(partWidth * i, 0, partWidth * i, src.Height, new CvScalar(50, 200, 50), thickness: 2);
            }
            return color_dst;
        }
开发者ID:DrReiz,项目名称:DrReiz.Robo-Gamer,代码行数:24,代码来源:Zuma.cs


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