Ejemplo n.º 1
0
        public Bitmap Process(Bitmap source, IPointsDetector detector)
        {
            LockableBitmap processedBitmap = new LockableBitmap(source);

            processedBitmap.LockBits();

            try
            {
                СharacteristicPoint[] characteristicsPoints = null;
                try
                {
                    characteristicsPoints = detector.Detect(processedBitmap);
                }
                finally
                {
                    processedBitmap.UnlockBits();
                }

                ProcessOutput(source, characteristicsPoints);
            }
            catch (Exception ex)
            {
                Log.Current.Error(ex);
            }

            return(source);
        }
Ejemplo n.º 2
0
        public Bitmap Determine(
            Bitmap source,
            int clustersNumber,
            Region[] regions,
            IClusteringAlgorithmFactory clusteringAlgorithmFactory)
        {
            LockableBitmap lockableBitmap = new LockableBitmap(source);

            lockableBitmap.LockBits();

            IClusteringAlgorithm <double> clusteringAlgorithm =
                clusteringAlgorithmFactory.Create(clustersNumber, lockableBitmap);

            try
            {
                Region[] clusteredRegions = clusteringAlgorithm.Distribute(regions);

                foreach (Region region in clusteredRegions)
                {
                    FillRegion(region, lockableBitmap);
                }
            }
            finally
            {
                lockableBitmap.UnlockBits();
            }

            return(source);
        }
Ejemplo n.º 3
0
        public Bitmap Process(Bitmap source, params IPerItemAlgorithm[] perItemAlgorithms)
        {
            LockableBitmap lockableBitmap = new LockableBitmap(source);

            lockableBitmap.LockBits();

            try
            {
                foreach (IPerItemAlgorithm algorithm in perItemAlgorithms)
                {
                    for (int x = 0; x < lockableBitmap.Width; x++)
                    {
                        for (int y = 0; y < lockableBitmap.Height; y++)
                        {
                            Color processedColor = algorithm.Execute(lockableBitmap.GetPixel(x, y));
                            lockableBitmap.SetPixel(x, y, processedColor);
                        }
                    }
                }
            }
            finally
            {
                lockableBitmap.UnlockBits();
            }

            return(source);
        }
Ejemplo n.º 4
0
        public Bitmap MarkRegions(Bitmap source, IScanner scanner, int threshold)
        {
            IDictionary <int, List <ScanInfo> > regions = scanner.Scan(source);

            LockableBitmap lockableBitmap = new LockableBitmap(source);

            lockableBitmap.LockBits();

            try
            {
                int regionIndex = 1;

                foreach (KeyValuePair <int, List <ScanInfo> > region in
                         regions.Where(region => region.Value.Count >= threshold))
                {
                    FillRegion(regionIndex, region, lockableBitmap);

                    regionIndex++;
                }
            }
            finally
            {
                lockableBitmap.UnlockBits();
            }

            return(source);
        }
Ejemplo n.º 5
0
        public int[] Calculate(Bitmap bitmap)
        {
            int[] lumininances = new int[256];

            LockableBitmap lockableBitmap = new LockableBitmap(bitmap);

            lockableBitmap.LockBits();

            try
            {
                for (int i = 0; i < lockableBitmap.Width; i++)
                {
                    for (int j = 0; j < lockableBitmap.Height; j++)
                    {
                        byte brightness = lockableBitmap.GetPixel(i, j).Brightness();
                        lumininances[brightness]++;
                    }
                }
            }
            finally
            {
                lockableBitmap.UnlockBits();
            }
            return(lumininances);
        }
Ejemplo n.º 6
0
        public Bitmap Process(Bitmap source, IFilter filter)
        {
            Bitmap         copiedSource = (Bitmap)source.Clone();
            LockableBitmap outBitmap    = new LockableBitmap(copiedSource);

            outBitmap.LockBits();

            LockableBitmap processedBitmap = new LockableBitmap(source);

            processedBitmap.LockBits();

            try
            {
                SobelImageEnumerator enumerator = new SobelImageEnumerator(processedBitmap);

                while (enumerator.MoveNext())
                {
                    try
                    {
                        ExecuteStep(enumerator.Current, filter, processedBitmap, outBitmap);
                    }
                    catch (Exception ex)
                    {
                        Log.Current.Error(ex);
                    }
                }
            }
            finally
            {
                processedBitmap.UnlockBits();
                outBitmap.UnlockBits();
            }

            return(copiedSource);
        }
Ejemplo n.º 7
0
        public Bitmap Process(Bitmap sourceToProcess)
        {
            Bitmap copiedSource = (Bitmap)sourceToProcess.Clone();

            LockableBitmap outBitmap = new LockableBitmap(copiedSource);

            outBitmap.LockBits();

            LockableBitmap lockableBitmap = new LockableBitmap(sourceToProcess);

            lockableBitmap.LockBits();

            try
            {
                IPattern <double> pattern          = RetrievePatternValue(lockableBitmap);
                double[]          processedPattern = hopfieldNeuralNetwork.Determine(pattern);

                ApplyPatternValues(processedPattern, outBitmap);
            }
            finally
            {
                lockableBitmap.UnlockBits();
                outBitmap.UnlockBits();
            }

            return(copiedSource);
        }
