Пример #1
0
        public Picture Histogram()
        {
            var chans = 3;
            var bins = 256;
            var range = new RangeF(0, 255);
            var hist = new DenseHistogram(bins, range);
            var split = this.bgra.Split();
            var colors = new Bgra[]
            {
                new Bgra(255, 0, 0, 255),
                new Bgra(0, 255, 0, 255),
                new Bgra(0, 0, 255, 255),
            };

            var hip = new Picture(bins * chans, bins + 1); // Todo, plus one Jaap, really? Tssssk... wrote Jaap to himself.
            hip.Bgra.SetValue(Color.Black.ToBgra());

            for (int chan = 0; chan < chans; ++chan)
            {
                hist.Calculate<byte>(new Image<Gray, byte>[] { split[chan] }, false, null);

                // Todo, Jaap, December 2010, hist.Normalize(bins - 1);
                float min, max;
                int[] minLoc, maxLoc;
                hist.MinMax(out min, out max, out minLoc, out maxLoc);
                if (max == min)
                    continue;

                var scale = 255.0f / (max - min);

                for (int x = 0; x < bins; ++x)
                {
                    var n = hip.Height - (int)(hist[x] * scale);
                    for (int y = hip.Height - 1; y > n; --y)
                        hip.Bgra[y, x + chan * bins] = colors[chan];
                }
            }

            foreach (var c in split)
                c.Dispose();

            return hip;
        }
        void dataStream(object sender, EventArgs e)
        {
            {
                RangeF[] range = new RangeF[2];
                range[0] = new RangeF(0, 180);
                range[1] = new RangeF(0, 255);

                pollColorImageStream();
                pollDepthImageStream();

                //Color------------------
                Bitmap bitmapColor = new Bitmap(colorImage.Width, colorImage.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                BitmapData bmd = bitmapColor.LockBits(new System.Drawing.Rectangle(0, 0, colorImage.Width, colorImage.Height), ImageLockMode.ReadWrite, bitmapColor.PixelFormat);

                Marshal.Copy(colorPixelData, 0, bmd.Scan0, colorPixelData.Length);

                bitmapColor.UnlockBits(bmd);

                Image<Bgr, Byte> colorTemp = new Image<Bgr, Byte>(bitmapColor);
                //Color------------------end

                //depth------------------

                byte[] byteDepth = new byte[640 * 480];
                byte[] remap = new byte[640 * 480];
                sensor.MapDepthFrameToColorFrame(DepthImageFormat.Resolution640x480Fps30, depthPixelData, ColorImageFormat.RgbResolution640x480Fps30, colorCoordinate);
                for (int y = 0; y < 480; y++)
                {
                    for (int x = 0; x < 640; x++)
                    {
                        int position = y * 640 + x;
                        short tempShort = depthPixelData[position];
                        //depthImage[y, x] = new Gray(tempShort);
                        byteDepth[position] = (byte)(tempShort >> 8);
                        //byteDepth[y, x] = new Gray((byte)(tempShort));

                        int positionRemap = colorCoordinate[position].Y * 640 + colorCoordinate[position].X;
                        if (positionRemap > 640 * 480)
                            continue;
                        depthRemapData[positionRemap] = depthPixelData[position];
                        remap[positionRemap] = (byte)(tempShort >> 8);
                        //byteDepth[y, x] = new Gray((byte)(tempShort));
                    }
                }
                Bitmap bitmapDepth = new Bitmap(depthImage.Width, depthImage.Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);

                BitmapData bmd2 = bitmapDepth.LockBits(new System.Drawing.Rectangle(0, 0, depthImage.Width, depthImage.Height), ImageLockMode.ReadWrite, bitmapDepth.PixelFormat);

                Marshal.Copy(byteDepth, 0, bmd2.Scan0, byteDepth.Length);

                bitmapDepth.UnlockBits(bmd2);

                Image<Gray, Byte> depthTemp = new Image<Gray, Byte>(bitmapDepth);
                //depth------------------end

                Byte[] backFrame = new Byte[640 * 480];
                BitmapImage trackingOut = new BitmapImage();

                if (trackingFlag != 0)
                {
                    Image<Hsv, Byte> hsv = new Image<Hsv, Byte>(640, 480);
                    CvInvoke.cvCvtColor(colorTemp, hsv, COLOR_CONVERSION.CV_BGR2HSV);
                    Image<Gray, Byte> hue = hsv.Split()[0];
                    //range of hist is 180 or 256? not quite sure
                    DenseHistogram hist = new DenseHistogram(180, new RangeF(0.0f, 179.0f));

                    Image<Gray, Byte> mask = new Image<Gray, Byte>(trackWindow.Width, trackWindow.Height);

                    for (int y = 0; y < 480; y++)
                    {
                        for (int x = 0; x < 640; x++)
                        {
                            if (x >= trackWindow.X && x < trackWindow.X + trackWindow.Width && y >= trackWindow.Y && y < trackWindow.Y + trackWindow.Height)
                                mask[y - trackWindow.Y, x - trackWindow.X] = hue[y, x];
                        }
                    }
                    hist.Calculate(new IImage[] { mask }, false, null);
                    //maybe need to re-scale the hist to 0~255?

                    //back projection

                    IntPtr backProject = CvInvoke.cvCreateImage(hsv.Size, IPL_DEPTH.IPL_DEPTH_8U, 1);
                    CvInvoke.cvCalcBackProject(new IntPtr[1] { hue }, backProject, hist);

                    CvInvoke.cvErode(backProject, backProject, IntPtr.Zero, 3);

                    //CAMshift
                    CvInvoke.cvCamShift(backProject, trackWindow, new MCvTermCriteria(50, 0.1), out trackComp, out trackBox);
                    trackWindow = trackComp.rect;
                    if (trackWindow.Width < 5 || trackWindow.Height < 5)
                    {
                        if (trackWindow.Width < 5)
                        {
                            trackWindow.X = trackWindow.X + trackWindow.Width / 2 - 3;
                            trackWindow.Width = 6;
                        }
                        if (trackWindow.Height < 5)
                        {
                            trackWindow.Y = trackWindow.Y + trackWindow.Height / 2 - 3;
                            trackWindow.Height = 6;
                        }
                    }

                    Image<Bgr, Byte> showFrame = colorTemp;
                    showFrame.Draw(trackWindow, new Bgr(System.Drawing.Color.Blue), 2);

                    using (var stream = new MemoryStream())
                    {
                        showFrame.Bitmap.Save(stream, ImageFormat.Bmp);
                        trackingOut.BeginInit();
                        trackingOut.StreamSource = new MemoryStream(stream.ToArray());
                        trackingOut.EndInit();
                    }

                    //calculate the average depth of tracking object
                    int min = 65528, max = 0, num = 0;
                    UInt32 sum = 0;
                    for (int y = 0; y < trackWindow.Height; y++)
                    {
                        for (int x = 0; x < trackWindow.Width; x++)
                        {
                            int position = (trackWindow.X + x) + (trackWindow.Y + y) * 640;
                            ushort temp = (ushort)depthRemapData[position];
                            if (temp != 65528 && temp != 0)//black
                            {
                                if (temp < min)
                                    min = temp;
                                if (temp > max)
                                    max = temp;
                                sum += temp;
                                num++;
                            }
                        }
                    }
                    ushort average = 0;
                    if (num != 0)
                    {
                        average = (ushort)(sum / num);
                    }
                    //Int32 depthInches = (Int32)((average >> DepthImageFrame.PlayerIndexBitmaskWidth) * 0.0393700787);
                    //Int32 depthFt = depthInches / 12;
                    //depthInches = depthInches % 12;
                    Int32 depth = average >> DepthImageFrame.PlayerIndexBitmaskWidth;
                    textBlock1.Text = String.Format("{0}mm", depth);
                    Double distanceInMeter = (Double)depth / 1000;
                    Messenger.Default.Send<Double>(distanceInMeter, "Distance");
                }

                //if (rbtnColorFrame.IsChecked == true)
                //{
                    if (trackingFlag != 0)
                        image1.Source = trackingOut;
                    else
                        image1.Source = BitmapSource.Create(640, 480, 96, 96, PixelFormats.Bgr32, null, colorPixelData, 640 * 4);
                //    else
                //        imageOutputBig.Source = BitmapSource.Create(640, 480, 96, 96, PixelFormats.Bgr32, null, colorPixelData, 640 * 4);
                //    //imageOutputBig.Source = BitmapSource.Create(640, 480, 96, 96, PixelFormats.Gray8, null, backFrame, 640);
                //    imageOutputSmall.Source = BitmapSource.Create(640, 480, 96, 96, PixelFormats.Gray16, null, depthRemapData, 640 * 2);
                //}
                //else
                //{
                //    imageOutputSmall.Source = BitmapSource.Create(640, 480, 96, 96, PixelFormats.Bgr32, null, colorPixelData, 640 * 4);
                //    imageOutputBig.Source = BitmapSource.Create(640, 480, 96, 96, PixelFormats.Gray16, null, depthRemapData, 640 * 2);
                //}
                //if(CvInvoke.cvWaitKey(0)=='q')
                //{
                //    trackingFlag = 0;
                //}
            }
        }
Пример #3
0
      /// <summary>
      /// Add a plot of the 1D histogram. You should call the Refresh() function to update the control after all modification is complete.
      /// </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>
      public void AddHistogram(String name, Color color, Mat histogram, int binSize, float[] ranges)
      {
         //Debug.Assert(histogram.Dimension == 1, Properties.StringTable.Only1DHistogramSupported);

         GraphPane pane = new GraphPane();
         // Set the Title
         pane.Title.Text = name;
         pane.XAxis.Title.Text = Properties.StringTable.Value;
         pane.YAxis.Title.Text = Properties.StringTable.Count;

         #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);
            PointPairList pointList = new PointPairList(
               bin,
               binVal);

            pane.AddCurve(name, pointList, color);
         }
         handle.Free();
         
         #endregion

         zedGraphControl1.MasterPane.Add(pane);
      }
Пример #4
0
        private void SetMomentArea(float min, float max)
        {
            var momentRange = new RangeF();

            momentRange.Min = min;
            momentRange.Max = max;

            _detectorInput.Settings.MomentArea = momentRange;

            UpdateMomentSlidersFromSettings();
        }