Exemplo n.º 1
0
        private void testButton__Click(object sender, EventArgs e)
        {
            using (var openFileDialog = new OpenFileDialog())
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    using (var bitmap = new Bitmap(openFileDialog.FileName))
                    {
                        using (var gs = GrayScaleImageHelper.ToGrayScale(bitmap))
                        {
                            using (var gsClone = (Bitmap)gs.Clone())
                            {
                                double[,] result;
                                GrayScaleImageHelper.GetMultiplier1(gs, gsClone, out result);
                                var maxValue = double.MinValue;
                                for (int wi = 0; wi < result.GetLength(0); wi++)
                                {
                                    for (int hi = 0; hi < result.GetLength(1); hi++)
                                    {
                                        if (maxValue < result[wi, hi])
                                        {
                                            maxValue = result[wi, hi];
                                        }
                                    }
                                }

                                MessageBox.Show("" + Math.Round(maxValue));
                            }
                        }
                    }
                }
            }
        }
        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.º 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 static void TakeScreenshot(Form form,
                                           string destinationFile,
                                           string destinationFileInverted)
        {
            if (File.Exists(destinationFile))
            {
                File.Delete(destinationFile);
            }

            if (File.Exists(destinationFileInverted))
            {
                File.Delete(destinationFileInverted);
            }

            using (var destinationBitmap = new Bitmap(form.Width, form.Height))
            {
                form.DrawToBitmap(destinationBitmap, new Rectangle(0, 0, destinationBitmap.Width, destinationBitmap.Height));
                destinationBitmap.Save(destinationFile);

                using (var grayScaleImage = GrayScaleImageHelper.ToGrayScale(destinationBitmap))
                {
                    var        bounds      = new Rectangle(0, 0, grayScaleImage.Width, grayScaleImage.Height);
                    BitmapData bitmapData  = grayScaleImage.LockBits(bounds, ImageLockMode.ReadOnly, grayScaleImage.PixelFormat);
                    var        grayScaleHW = new byte[grayScaleImage.Height * bitmapData.Stride];
                    Marshal.Copy(bitmapData.Scan0, grayScaleHW, 0, grayScaleImage.Height * bitmapData.Stride);
                    var stride = bitmapData.Stride;
                    grayScaleImage.UnlockBits(bitmapData);

                    GrayScaleImageHelper.Invert(grayScaleHW, stride, grayScaleImage.Width, grayScaleImage.Height);
                    var data = GrayScaleImageHelper.FromData2(grayScaleImage.Width, grayScaleImage.Height, stride, grayScaleHW);
                    File.WriteAllBytes(destinationFileInverted, data);
                }
            }
        }
Exemplo n.º 5
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.º 6
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.º 7
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.º 8
0
 private void OnImageToGrayscaleClick(object sender, EventArgs e)
 {
     using (var openFileDialog = new OpenFileDialog())
     {
         if (openFileDialog.ShowDialog() == DialogResult.OK)
         {
             using (var bitmap = new Bitmap(openFileDialog.FileName))
             {
                 if (BackgroundImage != null)
                 {
                     BackgroundImage.Dispose();
                 }
                 BackgroundImage = GrayScaleImageHelper.ToGrayScale(bitmap);
             }
         }
     }
 }
Exemplo n.º 9
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.º 10
0
        private void OnCalculateMeanAndVarianceClick(object sender, EventArgs e)
        {
            var baseDir  = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)));
            var testData = GrayScaleImageHelper.ToGrayScale(Path.Combine(baseDir, @"Test cases\MeanVarianceTest\Input.txt"));

            byte[,] meanTest;
            byte[,] varianceTest;
            GrayScaleImageHelper.CalculateMeanAndVarianceM9(testData, out meanTest, out varianceTest);
            var meanExpected     = ArrayHelper.FromFile(Path.Combine(baseDir, @"Test cases\MeanVarianceTest\MeanOutput.txt"));
            var varianceExpected = ArrayHelper.FromFile(Path.Combine(baseDir, @"Test cases\MeanVarianceTest\VarianceOutput.txt"));

            if (!ArrayHelper.Compare(meanExpected, meanTest, 1))
            {
                MessageBox.Show("Mean test failed.");
            }
            if (!ArrayHelper.Compare(varianceExpected, varianceTest, 1))
            {
                MessageBox.Show("Variance test failed.");
            }

            using (var openFileDialog = new OpenFileDialog())
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    using (var bitmap = new Bitmap(openFileDialog.FileName))
                    {
                        var grayscale = GrayScaleImageHelper.ToGrayScale(bitmap);
                        byte[,] mean;
                        byte[,] variance;
                        GrayScaleImageHelper.CalculateMeanAndVarianceM9(grayscale, out mean, out variance);
                        var viewResultForm = new ViewResultForm();
                        viewResultForm.Initialize("Average Brightness M9", GrayScaleImageHelper.ToGrayScale(mean));
                        viewResultForm.Show();

                        var viewResultForm2 = new ViewResultForm();
                        viewResultForm2.Initialize("Variance Brightness M9", GrayScaleImageHelper.ToGrayScale(variance));
                        viewResultForm2.Show();
                    }
                }
            }
        }
