Пример #1
0
    public static void Render(string pointFilePath, Size?resolution)
    {
        using var points = PointStream.Load(pointFilePath);
        var viewPort         = points.ViewPort;
        var actualResolution = resolution ?? points.ViewPort.Resolution;

        if (actualResolution != points.ViewPort.Resolution)
        {
            viewPort = new ViewPort(viewPort.Area, actualResolution);
        }

        var imageFilePath = pointFilePath + $".{actualResolution.Width}x{actualResolution.Height}.png";

        using var timer = TimedOperation.Start("points", totalWork: actualResolution.Area());
        var image = new FastImage(actualResolution);

        image.Fill(Color.White);

        Parallel.ForEach(points, point =>
        {
            image.SetPixel(viewPort.GetPosition(point), Color.Black);
            timer.AddWorkDone(1);
        });

        image.Save(imageFilePath);
    }
Пример #2
0
        protected override void InitializeCell()
        {
            var width  = CellWidth;
            var height = CellHeight;

            _titleLabel = new Label();

            AbsoluteLayout simpleLayout = new AbsoluteLayout {
                VerticalOptions = LayoutOptions.FillAndExpand,
                WidthRequest    = CellWidth,
                HeightRequest   = CellHeight,
            };

            _image = new FastImage()
            {
                Aspect          = Aspect.AspectFill,
                BackgroundColor = Color.Black,
            };

            _titleLabel = new Label {
                Text            = "",
                TextColor       = Color.White,
                FontSize        = 14,
                XAlign          = TextAlignment.Center,
                YAlign          = TextAlignment.Center,
                LineBreakMode   = LineBreakMode.TailTruncation,
                BackgroundColor = Color.FromHex("#272727"),
            };

            simpleLayout.Children.Add(_image, new Rectangle(0, 0, width, height - 30));
            simpleLayout.Children.Add(_titleLabel, new Rectangle(0, height - 30, width, 30));
            View = simpleLayout;
        }
Пример #3
0
    public static void RenderAllSpans(string edgeSpansPath, string imageFilePath, bool showDirections)
    {
        Log.Info($"Output image: {imageFilePath}");
        if (showDirections)
        {
            Log.Info("Displaying span directions as well.");
        }

        using var spans = EdgeSpanStream.Load(edgeSpansPath);
        using var timer = TimedOperation.Start("edge spans", totalWork: showDirections?spans.Count * 2 : spans.Count);
        var scale = showDirections ? 3 : 1;

        var resolution = spans.ViewPort.Resolution.Scale(scale);

        var image = new FastImage(resolution);

        image.Fill(Color.White);

        Parallel.ForEach(spans, logicalSpan =>
        {
            if (showDirections)
            {
                var scaledLocation = logicalSpan.Location.Scale(scale);
                for (int yDelta = 0; yDelta < 3; yDelta++)
                {
                    for (int xDelta = 0; xDelta < 3; xDelta++)
                    {
                        image.SetPixel(
                            scaledLocation.OffsetBy(xDelta, yDelta),
                            (logicalSpan.Location.X + logicalSpan.Location.Y) % 2 == 1 ? Color.Black : Color.Gray);
                    }
                }
            }
            else
            {
                image.SetPixel(
                    logicalSpan.Location,
                    Color.Black);
            }
            timer.AddWorkDone(1);
        });

        if (showDirections)
        {
            Parallel.ForEach(spans, locatedSpan =>
            {
                var scaledLocation = locatedSpan.Location.Scale(scale);
                var pointingTo     = scaledLocation.OffsetBy(1, 1).OffsetIn(locatedSpan.ToOutside);

                image.SetPixel(
                    pointingTo,
                    Color.Red);

                timer.AddWorkDone(1);
            });
        }

        image.Save(imageFilePath);
    }
