Ejemplo n.º 1
0
        private void newAnalyzeitWindowMenuItem_Click(object sender, EventArgs e)
        {
            var frm = new AnalyzeitForm {
                StartPosition = FormStartPosition.Manual,
                Width         = Width,
                Height        = Height,
                Location      = new Point(Location.X + Width - 13, Location.Y)
            };

            frm.Show();
        }
Ejemplo n.º 2
0
        public async Task Run(AnalyzeitForm owningForm)
        {
            var stepOne = new ScriptStartInfo(_stepOnePath, _pyExePath, onDataReceived: owningForm.OnScriptDataReceived);
            await Task.Run(() => ScriptDaemon.ExecuteScript(owningForm, stepOne, true));

            var test     = csv_calculateStandardDeviationfromColum(_scriptDir + _outputFilenameStep1, 1, true);
            var testMean = csv_calculateMeanfromColumn(_scriptDir + _outputFilenameStep1, 1, true);

            var test2     = csv_calculateStandardDeviationfromColum(_scriptDir + _outputFilenameStep1, 2, true);
            var test2Mean = csv_calculateMeanfromColumn(_scriptDir + _outputFilenameStep1, 2, true);

            owningForm.WriteLine("Standard dev - Max Pixels : " + test, Color.Blue);
            owningForm.WriteLine("Mean - Max Pixels : " + testMean, Color.Blue);
            owningForm.WriteLine("Standard dev - otsu threshold: " + test2, Color.Blue);
            owningForm.WriteLine("Mean - otsu threshold: " + test2Mean, Color.Blue);
        }
        public async Task Run(AnalyzeitForm owningForm)
        {
            await Task.Run(() => {
                if (_outputPath.GetLast(1) != @"\")
                {
                    _outputPath += @"\";
                }
                if (_inputPath.GetLast(1) != @"\")
                {
                    _inputPath += @"\";
                }
                _logPath = _outputPath + "pixelIntensity.csv";

                owningForm.WriteLine("Beginning Pixel Intensity test...", Color.Blue);
                owningForm.WriteLine($"     Threshold intensity: {_imgThreshold}...", Color.Blue);
                owningForm.WriteLine($"     Original images: {_inputPath}...", Color.Blue);
                owningForm.WriteLine($"     Modified images: {_outputPath}...", Color.Blue);
                owningForm.WriteLine($"     Data Path: {_logPath}...", Color.Blue);

                //create necessary directories and object initialization
                Directory.CreateDirectory(_outputPath);

                //Add headers to our data file.
                using (var file = new StreamWriter(_logPath, false))
                {
                    file.WriteLine("Image, Otsu I, Thr I, Avg. I > Thr, Max I, > Otsu I, > Thr I, > 10% I, > 20% I, > 30% I, > 40% I, >50% I, >60% I");
                }

                owningForm.WriteLine(" Test prep complete. Starting...", Color.Blue);
            });

            //stuff for progress bar
            double totalProgress       = 0;
            double progressStep        = 100.0 / Directory.GetFiles(_inputPath, _imgFlag, SearchOption.AllDirectories).Length;
            int    lastPercentComplete = -1;
            int    percentComplete;

            //Create a parallel task - 8 maximum threads, goes through all images and analyzes the grayscale intensity of each pixel,
            //  Also applies a manual threshold of OurThreshold to each image - any pixel above threshold gets flipped to RGB(255,255,255)
            await Task.Run(() => Parallel.ForEach(Directory.GetFiles(_inputPath, _imgFlag, SearchOption.AllDirectories),
                                                  new ParallelOptions {
                MaxDegreeOfParallelism = 8
            }, inputImage => {
                try
                {
                    using (var inputBitmap = new Bitmap(inputImage))
                    {
                        Console.WriteLine($"Processing  {Path.GetFileName(inputImage)}...");
                        var currentOtsuIntensity =
                            double.Parse(GetGrayScaleIntensity(_otsu.GetOtsuThreshold(inputBitmap)).ToString("F2"));

                        var pixelsOverOtsu               = 0;
                        var pixelsOverThreshold          = 0;
                        var pixelsOver10Percent          = 0;
                        var pixelsOver20Percent          = 0;
                        var pixelsOver30Percent          = 0;
                        var pixelsOver40Percent          = 0;
                        var pixelsOver50Percent          = 0;
                        var pixelsOver60Percent          = 0;
                        double mostIntensePixelIntensity = 0;

                        using (var outputBitmap = new Bitmap(inputImage))
                        {
                            var inData = inputBitmap.LockBits(new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height),
                                                              ImageLockMode.ReadOnly, inputBitmap.PixelFormat);

                            var outData = outputBitmap.LockBits(new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height),
                                                                ImageLockMode.WriteOnly, inputBitmap.PixelFormat);

                            var bitsPerPixel = Image.GetPixelFormatSize(inputBitmap.PixelFormat);

                            var overThresholdCount  = 0;
                            double overThresholdSum = 0;

                            unsafe
                            {
                                var inScan0  = (byte *)inData.Scan0.ToPointer();
                                var outScan0 = (byte *)outData.Scan0.ToPointer();

                                for (var y = 0; y < inData.Height; ++y)
                                {
                                    for (var x = 0; x < inData.Width; ++x)
                                    {
                                        var incoming = inScan0 + y * inData.Stride + x * bitsPerPixel / 8;
                                        var outgoing = outScan0 + y * inData.Stride + x * bitsPerPixel / 8;

                                        var currentPixelIntensity = GetGrayScaleIntensity(incoming[0], incoming[1], incoming[2]);

                                        if (currentPixelIntensity > mostIntensePixelIntensity)
                                        {
                                            mostIntensePixelIntensity = double.Parse(currentPixelIntensity.ToString("F2"));
                                        }

                                        if (currentPixelIntensity > 60)
                                        {
                                            pixelsOver60Percent++;
                                        }
                                        else if (currentPixelIntensity > 50)
                                        {
                                            pixelsOver50Percent++;
                                        }
                                        else if (currentPixelIntensity > 40)
                                        {
                                            pixelsOver40Percent++;
                                        }
                                        else if (currentPixelIntensity > 30)
                                        {
                                            pixelsOver30Percent++;
                                        }
                                        else if (currentPixelIntensity > 20)
                                        {
                                            pixelsOver20Percent++;
                                        }
                                        else if (currentPixelIntensity > 10)
                                        {
                                            pixelsOver10Percent++;
                                        }
                                        else if (currentPixelIntensity > _imgThreshold)
                                        {
                                            pixelsOverThreshold++;
                                        }
                                        else if (currentPixelIntensity > currentOtsuIntensity)
                                        {
                                            pixelsOverOtsu++;
                                        }

                                        outgoing[0] = currentPixelIntensity > _imgThreshold ? (byte)255 : incoming[0];
                                        outgoing[1] = currentPixelIntensity > _imgThreshold ? (byte)255 : incoming[1];
                                        outgoing[2] = currentPixelIntensity > _imgThreshold ? (byte)255 : incoming[2];

                                        if (!(currentPixelIntensity > _imgThreshold))
                                        {
                                            continue;
                                        }

                                        overThresholdSum += currentPixelIntensity;
                                        overThresholdCount++;
                                    }
                                }
                            }

                            inputBitmap.UnlockBits(inData);
                            outputBitmap.UnlockBits(outData);

                            var averageIntensityOverThreshold =
                                double.Parse((overThresholdSum / overThresholdCount).ToString("F2"));

                            //Save the output image
                            var outputFile = _outputPath + Path.GetFileName(inputImage);
                            outputBitmap.Save(outputFile);

                            //Add data for current image to log
                            lock (Locker)
                            {
                                using (var file = new StreamWriter(_logPath, true))
                                {
                                    file.WriteLine(
                                        $"{Path.GetFileName(inputImage)}, {currentOtsuIntensity},{_imgThreshold},{averageIntensityOverThreshold}, " +
                                        $"{mostIntensePixelIntensity},{pixelsOverOtsu}, {pixelsOverThreshold}, {pixelsOver10Percent}, {pixelsOver20Percent}, " +
                                        $"{pixelsOver30Percent}, {pixelsOver40Percent}, {pixelsOver50Percent}, {pixelsOver60Percent}");
                                }
                                totalProgress  += progressStep;
                                percentComplete = (int)Math.Floor(totalProgress);
                                if (lastPercentComplete != percentComplete)
                                {
                                    owningForm.OnProgressUpdate(percentComplete);
                                }
                                lastPercentComplete = percentComplete;
                            }
                        }
                    }
                }
                catch (Exception e) {
                    owningForm.WriteLine($"Error.. File: {inputImage}. More info: {e.Message}... {e.InnerException?.Message}.", Color.Red);
                }
            }));

            FileUtilities.IntegralCsvSort(_logPath, 0, true);
            owningForm.WriteLine("Pixel Intensity analysis complete.", Color.Blue);
        }