1. 程式人生 > >C#影象處理(二值化,灰階)

C#影象處理(二值化,灰階)

1. #region 灰階

2. /// <summary>

3. /// 灰階

4. /// </summary>

5. /// <param name="b">Bitmap物件</param>

6. /// <returns></returns> 

7. public Bitmap Gray(Bitmap b)   

8. {   

9.     BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),   

10.                         ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);   

11. int stride = bmData.Stride;   

12.     System.IntPtr Scan0 = bmData.Scan0;   

13. unsafe

14.     {   

15. byte* p = (byte*)(void*)Scan0;   

16. int nOffset = stride - b.Width * 3;   

17. byte red, green, blue;   

18. for (int y = 0; y < b.Height; ++y)   

19.         {   

20.

for (int x = 0; x < b.Width; ++x)   

21.             {   

22.                 blue = p[0];   

23.                 green = p[1];   

24.                 red = p[2];   

25.                 p[0] = p[1] = p[2] = (byte)(.299 * red + .587 * green + .114 * blue);   

26.                 p += 3;   

27.             }   

28.             p += nOffset;   

29.         }   

30.     }   

31.     b.UnlockBits(bmData);   

32. return b;   

33. }  

34. #endregion  

35.  

36. #region 固定閾值法二值化模組

37.

38. public Bitmap Threshoding(Bitmap b, byte threshold)   

39. {   

40. int width = b.Width;   

41. int height = b.Height;   

42.     BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);   

43. unsafe

44.     {   

45. byte* p = (byte*)data.Scan0;   

46. int offset = data.Stride - width * 4;   

47. byte R, G, B, gray;   

48. for (int y = 0; y < height; y++)   

49.         {   

50. for (int x = 0; x < width; x++)   

51.             {   

52.                 R = p[2];   

53.                 G = p[1];   

54.                 B = p[0];   

55.                 gray = (byte)((R * 19595 + G * 38469 + B * 7472) >> 16);   

56. if (gray >= threshold)   

57.                 {   

58.                     p[0] = p[1] = p[2] = 255;   

59.                 }   

60. else

61.                 {   

62.                     p[0] = p[1] = p[2] = 0;   

63.                 }   

64.                 p += 4;   

65.             }   

66.             p += offset;   

67.         }   

68.         b.UnlockBits(data);   

69. return b;   

70.     }   

71.

72. }  

73. #endregion  

74.  

75. #region Otsu閾值法二值化模組

76. /// <summary>

77. /// Otsu閾值

78. /// </summary>

79. /// <param name="b">點陣圖流</param>

80. /// <returns></returns>

81. public Bitmap OtsuThreshold(Bitmap b)   

82. {   

83. // 影象灰度化

84. // b = Gray(b);

85. int width = b.Width;   

86. int height = b.Height;   

87. byte threshold = 0;   

88. int[] hist = new int[256];   

89.

90. int AllPixelNumber = 0, PixelNumberSmall = 0, PixelNumberBig = 0;   

91. double MaxValue, AllSum = 0, SumSmall = 0, SumBig, ProbabilitySmall, ProbabilityBig, Probability;   

92.

93.     BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);   

94. unsafe

95.     {   

96. byte* p = (byte*)data.Scan0;   

97. int offset = data.Stride - width * 4;   

98.

99. for (int j = 0; j < height; j++)   

100.         {   

101. for (int i = 0; i < width; i++)   

102.             {   

103.                 hist[p[0]]++;   

104. 

105. 

106.                 p += 4;   

107.             }   

108.             p += offset;   

109.         }   

110.         b.UnlockBits(data);   

111. 

112.     }   

113. //計算灰度為I的畫素出現的概率

114. for (int i = 0; i < 256; i++)   

115.     {   

116. 

117.         AllSum += i * hist[i];     //   質量矩

118.         AllPixelNumber += hist[i];  //  質量

119. 

120.     }   

121. 

122.     MaxValue = -1.0;   

123. for (int i = 0; i < 256; i++)   

124.     {   

125.         PixelNumberSmall += hist[i];   

126.         PixelNumberBig = AllPixelNumber - PixelNumberSmall;   

127. if (PixelNumberBig == 0)   

128.         {   

129. break;   

130.         }   

131. 

132.         SumSmall += i * hist[i];   

133.         SumBig = AllSum - SumSmall;   

134.         ProbabilitySmall = SumSmall / PixelNumberSmall;   

135.         ProbabilityBig = SumBig / PixelNumberBig;   

136.         Probability = PixelNumberSmall * ProbabilitySmall * ProbabilitySmall + PixelNumberBig * ProbabilityBig * ProbabilityBig;   

137. if (Probability > MaxValue)   

138.         {   

139.             MaxValue = Probability;   

140.             threshold = (byte)i;   

141.         }   

142. 

143.     }   

144. 

145. return this.Threshoding(b, threshold);   

146. // end of OtsuThreshold 2  

147. #endregion

相關推薦

C#影象處理(,)

1. #region 灰階 2. /// <summary> 3. /// 灰階 4. /// </summary> 5. /// <param name="b">Bitmap物件</param> 6. /// <returns&g

使用matlab對影象進行處理

用matlab對影象進行二值化處理 >>m = imread('d:\image\logo.jpg'); >> imshow(n); >> n = graythre

利用opencv對影象進行處理