Exemplo n.º 11
0
        // Radometric Simillarity returns doubles 0..1.
        // They are converted to grayscale bitmap for quantification and future analysis.
        private Bitmap GetRadiometricSimmilarity(
            int width,
            int height,
            int stride,
            byte[] grayScaleData,
            out byte[,] dataHeigthWidth,
            out byte[,] imageTMeanWH,
            out byte[,] imageTVarianceWH)
        {
            GrayScaleImageHelper.CalculateMeanAndVarianceM9(
                width,
                height,
                stride,
                grayScaleData,

                out imageTMeanWH,
                out imageTVarianceWH);

            // Radiometric simillarity.
            GCHandle handle;
            var      result = GrayScaleImageHelper.BeginImage(width, height, out dataHeigthWidth, out handle);

            double[,] firstItemWH;
            GrayScaleImageHelper.GetMultiplier1(width, height, stride, grayScaleData, _grayScaleFrameTMinus1, out firstItemWH);
            for (int widthI = 1; widthI < (width - 1); widthI++)
            {
                for (int heightI = 1; heightI < (height - 1); heightI++)
                {
                    dataHeigthWidth[heightI, widthI] = (byte)
                                                       (
                        byte.MaxValue * (firstItemWH[widthI, heightI] -
                                         imageTMeanWH[widthI, heightI] * _imageTMinus1MeanWH[widthI, heightI]
                                         )
                        /
                        Math.Sqrt(imageTVarianceWH[widthI, heightI] * _imageTMinus1VarianceWH[widthI, heightI]));
                }
            }

            GrayScaleImageHelper.EndImage(handle);
            return(result);
        }
        public string Process(Bitmap bitmap)
        {
            // When video device initializes it adapts to
            // background for some frames.
            if (_skipStartingFrames > 0)
            {
                _skipStartingFrames--;
                return(null);
            }

            using (var grayScaleImage = GrayScaleImageHelper.ToGrayScale(bitmap))
            {
                if (_previous == null)
                {
                    byte[,] imageTMinus1MeanWH;
                    byte[,] imageTMinus1VarianceWH;
                    GrayScaleImageHelper.CalculateMeanAndVarianceM9(
                        grayScaleImage,
                        out imageTMinus1MeanWH,
                        out imageTMinus1VarianceWH);

                    _previous = new RadiometricSimilarityImage(
                        grayScaleImage,
                        imageTMinus1MeanWH,
                        imageTMinus1VarianceWH
                        );

                    _grayScaleBackground = (Bitmap)grayScaleImage.Clone();
                    return(null);
                }

                var        bounds      = new Rectangle(0, 0, grayScaleImage.Width, grayScaleImage.Height);
                BitmapData bitmapData  = grayScaleImage.LockBits(bounds, ImageLockMode.ReadOnly, grayScaleImage.PixelFormat);
                var        grayScaleHW = new byte[grayScaleImage.Height * bitmapData.Stride];
                Marshal.Copy(bitmapData.Scan0, grayScaleHW, 0, grayScaleImage.Height * bitmapData.Stride);
                grayScaleImage.UnlockBits(bitmapData);

                byte[,] imageTMean;
                byte[,] imageTVariance;
                byte[,] motionWH;
                bool motionPresents;
                GrayScaleImageHelper.CalculateMeanAndVarianceM9(
                    grayScaleImage.Width,
                    grayScaleImage.Height,
                    bitmapData.Stride,
                    grayScaleHW,

                    out imageTMean,
                    out imageTVariance);
                var current = new RadiometricSimilarityImage(grayScaleImage.Width,
                                                             grayScaleImage.Height,
                                                             bitmapData.Stride,
                                                             grayScaleHW,
                                                             imageTMean, imageTVariance);

                byte[,] motionData, stationaryData;
                Bitmap motionImage, stationaryImage;
                GrayScaleImageHelper.GetRadiometricSimmilarity(
                    _previous,
                    current,
                    RadiometricDifferenceThrethhold,
                    out motionData,
                    out motionImage,

                    out stationaryData,
                    out stationaryImage);
                {
                    using (motionImage)
                    {
                        _interceptor.Intercept(RadiometricSimmilarityFrameTFrameTMinus1Motion, ImageHelper.ToBytes(motionImage));
                    }

                    using (stationaryImage)
                    {
                        _interceptor.Intercept(RadiometricSimmilarityFrameTFrameTMinus1Stationary, ImageHelper.ToBytes(stationaryImage));
                    }

                    UpdateBackgroundOrForeground(
                        motionData,
                        grayScaleImage.Width,
                        grayScaleImage.Height,
                        bitmapData.Stride,
                        grayScaleHW,
                        out motionPresents,
                        out motionWH);
                }

                _previous = current;

                using (var image = GrayScaleImageHelper.FromWH(motionWH))
                {
                    _interceptor.Intercept(DifferenceImage, ImageHelper.ToBytes(image));
                }

                if (motionPresents)
                {
                    return("Movement detected!");
                }
            }
            return(null);
        }
