// Use this for initialization
        void Start()
        {
            // Plot data must be a 1xN or Nx1 matrix.
            // Plot data type must be double (CV_64F)
            Mat data = new Mat(30, 1, CvType.CV_64F);

            Core.randu(data, 0, 500);  // random values

            Mat plot_result = new Mat();

//            Plot2d plot = Plot.createPlot2d (data);
            Plot2d plot = Plot2d.create(data);

            plot.setPlotBackgroundColor(new Scalar(50, 50, 50));
            plot.setPlotLineColor(new Scalar(50, 50, 255));
            plot.render(plot_result);

            Imgproc.cvtColor(plot_result, plot_result, Imgproc.COLOR_BGR2RGB);

            Texture2D texture = new Texture2D(plot_result.cols(), plot_result.rows(), TextureFormat.RGBA32, false);

            Utils.matToTexture2D(plot_result, texture);

            gameObject.GetComponent <Renderer> ().material.mainTexture = texture;
        }
示例#2
0
        /// <summary>
        /// Add a plot of the 1D histogram.
        /// </summary>
        /// <param name="name">The name of the histogram</param>
        /// <param name="color">The drawing color</param>
        /// <param name="histogram">The 1D histogram to be drawn</param>
        /// <param name="binSize">The size of the bin</param>
        /// <param name="ranges">The ranges</param>
        /// <returns>The image of the histogram</returns>
        public Mat GenerateHistogram(String name, Color color, Mat histogram, int binSize, float[] ranges)
        {
            //Debug.Assert(histogram.Dimension == 1, Properties.StringTable.Only1DHistogramSupported);

            #region draw the histogram
            RangeF range = new RangeF(ranges[0], ranges[1]);

            float    step  = (range.Max - range.Min) / binSize;
            float    start = range.Min;
            double[] bin   = new double[binSize];
            for (int binIndex = 0; binIndex < binSize; binIndex++)
            {
                bin[binIndex] = start;
                start        += step;
            }

            double[] binVal = new double[histogram.Size.Height];
            GCHandle handle = GCHandle.Alloc(binVal, GCHandleType.Pinned);

            using (Matrix <double> m = new Matrix <double>(binVal.Length, 1, handle.AddrOfPinnedObject(),
                                                           sizeof(double)))
            {
                histogram.ConvertTo(m, DepthType.Cv64F);
            }
            handle.Free();

            using (VectorOfDouble x = new VectorOfDouble(bin))
                using (VectorOfDouble y = new VectorOfDouble(binVal))
                {
                    using (Emgu.CV.Plot.Plot2d plot = new Plot2d(x, y))
                    {
                        plot.SetShowText(false);
                        plot.SetPlotBackgroundColor(new MCvScalar(255, 255, 255));
                        plot.SetPlotLineColor(new MCvScalar(0, 0, 0));
                        plot.SetPlotGridColor(new MCvScalar(220, 220, 220));
                        plot.SetGridLinesNumber(255);
                        plot.SetPlotSize(512, 200);
                        plot.SetMinX(0);
                        plot.SetMaxX(256);
                        plot.SetMinY(-1);
                        plot.SetMaxY(binVal.Max());
                        plot.SetInvertOrientation(true);

                        Mat render = new Mat();
                        plot.Render(render);
                        CvInvoke.PutText(render, name, new Point(20, 30), FontFace.HersheyComplex, 0.8,
                                         new MCvScalar(0, 0, 255));
                        return(render);
                    }
                }
            #endregion
        }