Ejemplo n.º 8
0
        private void CalculateGradientsForAperture(int x, int y, LockableBitmap source)
        {
            double dx = 0.0;
            double dy = 0.0;

            int centerX = convolutionX.GetLength(0) / 2;
            int centerY = convolutionY.GetLength(0) / 2;

            for (int i = 0; i < convolutionX.GetLength(0); i++)
            {
                for (int j = 0; j < convolutionY.GetLength(0); j++)
                {
                    int indexX = x + (i - centerX);
                    int indexY = y + (j - centerY);

                    dx += apertureSelector.Invoke(indexX, indexY, x, y, source)
                          * convolutionX[j, i];

                    dy += apertureSelector.Invoke(indexX, indexY, x, y, source)
                          * convolutionY[j, i];
                }
            }

            dfx [x, y] = dx;
            dfy [x, y] = dy;
            dfxy[x, y] = dx * dy;
        }
Ejemplo n.º 9
0
 public SobelImageEnumerator(LockableBitmap source)
 {
     if (source == null)
     {
         throw new ArgumentNullException("source");
     }
     this.source = source;
 }
Ejemplo n.º 10
0
        public AverageBrightness(LockableBitmap source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            this.source = source;
        }
Ejemplo n.º 11
0
        private void InitializeGradients(LockableBitmap source)
        {
            dfx  = new double[source.Width, source.Height];
            dfy  = new double[source.Width, source.Height];
            dfxy = new double[source.Width, source.Height];

            for (int x = 0; x < source.Width; x++)
            {
                for (int y = 0; y < source.Height; y++)
                {
                    CalculateGradientsForAperture(x, y, source);
                }
            }
        }
Ejemplo n.º 12
0
        private void ApplyPatternValues(IEnumerable <double> processedPattern, LockableBitmap source)
        {
            Color[] colors = processedPattern.Select(BinaryToColor).ToArray();

            int colorIndex = 0;

            for (int x = 0; x < source.Width; x++)
            {
                for (int y = 0; y < source.Height; y++)
                {
                    source.SetPixel(x, y, colors[colorIndex]);

                    colorIndex++;
                }
            }
        }
Ejemplo n.º 13
0
        public void AddPattern(Bitmap source)
        {
            LockableBitmap lockableBitmap = new LockableBitmap(source);

            lockableBitmap.LockBits();

            try
            {
                IPattern <double> pattern = RetrievePatternValue(lockableBitmap);
                hopfieldNeuralNetwork.Teach(pattern);
            }
            finally
            {
                lockableBitmap.UnlockBits();
            }
        }
Ejemplo n.º 14
0
        private void FillRegion(
            Region region,
            LockableBitmap lockableBitmap)
        {
            Color color;

            if (!ColorMapper.TryMap(region.Number, out color))
            {
                color = colorGenerator.RandomColor();
            }

            foreach (ScanInfo info in region.Infos)
            {
                lockableBitmap.SetPixel(info.Coords.X, info.Coords.Y, color);
            }
        }
Ejemplo n.º 15
0
        private void FillRegion(
            int regionIndex,
            KeyValuePair <int, List <ScanInfo> > region,
            LockableBitmap lockableBitmap)
        {
            Color color;

            if (!ColorMapper.TryMap(regionIndex, out color))
            {
                color = colorGenerator.RandomColor();
            }

            foreach (ScanInfo info in region.Value)
            {
                lockableBitmap.SetPixel(info.Coords.X, info.Coords.Y, color);
            }
        }
Ejemplo n.º 16
0
        public LockableBitmapWrapper(LockableBitmap source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            this.source = source;

            infos = new ScanInfo[source.Width, source.Height];

            for (int y = 0; y < source.Height; y++)
            {
                for (int x = 0; x < source.Width; x++)
                {
                    infos[x, y] = new ScanInfo(source.GetPixel(x, y), new Point(x, y));
                }
            }
        }
Ejemplo n.º 17
0
        private IPattern <double> RetrievePatternValue(LockableBitmap lockableBitmap)
        {
            List <double> patternValues = new List <double>();

            for (int x = 0; x < lockableBitmap.Width; x++)
            {
                for (int y = 0; y < lockableBitmap.Height; y++)
                {
                    Color processedColor = perItemAlgorithm
                                           .Execute(lockableBitmap.GetPixel(x, y));

                    patternValues.Add(
                        ColorToBinary(processedColor));
                }
            }

            return(new Pattern(patternValues.ToArray()));
        }