Пример #4
0
 public Segmentare(FastImage image, int panelWidth, int panelHeight)
 {
     this.image       = image;
     this.panelWidth  = panelWidth;
     this.panelHeight = panelHeight;
     this.canvas      = new FastImage(new Bitmap(image.Width, image.Height));
     matriceVizitate  = new bool[image.Width + 1, image.Height + 1];
 }
Пример #5
0
 public Bitmap Compute(FastImage workImage)
 {
     image = workImage;
     image.Lock();
     DoKirsch();
     image.Unlock();
     return(image.GetBitMap());
 }
Пример #6
0
        private static bool SaltPepper(FastImage fastImage, int i, int j)
        {
            var color = fastImage.GetPixel(i, j);

            var isBlack = color.R == 0 && color.G == 0 && color.B == 0;
            var isWhite = color.R == 255 && color.G == 255 && color.B == 255;

            return(isBlack || isWhite);
        }
Пример #7
0
        public Gene(FastImage src, int minx, int miny, int maxx, int maxy, int minsz, int maxsz)
        {
            x  = _rnd.Next(minx, maxx);
            y  = _rnd.Next(miny, maxy);
            sz = _rnd.Next(minsz, maxsz);
            //color = new SimpleColor(_rnd.Next(0, 256), _rnd.Next(0, 256), _rnd.Next(0, 256));
            SimpleColor sc = src.GetPixel(_rnd.Next(0, src.width), _rnd.Next(0, src.height));

            color = new SimpleColor(sc.GetR(), sc.GetG(), sc.GetB());
        }
Пример #8
0
 public Candidate(FastImage src, int gCount, int minSqSz, int maxSqSz)
 {
     f     = new FastImage(src.width, src.height);
     genes = new List <Gene>();
     for (int i = 0; i < gCount; i++)
     {
         Gene g = new Gene(src, 0, 0, src.width, src.height, minSqSz, maxSqSz);
         genes.Add(g);
     }
 }
Пример #9
0
        public static void Laplace(FastImage fastImage, FastImage originalFastImage)
        {
            var H1 = new[, ]
            {
                { 0, 1, 0 },
                { 1, -4, 1 },
                { 0, 1, 0 }
            };

            var H2 = new[, ]
            {
                { -1, -1, -1 },
                { -1, 8, -1 },
                { -1, -1, -1 }
            };

            fastImage.Lock();
            originalFastImage.Lock();

            for (int row = 1; row < originalFastImage.Width - 2; row++)
            {
                for (int column = 1; column < originalFastImage.Height - 2; column++)
                {
                    GetConvolutionSums(originalFastImage, row, column, H1, out var sumRedH1, out var sumGreenH1, out var sumBlueH1);
                    GetConvolutionSums(originalFastImage, row, column, H2, out var sumRedH2, out var sumGreenH2, out var sumBlueH2);

                    var sumsRed = new List <int> {
                        sumRedH1, sumRedH2
                    };
                    var sumsGreen = new List <int> {
                        sumGreenH1, sumGreenH2
                    };
                    var sumsBlue = new List <int> {
                        sumBlueH1, sumBlueH2
                    };

                    var maxRed   = sumsRed.Max();
                    var maxGreen = sumsGreen.Max();
                    var maxBlue  = sumsBlue.Max();

                    maxRed   = Normalizare(maxRed, 0, 255);
                    maxGreen = Normalizare(maxGreen, 0, 255);
                    maxBlue  = Normalizare(maxBlue, 0, 255);

                    var newColor = Color.FromArgb(maxRed, maxGreen, maxBlue);
                    fastImage.SetPixel(row, column, newColor);
                }
            }

            fastImage.Unlock();
            originalFastImage.Unlock();
        }
