Advertisement
Vita94

C# SimilarityFactorMMS

Apr 25th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.56 KB | None | 0 0
  1.         //-----------------------------------------------------------------------------------------------------------------------------
  2.         private int SimilarityFactor(Color A, Color B)
  3.         {
  4.             return (int)Math.Sqrt(Math.Pow((A.R - B.R), 2) + Math.Pow((A.G - B.G), 2) + Math.Pow((A.B - B.B), 2));
  5.         }
  6.         public Bitmap ColorSimilarityFilter(Bitmap bmp, Color color, Point startPosition, int similarityFactor)
  7.         {
  8.             Bitmap newBmp = new Bitmap(bmp);
  9.             HashSet<Point> pointsToCompare = new HashSet<Point>(); // O(1)
  10.             HashSet<Point> pointsDone = new HashSet<Point>();
  11.             pointsToCompare.Add(startPosition); //first point to compare
  12.  
  13.             Color Y = bmp.GetPixel(startPosition.X, startPosition.Y);
  14.             Color X = color;
  15.  
  16.             while (pointsToCompare.Count != 0)
  17.             {
  18.                 //
  19.                 Point T = pointsToCompare.First();
  20.  
  21.                 //up
  22.                 if (T.Y - 1 > 0  && !pointsDone.Contains(new Point(T.X, T.Y - 1)))
  23.                 {
  24.                     if (SimilarityFactor(Y, bmp.GetPixel(T.X, T.Y - 1)) <= similarityFactor)
  25.                         pointsToCompare.Add(new Point(T.X, T.Y - 1));
  26.                 }
  27.                    
  28.                 //down
  29.                 if (T.Y + 1 < bmp.Height && !pointsDone.Contains(new Point(T.X, T.Y + 1)))
  30.                 {
  31.                     if (SimilarityFactor(Y, bmp.GetPixel(T.X, T.Y + 1)) <= similarityFactor)
  32.                         pointsToCompare.Add(new Point(T.X, T.Y + 1));
  33.                 }
  34.                    
  35.                 //left
  36.                 if (T.X - 1 > 0 && !pointsDone.Contains(new Point(T.X - 1, T.Y)))
  37.                 {
  38.                     if (SimilarityFactor(Y, bmp.GetPixel(T.X - 1, T.Y)) <= similarityFactor)
  39.                         pointsToCompare.Add(new Point(T.X - 1, T.Y));
  40.                 }
  41.                    
  42.                 //right
  43.                 if (T.X + 1 < bmp.Width && !pointsDone.Contains(new Point(T.X + 1, T.Y)))
  44.                 {
  45.                     if (SimilarityFactor(Y, bmp.GetPixel(T.X + 1, T.Y)) <= similarityFactor)
  46.                         pointsToCompare.Add(new Point(T.X + 1, T.Y));
  47.                 }
  48.                    
  49.  
  50.                 newBmp.SetPixel(T.X, T.Y, X);
  51.                 pointsDone.Add(T);
  52.                 pointsToCompare.Remove(T);
  53.  
  54.             }
  55.  
  56.             return newBmp;
  57.  
  58.         }
  59.         //-----------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement