Esempio n. 1
0
        private void saveAverageCapture()
        {
            //Console.WriteLine(Thread.CurrentThread.Name + " is started!");
            this._bSaveAverageFlag = true;
            //MessageBox.Show("sta, count: " + this._lstDepthCaptures.Count + ", " + this._bSaveAverageFlag);
            autoReset.WaitOne();

            switch (SelectedOutput.OutputType)
            {
            case OutputType.Depth:

                if (this._lstDepthCaptures.Count >= CAPTURE_CAPACITY)
                {
                    Capture cptSample       = this._lstDepthCaptures.ElementAt(0);
                    Capture cptDepthAverage = new Capture();
                    cptDepthAverage.Depth = new Image(cptSample.Depth.Format, cptSample.Depth.WidthPixels,
                                                      cptSample.Depth.HeightPixels, cptSample.Depth.StrideBytes);
                    cptDepthAverage.Depth.WhiteBalance        = cptSample.Depth.WhiteBalance;
                    cptDepthAverage.Depth.DeviceTimestamp     = cptSample.Depth.DeviceTimestamp;
                    cptDepthAverage.Depth.SystemTimestampNsec = cptSample.Depth.SystemTimestampNsec;

                    //foreach (Capture cptDepth in this._lstDepthCaptures) {
                    //    //long lTimes = aPart.Depth.SystemTimestampNsec;
                    //    //strInfo += lTimes + " _ ";
                    //}
                    short sValue = 0;
                    short sTotal = 0;
                    short sCount = 0;
                    int   index  = 0;
                    float[,] depthVals = new float[depthHeight, depthWidth];
                    float[,,] depthAll = new float[this._lstDepthCaptures.Count, depthHeight, depthWidth];
                    List <float[, ]> lstDepthAll = new List <float[, ]>();

                    for (int i = 0; i < depthHeight; i++)
                    {
                        for (int j = 0; j < depthWidth; j++)
                        {
                            sTotal = sCount = 0;
                            index  = 0;
                            foreach (Capture cptDepth in this._lstDepthCaptures)
                            {
                                sValue = cptDepth.Depth.GetPixel <short>(i, j);
                                if (sValue > 0)
                                {
                                    sTotal += sValue;
                                    sCount++;
                                }
                                depthAll[index, i, j] = sValue;
                                index++;
                            }
                            depthVals[i, j] = sCount == 0 ? 0 : sTotal / (float)sCount;
                            cptDepthAverage.Depth.SetPixel <short>(i, j, (short)depthVals[i, j]);
                        } // end of for
                    }     // end of for

                    //saveImageToFile(cptDepthAverage.Depth);
                    Image imgUpdated = GeneralUtil.updateImage(cptDepthAverage.Depth);
                    saveImageToFile(imgUpdated);

                    saveDepthDataToFile(depthVals);
                    Thread.Sleep(1000);

                    ////dogrulama icin
                    //for (int i = 0; i < this._lstDepthCaptures.Count; i++) {
                    //    float[,] temp = new float[depthHeight, depthWidth];

                    //    for (int j = 0; j < depthHeight; j++) {
                    //        for (int k = 0; k < depthWidth; k++) {
                    //            temp[j, k] = depthAll[i, j, k];
                    //        }
                    //    }
                    //    Thread.Sleep(1000);
                    //    saveDepthDataToFile(temp);
                    //}
                }     // end of if

                break;

            case OutputType.IR:

                if (this._lstIrCaptures.Count >= CAPTURE_CAPACITY)
                {
                    Capture cptSample    = this._lstIrCaptures.ElementAt(0);
                    Capture cptIrAverage = new Capture();
                    cptIrAverage.IR = new Image(cptSample.IR.Format, cptSample.IR.WidthPixels,
                                                cptSample.IR.HeightPixels, cptSample.IR.StrideBytes);
                    cptIrAverage.IR.WhiteBalance        = cptSample.IR.WhiteBalance;
                    cptIrAverage.IR.DeviceTimestamp     = cptSample.IR.DeviceTimestamp;
                    cptIrAverage.IR.SystemTimestampNsec = cptSample.IR.SystemTimestampNsec;

                    short sValue = 0;
                    short sTotal = 0;
                    short sCount = 0;
                    int   index  = 0;
                    float[,] irVals = new float[infraRedHeight, infraRedWidth];

                    for (int i = 0; i < infraRedHeight; i++)
                    {
                        for (int j = 0; j < infraRedWidth; j++)
                        {
                            sTotal = sCount = 0;
                            index  = 0;
                            foreach (Capture cptInfraRed in this._lstIrCaptures)
                            {
                                sValue = cptInfraRed.IR.GetPixel <short>(i, j);
                                if (sValue > 0)
                                {
                                    sTotal += sValue;
                                    sCount++;
                                }
                                index++;
                            }
                            irVals[i, j] = sCount == 0 ? 0 : sTotal / (float)sCount;
                            cptIrAverage.IR.SetPixel <short>(i, j, (short)irVals[i, j]);
                        } // end of for
                    }     // end of for

                    saveImageToFile(cptIrAverage.IR);
                    Thread.Sleep(1000);
                }     // end of if

                break;

            case OutputType.Colour:
            default:

                if (this._lstRgbCaptures.Count >= CAPTURE_CAPACITY)
                {
                    Capture cptSample     = this._lstRgbCaptures.ElementAt(0);
                    Capture cptRgbAverage = new Capture();
                    cptRgbAverage.Color = new Image(cptSample.Color.Format, cptSample.Color.WidthPixels,
                                                    cptSample.Color.HeightPixels, cptSample.Color.StrideBytes);
                    cptRgbAverage.Color.WhiteBalance        = cptSample.Color.WhiteBalance;
                    cptRgbAverage.Color.DeviceTimestamp     = cptSample.Color.DeviceTimestamp;
                    cptRgbAverage.Color.SystemTimestampNsec = cptSample.Color.SystemTimestampNsec;

                    MessageBox.Show("sdf w-" + colorWidth + " h- " + colorHeight + " " + cptSample.Color.Format);

                    int nValue = 0;
                    int nTotal = 0;
                    int nCount = 0;
                    int[,] rgbVals = new int[colorHeight, colorWidth];

                    for (int i = 0; i < colorHeight; i++)
                    {
                        for (int j = 0; j < colorWidth; j++)
                        {
                            nTotal = nCount = 0;
                            foreach (Capture cptColor in this._lstRgbCaptures)
                            {
                                //nValue = cptColor.Color.GetPixel<int>(i, j);
                                if (nValue > 0)
                                {
                                    nTotal += nValue;
                                    nCount++;
                                }
                            }
                            rgbVals[i, j] = nCount == 0 ? 0 : nTotal / nCount;
                            //cptRgbAverage.Color.SetPixel<int>(i, j, rgbVals[i, j]);
                        } // end of for
                    }     // end of for
                    MessageBox.Show("sdf 2");
                    saveImageToFile(cptRgbAverage.Color);
                    Thread.Sleep(1000);
                }     // end of if
                break;
            }

            if (CAPTURE_CAPACITY >= 1)
            {
                MessageBox.Show(String.Format("Average values found for {0} captures.", CAPTURE_CAPACITY));
            }

            Console.WriteLine(Thread.CurrentThread.Name + " is ended!");
            return;
        }
Esempio n. 2
0
        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            int nPixelValue = 0;
            int nLastIndex  = 0;

            //lblInfo.Content = this._sbdCaptureInfo.ToString();
            while (running)
            {
                using (Capture capture = await Task.Run(() => { return(this.kinect.GetCapture()); }))
                {
                    this._nCaptureCounter++;
                    switch (SelectedOutput.OutputType)
                    {
                    case OutputType.Depth:
                        if (null == capture.Depth)
                        {
                            continue;
                        }

                        //lblInfo.Content = capture.Depth.Format + " " + capture.Depth.WidthPixels + " X "
                        //        + capture.Depth.HeightPixels
                        //        + ", Count: " + this._nCaptureCounter;

                        //Memory<byte> sa = capture.Depth.Memory;
                        //lblDene.Content = sa.Length;
                        //updateMemory(sa);
                        //capture.Depth.SetPixel()

                        if (this._bSaveAverageFlag)
                        {
                            if (_lstDepthCaptures.Count >= CAPTURE_CAPACITY)
                            {
                                this._bSaveAverageFlag = false;
                                autoReset.Set();
                            }
                            this._lstDepthCaptures.Add(capture.Reference());
                        }

                        this._uiContext.Send(x =>
                        {
                            Image imgUpdated = GeneralUtil.updateImage(capture.Depth);
                            _bitmap          = imgUpdated.CreateBitmapSource();

                            _bitmap.Freeze();
                        }, null);

                        if (this.KinectImage.IsMouseOver)
                        {
                            if (capture.Depth.WidthPixels > xPosImage && capture.Depth.HeightPixels > yPosImage)
                            {
                                nPixelValue = capture.Depth.GetPixel <short>(yPosImage, xPosImage);
                                //this.lblPos.Content = String.Format("x:{0, -3}, y:{1, -3}, val: {2, -5} ", xPosImage, yPosImage, sPixelValue);

                                this._sbdPositionInfo.Clear();
                                this._sbdPositionInfo.Append(String.Format(
                                                                 " :: x: {0, -3}, y: {1, -3}, value: {2, -5} ", xPosImage, yPosImage, nPixelValue));
                            }
                        }

                        nLastIndex = this._sbdCaptureDepthInfo.ToString().LastIndexOf(":");
                        nLastIndex = (nLastIndex < 0 && nLastIndex >= this._sbdCaptureDepthInfo.Length - 1 ? 0 : nLastIndex + 2);
                        this._sbdCaptureDepthInfo.Remove(nLastIndex, this._sbdCaptureDepthInfo.Length - nLastIndex);
                        this._sbdCaptureDepthInfo.Append(this._nCaptureCounter);

                        lblInfo.Content = this._sbdCaptureDepthInfo.ToString() + this._sbdPositionInfo.ToString();

                        break;

                    case OutputType.IR:
                        if (null == capture.IR)
                        {
                            continue;
                        }
                        //lblInfo.Content = "IR " + capture.IR.WidthPixels + " X "
                        //        + capture.IR.HeightPixels + ", Format: " + capture.IR.Format + ", Count: " + this._nCaptureCounter;

                        if (this._bSaveAverageFlag)
                        {
                            if (_lstIrCaptures.Count >= CAPTURE_CAPACITY)
                            {
                                this._bSaveAverageFlag = false;
                                autoReset.Set();
                            }
                            this._lstIrCaptures.Add(capture.Reference());
                        }

                        this._uiContext.Send(x =>
                        {
                            _bitmap = capture.IR.CreateBitmapSource();
                            _bitmap.Freeze();
                        }, null);

                        if (this.KinectImage.IsMouseOver)
                        {
                            //int nArrayPos = yPosImage * capture.IR.WidthPixels + xPosImage;
                            //if (capture.IR.Memory.Length > nArrayPos)
                            if (capture.IR.WidthPixels > xPosImage && capture.IR.HeightPixels > yPosImage)
                            {
                                nPixelValue = capture.IR.GetPixel <short>(yPosImage, xPosImage);
                                //this.lblPos.Content = String.Format("x:{0, -3}, y:{1, -3}, val: {2, -5} ", xPosImage, yPosImage, sPixelValue);

                                this._sbdPositionInfo.Clear();
                                this._sbdPositionInfo.Append(String.Format(" :: x: {0, -3}, y: {1, -3}, value: {2, -5} ",
                                                                           xPosImage, yPosImage, nPixelValue));
                            }     // end of if
                        }

                        nLastIndex = this._sbdCaptureInfraRedInfo.ToString().LastIndexOf(":");
                        nLastIndex = (nLastIndex < 0 && nLastIndex >= this._sbdCaptureInfraRedInfo.Length - 1 ? 0 : nLastIndex + 2);
                        this._sbdCaptureInfraRedInfo.Remove(nLastIndex, this._sbdCaptureInfraRedInfo.Length - nLastIndex);
                        this._sbdCaptureInfraRedInfo.Append(this._nCaptureCounter);

                        lblInfo.Content = this._sbdCaptureInfraRedInfo.ToString() + this._sbdPositionInfo.ToString();

                        break;

                    case OutputType.Colour:
                    default:
                        if (null == capture.Color)
                        {
                            continue;
                        }

                        //lblInfo.Content = "Color " + capture.Color.WidthPixels + " X "
                        //        + capture.Color.HeightPixels + ", Format: " + capture.Color.Format + ", Count: " + this._nCaptureCounter;

                        if (this._bSaveAverageFlag)
                        {
                            if (_lstRgbCaptures.Count >= CAPTURE_CAPACITY)
                            {
                                this._bSaveAverageFlag = false;
                                autoReset.Set();
                            }
                            this._lstRgbCaptures.Add(capture.Reference());
                        }

                        this._uiContext.Send(x =>
                        {
                            _bitmap = capture.Color.CreateBitmapSource();
                            _bitmap.Freeze();
                        }, null);

                        if (this.KinectImage.IsMouseOver)
                        {
                            if (capture.Color.WidthPixels > xPosImage && capture.Color.HeightPixels > yPosImage)
                            {
                                nPixelValue = capture.Color.GetPixel <int>(yPosImage, xPosImage);
                                int nRedValue   = nPixelValue >> 16 & 0x000000FF;
                                int nGreenValue = nPixelValue >> 8 & 0x000000FF;
                                int nBlueValue  = nPixelValue & 0x000000FF;
                                //this.lblPos.Content = String.Format("x:{0, -4}, y:{1, -4}," +
                                //"  red: {2,-3}, g: {3,-3}, b: {4,-3}  ", xPosImage, yPosImage, nRedValue, nGreenValue, nBlueValue);

                                this._sbdPositionInfo.Clear();
                                this._sbdPositionInfo.Append(String.Format(" :: x: {0, -4}, y: {1, -4}," +
                                                                           "  red: {2,-3}, green: {3,-3}, blue: {4,-3}  ", xPosImage, yPosImage, nRedValue, nGreenValue, nBlueValue));
                            }     // end of if
                        }

                        //this.StatusText = "Received Capture: " + capture.Depth.DeviceTimestamp;
                        //this.bitmap.Lock();
                        //var color = capture.Color;
                        //var region = new Int32Rect(0, 0, color.WidthPixels, color.HeightPixels);
                        //unsafe                 //{
                        //    using (var pin = color.Memory.Pin()) {
                        //        this.bitmap.WritePixels(region, (IntPtr)pin.Pointer, (int)color.Size, color.StrideBytes);
                        //    } }
                        //this.bitmap.AddDirtyRect(region);
                        //this.bitmap.Unlock();

                        nLastIndex = this._sbdCaptureColorInfo.ToString().LastIndexOf(":");
                        //MessageBox.Show("count: " + _nCaptureCounter + ", last: " + nLastIndex
                        //   + ", cap before remove__" + this._sbdCaptureInfo.ToString() + "__" + this._sbdCaptureInfo.Length);
                        nLastIndex = (nLastIndex < 0 && nLastIndex >= this._sbdCaptureColorInfo.Length - 1 ? 0 : nLastIndex + 2);
                        this._sbdCaptureColorInfo.Remove(nLastIndex, this._sbdCaptureColorInfo.Length - nLastIndex);
                        this._sbdCaptureColorInfo.Append(this._nCaptureCounter);

                        lblInfo.Content = this._sbdCaptureColorInfo.ToString() + this._sbdPositionInfo.ToString();

                        break;
                    }
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("CurrentCameraImage"));
                }
            }
            return;
        }