Пример #10
0
        // TODO: This code should not be in this class - its too specialized
        public IImage <bool> CreateBinaryImage(double threshhold)
        {
            IImage <bool> binaryImage = new FastImage <bool>(Width, Height);

            for (int i = 0; i < Width; ++i)
            {
                for (int j = 0; j < Height; ++j)
                {
                    binaryImage[i, j] = this[i, j] > threshhold;
                }
            }
            return(binaryImage);
        }
        public static void Export(MetaMap map, MapPalette palette, string outputFilePath, int scale = 1)
        {
            using var image = new FastImage(map.Width, map.Height, scale);
            for (var tileY = 0; tileY < map.Height; tileY++)
            {
                for (var tileX = 0; tileX < map.Width; tileX++)
                {
                    var tileColor = palette.PickColor(map[tileX, tileY]);
                    image.SetPixel(tileX, tileY, tileColor);
                }
            }

            image.Save(outputFilePath);
        }
Пример #12
0
        private static string GetNewFolder(string oldPath)
        {
            string   root;
            DateTime dateTaken = DateTime.MinValue;

            string extension = Path.GetExtension(oldPath).ToUpper();

            switch (extension)
            {
            case ".MP4":
                root      = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
                dateTaken = File.GetCreationTime(oldPath);
                break;

            case ".THM":
            case ".LRV":
                root      = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
                dateTaken = File.GetLastWriteTime(oldPath);
                break;

            default:
                root = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
                break;
            }

            try
            {
                FastImage img = new FastImage(oldPath);

                dateTaken = img.DateTaken;
            }
            catch
            {
                // No Date Info use default.
            }

            if (dateTaken == DateTime.MinValue)
            {
                return(String.Format("{0}\\No Date", root));
            }
            else
            {
                return(String.Format("{0}\\{1:d4}\\{1:d4}-{2:d2}\\{1:d4}-{2:d2}-{3:d2}",
                                     root, dateTaken.Year, dateTaken.Month, dateTaken.Day));
            }
        }
Пример #13
0
        private static void GetConvolutionSumsForRobert(FastImage fastImage, int x, int y, int[,] matrix, out int sumRed, out int sumGreen, out int sumBlue)
        {
            sumRed   = 0;
            sumGreen = 0;
            sumBlue  = 0;

            for (int i = x; i <= x + 1; i++)
            {
                for (int j = y; j <= y + 1; j++)
                {
                    var pixel = fastImage.GetPixel(i, j);
                    sumRed   += pixel.R * matrix[i - x, j - y];
                    sumGreen += pixel.G * matrix[i - x, j - y];
                    sumBlue  += pixel.B * matrix[i - x, j - y];
                }
            }
        }
Пример #14
0
        public static IFastImage Render(LightMap lightMap, int scale = 10)
        {
            var image = new FastImage(lightMap.Size.Width, lightMap.Size.Height, scale);

            for (int y = 0; y < lightMap.Size.Height; y++)
            {
                for (int x = 0; x < lightMap.Size.Width; x++)
                {
                    var  light     = lightMap[x, y];
                    byte intensity = (byte)((((double)light + lightMap.Range.DarkLevels) / lightMap.Range.Total) * 256);

                    image.SetPixel(x, y, new SKColor(intensity, intensity, intensity));
                }
            }

            return(image);
        }
Пример #15
0
        private static byte CBP(FastImage fastImage, int x, int y, int contextSize, int searchRadius, int sumLimit)
        {
            var intensities = new int[256];

            for (int i = x - searchRadius; i <= x + searchRadius; i++)
            {
                if (i < 0 || i >= fastImage.Width)
                {
                    continue;
                }

                for (int j = y - searchRadius; j < y + searchRadius; j++)
                {
                    if (j < 0 || j >= fastImage.Height)
                    {
                        continue;
                    }

                    if (i == y && j == x)
                    {
                        continue;
                    }

                    if (SAD(fastImage, x, y, i, j, contextSize) < sumLimit && !SaltPepper(fastImage, i, j))
                    {
                        var color = fastImage.GetPixel(i, j);
                        intensities[color.R]++;
                    }
                }
            }

            int indexOfBiggestValue = 0;
            var max = 0;

            for (int i = 0; i < intensities.Length; i++)
            {
                if (intensities[i] > max)
                {
                    max = intensities[i];
                    indexOfBiggestValue = i;
                }
            }

            return((byte)indexOfBiggestValue);
        }
