Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.94 KB | None | 0 0
  1. public static class OpeningClosingTools
  2.     {
  3.         public static byte[,] MakeStructuralElement(int linelen, int degrees)
  4.         {
  5.             if (degrees > 180)
  6.                 degrees = degrees - 180;
  7.  
  8.             double alpha = degrees * Math.PI / 180;
  9.  
  10.             double ray = Math.Ceiling(((double)linelen - 1) / 2);
  11.  
  12.             int columns = (int)(Math.Ceiling(linelen * Math.Cos(alpha))) + 1;
  13.             int rows = (int)(Math.Ceiling(linelen * Math.Sin(alpha))) + 1;
  14.  
  15.             if (columns < 0)
  16.                 columns = (columns - 1) * (-1);
  17.  
  18.             if (rows < 0)
  19.                 rows = (rows - 1) * (-1);
  20.  
  21.             if (degrees > 90)
  22.                 columns++;
  23.  
  24.             if (columns % 2 == 0)
  25.                 columns--;
  26.  
  27.             if (rows % 2 == 0)
  28.                 rows--;
  29.  
  30.             byte[,] line = new byte[rows, columns];
  31.             double a;
  32.  
  33.             double counter = ((rows - 1) / 2);
  34.             double denominator = ((columns - 1) / 2);
  35.             a = counter / denominator;
  36.  
  37.  
  38.             if (double.IsInfinity(a))
  39.             {
  40.                 for (int i = 0; i < rows; i++)
  41.                 {
  42.                     line[i, 0] = 1;
  43.                 }
  44.             }
  45.             else
  46.             {
  47.                 for (int i = 0; i < columns; i++)
  48.                 {
  49.                     double y = (rows - 1) - (a * i);
  50.  
  51.                     if (i < ((columns - 1) / 2))
  52.                     {
  53.                         y = Math.Ceiling(y);
  54.                     }
  55.                     else if (i > ((columns - 1) / 2))
  56.                     {
  57.                         y = Math.Floor(y);
  58.                     }
  59.  
  60.  
  61.                     line[(int)y, i] = 1;
  62.  
  63.                 }
  64.             }
  65.  
  66.             if (degrees > 90)
  67.             {
  68.                 for (int i = 0; i < rows; i++)
  69.                 {
  70.                     for (int j = 0; j < columns; j++)
  71.                     {
  72.                         if ((i > (rows - 1) / 2) && (j > (columns - 1) / 2))
  73.                             break;
  74.  
  75.                         if (line[i, j] == 1)
  76.                         {
  77.                             int newindex = (columns - 1) - j;
  78.                             line[i, j] = 0;
  79.                             line[i, newindex] = 1;
  80.                         }
  81.                     }
  82.                 }
  83.             }
  84.  
  85.             return line;
  86.         }
  87.  
  88.        
  89.         public static Bitmap DilateAndErodeFilter(Bitmap sourceBitmap, byte[,] structuralElement, MorphologyType morphType)
  90.         {
  91.             //Create image dimension variables for convenience
  92.             int width = sourceBitmap.Width;
  93.             int height = sourceBitmap.Height;
  94.  
  95.             //Lock bits to system memory for fast processing
  96.             Rectangle canvas = new Rectangle(0, 0, width, height);
  97.  
  98.             BitmapData srcData = sourceBitmap.LockBits(canvas, ImageLockMode.ReadOnly,
  99.         PixelFormat.Format32bppArgb);
  100.  
  101.             int stride = srcData.Stride;
  102.             int bytes = stride * srcData.Height;
  103.  
  104.             //Create byte arrays that will hold all pixel data, one for processing, one for output
  105.             byte[] pixelBuffer = new byte[bytes];
  106.             byte[] resultBuffer = new byte[bytes];
  107.  
  108.             //Write pixel data to array meant for processing
  109.             Marshal.Copy(srcData.Scan0, pixelBuffer, 0, bytes);
  110.             sourceBitmap.UnlockBits(srcData);
  111.  
  112.             int rows = structuralElement.GetLength(0);
  113.             int columns = structuralElement.GetLength(1);
  114.  
  115.             int horizontalOffset = (rows - 1) / 2;
  116.             int verticalOffset = (columns - 1) / 2;
  117.  
  118.             int calcOffset = 0;
  119.             int byteOffset = 0;
  120.  
  121.             for (int x = horizontalOffset; x < width - horizontalOffset; x++)
  122.             {
  123.                 for (int y = verticalOffset; y < height - verticalOffset; y++)
  124.                 {
  125.                     byte value = 0;
  126.  
  127.                     if (morphType == MorphologyType.Erosion)
  128.                         value = 255;
  129.  
  130.                     byteOffset = y * stride + x * 4;
  131.  
  132.                     for (int xMatrix = 0; xMatrix < rows; xMatrix++)
  133.                     {
  134.                         for (int yMatrix = 0; yMatrix < columns; yMatrix++)
  135.                         {
  136.                             if (structuralElement[xMatrix, yMatrix] == 1)
  137.                             {
  138.                                 calcOffset = byteOffset + xMatrix * stride + yMatrix * 4;
  139.  
  140.                                 if (calcOffset >= bytes)
  141.                                 {
  142.                                     continue;
  143.                                 }
  144.  
  145.                                 if (morphType == MorphologyType.Dilation)
  146.                                 {
  147.                                     value = Math.Max(value, pixelBuffer[calcOffset]);
  148.                                 }
  149.                                 else if (morphType == MorphologyType.Erosion)
  150.                                 {
  151.                                     value = Math.Min(value, pixelBuffer[calcOffset]);
  152.                                 }
  153.                             }
  154.                             else
  155.                             {
  156.                                 continue;
  157.                             }
  158.                         }
  159.                     }
  160.  
  161.                     //Write processed data into the second array
  162.                     resultBuffer[byteOffset] = value;
  163.                     resultBuffer[byteOffset + 1] = value;
  164.                     resultBuffer[byteOffset + 2] = value;
  165.                     resultBuffer[byteOffset + 3] = 255;
  166.  
  167.                 }
  168.             }
  169.  
  170.             //Create output bitmap of this function
  171.             Bitmap rsltImg = new Bitmap(width, height);
  172.             BitmapData rsltData = rsltImg.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
  173.  
  174.             //Write processed data into bitmap form
  175.             Marshal.Copy(resultBuffer, 0, rsltData.Scan0, bytes);
  176.             rsltImg.UnlockBits(rsltData);
  177.             return rsltImg;
  178.         }
  179.  
  180.         public static Bitmap OpenMorphologyFilter(this Bitmap sourceBitmap,
  181.                                                  byte[,] structuralElement)
  182.         {
  183.             Bitmap resultBitmap = DilateAndErodeFilter(sourceBitmap, structuralElement, MorphologyType.Erosion);
  184.             return DilateAndErodeFilter(resultBitmap, structuralElement, MorphologyType.Dilation);
  185.         }
  186.  
  187.         public static Bitmap CloseMorphologyFilter(this Bitmap sourceBitmap,
  188.                                                  byte[,] structuralElement)
  189.         {
  190.             Bitmap resultBitmap = DilateAndErodeFilter(sourceBitmap, structuralElement, MorphologyType.Dilation);
  191.             return DilateAndErodeFilter(resultBitmap, structuralElement, MorphologyType.Erosion);
  192.         }
  193.  
  194.         public enum MorphologyType
  195.         {
  196.             Dilation,
  197.             Erosion
  198.         }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement