2014-05-22 114 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个额外的}在你的代码。考虑到一个是关闭类,另一个可能导致你的代码不能编译。