Esempio n. 1
0
        public float[] SegmentImage(float[] image, int rows, int columns, out int[,] mask, bool useCuda = false)
        {
            if (useCuda)
            {
                var maskRows       = (int)Math.Ceiling((double)rows / Constants.SegmentationWindowSize);
                var maskColumns    = (int)Math.Ceiling((double)columns / Constants.SegmentationWindowSize);
                var maskLinearized = new int[maskRows * maskColumns];

                var sw = new Stopwatch();
                sw.Start();
                CUDASegmentator(image, columns, rows,
                                (float)Constants.SegmentationWeight, Constants.SegmentationWindowSize, maskLinearized, maskColumns, maskRows);
                sw.Stop();
                mask = maskLinearized.Make2D(maskRows, maskColumns);

                Segmentator.PostProcessing(mask, Constants.SegmentationThreshold);

                var bigMask = Segmentator.GetBigMask(mask, rows, columns,
                                                     Constants.SegmentationWindowSize);

                // we discard border regions in order to not catch minutiae there
                var oldMask = mask;
                mask = oldMask.Select2D((x, row, column) =>
                {
                    if (x == 0)
                    {
                        return(0);
                    }
                    if (row > 0)
                    {
                        if (oldMask[row - 1, column] == 0)
                        {
                            return(0);
                        }
                        if (column > 0)
                        {
                            if (oldMask[row - 1, column - 1] == 0)
                            {
                                return(0);
                            }
                        }
                        if (column < maskColumns - 1)
                        {
                            if (oldMask[row - 1, column + 1] == 0)
                            {
                                return(0);
                            }
                        }
                        if (row < maskRows - 1)
                        {
                            if (oldMask[row + 1, column] == 0)
                            {
                                return(0);
                            }
                            if (column > 0)
                            {
                                if (oldMask[row + 1, column - 1] == 0)
                                {
                                    return(0);
                                }
                            }
                            if (column < maskColumns - 1)
                            {
                                if (oldMask[row + 1, column + 1] == 0)
                                {
                                    return(0);
                                }
                            }
                        }
                    }
                    if (column > 0)
                    {
                        if (oldMask[row, column - 1] == 0)
                        {
                            return(0);
                        }
                    }
                    if (column < maskColumns - 1)
                    {
                        if (oldMask[row, column + 1] == 0)
                        {
                            return(0);
                        }
                    }
                    return(1);
                });


                return(Segmentator.ColorImage(image, rows, columns, bigMask));
            }
            mask = Segmentator.Segmetator(image.Make2D(rows, columns).Select2D(x => (double)x),
                                          Constants.SegmentationWindowSize, Constants.SegmentationWeight,
                                          Constants.SegmentationThreshold);
            return(Segmentator.ColorImage(image.Make2D(rows, columns).Select2D(x => (double)x),
                                          Segmentator.GetBigMask(mask, image.GetLength(0), image.GetLength(1),
                                                                 Constants.SegmentationWindowSize)).Make1D().Select(x => (float)x).ToArray());
        }
Esempio n. 2
0
 public static double[,] SegmentImage(double[,] image, int rows, int columns, out int[,] mask)
 {
     mask = Segmentator.Segmetator(image, Constants.SegmentationWindowSize, Constants.SegmentationWeight, Constants.SegmentationThreshold);
     return(Segmentator.ColorImage(image, Segmentator.GetBigMask(mask, image.GetLength(0), image.GetLength(1), Constants.SegmentationWindowSize)));
 }