示例#1
0
        public static int Blackout(ByteImage btm)
        {
            int    pixAmount = 0;
            double sum       = 0;

            for (int x = (int)(0.15 * btm.Width); x < btm.Width - (int)(0.15 * btm.Width); x++)
            {
                for (int y = (int)(0.15 * btm.Height); y < btm.Height - (int)(0.15 * btm.Height); y++)
                {
                    byte[] oldColour;
                    oldColour = btm.getPixel(x, y);
                    sum      += (double)oldColour[1];
                    pixAmount++;
                }
            }
            int treshold = (int)sum / pixAmount;

            for (int x = 0; x < btm.Width; x++)
            {
                for (int y = 0; y < btm.Height; y++)
                {
                    byte[] oldColour;
                    oldColour = btm.getPixel(x, y);
                    var value = oldColour[1] < ((byte)(1.2 * treshold)) ? 0 : oldColour[1];
                    btm.setPixel(x, y, oldColour[0], (byte)value, (byte)value, (byte)value);
                }
            }
            return(treshold);
        }
示例#2
0
        public static void Binarization(ByteImage btm, int suggestedtreshold)
        {
            int    pixAmount = 0;
            double sum       = 0;

            for (int x = (int)(0.15 * btm.Width); x < btm.Width - (int)(0.15 * btm.Width); x++)
            {
                for (int y = (int)(0.15 * btm.Height); y < btm.Height - (int)(0.15 * btm.Height); y++)
                {
                    byte[] oldColour;
                    oldColour = btm.getPixel(x, y);
                    sum      += (double)oldColour[1];
                    pixAmount++;
                }
            }
            int treshold = (int)sum / pixAmount;

            if (suggestedtreshold > 0)
            {
                treshold = suggestedtreshold;
            }
            for (int x = 0; x < btm.Width; x++)
            {
                for (int y = 0; y < btm.Height; y++)
                {
                    byte[] oldColour;
                    oldColour = btm.getPixel(x, y);
                    var value = oldColour[1] < treshold ? 0 : 255;
                    btm.setPixel(x, y, oldColour[0], (byte)value, (byte)value, (byte)value);
                }
            }
        }
示例#3
0
        public static byte[] MarkFaceCenter(ByteImage btm, int X, int Y)
        {
            var currPix = btm.getPixel(X, Y);

            byte[] newCol = new byte[] { currPix[0], (byte)220, (byte)0, (byte)0 };

            btm.setPixel(X, Y, newCol);
            btm.setPixel(X - 1, Y, newCol);
            btm.setPixel(X + 1, Y, newCol);

            btm.setPixel(X + 1, Y + 1, newCol);
            btm.setPixel(X, Y - 1, newCol);
            btm.setPixel(X, Y + 1, newCol);

            btm.setPixel(X - 1, Y - 1, newCol);
            btm.setPixel(X - 2, Y, newCol);
            btm.setPixel(X + 2, Y, newCol);

            btm.setPixel(X - 2, Y - 2, newCol);
            btm.setPixel(X + 2, Y + 2, newCol);
            btm.setPixel(X, Y - 2, newCol);
            btm.setPixel(X, Y + 2, newCol);

            return(newCol);
        }
示例#4
0
 public static void Test(ByteImage btm)
 {
     for (int x = 0; x < btm.Width; x++)
     {
         for (int y = 0; y < btm.Height; y++)
         {
             byte[] oldColour;
             oldColour = btm.getPixel(x, y);
             var value = CastColor((byte)(Math.Abs((double)oldColour[1] - (double)oldColour[2])));
             btm.setPixel(x, y, oldColour[0], (byte)value, (byte)value, (byte)value);
         }
     }
 }
示例#5
0
 public static void StretchColors(ByteImage btm, int treshold, double a, double b)
 {
     for (int x = 0; x < btm.Width; x++)
     {
         for (int y = 0; y < btm.Height; y++)
         {
             byte[] oldColour;
             oldColour = btm.getPixel(x, y);
             var value = oldColour[1] < treshold ? (byte)(a * (double)oldColour[1]) : (byte)(b * (double)oldColour[1]);
             btm.setPixel(x, y, oldColour[0], (byte)value, (byte)value, (byte)value);
         }
     }
 }
示例#6
0
 public static void GrayScale(ByteImage btm)
 {
     for (int x = 0; x < btm.Width; x++)
     {
         for (int y = 0; y < btm.Height; y++)
         {
             byte[] oldColour;
             oldColour = btm.getPixel(x, y);
             var value = (0.2 * (double)oldColour[1]) + (0.7 * (double)oldColour[2]) + (0.1 * (double)oldColour[3]);
             btm.setPixel(x, y, oldColour[0], (byte)value, (byte)value, (byte)value);
         }
     }
 }
