private void UpdateBackgroundOrForeground(
            byte[,] rItItm1HW,
            int width,
            int height,
            int stride,
            byte[] currentFrame,
            //byte[,] background,
            out bool motionPresents,
            out byte[,] motionWH)
        {
            motionPresents = false;

//TODO: get access to bytes of tiar
//TODO: get access to bytes of background
//TODO: update background.

            motionWH = new byte[width, height];
            for (int wi = 1; wi < (width - 1); wi++)
            {
                for (int hi = 1; hi < (height - 1); hi++)
                {
                    if (rItItm1HW[hi, wi] == 1)
                    {
                        motionWH[wi, hi] = currentFrame[GrayScaleImageHelper.ToDataPosition(wi, hi, stride)];
                        motionPresents   = true;
                    }
                    else
                    {
                        //TODO:
                    }
                }
            }
        }
Exemplo n.º 2
0
        private bool CheckProveShadow2(byte[] frame, int widthI, int heightI, int stride)
        {
            var standardDeviationOfRegion = new List <double>();

            for (int regionWidthI = widthI - _regionProveShadowDivided2;
                 regionWidthI < (widthI + _regionProveShadowDivided2 + 1);
                 regionWidthI++)
            {
                for (
                    int regionHeightI = (heightI - _regionProveShadowDivided2);
                    regionHeightI < (heightI + _regionProveShadowDivided2 + 1);
                    regionHeightI++)
                {
                    var posCheckRegion = GrayScaleImageHelper.ToDataPosition(regionWidthI, regionHeightI, stride);
                    if (_backgroundModel._averageBackground[posCheckRegion] != 0)
                    {
                        standardDeviationOfRegion.Add((double)frame[posCheckRegion] / _backgroundModel._averageBackground[posCheckRegion]);
                    }
                }
            }

            var deviation = ArrayHelper.GetStandardDeviation(standardDeviationOfRegion.ToArray());

            return(deviation < _Lstd);
        }
Exemplo n.º 3
0
        private byte[] GetForefround(
            byte[] grayScaleHW,
            int width,
            int height,
            int stride)
        {
            var foregroundHW = new byte[grayScaleHW.Length];

            for (int widthI = 0; widthI < width; widthI++)
            {
                for (int heightI = 0; heightI < height; heightI++)
                {
                    var position  = GrayScaleImageHelper.ToDataPosition(widthI, heightI, stride);
                    var threshold = GetThreshold(position);
                    var pixel     = grayScaleHW[position];
                    foregroundHW[position] = byte.MaxValue;

                    if (_backgroundModel._minIntensity[position] - threshold <= pixel &&
                        pixel <= _backgroundModel._maxIntensity[position] + threshold)
                    {
                        foregroundHW[position] = _backgroundPixel; // white
                    }

                    else
                    {
                        foregroundHW[position] = _foregroundPixel;
                    }
                }
            }
            return(foregroundHW);
        }
Exemplo n.º 4
0
        private bool CheckProveShadowOriginal(byte[] frame, int widthI, int heightI, int stride)
        {
            double er = 0;
            double eb = _backgroundModel._energyBackground[GrayScaleImageHelper.ToDataPosition(widthI, heightI, stride)];
            double et = 0;

            for (int regionWidthI = widthI - _regionFrameSizeDivided2;
                 regionWidthI < (widthI + _regionFrameSizeDivided2 + 1);
                 regionWidthI++)
            {
                for (
                    int regionHeightI = (heightI - _regionFrameSizeDivided2);
                    regionHeightI < (heightI + _regionFrameSizeDivided2 + 1);
                    regionHeightI++)
                {
                    var posCheckRegion = GrayScaleImageHelper.ToDataPosition(regionWidthI, regionHeightI, stride);
                    er += _backgroundModel._averageBackground[posCheckRegion] * frame[posCheckRegion];
                    et += frame[posCheckRegion] * frame[posCheckRegion];
                }
            }
            et = Math.Sqrt(et);

            var ncc = er / (eb * et);

            return(ncc > _Lncc && et < eb);
        }
