2014-05-22 120 views
-1

我想找到具有白色背景的黑色矩形的邊緣,但我不知道如何找到矩形的邊緣。二維邊緣檢測黑色矩形與白色背景

到目前爲止的代碼是:

private void Vicky(object sender, MouseEventArgs e) 
{ 
    OpenFileDialog file = new OpenFileDialog(); 
    if (file.ShowDialog() == DialogResult.OK) 
    { 
     pictureBox1.Image = new Bitmap(file.FileName); 
    } 
} 

private void process(object sender, EventArgs e) 
{ 
    Bitmap bmp = new Bitmap(pictureBox1.Image); 
    for (int i = 0; i < bmp.Width; i++) 
    { 
     for (int j = 0; j < bmp.Height; j++) 
     { 
      Color pixelColor = bmp.GetPixel(i, j); 
      if (pixelColor.R == 0 && pixelColor.G == 0 && pixelColor.B == 0) 
      { 
       for (int x = i; x < bmp.Width; x++) 
       { 
        for (int y = x; y < bmp.Height; y++) 
        { 
        } 
       } 
      } // end if. 
     } // end inner for. 
    } //end outer for. 
    pictureBox1.Image = bmp; 
} //end process. 

回答

0

你就不能去所有的4個方向,直到你找到一個像素,是不是白色的?

int left, right, top, bottom; 
for (int x = i; x < bmp.Width; x++) 
{ 
    Color c = bmp.GetPixel(x, y); 
    if (c.R != 0 || c.G != 0 || c.B != 0) { 
     right = x; 
     break; 
    } 
} 
for (int x = i; x > 0; x--) 
{ 
    Color c = bmp.GetPixel(x, y); 
    if (c.R != 0 || c.G != 0 || c.B != 0) { 
     left = x; 
     break; 
    } 
} 
// ... Two more loops for top and bottom 
0

如果您確定邊緣1px的寬且直,你可以測試bmp.GetPixel(我+ 1,J);和bmp.GetPixel(i,j + 1);爲黑暗。這意味着你有左上角。然後簡單地繼續在兩邊確定寬度和高度。

順便說一句,你有2個額外的}在你的代碼。考慮到一個是關閉類,另一個可能導致你的代碼不能編譯。