public static void DrawLine(Point[] points, byte[] color, ByteImage picture) { bool horizontal = false; if (points[1].X - points[0].X > 0) { horizontal = true; } if (horizontal) { for (int x = points[0].X; x < points[1].X; x++) { picture.setPixel(x, points[0].Y, color); } } else { for (int y = points[0].Y; y < points[1].Y; y++) { picture.setPixel(points[0].X, y, color); } } }
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); }
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); } } }
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); } } }
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); } } }
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); } } }
private static List <Point> DrawBorders(ByteImage ori, List <int> horizontalBorders, List <int> verticalBorders, byte[] color) { List <Point> corners = new List <Point>(); Point center = new Point(horizontalBorders.ElementAt(1), verticalBorders.ElementAt(1)); Point upLeft = new Point(horizontalBorders.ElementAt(0), verticalBorders.ElementAt(0)); Point upRight = new Point(horizontalBorders.ElementAt(2), verticalBorders.ElementAt(0)); Point bottomLeft = new Point(horizontalBorders.ElementAt(0), verticalBorders.ElementAt(2)); Point bottomRight = new Point(horizontalBorders.ElementAt(2), verticalBorders.ElementAt(2)); Point meanCenter = new Point((((Math.Abs(upRight.X - upLeft.X) / 2) + upLeft.X) + center.X) / 2, (((Math.Abs(upRight.Y - bottomRight.Y) / 2) + upRight.Y) + center.Y) / 2); int meanWidth = (Math.Abs(upLeft.X - meanCenter.X) + Math.Abs(meanCenter.X - upRight.X)) / 2; int meanHeigt = (Math.Abs(upLeft.Y - meanCenter.Y) + Math.Abs(meanCenter.Y - upRight.Y)) / 2; upLeft = CorrectPoint(new Point(meanCenter.X - meanWidth - (int)(0.1 * meanWidth), meanCenter.Y - meanHeigt - (int)(0.1 * meanHeigt)), ori); upRight = CorrectPoint(new Point(meanCenter.X + meanWidth + (int)(0.1 * meanWidth), meanCenter.Y - meanHeigt - (int)(0.1 * meanHeigt)), ori); bottomLeft = CorrectPoint(new Point(meanCenter.X - meanWidth - (int)(0.1 * meanWidth), meanCenter.Y + meanHeigt + (int)(0.1 * meanHeigt)), ori); bottomRight = CorrectPoint(new Point(meanCenter.X + meanWidth + (int)(0.1 * meanWidth), meanCenter.Y + meanHeigt + (int)(0.1 * meanHeigt)), ori); corners.Add(upLeft); corners.Add(upRight); corners.Add(bottomLeft); corners.Add(bottomRight); for (int x = bottomLeft.X; x < bottomRight.X; x++) { ori.setPixel(x, upLeft.Y, color); ori.setPixel(x, bottomLeft.Y, color); } for (int y = upLeft.Y; y < bottomLeft.Y; y++) { ori.setPixel(upLeft.X, y, color); ori.setPixel(upRight.X, y, color); } return(corners); }
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); }
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))); } } }
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--; } } }
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); }