Пример #16
0
        public static void LowPassFiler(FastImage fastImage, FastImage originalFastImage, int n)
        {
            if (n < 1)
            {
                throw new ArgumentException();
            }

            var matrix = GetLowPassFilterMatrix(n);

            fastImage.Lock();
            originalFastImage.Lock();

            for (int row = 1; row < originalFastImage.Width - 2; row++)
            {
                for (int column = 1; column < originalFastImage.Height - 2; column++)
                {
                    var sumRed   = 0;
                    var sumGreen = 0;
                    var sumBlue  = 0;

                    for (int i = row - 1; i <= row + 1; i++)
                    {
                        for (int j = column - 1; j <= column + 1; j++)
                        {
                            var pixel = originalFastImage.GetPixel(i, j);
                            sumRed   += pixel.R * matrix[i - row + 1, j - column + 1];
                            sumGreen += pixel.G * matrix[i - row + 1, j - column + 1];
                            sumBlue  += pixel.B * matrix[i - row + 1, j - column + 1];
                        }
                    }

                    var divide   = (n + 2) * (n + 2);
                    var newRed   = sumRed / divide;
                    var newGreen = sumGreen / divide;
                    var newBlue  = sumBlue / divide;
                    var newColor = Color.FromArgb(newRed, newGreen, newBlue);

                    fastImage.SetPixel(row, column, newColor);
                }
            }

            fastImage.Unlock();
            originalFastImage.Unlock();
        }
Пример #17
0
        public static void GrayScaleFastImage(FastImage fastImage)
        {
            fastImage.Lock();

            for (var i = 0; i < fastImage.Width; i++)
            {
                for (var j = 0; j < fastImage.Height; j++)
                {
                    var color   = fastImage.GetPixel(i, j);
                    var average = (byte)((color.R + color.G + color.B) / 3);

                    color = Color.FromArgb(average, average, average);

                    fastImage.SetPixel(i, j, color);
                }
            }

            fastImage.Unlock();
        }
Пример #18
0
        public static void Roberts(FastImage fastImage, FastImage originalFastImage)
        {
            var P = new[, ]
            {
                { -1, 0 },
                { 0, 1 },
            };

            var Q = new[, ]
            {
                { 0, 1 },
                { -1, 0 },
            };

            fastImage.Lock();
            originalFastImage.Lock();

            const int k = 7;

            for (int row = 1; row < originalFastImage.Width - 2; row++)
            {
                for (int column = 1; column < originalFastImage.Height - 2; column++)
                {
                    GetConvolutionSumsForRobert(originalFastImage, row, column, P, out var sumRedP, out var sumGreenP, out var sumBlueP);
                    GetConvolutionSumsForRobert(originalFastImage, row, column, Q, out var sumRedQ, out var sumGreenQ, out var sumBlueQ);

                    int newRed   = k * (int)Math.Sqrt(Math.Pow(sumRedP, 2) + Math.Pow(sumRedQ, 2));
                    int newGreen = k * (int)Math.Sqrt(Math.Pow(sumGreenP, 2) + Math.Pow(sumGreenQ, 2));
                    int newBlue  = k * (int)Math.Sqrt(Math.Pow(sumBlueP, 2) + Math.Pow(sumBlueQ, 2));

                    newRed   = Normalizare(newRed, 0, 255);
                    newGreen = Normalizare(newGreen, 0, 255);
                    newBlue  = Normalizare(newBlue, 0, 255);

                    var newColor = Color.FromArgb(newRed, newGreen, newBlue);
                    fastImage.SetPixel(row, column, newColor);
                }
            }

            fastImage.Unlock();
            originalFastImage.Unlock();
        }
Пример #19
0
        public static void Sobel(FastImage fastImage, FastImage originalFastImage)
        {
            var P = new[, ]
            {
                { -1, -2, -1 },
                { 0, 0, 0 },
                { 1, 2, 1 }
            };

            var Q = new[, ]
            {
                { -1, 0, 1 },
                { -2, 0, 2 },
                { -1, 0, 1 }
            };

            fastImage.Lock();
            originalFastImage.Lock();

            for (int row = 1; row < originalFastImage.Width - 2; row++)
            {
                for (int column = 1; column < originalFastImage.Height - 2; column++)
                {
                    GetConvolutionSums(originalFastImage, row, column, P, out var sumRedP, out var sumGreenP, out var sumBlueP);
                    GetConvolutionSums(originalFastImage, row, column, Q, out var sumRedQ, out var sumGreenQ, out var sumBlueQ);

                    var maxRed   = (int)Math.Sqrt(Math.Pow(sumRedP, 2) + Math.Pow(sumRedQ, 2));
                    var maxGreen = (int)Math.Sqrt(Math.Pow(sumGreenP, 2) + Math.Pow(sumGreenQ, 2));
                    var maxBlue  = (int)Math.Sqrt(Math.Pow(sumBlueP, 2) + Math.Pow(sumBlueQ, 2));

                    maxRed   = Normalizare(maxRed, 0, 255);
                    maxGreen = Normalizare(maxGreen, 0, 255);
                    maxBlue  = Normalizare(maxBlue, 0, 255);

                    var newColor = Color.FromArgb(maxRed, maxGreen, maxBlue);
                    fastImage.SetPixel(row, column, newColor);
                }
            }

            fastImage.Unlock();
            originalFastImage.Unlock();
        }
Пример #20
0
        public static void CBPF(FastImage fastImage, FastImage originalFastImage, int contextSize, int searchRadius, int sumLimit)
        {
            fastImage.Lock();
            originalFastImage.Lock();

            for (int i = 0; i < originalFastImage.Width; i++)
            {
                for (int j = 0; j < originalFastImage.Height; j++)
                {
                    if (SaltPepper(originalFastImage, i, j))
                    {
                        var newColor = CBP(originalFastImage, i, j, contextSize, searchRadius, sumLimit);
                        fastImage.SetPixel(i, j, Color.FromArgb(newColor, newColor, newColor));
                    }
                }
            }

            fastImage.Unlock();
            originalFastImage.Unlock();
        }
Пример #21
0
        public static long Error(FastImage a, FastImage b)
        {
            long error = 0;

            for (int x = 0; x < a.width; x++)
            {
                for (int y = 0; y < a.height; y++)
                {
                    SimpleColor old    = a.GetPixel(x, y);
                    SimpleColor srcCol = b.GetPixel(x, y);

                    error += (long)Math.Sqrt(
                        Math.Pow(old.GetR() - srcCol.GetR(), 2) +
                        Math.Pow(old.GetG() - srcCol.GetG(), 2) +
                        Math.Pow(old.GetB() - srcCol.GetB(), 2));
                }
            }

            return(error);
        }
Пример #22
0
        private static int SAD(FastImage fastImage, int x1, int y1, int x2, int y2, int contextSize)
        {
            int sum = 0;

            for (int i = -contextSize / 2; i <= contextSize / 2; i++)
            {
                if (i + x1 < 0 || i + x1 >= fastImage.Width)
                {
                    continue;
                }

                if (i + x2 < 0 || i + x2 >= fastImage.Width)
                {
                    continue;
                }

                for (int j = -contextSize / 2; j < contextSize / 2; j++)
                {
                    if (i + y1 < 0 || i + y1 >= fastImage.Height)
                    {
                        continue;
                    }

                    if (i + y2 < 0 || i + y2 >= fastImage.Height)
                    {
                        continue;
                    }

                    if (i == 0 && j == 0)
                    {
                        continue;
                    }

                    var color1 = fastImage.GetPixel(j + x1, i + y1);
                    var color2 = fastImage.GetPixel(j + x2, i + y2);
                    sum += Math.Abs(color1.R - color2.R);
                }
            }

            return(sum);
        }