Exemplo n.º 5
0
            private void CalculateEnergyBackground()
            {
                ArrayHelper.SetToAll(_energyBackground, 0);
                for (int widthI = _regionFrameSizeDivided2; widthI < (_width - _regionFrameSizeDivided2 - 1); widthI++)
                {
                    for (int heightI = _regionFrameSizeDivided2; heightI < (_height - _regionFrameSizeDivided2 - 1); heightI++)
                    {
                        long result = 0;

                        for (
                            int regionWidthI = (widthI - _regionFrameSizeDivided2);
                            regionWidthI < (widthI + _regionFrameSizeDivided2 + 1);
                            regionWidthI++)
                        {
                            for (
                                int regionHeightI = (heightI - _regionFrameSizeDivided2);
                                regionHeightI < (heightI + _regionFrameSizeDivided2 + 1);
                                regionHeightI++)
                            {
                                result += _averageBackground[GrayScaleImageHelper.ToDataPosition(regionWidthI, regionHeightI, _stride)];
                            }
                        }

                        _energyBackground[GrayScaleImageHelper.ToDataPosition(widthI, heightI, _stride)] = Math.Sqrt(result);
                    }
                }
            }
Exemplo n.º 6
0
 private void ExcludeShadows(byte[] frame, byte[] foreground, int width, int height, int stride)
 {
     for (int widthI = _borderDivided2; widthI < (width - _borderDivided2 - 1); widthI++)
     {
         for (int heightI = _borderDivided2; heightI < (height - _borderDivided2 - 1); heightI++)
         {
             var pos = GrayScaleImageHelper.ToDataPosition(widthI, heightI, stride);
             if (foreground[pos] == _foregroundPixel &&
                 CheckProveShadow3(frame[pos], _backgroundModel._averageBackground[pos]) &&
                 CheckProveShadow2(frame, widthI, heightI, stride) &&
                 CheckProveShadowOriginal(frame, widthI, heightI, stride))
             {
                 foreground[pos] = _backgroundPixel;
             }
         }
     }
 }
Exemplo n.º 7
0
            private void DumpRegionOfInterestData()
            {
                return;

                //line=363;col=374 --403;415
                var roeStart = new Rectangle(363, 374, 40, 40);

                var trainingData = @"D:\temp\dumps";
                var dumpFile     = Path.Combine(trainingData, "Dump-Region Of Interest.txt");

                var outf = new StreamWriter(dumpFile);


                //todo: dump median
                outf.WriteLine("Median dump...");

                for (int heightI = roeStart.Top; heightI < roeStart.Bottom; heightI++)
                {
                    for (int widthI = roeStart.Left; widthI < roeStart.Right; widthI++)
                    {
                        outf.Write("\t{0:00.0}", tempMedianHW_[heightI, widthI]);
                    }
                    outf.WriteLine();
                }
                //todo: dump disperce

                outf.WriteLine("Disperce dump...");

                for (int heightI = roeStart.Top; heightI < roeStart.Bottom; heightI++)
                {
                    for (int widthI = roeStart.Left; widthI < roeStart.Right; widthI++)
                    {
                        outf.Write("\t{0:00.0}", tempDisperceHW_[heightI, widthI]);
                    }
                    outf.WriteLine();
                }

                //todo: dump background model
                outf.WriteLine("Background model dump...");

                for (int heightI = roeStart.Top; heightI < roeStart.Bottom; heightI++)
                {
                    for (int widthI = roeStart.Left; widthI < roeStart.Right; widthI++)
                    {
                        var position = GrayScaleImageHelper.ToDataPosition(widthI, heightI, _stride);

                        outf.Write("\t{0:00.0}", _maxPerFrameDifference[position]);
                    }
                    outf.WriteLine();
                }


                //todo: dump minimum frame

                outf.WriteLine("Minimum intensity dump...");

                for (int heightI = roeStart.Top; heightI < roeStart.Bottom; heightI++)
                {
                    for (int widthI = roeStart.Left; widthI < roeStart.Right; widthI++)
                    {
                        var position = GrayScaleImageHelper.ToDataPosition(widthI, heightI, _stride);

                        outf.Write("\t{0:00.0}", _minIntensity[position]);
                    }
                    outf.WriteLine();
                }

                //todo: dump maximum frame

                outf.WriteLine("Maximum intensity dump...");

                for (int heightI = roeStart.Top; heightI < roeStart.Bottom; heightI++)
                {
                    for (int widthI = roeStart.Left; widthI < roeStart.Right; widthI++)
                    {
                        var position = GrayScaleImageHelper.ToDataPosition(widthI, heightI, _stride);

                        outf.Write("\t{0:00.0}", _maxIntensity[position]);
                    }
                    outf.WriteLine();
                }

                outf.Dispose();
            }
