C#圖片灰度處理(位深度24→位深度8)c++
#region 灰度處理 /// <summary> /// 將源圖像灰度化,並轉化爲8位灰度圖像。 /// </summary> /// <param name="original"> 源圖像。 </param> /// <returns> 8位灰度圖像。 </returns> public static Bitmap RgbToGrayScale(Bitmap original) { if (original != null) { // 將源圖像內存區域鎖定 Rectangle rect = new Rectangle(0, 0, original.Width, original.Height); BitmapData bmpData = original.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); // 獲取圖像參數 int width = bmpData.Width; int height = bmpData.Height; int stride = bmpData.Stride; // 掃描線的寬度,比實際圖片要大 int offset = stride - width * 3; // 顯示寬度與掃描線寬度的間隙 IntPtr ptr = bmpData.Scan0; // 獲取bmpData的內存起始位置的指針 int scanBytesLength = stride * height; // 用stride寬度,表示這是內存區域的大小 // 分別設置兩個位置指針,指向源數組和目標數組 int posScan = 0, posDst = 0; byte[] rgbValues = new byte[scanBytesLength]; // 爲目標數組分配內存 Marshal.Copy(ptr, rgbValues, 0, scanBytesLength); // 將圖像數據拷貝到rgbValues中 // 分配灰度數組 byte[] grayValues = new byte[width * height]; // 不含未用空間。 // 計算灰度數組 byte blue, green, red, YUI; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { blue = rgbValues[posScan]; green = rgbValues[posScan + 1]; red = rgbValues[posScan + 2]; YUI = (byte)(0.229 * red + 0.587 * green + 0.144 * blue); //grayValues[posDst] = (byte)((blue + green + red) / 3); grayValues[posDst] = YUI; posScan += 3; posDst++; } // 跳過圖像數據每行未用空間的字節,length = stride - width * bytePerPixel posScan += offset; } // 內存解鎖 Marshal.Copy(rgbValues, 0, ptr, scanBytesLength); original.UnlockBits(bmpData); // 解鎖內存區域 // 構建8位灰度位圖 Bitmap retBitmap = BuiltGrayBitmap(grayValues, width, height); return retBitmap; } else { return null; } } /// <summary> /// 用灰度數組新建一個8位灰度圖像。 /// </summary> /// <param name="rawValues"> 灰度數組(length = width * height)。 </param> /// <param name="width"> 圖像寬度。 </param> /// <param name="height"> 圖像高度。 </param> /// <returns> 新建的8位灰度位圖。 </returns> private static Bitmap BuiltGrayBitmap(byte[] rawValues, int width, int height) { // 新建一個8位灰度位圖,並鎖定內存區域操做 Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed); BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); // 計算圖像參數 int offset = bmpData.Stride - bmpData.Width; // 計算每行未用空間字節數 IntPtr ptr = bmpData.Scan0; // 獲取首地址 int scanBytes = bmpData.Stride * bmpData.Height; // 圖像字節數 = 掃描字節數 * 高度 byte[] grayValues = new byte[scanBytes]; // 爲圖像數據分配內存 // 爲圖像數據賦值 int posSrc = 0, posScan = 0; // rawValues和grayValues的索引 for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { grayValues[posScan++] = rawValues[posSrc++]; } // 跳過圖像數據每行未用空間的字節,length = stride - width * bytePerPixel posScan += offset; } // 內存解鎖 Marshal.Copy(grayValues, 0, ptr, scanBytes); bitmap.UnlockBits(bmpData); // 解鎖內存區域 // 修改生成位圖的索引表,從僞彩修改成灰度 ColorPalette palette; // 獲取一個Format8bppIndexed格式圖像的Palette對象 using (Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed)) { palette = bmp.Palette; } for (int i = 0; i < 256; i++) { palette.Entries[i] = Color.FromArgb(i, i, i); } // 修改生成位圖的索引表 bitmap.Palette = palette; return bitmap; } #endregion
C#圖片二值化處理(位深度8→位深度1)數組
#region 二值化 #region Otsu閾值法二值化模塊 /// <summary> /// Otsu閾值 /// </summary> /// <param name="b">位圖流</param> /// <returns></returns> public Bitmap OtsuThreshold() { // 圖像灰度化 // b = Gray(b); int width = bitmap.Width; int height = bitmap.Height; byte threshold = 0; int[] hist = new int[256]; int AllPixelNumber = 0, PixelNumberSmall = 0, PixelNumberBig = 0; double MaxValue, AllSum = 0, SumSmall = 0, SumBig, ProbabilitySmall, ProbabilityBig, Probability; BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); unsafe { byte* p = (byte*)bmpData.Scan0; int offset = bmpData.Stride - width * 4; for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { hist[p[0]]++; p += 4; } p += offset; } bitmap.UnlockBits(bmpData); } //計算灰度爲I的像素出現的機率 for (int i = 0; i < 256; i++) { AllSum += i * hist[i]; // 質量矩 AllPixelNumber += hist[i]; // 質量 } MaxValue = -1.0; for (int i = 0; i < 256; i++) { PixelNumberSmall += hist[i]; PixelNumberBig = AllPixelNumber - PixelNumberSmall; if (PixelNumberBig == 0) { break; } SumSmall += i * hist[i]; SumBig = AllSum - SumSmall; ProbabilitySmall = SumSmall / PixelNumberSmall; ProbabilityBig = SumBig / PixelNumberBig; Probability = PixelNumberSmall * ProbabilitySmall * ProbabilitySmall + PixelNumberBig * ProbabilityBig * ProbabilityBig; if (Probability > MaxValue) { MaxValue = Probability; threshold = (byte)i; } } this.Threshoding(bitmap, threshold); bitmap = twoBit(bitmap); return bitmap; ; } #endregion #region 固定閾值法二值化模塊 public Bitmap Threshoding(Bitmap b, byte threshold) { int width = b.Width; int height = b.Height; BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); unsafe { byte* p = (byte*)data.Scan0; int offset = data.Stride - width * 4; byte R, G, B, gray; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { R = p[2]; G = p[1]; B = p[0]; gray = (byte)((R * 19595 + G * 38469 + B * 7472) >> 16); if (gray >= threshold) { p[0] = p[1] = p[2] = 255; } else { p[0] = p[1] = p[2] = 0; } p += 4; } p += offset; } b.UnlockBits(data); return b; } } #endregion #region 建立1位圖像 /// <summary> /// 建立1位圖像 /// </summary> /// <param name="srcBitmap"></param> /// <returns></returns> public Bitmap twoBit(Bitmap srcBitmap) { int midrgb = Color.FromArgb(128, 128, 128).ToArgb(); int stride;//簡單公式((width/8)+3)&(~3) stride = (srcBitmap.Width % 8) == 0 ? (srcBitmap.Width / 8) : (srcBitmap.Width / 8) + 1; stride = (stride % 4) == 0 ? stride : ((stride / 4) + 1) * 4; int k = srcBitmap.Height * stride; byte[] buf = new byte[k]; int x = 0, ab = 0; for (int j = 0; j < srcBitmap.Height; j++) { k = j * stride;//因圖像寬度不一樣、有的可能有填充字節須要跳越 x = 0; ab = 0; for (int i = 0; i < srcBitmap.Width; i++) { //從灰度變單色(下法若是直接從彩色變單色效果不太好,不過反相也能夠在這裏控制) if ((srcBitmap.GetPixel(i, j)).ToArgb() > midrgb) { ab = ab * 2 + 1; } else { ab = ab * 2; } x++; if (x == 8) { buf[k++] = (byte)ab;//每字節賦值一次,數組buf中存儲的是十進制。 ab = 0; x = 0; } } if (x > 0) { //循環實現:剩餘有效數據不滿1字節的狀況下須把它們移往字節的高位部分 for (int t = x; t < 8; t++) ab = ab * 2; buf[k++] = (byte)ab; } } int width = srcBitmap.Width; int height = srcBitmap.Height; Bitmap dstBitmap = new Bitmap(width, height, PixelFormat.Format1bppIndexed); BitmapData dt = dstBitmap.LockBits(new Rectangle(0, 0, dstBitmap.Width, dstBitmap.Height), ImageLockMode.ReadWrite, dstBitmap.PixelFormat); Marshal.Copy(buf, 0, dt.Scan0, buf.Length); dstBitmap.UnlockBits(dt); return dstBitmap; } #endregion #endregion