Пример #23
0
        public GeneticAlgorithm(Bitmap sourceImage, int keepEvery, int maxGens, double mutRate,
                                int topn, int numCands, int numGenes, int min, int max)
        {
            rnd            = new Random();
            src            = new FastImage(sourceImage);
            this.keepEvery = keepEvery;
            this.maxGens   = maxGens;
            this.mutN      = (int)(1 / mutRate);
            this.topn      = topn;
            this.numCands  = numCands;
            this.numGenes  = numGenes;
            this.min       = min;
            this.max       = max;

            candidates = new List <Candidate>();
            for (int i = 0; i < numCands; i++)
            {
                Candidate n = new Candidate(src, numGenes, min, max);
                candidates.Add(n);
            }
        }
Пример #24
0
        public static void NegateFastImage(FastImage fastImage)
        {
            fastImage.Lock();

            for (var i = 0; i < fastImage.Width; i++)
            {
                for (var j = 0; j < fastImage.Height; j++)
                {
                    var color = fastImage.GetPixel(i, j);

                    var newRed   = (byte)(255 - color.R);
                    var newGreen = (byte)(255 - color.G);
                    var newBlue  = (byte)(255 - color.B);

                    color = Color.FromArgb(newRed, newGreen, newBlue);

                    fastImage.SetPixel(i, j, color);
                }
            }

            fastImage.Unlock();
        }
Пример #25
0
        public System.Drawing.Bitmap GetAsBitmap()
        {
            GR.Image.FastImage fastImage = new FastImage(Width, Height, PixelFormat);

            for (int i = 0; i < PaletteEntryCount; ++i)
            {
                fastImage.SetPaletteColor(i, PaletteRed(i), PaletteGreen(i), PaletteBlue(i));
            }

            for (int x = 0; x < Width; ++x)
            {
                for (int y = 0; y < Height; ++y)
                {
                    fastImage.SetPixel(x, y, GetPixel(x, y));
                }
            }
            System.Drawing.Bitmap bitmap = fastImage.GetAsBitmap();

            fastImage.Dispose();

            return(bitmap);
        }
Пример #26
0
        public static void ApplyEqualization(FastImage fastImage, FastImage originalFastImage)
        {
            var oldGrayScaleHistogram = originalFastImage.GrayScaleHistogram;
            var newGrayScaleHistogram = new int[256];

            newGrayScaleHistogram[0] = oldGrayScaleHistogram[0];

            for (var i = 1; i < oldGrayScaleHistogram.Length; i++)
            {
                newGrayScaleHistogram[i] = newGrayScaleHistogram[i - 1] + oldGrayScaleHistogram[i];
            }

            var transf = new int[256];

            for (var i = 0; i < transf.Length; i++)
            {
                transf[i] = (newGrayScaleHistogram[i] * 255) / (originalFastImage.Width * originalFastImage.Height);
            }

            originalFastImage.Lock();
            fastImage.Lock();

            for (var i = 0; i < fastImage.Width; i++)
            {
                for (var j = 0; j < fastImage.Height; j++)
                {
                    var color    = originalFastImage.GetPixel(i, j);
                    var gray     = (color.R + color.G + color.B) / 3;
                    var newColor = Color.FromArgb(transf[gray], transf[gray], transf[gray]);

                    fastImage.SetPixel(i, j, newColor);
                }
            }

            originalFastImage.Unlock();
            fastImage.Unlock();
        }
Пример #27
0
    public static void Render(string gridFilePath, string imageFilePath)
    {
        Log.Info($"Output image: {imageFilePath}");

        using var grid  = PointGrid.Load(gridFilePath);
        using var timer = TimedOperation.Start("points", totalWork: grid.ViewPort.Resolution.Area());
        var image = new FastImage(grid.ViewPort.Resolution);

        image.Fill(Color.White);

        Parallel.ForEach(grid, row =>
        {
            foreach (var setSegment in row.GetSegmentsInSet())
            {
                foreach (var x in Enumerable.Range(setSegment.StartCol, setSegment.Length))
                {
                    image.SetPixel(x, row.Y, Color.Black);
                }
            }
            timer.AddWorkDone(grid.ViewPort.Resolution.Width);
        });

        image.Save(imageFilePath);
    }