示例#7
0
        private static ByteImage CutOffFace(List <Point> corners, ByteImage ori)
        {
            int       width  = corners.ElementAt(1).X - corners.ElementAt(0).X;
            int       height = corners.ElementAt(2).Y - corners.ElementAt(0).Y;
            ByteImage face   = new ByteImage(width, height);

            for (int x = 0; x < face.Width; x++)
            {
                for (int y = 0; y < face.Height; y++)
                {
                    face.setPixel(x, y, ori.getPixel(x + corners.ElementAt(0).X, y + corners.ElementAt(0).Y));
                }
            }
            return(face);
        }
示例#8
0
        public static void GaussFiltr(ByteImage btm)
        {
            double a = 2.0;

            double[,] wages = new double[5, 5] {
                { 1, 1, 1, 1, 1 }, { 1, 1, a, 1, 1 }, { 1, a, a *a, a, 1 }, { 1, 1, a, 1, 1 }, { 1, 1, 1, 1, 1 }
            };

            for (int x = 0; x < btm.Width; x++)
            {
                for (int y = 0; y < btm.Height; y++)
                {
                    var    currpix = btm.getPixel(x, y);
                    double sumR    = 0.0;
                    double sumG    = 0.0;
                    double sumB    = 0.0;
                    double dividor = 0.0;
                    for (int x2 = -2; x2 < 3; x2++)
                    {
                        for (int y2 = -2; y2 < 3; y2++)
                        {
                            if (x + x2 >= 0 && y + y2 >= 0 && x + x2 < btm.Width && y + y2 < btm.Height)
                            {
                                double currWage = wages[x2 + 2, y2 + 2];
                                var    pix      = btm.getPixel(x + x2, y + y2);
                                sumR    += (double)(pix[1]) * currWage;
                                sumG    += (double)(pix[2]) * currWage;
                                sumB    += (double)(pix[3]) * currWage;
                                dividor += currWage;
                            }
                        }
                    }
                    btm.setPixel(x, y, currpix[0], CastColor((byte)(sumR / dividor)), CastColor((byte)(sumG / dividor)), CastColor((byte)(sumB / dividor)));
                }
            }
        }
示例#9
0
 public static int[] GetVerticalHistogram(ByteImage btm, byte comparedColor)
 {
     int[] histogram = new int[btm.Height];
     for (int y = 0; y < btm.Height; y++)
     {
         for (int x = 0; x < btm.Width; x++)
         {
             byte[] oldColour;
             oldColour = btm.getPixel(x, y);
             var value = oldColour[1] == comparedColor ? 1 : 0;
             histogram[y] += value;
         }
     }
     return(histogram);
 }
示例#10
0
        public static int GetTreshold(ByteImage btm)
        {
            int    pixAmount = 0;
            double sum       = 0;

            for (int x = 0; x < btm.Width; x++)
            {
                for (int y = 0; y < btm.Height; y++)
                {
                    byte[] oldColour;
                    oldColour = btm.getPixel(x, y);
                    sum      += (double)oldColour[1];
                    pixAmount++;
                }
            }
            int treshold = (int)sum / pixAmount;

            return(treshold);
        }
示例#11
0
        private static int GetSkinCollor(ByteImage picture)
        {
            int iteration    = 15;
            int colorCounter = 3;

            int[] Sums    = new int[colorCounter];
            int[] Counter = new int[colorCounter];
            int[] Values  = new int[colorCounter];
            for (int i = 0; i < colorCounter; i++)
            {
                Values[i]  = (255 / colorCounter) * i;
                Sums[i]    = 0;
                Counter[i] = 0;
            }

            for (int i = 0; i < iteration; i++)
            {
                for (int x = 0; x < picture.Width; x++)
                {
                    for (int y = 0; y < picture.Height; y++)
                    {
                        byte[] color = picture.getPixel(x, y);
                        int    dist  = 255;
                        int    indx  = 0;
                        for (int c = 0; c < colorCounter; c++)
                        {
                            if (Math.Abs(color[1] - Values[c]) < dist)
                            {
                                dist = Math.Abs(color[1] - Values[c]);
                                indx = c;
                            }
                        }
                        Sums[indx] += color[1];
                        Counter[indx]++;
                    }
                }

                for (int c = 0; c < colorCounter; c++)
                {
                    Values[c]  = Sums[c] / Counter[c];
                    Sums[c]    = 0;
                    Counter[c] = 0;
                }
            }

            for (int x = 0; x < picture.Width; x++)
            {
                for (int y = 0; y < picture.Height; y++)
                {
                    byte[] color = picture.getPixel(x, y);
                    int    dist  = 255;
                    int    indx  = 0;
                    for (int c = 0; c < colorCounter; c++)
                    {
                        if (Math.Abs(color[1] - Values[c]) < dist)
                        {
                            dist = Math.Abs(color[1] - Values[c]);
                            indx = c;
                        }
                    }
                    Sums[indx] += color[1];
                    Counter[indx]++;
                }
            }

            int mostCommonCounter = 0;
            int mostCommonIndex   = 0;

            for (int c = 0; c < colorCounter; c++)
            {
                if (Counter[c] > mostCommonCounter)
                {
                    mostCommonCounter = Counter[c];
                    mostCommonIndex   = c;
                }
            }

            /*for (int x = 0; x < picture.Width; x++)
             * {
             *  for (int y = 0; y < picture.Height; y++)
             *  {
             *      byte[] color = picture.getPixel(x, y);
             *      int dist = 255;
             *      int indx = 0;
             *      for (int c = 0; c < colorCounter; c++)
             *      {
             *          if (Math.Abs(color[1] - Values[c]) < dist)
             *          {
             *              dist = Math.Abs(color[1] - Values[c]);
             *              indx = c;
             *          }
             *      }
             *      picture.setPixel(x, y, color[0], (byte)Values[indx], (byte)Values[indx], (byte)Values[indx]);
             *  }
             * }*/

            return(Values[mostCommonIndex]);
        }
