private static void DFS(Vector2D START) { Stack<Vector2D> DFS_Stack = new Stack<Vector2D>(); DFS_Stack.Push(START); while (DFS_Stack.Count > 0) { Vector2D Curr = DFS_Stack.Pop(); if(Helper.Vaild_Pixel((int)Curr.X, (int)Curr.Y, selected_image)) { if(!selected_image[(int)Curr.Y, (int)Curr.X].block) { selected_image[(int)Curr.Y, (int)Curr.X].block = true; //black or whiteen the pixel selected_image[(int)Curr.Y, (int)Curr.X].blue = 240; //bgclor selected_image[(int)Curr.Y, (int)Curr.X].green = 240; selected_image[(int)Curr.Y, (int)Curr.X].red = 240; DFS_Stack.Push(new Vector2D(Curr.X, Curr.Y + 1)); DFS_Stack.Push(new Vector2D(Curr.X, Curr.Y - 1)); DFS_Stack.Push(new Vector2D(Curr.X + 1, Curr.Y)); DFS_Stack.Push(new Vector2D(Curr.X - 1, Curr.Y)); } } } }
/// <summary> /// Calculate Gradient vector between the given pixel and its right and bottom ones /// </summary> /// <param name="x">pixel x-coordinate</param> /// <param name="y">pixel y-coordinate</param> /// <param name="ImageMatrix">colored image matrix</param> /// <returns></returns> private static Vector2D CalculateGradientAtPixel(int x, int y, RGBPixel[,] ImageMatrix) { Vector2D gradient = new Vector2D(0, 0); RGBPixel mainPixel = ImageMatrix[y, x]; double pixelGrayVal = 0.21 * mainPixel.red + 0.72 * mainPixel.green + 0.07 * mainPixel.blue; if (y == GetHeight(ImageMatrix) - 1) { //boundary pixel. for (int i = 0; i < 3; i++) { gradient.Y = 0; } } else { RGBPixel downPixel = ImageMatrix[y + 1, x]; double downPixelGrayVal = 0.21 * downPixel.red + 0.72 * downPixel.green + 0.07 * downPixel.blue; gradient.Y = pixelGrayVal - downPixelGrayVal; } if (x == GetWidth(ImageMatrix) - 1) { //boundary pixel. gradient.X = 0; } else { RGBPixel rightPixel = ImageMatrix[y, x + 1]; double rightPixelGrayVal = 0.21 * rightPixel.red + 0.72 * rightPixel.green + 0.07 * rightPixel.blue; gradient.X = pixelGrayVal - rightPixelGrayVal; } return gradient; }
/// <summary> /// Calculate edge energy between /// 1. the given pixel and its right one (X) /// 2. the given pixel and its bottom one (Y) /// </summary> /// <param name="x">pixel x-coordinate</param> /// <param name="y">pixel y-coordinate</param> /// <param name="ImageMatrix">colored image matrix</param> /// <returns>edge energy with the right pixel (X) and with the bottom pixel (Y)</returns> public static Vector2D CalculatePixelEnergies(int x, int y, RGBPixel[,] ImageMatrix) { if (ImageMatrix == null) throw new Exception("image is not set!"); Vector2D gradient = CalculateGradientAtPixel(x, y, ImageMatrix); double gradientMagnitude = Math.Sqrt(gradient.X * gradient.X + gradient.Y * gradient.Y); double edgeAngle = Math.Atan2(gradient.Y, gradient.X); double rotatedEdgeAngle = edgeAngle + Math.PI / 2.0; Vector2D energy = new Vector2D( Math.Abs(gradientMagnitude * Math.Cos(rotatedEdgeAngle)), Math.Abs(gradientMagnitude * Math.Sin(rotatedEdgeAngle))); return energy; }