Ejemplo n.º 18
0
        public Bitmap Process(Bitmap source, IMultiFilter filter)
        {
            Bitmap         copiedSource = (Bitmap)source.Clone();
            LockableBitmap outBitmap    = new LockableBitmap(copiedSource);

            outBitmap.LockBits();

            LockableBitmap processedBitmap = new LockableBitmap(source);

            processedBitmap.LockBits();

            try
            {
                GaussEnumerator enumerator = new GaussEnumerator(processedBitmap);

                while (enumerator.MoveNext())
                {
                    try
                    {
                        Color processedColor = filter.Execute(enumerator.Current);

                        ApertureItem processedApertureItem =
                            enumerator.Current[enumerator.Current.GetLength(0) / 2, enumerator.Current.GetLength(0) / 2];

                        ProcessOutput(processedApertureItem, processedColor, outBitmap);
                    }
                    catch (Exception exception)
                    {
                        Log.Current.Error(exception);
                    }
                }
            }
            finally
            {
                processedBitmap.UnlockBits();
                outBitmap.UnlockBits();
            }

            return(copiedSource);
        }
Ejemplo n.º 19
0
        public IClusteringAlgorithm <double> Create(
            int clustersNumber,
            LockableBitmap source)
        {
            ISign <double>[] signs =
            {
                //new AverageBrightness(source),
                //new AverageColor(source),
                //new BrightnessDispersion(),
                new Density(new Square(),       new Perimeter()),
                new Elongation(),
                new PrincipalAxisOrientation(),
                new Square(),
                new Perimeter(),
            };

            return(new Kmedian.Implementation.Kmedian(
                       clustersNumber,
                       signs,
                       new EuclideanDistance(),
                       new StartSignsVectorGenerator(),
                       new MedianAlgorithm()));
        }
Ejemplo n.º 20
0
        public СharacteristicPoint[] Detect(LockableBitmap source)
        {
            InitializeGradients(source);

            СharacteristicPoint[,] responces = new СharacteristicPoint[source.Width, source.Height];

            for (int y = 0; y < source.Height; y++)
            {
                for (int x = 0; x < source.Width; x++)
                {
                    double response = responseAngle.Calculate(dfx[x, y], dfy[x, y], dfxy[x, y]);

                    response = response > threshold ? response : default(double);

                    responces[x, y] = new СharacteristicPoint(
                        new Point(x, y),
                        response);
                }
            }

            List <СharacteristicPoint> filteredPoints = DetectLocalMaximumPoints(responces);

            return(filteredPoints.ToArray());
        }
Ejemplo n.º 21
0
 protected static void ProcessOutput(Aperture aperture, LockableBitmap outBitmap, Color filteredColor)
 {
     outBitmap.SetPixel(aperture.CurrentX, aperture.CurrentY, filteredColor);
 }
Ejemplo n.º 22
0
        protected virtual void ExecuteStep(Aperture aperture, IFilter filter, LockableBitmap processed, LockableBitmap outBitmap)
        {
            Color filteredColor = filter.Execute(aperture.CurrentColor, ref aperture);

            ProcessOutput(aperture, outBitmap, filteredColor);
        }
Ejemplo n.º 23
0
        public IDictionary <int, List <ScanInfo> > Scan(Bitmap source)
        {
            LockableBitmap lockableBitmap = new LockableBitmap(source);

            lockableBitmap.LockBits();
            LockableBitmapWrapper lockableBitmapWrapper = new LockableBitmapWrapper(lockableBitmap);

            try
            {
                SequentialEnumerator enumerator = new SequentialEnumerator(lockableBitmapWrapper);
                while (enumerator.MoveNext())
                {
                    AbcMask mask = enumerator.Current;
                    if (mask.Current.Color.IsWhite())
                    {
                        if (!mask.Left.IsLabeled && !mask.Top.IsLabeled)
                        {
                            if (regions.Count > 0)
                            {
                                mask.Current.RegionNumber = regions.Keys.Max() + 1;
                            }
                            else
                            {
                                mask.Current.RegionNumber = 0;
                            }

                            AddRegionAndMove(
                                regionNumber: mask.Current.RegionNumber,
                                info: mask.Current);
                        }
                        else if (!mask.Left.IsLabeled && mask.Top.IsLabeled)
                        {
                            mask.Current.RegionNumber = mask.Top.RegionNumber;

                            Move(regionNumber: mask.Current.RegionNumber, info: mask.Current);
                        }
                        else if (mask.Left.IsLabeled && !mask.Top.IsLabeled)
                        {
                            mask.Current.RegionNumber = mask.Left.RegionNumber;

                            Move(regionNumber: mask.Current.RegionNumber, info: mask.Current);
                        }
                        else if (mask.Left.IsLabeled && mask.Top.IsLabeled)
                        {
                            if (mask.Left.RegionNumber == mask.Top.RegionNumber)
                            {
                                mask.Current.RegionNumber = mask.Left.RegionNumber;

                                Move(regionNumber: mask.Current.RegionNumber, info: mask.Current);
                            }
                            else
                            {
                                ReAllocate(mask);
                            }
                        }
                        mask.Current.IsLabeled = true;
                    }
                }
            }
            finally
            {
                lockableBitmap.UnlockBits();
            }

            return(regions);
        }
Ejemplo n.º 24
0
 private void ProcessOutput(ApertureItem processedApertureItem, Color processedColor, LockableBitmap outBitmap)
 {
     outBitmap.SetPixel(
         processedApertureItem.Coords.X,
         processedApertureItem.Coords.Y,
         processedColor);
 }