Exemplo n.º 13
0
        public string Process(Bitmap bitmap)
        {
            // When video device initializes it adapts to
            // background for some frames.
            if (_skipStartingFrames > 0)
            {
                _skipStartingFrames--;
                return(null);
            }

            using (var grayScaleImage = GrayScaleImageHelper.ToGrayScale(bitmap))
            {
                if (_grayScaleFrameTMinus1 == null ||
                    _grayScaleBackground == null)
                {
                    _grayScaleFrameTMinus1 = (Bitmap)grayScaleImage.Clone();
                    GrayScaleImageHelper.CalculateMeanAndVarianceM9(
                        _grayScaleFrameTMinus1,
                        out _imageTMinus1MeanWH,
                        out _imageTMinus1VarianceWH);
                    _grayScaleBackground = (Bitmap)grayScaleImage.Clone();
                    return(null);
                }

                var        bounds      = new Rectangle(0, 0, grayScaleImage.Width, grayScaleImage.Height);
                BitmapData bitmapData  = grayScaleImage.LockBits(bounds, ImageLockMode.ReadOnly, grayScaleImage.PixelFormat);
                var        grayScaleHW = new byte[grayScaleImage.Height * bitmapData.Stride];
                Marshal.Copy(bitmapData.Scan0, grayScaleHW, 0, grayScaleImage.Height * bitmapData.Stride);

                byte[,] imageTMean;
                byte[,] imageTVariance;
                byte[,] temporalImageAnalysisResultHW;
                byte[,] motionWH;
                bool motionPresents;
                using (var temporalImageAnalysisResult = GetRadiometricSimmilarity(
                           grayScaleImage.Width,
                           grayScaleImage.Height,
                           bitmapData.Stride,
                           grayScaleHW,
                           out temporalImageAnalysisResultHW,
                           out imageTMean,
                           out imageTVariance))
                {
                    _interceptor.Intercept(RadiometricSimmilarity, temporalImageAnalysisResult);

                    UpdateBackgroundOrForeground(
                        temporalImageAnalysisResultHW,
                        grayScaleImage.Width,
                        grayScaleImage.Height,
                        bitmapData.Stride,
                        grayScaleHW,
                        out motionPresents,
                        out motionWH);
                }

                _imageTMinus1MeanWH     = imageTMean;
                _imageTMinus1VarianceWH = imageTVariance;
                _grayScaleFrameTMinus1  = (Bitmap)grayScaleImage.Clone();
                grayScaleImage.UnlockBits(bitmapData);

                _interceptor.Intercept(DifferenceImage, GrayScaleImageHelper.FromWH(motionWH));

                if (motionPresents)
                {
                    return("Movement detected!");
                }
            }
            return(null);
        }
