基於c#映像灰階化、灰階反轉、二值化的實現方法詳解

來源:互聯網
上載者:User

映像灰階化:
將彩色映像轉化成為灰階映像的過程成為映像的灰階化處理。彩色映像中的每個像素的顏色有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;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.