Пример #28
0
        public void TestFitness(FastImage src)
        {
            f.Clear();
            _error = 0;

            foreach (Gene gene in genes)
            {
                for (int x = gene.x; x < src.width && x < gene.x + gene.sz; x++)
                {
                    for (int y = gene.y; y < src.height && y < gene.y + gene.sz; y++)
                    {
                        f.SetPixel(x, y, gene.color);
                    }
                }
            }

            for (int x = 0; x < src.width; x++)
            {
                for (int y = 0; y < src.height; y++)
                {
                    _error += src.GetPixel(x, y).Difference(f.GetPixel(x, y));
                }
            }
        }
Пример #29
0
        private static void GetConvolutionSumsForFreiChen(FastImage fastImage, int x, int y, List <double[, ]> matrixes, out int sumRed, out int sumGreen, out int sumBlue)
        {
            var firstSumR  = 0d;
            var firstSumG  = 0d;
            var firstSumB  = 0d;
            var secondSumR = 0d;
            var secondSumG = 0d;
            var secondSumB = 0d;

            for (var index = 0; index < matrixes.Count; index++)
            {
                var matrix = matrixes[index];

                for (int i = x; i <= x + 1; i++)
                {
                    for (int j = y; j <= y + 1; j++)
                    {
                        var pixel = fastImage.GetPixel(i, j);
                        secondSumR += Math.Pow(pixel.R * matrix[i - x, j - y], 2);
                        secondSumG += Math.Pow(pixel.G * matrix[i - x, j - y], 2);
                        secondSumB += Math.Pow(pixel.B * matrix[i - x, j - y], 2);

                        if (index < 4)
                        {
                            firstSumR += Math.Pow(pixel.R * matrix[i - x, j - y], 2);
                            firstSumG += Math.Pow(pixel.G * matrix[i - x, j - y], 2);
                            firstSumB += Math.Pow(pixel.B * matrix[i - x, j - y], 2);
                        }
                    }
                }
            }

            sumRed   = (int)(Math.Sqrt(firstSumR / secondSumR) * 255);
            sumGreen = (int)(Math.Sqrt(firstSumG / secondSumG) * 255);
            sumBlue  = (int)(Math.Sqrt(firstSumB / secondSumB) * 255);
        }
Пример #30
0
        static void Main(string[] args)
        {
            Uri            horizonUri = UriFromArg(args, 0);
            GeodysseyModel model      = new GeodysseyModel();

            LoaderController.Instance.Open(horizonUri, model);
            IRegularGrid2D horizon = model[0];

            //string pathOriginal = horizonUri.LocalPath.Replace(".dat", ".grd");
            //horizon.WriteSurfer6BinaryFile(pathOriginal);

            // TODO: Make IRegularGrid IEnumerable

            var           extentMap = new FastImage <bool>(horizon.SizeI, horizon.SizeJ, horizon.Select(item => item.HasValue));
            IImage <bool> faultMap  = Morphology.Invert(extentMap);

            IImage <double> distanceMap = DistanceMap.EuclideanTransform(extentMap);



            // Remove anything above a threshold distance from data
            const double threshold       = 50;
            var          clippedFaultMap = extentMap.CloneTransform((i, j) => distanceMap[i, j] < threshold &&
                                                                    faultMap[i, j]);

            Trace.WriteLine("Pepper filter");
            IImage <bool> filtered = Morphology.PepperFiltering(5, clippedFaultMap);

            Trace.WriteLine("Closing gaps");
            IImage <bool> closed = Morphology.Closing(filtered);

            Trace.WriteLine("Thinning until convergence");
            IImage <bool> thinned = Morphology.ThinUntilConvergence(closed);

            Trace.WriteLine("Thinning blocks until convergence");
            IImage <bool> blockthinned = Morphology.ThinBlockUntilConvergence(thinned);

            Trace.WriteLine("Filling");
            IImage <bool> filled = Morphology.Fill(blockthinned);

            WriteBinaryImageSurfer6Grid(horizon, filled, horizonUri.LocalPath.Replace(".grd", "_filled.grd"));

            //// Create a double valued 'binary' image showing the extent of the horizon data
            //FastImage<double> extentMap = new FastImage<double>(horizon.SizeI, horizon.SizeJ);
            //for (int i = 0; i < horizon.SizeI; ++i)
            //{
            //    for (int j = 0; j < horizon.SizeJ; ++j)
            //    {
            //        bool hasValue = horizon[i, j].HasValue;
            //        extentMap[i, j] = hasValue ? 1.0 : 0.0;
            //    }
            //}

            //int extent = extentMap.Where(v => v == 1.0).Count();
            //double extentProportion = (double) extent / (extentMap.Width * extentMap.Height);

            //IImage<double> scaledExtentMap = Scaler.Downscale(extentMap, 5);

            //IImage<double> smoothedExtentMap = scaledExtentMap.CloneSize();
            //Convolver.GaussianSmooth(scaledExtentMap, smoothedExtentMap, 3.0);

            //IRegularGrid2D smoothedGrid = horizon.CloneSize(smoothedExtentMap.Width, smoothedExtentMap.Height);

            //for (int i = 0 ; i < smoothedGrid.SizeI; ++i)
            //{
            //    for (int j = 0 ; j < smoothedGrid.SizeJ; ++j)
            //    {
            //        smoothedGrid[i, j] = smoothedExtentMap[i, j];
            //    }
            //}

            //PriorityQueue<double> orderedIntensities = PriorityQueue<double>.CreateHighFirstOut(smoothedExtentMap);
            //int k =  (int) (extentProportion * orderedIntensities.Count);
            //Debug.Assert(k >= 0);
            //for (int i = 0 ; i < k - 1; ++i)
            //{
            //    orderedIntensities.Dequeue();
            //}
            //double threshold = orderedIntensities.Dequeue();


            //string pathSmoothed = horizonUri.LocalPath.Replace(".grd", "_smoothed.grd");
            //smoothedGrid.WriteSurfer6BinaryFile(pathSmoothed);

            //IImage<bool> thresholdMap = BitImage.Analysis.Threshold(smoothedExtentMap, threshold * 2.0);

            //int actual = thresholdMap.Where(v => v).Count();
            //double actualProportion = (double) actual / (thresholdMap.Width * thresholdMap.Height);

            //IRegularGrid2D thresholdGrid = horizon.CloneSize(thresholdMap.Width, thresholdMap.Height);

            //for (int i = 0; i < smoothedGrid.SizeI; ++i)
            //{
            //    for (int j = 0; j < smoothedGrid.SizeJ; ++j)
            //    {
            //        thresholdGrid[i, j] = thresholdMap[i, j] ? 1.0 : 0.0;
            //    }
            //}

            //string pathThresholded = horizonUri.LocalPath.Replace(".grd", "_thresholded.grd");
            //thresholdGrid.WriteSurfer6BinaryFile(pathThresholded);

            //IImage<double> distanceMap = DistanceMap.EuclideanTransform(scaledExtentMap);



            // Convert the image back to a grid for convenient output
            IRegularGrid2D distanceGrid = horizon.CloneSize(distanceMap.Width, distanceMap.Height);

            for (int i = 0; i < distanceMap.Width; ++i)
            {
                for (int j = 0; j < distanceMap.Height; ++j)
                {
                    distanceGrid[i, j] = distanceMap[i, j];
                }
            }

            string pathDistance = horizonUri.LocalPath.Replace(".grd", "_distance.grd");

            distanceGrid.WriteSurfer6BinaryFile(pathDistance);
        }