Exemplo n.º 14
0
        protected override string ProcessInternal(Bitmap bitmap)
        {
            using (var grayScaleImage = GrayScaleImageHelper.ToGrayScale(bitmap))
            {
                var        bounds      = new Rectangle(0, 0, grayScaleImage.Width, grayScaleImage.Height);
                BitmapData bitmapData  = grayScaleImage.LockBits(bounds, ImageLockMode.ReadOnly, grayScaleImage.PixelFormat);
                var        grayScaleHW = new byte[grayScaleImage.Height * bitmapData.Stride];
                Marshal.Copy(bitmapData.Scan0, grayScaleHW, 0, grayScaleImage.Height * bitmapData.Stride);
                var stride = bitmapData.Stride;
                grayScaleImage.UnlockBits(bitmapData);

                if (!_backgroundModel.IsOperational())
                {
                    _backgroundModel.Train(grayScaleHW, grayScaleImage.Width, grayScaleImage.Height, stride);

                    if (_backgroundModel.IsOperational())
                    {
                        _interceptor.Intercept(MinIntensityBackgroundDebugView, GrayScaleImageHelper.FromData2(_backgroundModel._width, _backgroundModel._height, _backgroundModel._stride, _backgroundModel._minIntensity));
                        _interceptor.Intercept(MaxIntensityBackgroundDebugView, GrayScaleImageHelper.FromData2(_backgroundModel._width, _backgroundModel._height, _backgroundModel._stride, _backgroundModel._maxIntensity));
                        _interceptor.Intercept(MaxPerFrameDifferenceDebugView, GrayScaleImageHelper.FromData2(_backgroundModel._width, _backgroundModel._height, _backgroundModel._stride, _backgroundModel._maxPerFrameDifference));
                    }

                    return(null);
                }

                var foreground = GetForefround(grayScaleHW, grayScaleImage.Width, grayScaleImage.Height, stride);

                //excluded, because does not work.
                //ExcludeShadows(grayScaleHW, foreground, grayScaleImage.Width, grayScaleImage.Height, stride);

                var temp = GrayScaleImageHelper.FromData(
                    _backgroundModel._width,
                    _backgroundModel._height,
                    _backgroundModel._stride,
                    foreground);

                int countDetected = 0;
                // create filter
                var     diamondMask = CreateClosingOpeningFileter();
                Closing filter      = new Closing(diamondMask);
                Opening filter2     = new Opening(diamondMask);
                // apply the filter
                filter.ApplyInPlace(temp);
                filter2.ApplyInPlace(temp);
                _interceptor.Intercept(
                    XXX,
                    ImageHelper.ToBytes(temp));

                // blobs!
                var bc = new BlobCounter {
                    BackgroundThreshold = Color.FromArgb(254, 254, 254)
                };
                bc.ProcessImage(temp);

                Rectangle[] rects            = bc.GetObjectsRectangles();
                var         rectanglesToDraw = new List <Rectangle>();
                foreach (Rectangle rect in rects)
                {
                    if (rect.Width < _maximumDetectionWidthPixels &&
                        rect.Width > _minimumDetectionWidthPixels &&
                        rect.Height < _maximumDetectionHeightPixels &&
                        rect.Height > _minimumDetectionHeightPixels)
                    {
                        countDetected++;
                        rectanglesToDraw.Add(rect);
                    }
                }
                temp.Dispose();

                _interceptor.Intercept(InputFrameDebugView, GrayScaleImageHelper.FromData2(_backgroundModel._width, _backgroundModel._height, _backgroundModel._stride, grayScaleHW));
                _interceptor.Intercept(DifferenceDebugView, GrayScaleImageHelper.FromData2(_backgroundModel._width, _backgroundModel._height, _backgroundModel._stride, foreground));

                using (Graphics graphics = Graphics.FromImage(bitmap))
                    using (var brush = new SolidBrush(Color.Red))
                        using (var pen = new Pen(brush, 3))
                        {
                            if (rectanglesToDraw.Any())
                            {
                                graphics.DrawRectangles(pen, rectanglesToDraw.ToArray());
                            }
                            _interceptor.Intercept(DetectedBlobDebugView, ImageHelper.ToBytes(bitmap));
                        }

                if (countDetected > 0)
                {
                    return("Alarm: " + countDetected);
                }
            }
            return(null);
        }
Exemplo n.º 15
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.º 16
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;
            }