Beispiel #1
0
        private static void HysteresisConnect(PNM image, int index, int width, int height, byte lowValue, byte highValue)
        {
            int x = index % width;
            int y = index / height;

            for (int x0 = x - 1; x0 < x + 1; x0++)
            {
                for (int y0 = y - 1; y0 < y + 1; y0++)
                {
                    int currentIndex = (width * y0) + x0;
                    if (!IsPixelOnEdge(currentIndex, width, height))
                    {
                        byte l;
                        image.GetPixel(currentIndex, out l, out l, out l);
                        if (l != 255)
                        {
                            if (l >= lowValue)
                            {
                                image.SetPixel(currentIndex, 255, 255, 255);
                                HysteresisConnect(image, currentIndex, width, height, lowValue, highValue);
                            }
                        }
                    }
                }
            }
        }
Beispiel #2
0
 public static PNM ApplyDistanceTransform(this PNM image)
 {
     PNM binaryImage = image.ApplyCannyDetector();
     int pixelCount = 0;
     double[][] distanceMap = ToInitialDistanceMap(binaryImage, ref pixelCount);
     CalculateDistances(distanceMap, pixelCount);
     PNM distancedImage = new PNM(image.Width, image.Height);
     for(int i=0; i < image.Width * image.Height; i++)
     {
         byte distance = Filter.Coerce(distanceMap[i % image.Width][i / image.Width]);
         distancedImage.SetPixel(i, distance, distance, distance);
     }
     return distancedImage;
 }
Beispiel #3
0
        public static PNM ApplyDistanceTransform(this PNM image)
        {
            PNM binaryImage = image.ApplyCannyDetector();
            int pixelCount  = 0;

            double[][] distanceMap = ToInitialDistanceMap(binaryImage, ref pixelCount);
            CalculateDistances(distanceMap, pixelCount);
            PNM distancedImage = new PNM(image.Width, image.Height);

            for (int i = 0; i < image.Width * image.Height; i++)
            {
                byte distance = Filter.Coerce(distanceMap[i % image.Width][i / image.Width]);
                distancedImage.SetPixel(i, distance, distance, distance);
            }
            return(distancedImage);
        }
Beispiel #4
0
        private static PNM ApplyHysteresis(byte[] suppressed, int width, int height, double low, double high)
        {
            PNM  image     = new PNM(width, height);
            byte lowValue  = (byte)(low * 255);
            byte highValue = (byte)(high * 255);

            for (int i = 0; i < suppressed.Length; i++)
            {
                if (suppressed[i] >= highValue)
                {
                    image.SetPixel(i, 255, 255, 255);
                    HysteresisConnect(image, i, width, height, lowValue, highValue);
                }
            }
            return(image);
        }
Beispiel #5
0
        public static PNM ApplyZeroCrossingDetector(this PNM image)
        {
            // preprare
            PNM workImage = PNM.Copy(image);

            Filter.Pad(workImage, 4);
            // apply loG
            Tuple <float[], float[], float[]> LoGRaster = Filter.ApplyConvolutionUnbound(workImage, LoG, 9);
            PNM returnImage = new PNM(image.Width, image.Height);

            // Apply zero crossing except last row and last column
            Parallel.For(0, image.Height - 1, i =>
            {
                for (int j = 0; j < image.Width - 1; j++)
                {
                    byte r = 0;
                    byte g = 0;
                    byte b = 0;
                    // current index position
                    int position     = i * image.Width + j;
                    float currentR   = LoGRaster.Item1[position];
                    float neighbourR = LoGRaster.Item1[position + image.Width + 1];
                    float currentG   = LoGRaster.Item2[position];
                    float neighbourG = LoGRaster.Item2[position + image.Width + 1];
                    float currentB   = LoGRaster.Item3[position];
                    float neighbourB = LoGRaster.Item3[position + image.Width + 1];
                    if ((currentR * neighbourR) < 0 && (Math.Abs(currentR) < Math.Abs(neighbourR)))
                    {
                        r = 255;
                    }
                    if ((currentG * neighbourG) < 0 && (Math.Abs(currentG) < Math.Abs(neighbourG)))
                    {
                        g = 255;
                    }
                    if ((currentB * neighbourB) < 0 && (Math.Abs(currentB) < Math.Abs(neighbourB)))
                    {
                        b = 255;
                    }

                    returnImage.SetPixel(position, r, g, b);
                }
            });
            return(returnImage);
        }
Beispiel #6
0
        public static PNM ApplyZeroCrossingDetector(this PNM image)
        {
            // preprare
            PNM workImage = PNM.Copy(image);
            Filter.Pad(workImage, 4);
            // apply loG
            Tuple<float[], float[], float[]> LoGRaster = Filter.ApplyConvolutionUnbound(workImage, LoG, 9);
            PNM returnImage = new PNM(image.Width, image.Height);
            // Apply zero crossing except last row and last column
            Parallel.For(0, image.Height - 1, i =>
            {
                for (int j = 0; j < image.Width - 1; j++)
                {
                    byte r = 0;
                    byte g = 0;
                    byte b = 0;
                    // current index position
                    int position = i * image.Width + j;
                    float currentR = LoGRaster.Item1[position];
                    float neighbourR = LoGRaster.Item1[position + image.Width + 1];
                    float currentG = LoGRaster.Item2[position];
                    float neighbourG = LoGRaster.Item2[position + image.Width + 1];
                    float currentB = LoGRaster.Item3[position];
                    float neighbourB = LoGRaster.Item3[position + image.Width + 1];
                    if ((currentR * neighbourR) < 0 && (Math.Abs(currentR) < Math.Abs(neighbourR)))
                        r = 255;
                    if ((currentG * neighbourG) < 0 && (Math.Abs(currentG) < Math.Abs(neighbourG)))
                        g = 255;
                    if ((currentB * neighbourB) < 0 && (Math.Abs(currentB) < Math.Abs(neighbourB)))
                        b = 255;

                    returnImage.SetPixel(position, r, g, b);
                }
            });
            return returnImage;
        }
Beispiel #7
0
 private static void HysteresisConnect(PNM image, int index, int width, int height, byte lowValue, byte highValue)
 {
     int x = index % width;
     int y = index / height;
     for (int x0 = x - 1; x0 < x + 1; x0++)
     {
         for (int y0 = y - 1; y0 < y + 1; y0++)
         {
             int currentIndex = (width * y0) + x0;
             if (!IsPixelOnEdge(currentIndex, width, height))
             {
                 byte l;
                 image.GetPixel(currentIndex, out l, out l, out l);
                 if (l != 255)
                 {
                     if (l >= lowValue)
                     {
                         image.SetPixel(currentIndex, 255, 255, 255);
                         HysteresisConnect(image, currentIndex, width, height, lowValue, highValue);
                     }
                 }
             }
         }
     }
 }
Beispiel #8
0
 private static PNM ApplyHysteresis(byte[] suppressed, int width, int height, double low, double high)
 {
     PNM image = new PNM(width, height);
     byte lowValue = (byte)(low*255);
     byte highValue = (byte)(high*255);
     for (int i = 0; i < suppressed.Length; i++)
     {
         if (suppressed[i] >= highValue)
         {
             image.SetPixel(i, 255, 255, 255);
             HysteresisConnect(image, i, width, height, lowValue, highValue);
         }
     }
     return image;
 }