2010-11-17 25 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讀他的頁面。儘管起初可能很難理解,但隨着您的進步,它會變得更加簡單。他的頁面比我在這個答案中的詳細得多,他可能有工作的例子。