示例#12
0
        private static void FloodFill(ByteImage bmp, System.Drawing.Point pt, List <byte[]> targetColors, byte[] replacementColor)
        {
            List <Queue <System.Drawing.Point> > stackList = new List <Queue <System.Drawing.Point> >();

            bool[][] isAdded = new bool[bmp.Width][];
            for (int i = 0; i < isAdded.Length; i++)
            {
                isAdded[i] = new bool[bmp.Height];
                for (int j = 0; j < bmp.Height; j++)
                {
                    isAdded[i][j] = false;
                }
            }
            Queue <System.Drawing.Point> pixels = new Queue <System.Drawing.Point>();

            //ByteImage tempPict = new ByteImage(bmp);
            pixels.Enqueue(pt);
            stackList.Add(pixels);
            isAdded[pt.X][pt.Y] = true;
            int lB = 0;
            int rB = bmp.Width;
            int uB = bmp.Height;
            int bB = 0;

            int stackIndex = 0;

            while (ShouldContinue(stackList))
            {
                System.Drawing.Point a = stackList[stackIndex].Dequeue();

                if (a.X < rB && a.X >= lB &&
                    a.Y < uB && a.Y >= bB)    //make sure we stay within bounds
                {
                    bool isPushed = false;
                    foreach (var c in targetColors)
                    {
                        if (!isPushed && bmp.getPixel(a.X, a.Y)[1] == c[1])
                        {
                            bmp.setPixel(a.X, a.Y, replacementColor);
                            if (a.X - 1 < rB && a.X - 1 >= lB && a.Y < uB && a.Y >= bB && !isAdded[a.X - 1][a.Y])
                            {
                                stackList[stackIndex].Enqueue(new System.Drawing.Point(a.X - 1, a.Y));
                                isAdded[a.X - 1][a.Y] = true;
                            }
                            if (a.X + 1 < rB && a.X + 1 >= lB && a.Y < uB && a.Y >= bB && !isAdded[a.X + 1][a.Y])
                            {
                                stackList[stackIndex].Enqueue(new System.Drawing.Point(a.X + 1, a.Y));
                                isAdded[a.X + 1][a.Y] = true;
                            }
                            if (a.X < rB && a.X >= lB && a.Y - 1 < uB && a.Y - 1 >= bB && !isAdded[a.X][a.Y - 1])
                            {
                                stackList[stackIndex].Enqueue(new System.Drawing.Point(a.X, a.Y - 1));
                                isAdded[a.X][a.Y - 1] = true;
                            }
                            if (a.X < rB && a.X >= lB && a.Y + 1 < uB && a.Y + 1 >= bB && !isAdded[a.X][a.Y + 1])
                            {
                                stackList[stackIndex].Enqueue(new System.Drawing.Point(a.X, a.Y + 1));
                                isAdded[a.X][a.Y + 1] = true;
                            }
                            isPushed = true;
                        }
                    }
                }
                if (stackList[stackIndex].Count > 30000)
                {
                    stackIndex++;
                    stackList.Add(new Queue <System.Drawing.Point>());
                    System.Drawing.Point b = stackList[stackIndex - 1].Dequeue();
                    stackList[stackIndex].Enqueue(b);
                }
                else if (stackList[stackIndex].Count == 0)
                {
                    stackList.RemoveAt(stackIndex);
                    stackIndex--;
                }
            }
        }