2010-11-17 42 views
0

您好我真的在C#中进行图像处理的新功能,下面的代码基本上是从我的计算机浏览的图像中获取getpixel,并将像素的RGB值与正确的像素进行比较,如果它相同值,它将像素设置为青色。问题在于getpixel,即使在一张小分辨率的照片上它真的非常慢,我也希望为它增加更多的功能。我已经阅读了有关lockbits的文章,并试用了它,但无法成功编写代码。无法成功使用锁定位

namespace Disimage 
{ 
    public partial class Form1 : Form 
    { 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    public Bitmap pic; 
    public Bitmap pic2; 

    private bool compare_colour_constant(int original, int sample) 
    { 
     if (original == sample) 
      return true; 
     else 
      return false;    
    } 

    public void btn_browse_Click_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      OpenFileDialog open = new OpenFileDialog(); 
      open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp"; 
      if (open.ShowDialog() == DialogResult.OK) 
      { 
       pic = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); 
       pic2 = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); 

       //pictureBox1.Image = new Bitmap(open.FileName); 
       pic = new Bitmap(open.FileName); 
       pic2 = new Bitmap(open.FileName); 
       pictureBox1.Image = pic; 
       pictureBox2.Image = pic2; 
       pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; 
       textBox1.Text = open.FileName; 
       pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;       
      } 
     } 
     catch (Exception) 
     { 
      throw new ApplicationException("Failed loading image"); 
     } 
    } 


    public void scan_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      //Bitmap pic = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); 
      //Bitmap pic2 = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); 

      pictureBox1.Image = pic; 
      pictureBox2.Image = pic2; 
      progressBar1.Minimum = 0; 
      progressBar1.Maximum = pic.Width; 
      int []RGB = pic.GetPixel(); 

       for (int w = 1; w < pic.Width - 1; w++) 
       { 
        progressBar1.Step = 1; 
        progressBar1.PerformStep(); 

        if (progressBar1.Value == progressBar1.Maximum)     
         progressBar1.Value = 0; 

        for (int h = 1; h < pic.Height - 1; h++) 
        { 
         int red = pic.GetPixel(w, h).R; 
         int green = pic.GetPixel(w, h).G; 
         int blue = pic.GetPixel(w, h).B; 
         int colour = pic.GetPixel(w, h).R + pic.GetPixel(w, h).G + pic.GetPixel(w, h).B; 
         int colour2 = pic.GetPixel(w + 1, h).R + pic.GetPixel(w + 1, h).G + pic.GetPixel(w + 1, h).B; 

         /*textBox2.Text = red.ToString(); 
         textBox3.Text = green.ToString(); 
         textBox4.Text = blue.ToString(); 
         */ 

         int Lred = pic.GetPixel(w - 1, h).R; 
         int Lgreen = pic.GetPixel(w - 1, h).G; 
         int Lblue = pic.GetPixel(w - 1, h).B; 

         int Rred = pic.GetPixel(w + 1, h).R; 
         int Rgreen = pic.GetPixel(w + 1, h).G; 
         int Rblue = pic.GetPixel(w + 1, h).B; 

         if (compare_colour_constant(colour, colour2) == true) 
          pic2.SetPixel(w, h, Color.Cyan); 
        } 
       } 
     }    
     catch (Exception) 
     { 
      throw new ApplicationException("Failed loading image"); 
     } 
    } 
} 

}

+2

请删除代码示例中不必要的内容。它没有任何用处。 – leppie 2010-11-17 09:33:42

回答

1

虽然晚了一点,我很高兴回答你的问题,其他用户。你需要做的第一件事是声明一个BitmapData变量,该变量可以显着地保存已经放入内存的位图图像中的数据。要这样做:

System.Drawing.Imaging.BitmapData bmpdata = pic.LockBits(new Rectangle(pictureBox1.Location.X, pictureBox1.Location.Y, pictureBox1.Width, pictureBox1.Height), 
System.Drawing.Imaging.ImageLockMode.ReadWrite, 
System.Drawing.Imaging.PixelFormat); 

调用此代码后,您可以继续编辑您喜欢的BitmapData。在这种情况下,您可以调用通过数据字节数组的循环,并将RGB与像素的RGB立即比较,并确定相似性。例如:

unsafe 
{ 
    for (int y = 0; y < bmpdata.Height; y++) // Repeats for each row 
    { 
     byte* row = (byte*)bmpdata.Scan0 + (y * bmpdata.Stride); // Array of bytes for the current row of pixels 
     for (int x = 0; x < bmpdata.Width; x++) // Repeats for each pixel on each row 
     { 
      if (row[x * 4] == row[(x + 1) * 4] && row[(x * 4) + 1] == row[((x + 1) * 4) + 1] && row[(x * 4) + 2] == row[((x + 1) * 4) + 2]) 
      { 
       row[x * 4] = 255; // Blue value of current pixel 
       row[(x * 4) + 1] = 255; // Green Value of current pixel 
       row[(x * 4) + 2] = 0; // Red value of current pixel 
      } 
     } 
    } 
} 

注意:虽然上述威力工作(让我强调力量),它很可能是更可靠去Bob Powell's site和LockBits读他的页面。尽管起初可能很难理解,但随着您的进步,它会变得更加简单。他的页面比我在这个答案中的详细得多,他可能有工作的例子。