2017-03-25 39 views
1

我知道標題是有點怪,但我不知道爲什麼這是給錯誤。for循環工作寬度X高度,但不是高度X寬度爲BMP圖像

我的任務

獲取從圖像的開始和結束的x,y的索引,所以我可以畫他們周圍的矩形。目前我正在努力獲取圖像中起始和結束像素的位置(x,y)。

這是我4個功能4位中

static int[] getStartX(Bitmap bmp) //run perfect 
     { 
      int[] arr = new int[2]; 
      for (int x = 0; x < bmp.Width - 1; x++) 
      { 
       for (int y = 0; y < bmp.Height - 1; y++) 
       { 
        Color color = bmp.GetPixel(x, y); 
        if (color.ToArgb() == Color.Black.ToArgb()) 
        { 
         arr[0] = x; 
         arr[1] = y; 
         return arr; 
        } 
       } 
      } 
      return arr; 
     } 
     static int[] getStartY(Bitmap bmp) 
     { 
      int[] arr = new int[2]; 
      for (int x = 0; x < bmp.Height - 1; x++) 
      { 
       for (int y = 0; y < bmp.Width - 1; y++) 
       { 
        Color color = bmp.GetPixel(x, y); //Exception out of range 
        if (color.ToArgb() == Color.Black.ToArgb()) 
        { 
         arr[0] = x; 
         arr[1] = y; 
         return arr; 
        } 
       } 
      } 
      return arr; 
     } 
     static int[] getEndX(Bitmap bmp) 

     { 
      int[] arr = new int[2]; 
      for (int x = bmp.Width-1 ; x >= 0; x--) 
      { 
       for (int y = bmp.Height - 1; y >=0 ; y--) 
       { 
        Color color = bmp.GetPixel(x, y);//Exception out of range 
        if (color.ToArgb() == Color.Black.ToArgb()) 
        { 
         arr[0] = x; 
         arr[1] = y; 
         return arr; 
        } 
       } 
      } 
      return arr; 
     } 
     static int[] getENdY(Bitmap bmp) 
     { 
      int[] arr = new int[2]; 
      for (int x = bmp.Height - 1; x >= 0; x--) 
      { 
       for (int y = bmp.Width - 1; y >= 0; y--) 
       { 
        Color color = bmp.GetPixel(x, y); //Exception out of range 
        if (color.ToArgb() == Color.Black.ToArgb()) 
        { 
         arr[0] = x; 
         arr[1] = y; 
         return arr; 
        } 
       } 
      } 
      return arr; 
     } 

只有我的第一個功能正常工作和所有其他給予異常超出範圍的錯誤就行代碼指出。

這是我Image

預計輸出像素爲紅色顯示:Output 主要

static void Main(string[] args) 
     { 
      Bitmap bmp = new Bitmap("C:\\Users\\AV\\5.bmp"); 
      int[] arr = new int[2]; 
      arr = getStartX(bmp); 
      bmp.SetPixel(arr[0], arr[1], Color.Blue); 
      arr = getStartY(bmp); 
      bmp.SetPixel(arr[0], arr[1], Color.Blue); 
      arr = getEndX(bmp); 
      bmp.SetPixel(arr[0], arr[1], Color.Blue); 
      arr = getENdY(bmp); 
      bmp.SetPixel(arr[0], arr[1], Color.Blue); 
      bmp.Save("1.bmp"); 
} 

上,我想每一個位置繪製藍色的點,來表示。 我想只得到4個位置,所以我可以在它周圍繪製一個矩形。 如果任何人都可以提前幫助我。

+0

那麼,你的目標周圍繪製圖像中的線條的長方形?如果不是,那麼「x,y索引的開始和結束」是什麼意思。 –

+0

我的目標是圍繞這條線繪製一個矩形,但是它的區域的矩形,通過開始和結束x,y軸,我是指來自每一側的起始部分線。 –

+0

對不起,還是有點困惑。你可以張貼預期輸出的圖片嗎? –

回答

3

對於第一個例外,我認爲這是因爲你打電話給GetPixel,首先是高度,然後是寬度。您可以考慮使用更有意義的名稱,以避免混淆,但這應該工作:

Color color = bmp.GetPixel(y, x); 

我想給一些更有意義的名稱的變量和方法可以幫助,至少它爲我。另外,我認爲這些方法的回報比他們需要的要多。您真正需要的是最上方,下方,左側和右側像素的x和y座標。這裏有一些改進的方法是做(主要是你的代碼):

/// <summary> 
/// This returns the x-coordinate of the point that is closest to the left of the image 
/// </summary> 
/// <param name="bmp">The image to search</param> 
/// <returns>The x-coordinate of the left-most point</returns> 
static int GetLeftX(Bitmap bmp) 
{ 
    int leftmostPointX = 0; 

    // Start at top left, and look down each column as we move to the right 
    for (int x = 0; x < bmp.Width - 1; x++) 
    { 
     for (int y = 0; y < bmp.Height - 1; y++) 
     { 
      Color color = bmp.GetPixel(x, y); 

      if (color.ToArgb() == Color.Black.ToArgb()) 
      { 
       leftmostPointX = x; 
       break; 
      } 
     } 

     if (leftmostPointX > 0) break; 
    } 

    return leftmostPointX; 
} 

/// <summary> 
/// This returns the y-coordinate of the point that is closest to the top of the image 
/// </summary> 
/// <param name="bmp">The image to search</param> 
/// <returns>The y-coordinate of the top-most point</returns> 
static int GetTopY(Bitmap bmp) 
{ 
    int topmostPointY = 0; 

    // Start at top left, and look across each row as we move down 
    for (int y = 0; y < bmp.Height - 1; y++) 
    { 
     for (int x = 0; x < bmp.Width - 1; x++) 
     { 
      Color color = bmp.GetPixel(x, y); 

      if (color.ToArgb() == Color.Black.ToArgb()) 
      { 
       topmostPointY = y; 
       break; 
      } 
     } 

     if (topmostPointY > 0) break; 
    } 

    return topmostPointY; 
} 

/// <summary> 
/// This returns the s-coordinate of the point that is closest to the right of the image 
/// </summary> 
/// <param name="bmp">The image to search</param> 
/// <returns>The x-coordinate of the right-most point</returns> 
static int GetRightX(Bitmap bmp) 
{ 
    int rightmostPointX = bmp.Width - 1; 

    // Start at top right, and look down each column as we move to the left 
    for (int x = bmp.Width - 1; x >= 0; x--) 
    { 
     for (int y = 0; y < bmp.Height - 1; y++) 
     { 
      Color color = bmp.GetPixel(x, y); 

      if (color.ToArgb() == Color.Black.ToArgb()) 
      { 
       rightmostPointX = x; 
       break; 
      } 
     } 

     if (rightmostPointX < bmp.Width - 1) break; 
    } 

    return rightmostPointX; 
} 

/// <summary> 
/// This returns the y-coordinate of the point that is closest to the right of the image 
/// </summary> 
/// <param name="bmp">The image to search</param> 
/// <returns>The y-coordinate of the right-most point</returns> 
static int GetBottomY(Bitmap bmp) 
{ 
    int lowestPointY = bmp.Height - 1; 

    // Start at bottom left, and look across each row as we move up 
    for (int y = bmp.Height - 1; y >= 0; y--) 
    { 
     for (int x = 0; x < bmp.Width - 1; x++) 
     { 
      Color color = bmp.GetPixel(x, y); 

      if (color.ToArgb() == Color.Black.ToArgb()) 
      { 
       lowestPointY = y; 
       break; 
      } 
     } 

     if (lowestPointY < bmp.Height - 1) break; 
    } 

    return lowestPointY; 
} 

然後在這裏是我會怎樣調用這些方法,使用有意義的名稱,並以最小的零件開始,然後建立到我們所需要的(從單個座標到點陣):

private static void Main() 
{ 
    // Coordinates 
    int leftX = GetLeftX(bmp); 
    int rightX = GetRightX(bmp); 
    int topY = GetTopY(bmp); 
    int bottomY = GetBottomY(bmp); 

    // Create Points from the coordinates 
    var topLeft = new Point(leftX, topY); 
    var topRight = new Point(rightX, topY); 
    var bottomLeft = new Point(leftX, bottomY); 
    var bottomRight = new Point(rightX, bottomY); 

    // An array of points representing our box 
    var box = new[] {topLeft, topRight, bottomRight, bottomLeft, topLeft}; 

    // Draw a box 
    var graphics = Graphics.FromImage(bmp); 
    var pen = new Pen(Color.Blue); 
    graphics.DrawLines(pen, box); 

    // Save the new image 
    bmp.Save(@"f:\public\temp\modified.bmp"); 
} 
+0

我已經試過了,但它沒有給我正確的位置。 @Rufus –

+0

@MohammadTayyab - **總是**使用相同的名稱來表示同樣的事情。使用變量'x'來表示一個地方的水平座標,另一個垂直的座標是每個人都會感到困惑的,並且很容易犯錯誤。比如在這裏注意到的那個,再加上在需要返回值的地方稍微低一點。 –

0

感謝您的幫助!我標出了我的錯誤!

最後我得到我的形象! Image

主要

static void Main(string[] args) 
     { 
      Bitmap bmp = new Bitmap("C:\\Users\\AV\\5.bmp"); 
      int[] arr = new int[2]; 
      int[] arr2 = new int[2]; 
      int[] arr3 = new int[2]; 
      int[] arr4 = new int[2]; 
      arr = getStartX(bmp); 
      bmp.SetPixel(arr[0], arr[1], Color.Blue); 
      arr2 = getStartY(bmp); 
      bmp.SetPixel(arr[0], arr[1], Color.Blue); 
      arr3 = getEndX(bmp); 
      bmp.SetPixel(arr[0], arr[1], Color.Blue); 
      arr4 = getENdY(bmp); 
      bmp.SetPixel(arr[0], arr[1], Color.Blue); 
      Graphics g = Graphics.FromImage(bmp); 
      g.DrawLines(new Pen(Color.Blue), new Point[] 
                { 
                 new Point(arr[0], arr[1]), new Point(arr[0], arr2[1]), 
                 new Point(arr[0], arr2[1]), new Point(arr2[0], arr2[1]), 
                 new Point(arr2[0], arr2[1]), new Point(arr3[0], arr2[1]), 
                 new Point(arr3[0], arr2[1]), new Point(arr3[0], arr3[1]), 
                 new Point(arr3[0], arr3[1]), new Point(arr3[0], arr4[1]), 
                 new Point(arr3[0], arr4[1]), new Point(arr4[0], arr4[1]), 
                 new Point(arr4[0], arr4[1]), new Point(arr[0], arr4[1]), 
                 new Point(arr[0], arr4[1]), new Point(arr[0], arr[1]), 

                }); 
      bmp.Save("1.bmp"); 
} 

功能

static int[] getStartX(Bitmap bmp) 
     { 
      int[] arr = new int[2]; 
      for (int x = 0; x < bmp.Width - 1; x++) 
      { 
       for (int y = 0; y < bmp.Height - 1; y++) 
       { 
        Color color = bmp.GetPixel(x, y); 
        if (color.ToArgb() == Color.Black.ToArgb()) 
        { 
         arr[0] = x-1; 
         arr[1] = y; 
         return arr; 
        } 
       } 
      } 
      return arr; 
     } 
     static int[] getStartY(Bitmap bmp) 
     { 
      int[] arr = new int[2]; 
      for (int x = 0; x < bmp.Height - 1; x++) 
      { 
       for (int y = 0; y < bmp.Width - 1; y++) 
       { 
        Color color = bmp.GetPixel(y, x); //mistake 
        if (color.ToArgb() == Color.Black.ToArgb()) 
        { 
         arr[0] = y; //mistake 
         arr[1] = x-1; //mistake 
         return arr; 
        } 
       } 
      } 
      return arr; 
     } 
     static int[] getEndX(Bitmap bmp) 

     { 
      int[] arr = new int[2]; 
      for (int x = bmp.Width-1 ; x >= 0; x--) 
      { 
       for (int y = bmp.Height - 1; y >=0 ; y--) 
       { 
        Color color = bmp.GetPixel(x, y); 
        if (color.ToArgb() == Color.Black.ToArgb()) 
        { 
         arr[0] = x+1; 
         arr[1] = y; 
         return arr; 
        } 
       } 
      } 
      return arr; 
     } 
     static int[] getENdY(Bitmap bmp) 
     { 
      int[] arr = new int[2]; 
      for (int x = bmp.Height - 1; x >= 0; x--) 
      { 
       for (int y = bmp.Width - 1; y >= 0; y--) 
       { 
        Color color = bmp.GetPixel(y, x); //mistake 
        if (color.ToArgb() == Color.Black.ToArgb()) 
        { 
         arr[0] = y; //mistake 
         arr[1] = x+1; //mistake 
         return arr; 
        } 
       } 
      } 
      return arr; 
     } 
+0

太棒了!真高興你做到了。似乎你不需要任何幫助,但是我繼續根據你的代碼用一些修改後的方法更新我的答案。如果它有幫助,請隨意使用它。 –

+0

是啊!我正在檢查它。謝謝! @RufusL –