利用該方法對圖形進行二值化處理,能夠很好的除去光線對圖片的影響 #include<iostream> #include<opencv2\opencv.hpp> using namespace cv; using namespace

c#實現圖片例子(黑白效果)

rec con devel 圖片 round amp bsp 操作 spl C#將圖片2值化示例代碼,原圖及二值化後的圖片如下: 原圖: 二值化後的圖像: 實現代碼:using System; using System.Drawing; namespace BMP2G

十三種基於直方圖的影象全域性演算法原理、實現、程式碼及效果。

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

macOS opencv python 影象簡單

python opencv 影象二值化 全域性閥值 1,cv2.threshold 全域性閥值 1,cv2.threshold 全域性閥值 #! /usr/local/bin/python3 # coding:utf-8 """ 影象二值化 全

python驗證碼圖片處理--

寫在最前面: 這個我打算分幾次寫,由於我們通過selenium拿到的圖片會很模糊,所以使用Tesseract識別之前要對圖片先進行處理。 第一步就是二值化,設定閾值,低於閾值全部為白色(置0),其餘黑色(置1)。 import pytesseract from PIL import

opencv學習(十六):影象

影象二值化介紹:https://blog.csdn.net/qq_30490125/article/details/80458500                       &nbs

【OpenCV入門指南】第四篇 影象

【OpenCV入門指南】第四篇 影象的二值化    在上一篇《【OpenCV入門指南】第三篇Canny邊緣檢測》中介紹了使用Canny運算元對影象進行邊緣檢測。與邊緣檢測相比,輪廓檢測有時能更好的反映影象的內容。而要對影象進行輪廓檢測,則必須要先對影象進行二值化,影象的二值化就是

C# 影象處理()(螢幕擷取、截圖外掛)

雙擊執行截圖外掛,工具預設延時5秒後截圖 可擷取全屏可擷取到快捷選單和滑鼠 也可通過cmd命令執行工具,呼叫示例引數如下: REM 呼叫截圖工具進行截圖 call "%~dp0getScreen.exe" REM 儲存到指定目錄(D:\tmp\截圖.png) call

【OpenCV學習筆記 004】 影象的縮放、Canny邊緣檢測和影象

一、影象的縮放 本篇將介紹使用OpenCV來縮放圖片。首先介紹幾個關鍵函式——cvResize和cvCreateImage 1.主要函式介紹 1.1 cvResize 函式功能:影象大小變換 函式原型: voidcvResize(   const CvArr* src,

【數字影象C++8位和24位BMP點陣圖的平滑、銳處理,以及24位真彩圖的

BMP標頭檔案: #ifndef BMP_H//前處理器 #define BMP_H typedef unsigned char BYTE; typedef unsigned short WORD; typedef unsigned int DWORD; typedef

MATLA影象處理以及處理

首先先來明白幾個概念: 1、彩色影象(RGB):影象的每個畫素點都是由紅(R)、綠(G)、藍(B)三個分量來表示的,每一個分量一般分別介於0-255之間,當然如果每一個顏色分量用更多的位數去表示的話,那

影象-----c++實現

前天閒著沒事幹,就寫了寫BMP影象處理,感覺大家還比較感興趣。。所以現在沒事,繼續更新。。這次簡單的寫了灰度影象二值化。。這是什麼概念呢? 影象的二值化的基本原理    影象的二值化處理就是將影象上的點的灰度置為0或255,也就是講整個影象呈現出明顯的黑白效果。即將256個

C++——bmp影象+

本文實現bmp影象的灰度化及二值化操作:1、灰度化       對於彩色轉灰度,有一個很著名的心理學公式:                          Gray = R*0.299 + G*0.587 + B*0.114而實際應用時,希望避免低速的浮點運算,所以需要整數

影象處理,反色

灰度化 (grayscale) 將彩色影象轉化為灰度影象的過程稱為影象灰度化。彩色影象中的畫素值由RGB三個分量決定,每個分量都有0-255(256種)選擇,這樣一個畫素點的畫素值可以有1600萬種可能(256*256*256),而灰度圖的畫素點的畫素值是RGB三個分量值相

影象處理)opencv處理影象

這裡主要實現的 opencv 基於 android 對影象進行常用處理,比如說灰度化,二值化,rgb的轉換,這裡就不貼主要程式碼,只是工具程式碼。 Utils.xxx方法的使用需要在MainActivity.class中新增此方法(好像是掉用opencv,an

java影象處理,降噪,切割,裁剪,識別,找相似等

前段時間做爬蟲,涉及到對圖片驗證碼的破解,這裡羅列一些常用的影象處理方法,都很簡單並沒用到什麼複雜的演算法,所以不涉及opencv,都是一些直接對rgb畫素點的操作,很簡單也很好理解,至於識別直接用的tesseract-ocr,也可以用svm。(ps:圖片的畫素值矩陣的原點在

java影象處理---處理

java實現圖片灰度化(二值化) 此函式功能: 1. 讀圖 2. 建立緩衝區 3. 將圖片畫素複製到緩衝區的相應位置 4. 輸出比較 輸入: RGB

一些基本的opencv影象處理函式直接用法(,otsu,腐蝕膨脹,canny)

#include <cv.h> #include <cxcore.h> #include <highgui.h>   using namespace std;   using namespace cv;   int main(int a