c#圖像灰度化、灰度反轉、二值化


圖像灰度化
將彩色圖像轉化成為灰度圖像的過程成為圖像的灰度化處理。彩色圖像中的每個像素的顏色有 R、G、B三個分量決定,而每個分量有255中值可取,這樣一個像素點可以有1600多萬(255*255*255)的顏色的變化范圍。而灰度圖像是R、 G、B三個分量相同的一種特殊的彩色圖像,其一個像素點的變化范圍為255種,所以在數字圖像處理種一般先將各種格式的圖像轉變成灰度圖像以使后續的圖像 的計算量變得少一些。灰度圖像的描述與彩色圖像一樣仍然反映了整幅圖像的整體和局部的色度和亮度等級的分布和特征。圖像的灰度化處理可用兩種方法來實現。
第一種方法使求出每個像素點的R、G、B三個分量的平均值,然后將這個平均值賦予給這個像素的三個分量。
第二種方法是根據YUV的顏色空間中,Y的分量的物理意義是點的亮度,由該值反映亮度等級,根據RGB和YUV顏色空間的變化關系可建立亮度Y與R、G、B三個顏色分量的對應:Y=0.3R+0.59G+0.11B,以這個亮度值表達圖像的灰度值。

/// <summary>
       /// 圖像灰度化
       /// </summary>
       /// <param name="bmp"></param>
       /// <returns></returns>
       public static Bitmap ToGray(Bitmap bmp)
       {
           for ( int i = 0; i < bmp.Width; i++)
           {
               for ( int j = 0; j < bmp.Height; j++)
               {
                   //獲取該點的像素的RGB的顏色
                   Color color = bmp.GetPixel(i, j);
                   //利用公式計算灰度值
                   int gray = ( int )(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
                   Color newColor = Color.FromArgb(gray, gray, gray);
                   bmp.SetPixel(i, j, newColor);
               }
           }
           return bmp;
       }

灰度反轉
把每個像素點的R、G、B三個分量的值0的設為255,255的設為0。

/// <summary>
       /// 圖像灰度反轉
       /// </summary>
       /// <param name="bmp"></param>
       /// <returns></returns>
       public static Bitmap GrayReverse(Bitmap bmp)
       {
           for ( int i = 0; i < bmp.Width; i++)
           {
               for ( int j = 0; j < bmp.Height; j++)
               {
                   //獲取該點的像素的RGB的顏色
                   Color color = bmp.GetPixel(i, j);
                   Color newColor = Color.FromArgb(255 - color.R, 255 - color.G, 255 - color.B);
                   bmp.SetPixel(i, j, newColor);
               }
           }
           return bmp;
       }

灰度圖像二值化
在進行了灰度化處理之后,圖像中的每個象素只有一個值,那就是象素的灰度值。它的大小 決定了象素的亮暗程度。為了更加便利的開展下面的圖像處理操作,還需要對已經得到的灰度圖像做一個二值化處理。圖像的二值化就是把圖像中的象素根據一定的 標准分化成兩種顏色。在系統中是根據象素的灰度值處理成黑白兩種顏色。和灰度化相似的,圖像的二值化也有很多成熟的算法。它可以采用自適應閥值法,也可以 采用給定閥值法。

   /// <summary>
         /// 圖像二值化1:取圖片的平均灰度作為閾值,低於該值的全都為0,高於該值的全都為255
         /// </summary>
         /// <param name="bmp"></param>
         /// <returns></returns>
         public static Bitmap ConvertTo1Bpp1(Bitmap bmp)
         {
             int average = 0;
             for ( int i = 0; i < bmp.Width; i++)
             {
                 for ( int j = 0; j < bmp.Height; j++)
                 {
                     Color color = bmp.GetPixel(i, j);
                     average += color.B;                   
                 }
             }
             average = ( int )average / (bmp.Width * bmp.Height);
 
             for ( int i = 0; i < bmp.Width; i++)
             {
                 for ( int j = 0; j < bmp.Height; j++)
                 {
                     //獲取該點的像素的RGB的顏色
                     Color color = bmp.GetPixel(i, j);
                     int value = 255 - color.B;
                     Color newColor = value > average ? Color.FromArgb(0, 0, 0): Color.FromArgb(255,
 
255, 255);                  
                     bmp.SetPixel(i, j, newColor);
                 }
             }
             return bmp;
         }
         
         /// <summary>
         /// 圖像二值化2
         /// </summary>
         /// <param name="img"></param>
         /// <returns></returns>
         public static Bitmap ConvertTo1Bpp2(Bitmap img)
         {
             int w = img.Width;
             int h = img.Height;
             Bitmap bmp = new Bitmap(w, h, PixelFormat.Format1bppIndexed);
             BitmapData data = bmp.LockBits( new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite,
 
PixelFormat.Format1bppIndexed);
             for ( int y = 0; y < h; y++)
             {
                 byte [] scan = new byte [(w + 7) / 8];
                 for ( int x = 0; x < w; x++)
                 {
                     Color c = img.GetPixel(x, y);
                     if (c.GetBrightness() >= 0.5) scan[x / 8] |= ( byte )(0x80 >> (x % 8));
                 }
                 Marshal.Copy(scan, 0, (IntPtr)(( int )data.Scan0 + data.Stride * y), scan.Length);
             }
             return bmp;
         }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM