C#數(shù)字圖像處理之圖像二值化(彩色變黑白)的方法
更新時(shí)間:2015年04月24日 09:12:38 作者:滄海一粟……
這篇文章主要介紹了C#數(shù)字圖像處理之圖像二值化(彩色變黑白)的方法,可實(shí)現(xiàn)圖像從彩色轉(zhuǎn)變?yōu)楹诎椎男Ч?非常具有實(shí)用價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了C#數(shù)字圖像處理之圖像二值化(彩色變黑白)的方法。分享給大家供大家參考。具體如下:
//定義圖像二值化函數(shù) private static Bitmap PBinary(Bitmap src,int v) { int w = src.Width; int h = src.Height; Bitmap dstBitmap = new Bitmap(src.Width ,src.Height ,System .Drawing .Imaging .PixelFormat .Format24bppRgb ); System.Drawing.Imaging.BitmapData srcData = src.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); System.Drawing.Imaging.BitmapData dstData = dstBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); unsafe { byte* pIn = (byte*)srcData.Scan0.ToPointer(); byte* pOut = (byte*)dstData.Scan0.ToPointer(); byte* p; int stride = srcData.Stride; int r, g, b; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { p = pIn; r = p[2]; g = p[1]; b = p[0]; pOut [0]=pOut [1]=pOut [2]=(byte)(((byte)(0.2125 * r + 0.7154 * g + 0.0721 * b) >= v) ? 255 : 0); pIn += 3; pOut += 3; } pIn += srcData.Stride - w * 3; pOut += srcData.Stride - w * 3; } src.UnlockBits(srcData ); dstBitmap.UnlockBits(dstData ); return dstBitmap; } }
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
AjaxControlToolkit AjaxFileUpload 顯示英文改成中文的解決方法
AjaxControlToolkit AjaxFileUpload 顯示英文改成中文的解決方法,需要的朋友可以參考一下2013-03-03C# 撒列實(shí)現(xiàn)關(guān)鍵字過(guò)濾的實(shí)例
C# 撒列實(shí)現(xiàn)關(guān)鍵字過(guò)濾的實(shí)例,需要的朋友可以參考一下2013-04-04C# 操作 access 數(shù)據(jù)庫(kù)的實(shí)例代碼
這篇文章主要介紹了C# 操作 access 數(shù)據(jù)庫(kù)的實(shí)例代碼,需要的朋友可以參考下2018-03-03C#動(dòng)態(tài)生成DropDownList執(zhí)行失敗原因分析
這篇文章主要介紹了C#動(dòng)態(tài)生成DropDownList執(zhí)行失敗原因分析,以一個(gè)實(shí)例形式分析了C#動(dòng)態(tài)生成DropDownList的相關(guān)注意要點(diǎn)與使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03