Exemplo n.º 8
0
            private void CalculateInitialBackgroundModelState()
            {
                tempMedianHW_   = new double[_height, _width];
                tempDisperceHW_ = new double[_height, _width];

                for (int widthI = 0; widthI < _width; widthI++)
                {
                    for (int heightI = 0; heightI < _height; heightI++)
                    {
                        var position = GrayScaleImageHelper.ToDataPosition(widthI, heightI, _stride);

                        double medium   = 0;
                        double disperce = 0;
                        if (_siarheiKuchuk)
                        {
                            for (int frameNo = 0; frameNo < _amountOfTrainingFrames; frameNo++)
                            {
                                medium += _trainingDataNHW[frameNo][position];
                            }
                            medium = (1.0 * medium) / _amountOfTrainingFrames;
                        }
                        else
                        {
                            var list = new List <byte>();
                            for (int frameNo = 0; frameNo < _amountOfTrainingFrames; frameNo++)
                            {
                                list.Add(_trainingDataNHW[frameNo][position]);
                            }
                            medium = ArrayHelper.GetMedian(list.ToArray());
                        }

                        _averageBackground[position] = (byte)medium;


                        for (int frameNo = 0; frameNo < _amountOfTrainingFrames; frameNo++)
                        {
                            disperce += Math.Abs(_trainingDataNHW[frameNo][position] - medium);
                        }
                        disperce = (1.0 * disperce) / _amountOfTrainingFrames;

                        var minIntensity          = -1;
                        var maxIntensity          = -1;
                        var maxPerFrameDifference = byte.MinValue;

                        int previousTrainingValue = -1;
                        for (int frameNo = 0; frameNo < _amountOfTrainingFrames; frameNo++)
                        {
                            var val = _trainingDataNHW[frameNo][position];
                            if (Math.Abs(val - medium) <= _dK * disperce)
                            {
                                if (minIntensity == -1 ||
                                    val < minIntensity)
                                {
                                    minIntensity = val;
                                }
                                if (maxIntensity == -1 ||
                                    val > maxIntensity)
                                {
                                    maxIntensity = val;
                                }

                                if (previousTrainingValue != -1)
                                {
                                    var difference = (byte)Math.Abs(val - previousTrainingValue);
                                    if (difference > maxPerFrameDifference)
                                    {
                                        maxPerFrameDifference = difference;
                                    }
                                }
                                previousTrainingValue = val;
                            }
                        }

                        if (minIntensity == -1)
                        {
                            throw new InvalidDataException("min intensity!");
                        }

                        if (maxIntensity == -1)
                        {
                            throw new InvalidDataException("max intensity!");
                        }

                        _minIntensity[position]          = (byte)minIntensity;
                        _maxIntensity[position]          = (byte)maxIntensity;
                        _maxPerFrameDifference[position] = maxPerFrameDifference;

                        tempMedianHW_[heightI, widthI]   = medium;
                        tempDisperceHW_[heightI, widthI] = disperce;
                    }
                }

                DumpRegionOfInterestData();

                CalculateEnergyBackground();

#warning : DEBUG code
                DumpTrainingData();
#warning END

                _trainingDataNHW = null;
            }