NoiseDataFilter.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. using System;
  2. using System.Linq;
  3. using System.Drawing;
  4. using System.Collections.Generic;
  5. namespace shjxCamera
  6. {
  7. /// <summary>
  8. /// 将队列中的前后两幅图像像素点进行相减,最大的10%和最小的10%视为噪点,其余值进行平均值计算
  9. /// </summary>
  10. public class NoiseDataFilter
  11. {
  12. public NoiseDataFilter()
  13. {
  14. Count = 2;
  15. lstImages = new List<List<Color>>();
  16. NoiseCnt = 10;
  17. }
  18. #region fields
  19. /// <summary>
  20. /// 队列中保留图像数,默认为2
  21. /// </summary>
  22. public int Count { get; set; }
  23. /// <summary>
  24. /// 过滤噪点数,单向
  25. /// </summary>
  26. public int NoiseCnt { get; set; }
  27. /// <summary>
  28. /// 用于比较的图像像素队列
  29. /// </summary>
  30. protected List<List<Color>> lstImages { get; set; }
  31. #endregion
  32. #region methods
  33. /// <summary>
  34. /// 加入一幅图像到队列,不做处理
  35. /// </summary>
  36. /// <param name="newImage"></param>
  37. public void AddImage(List<Color> newImage)
  38. {
  39. lstImages.Add(newImage);
  40. while (lstImages.Count > this.Count)
  41. {
  42. lstImages.RemoveAt(0);
  43. }
  44. }
  45. /// <summary>
  46. /// 对图像进行噪点过滤,然后计算rgb总值
  47. /// </summary>
  48. /// <param name="newImage"></param>
  49. /// <param name="sumR"></param>
  50. /// <param name="sumG"></param>
  51. /// <param name="sumB"></param>
  52. public void CalcImage(List<Color> newImage, ref int sumR, ref int sumG, ref int sumB)
  53. {
  54. AddImage(newImage);
  55. CalcAvg(ref sumR, ref sumG, ref sumB);
  56. }
  57. /// <summary>
  58. /// 清除队列数据
  59. /// </summary>
  60. public void Clear()
  61. {
  62. if (lstImages != null)
  63. {
  64. lstImages.Clear();
  65. }
  66. }
  67. #endregion
  68. #region protected methods
  69. /// <summary>
  70. /// 计算后图减前图的过滤图的总值
  71. /// </summary>
  72. /// <param name="sumR"></param>
  73. /// <param name="sumG"></param>
  74. /// <param name="sumB"></param>
  75. protected void calcAvg1(ref int sumR, ref int sumG, ref int sumB)
  76. {
  77. if (lstImages == null)
  78. {
  79. throw new Exception("队列没有数据");
  80. }
  81. if (lstImages.Count >= 2)
  82. {
  83. // 计算滤噪后的总值
  84. List<Color> img1 = lstImages[lstImages.Count - 2]; // 上一幅
  85. List<Color> img2 = lstImages[lstImages.Count - 1]; // 当前幅
  86. #region 计算后图减前图的差值
  87. string strr = "R:\r\n", strg = "G:\r\n", strb = "B:\r\n";
  88. List<IntRGB> imgsub = new List<IntRGB>();
  89. IntRGB val;
  90. for (int i = 0; i < img2.Count; i++)
  91. {
  92. val = new IntRGB(
  93. img2[i].R - img1[i].R,
  94. img2[i].G - img1[i].G,
  95. img2[i].B - img1[i].B);
  96. imgsub.Add(val);
  97. /*if (this.IsLog)
  98. {
  99. strr = string.Format("{0}{1},", strr, val.R.ToString().PadLeft(3, ' '));
  100. strg = string.Format("{0}{1},", strg, val.G.ToString().PadLeft(3, ' '));
  101. strb = string.Format("{0}{1},", strb, val.B.ToString().PadLeft(3, ' '));
  102. if (i % this.LogWidth == 11)
  103. {
  104. strr = string.Format("{0}\r\n", strr);
  105. strg = string.Format("{0}\r\n", strg);
  106. strb = string.Format("{0}\r\n", strb);
  107. }
  108. }*/
  109. }
  110. /*if (this.IsLog)
  111. {
  112. //strr = string.Format("{0}\r\n", strr);
  113. //strg = string.Format("{0}\r\n", strg);
  114. //strb = string.Format("{0}\r\n", strb);
  115. swLog.WriteLine("相减数据:");
  116. swLog.WriteLine(strr);
  117. swLog.WriteLine(strg);
  118. swLog.WriteLine(strb);
  119. swLog.WriteLine();
  120. }*/
  121. #endregion
  122. #region 统计差值,以G值为准
  123. Dictionary<int, int> dicimg = new Dictionary<int, int>();
  124. foreach (IntRGB subval in imgsub)
  125. {
  126. if (dicimg.Keys.Contains(subval.G))
  127. {
  128. dicimg[subval.G] += 1;
  129. }
  130. else
  131. {
  132. dicimg.Add(subval.G, 1);
  133. }
  134. }
  135. List<KeyValuePair<int, int>> lstsub = dicimg.OrderByDescending(c => c.Key).ToList();
  136. #endregion
  137. #region 计算阈值参数
  138. NoiseParam npm = new NoiseParam(NoiseCnt);
  139. npm.Threhold = img2.Count / 10;
  140. int tmp = 0; // 计算上阈值
  141. for (int i = 0; i < lstsub.Count; i++)
  142. {
  143. tmp += lstsub[i].Value;
  144. if (tmp < npm.Threhold)
  145. {
  146. //if (this.IsLog) { swLog.WriteLine("滤除噪点+:" + lstsub[i].Key.ToString() + "[" + lstsub[i].Value.ToString() + "点]"); }
  147. continue; // 未够数,继续下一组
  148. }
  149. else if (tmp == npm.Threhold)
  150. {
  151. // 正好够数,记录阈值,退出循环
  152. npm.Upper = lstsub[i].Key;
  153. npm.UpCnt = lstsub[i].Value; // 该阈值需消除的点数
  154. //if (this.IsLog) { swLog.WriteLine("滤除噪点+:" + lstsub[i].Key.ToString() + "[" + npm.UpCnt.ToString() + "点]"); }
  155. break;
  156. }
  157. else
  158. {
  159. // 过数,计算阈值,退出循环
  160. npm.Upper = lstsub[i].Key;
  161. npm.UpCnt = lstsub[i].Value - (tmp - npm.Threhold);
  162. //if (this.IsLog) { swLog.WriteLine("滤除噪点+:" + lstsub[i].Key.ToString() + "[" + npm.UpCnt.ToString() + "点]"); }
  163. break;
  164. }
  165. }
  166. tmp = 0; // 计算下阈值
  167. for (int i = lstsub.Count - 1; i >= 0; i--)
  168. {
  169. tmp += lstsub[i].Value;
  170. if (tmp < npm.Threhold)
  171. {
  172. //if (this.IsLog) { swLog.WriteLine("滤除噪点-:" + lstsub[i].Key.ToString() + "[" + lstsub[i].Value.ToString() + "点]"); }
  173. continue; // 未够数,继续下一组
  174. }
  175. else if (tmp == npm.Threhold)
  176. {
  177. // 正好够数,记录阈值,退出循环
  178. npm.Lower = lstsub[i].Key;
  179. npm.LowCnt = lstsub[i].Value; // 该阈值需消除的点数
  180. //if (this.IsLog) { swLog.WriteLine("滤除噪点-:" + lstsub[i].Key.ToString() + "[" + npm.LowCnt.ToString() + "点]"); }
  181. break;
  182. }
  183. else
  184. {
  185. // 过数,计算阈值,退出循环
  186. npm.Lower = lstsub[i].Key;
  187. npm.LowCnt = lstsub[i].Value - (tmp - npm.Threhold);
  188. //if (this.IsLog) { swLog.WriteLine("滤除噪点-:" + lstsub[i].Key.ToString() + "[" + npm.LowCnt.ToString() + "点]"); }
  189. break;
  190. }
  191. }
  192. #endregion
  193. #region 滤除噪点后,计算总值
  194. for (int i = 0; i < img2.Count; i++)
  195. {
  196. if (imgsub[i].G > npm.Upper || imgsub[i].G < npm.Lower)
  197. {
  198. continue; // 噪点,滤去
  199. }
  200. else if (imgsub[i].G == npm.Upper && npm.UpCnt > 0)
  201. {
  202. // 部分噪点,滤去部分
  203. npm.UpCnt--;
  204. continue;
  205. }
  206. else if (imgsub[i].G == npm.Lower && npm.LowCnt > 0)
  207. {
  208. // 部分噪点,滤去部分
  209. npm.LowCnt--;
  210. continue;
  211. }
  212. else
  213. {
  214. // 非噪点,计算总值
  215. sumR += img2[i].R;
  216. sumG += img2[i].G;
  217. sumB += img2[i].B;
  218. }
  219. }
  220. /*if (this.IsLog)
  221. {
  222. tmp = img2.Count - npm.Threhold * 2;
  223. strr = string.Format("总值:(R, G, B)({0}, {1}, {2}) 数据个数:{3}", sumR, sumG, sumB, tmp);
  224. swLog.WriteLine(strr);
  225. strr = string.Format("均值:(R, G, B)({0}, {1}, {2})\r\n", (sumR * 1.0 / tmp).ToString("F2"), (sumG * 1.0 / tmp).ToString("F2"), (sumB * 1.0 / tmp).ToString("F2"));
  226. swLog.WriteLine(strr);
  227. swLog.WriteLine();
  228. }*/
  229. #endregion
  230. }
  231. else
  232. {
  233. #region 仅一图,无法去噪,直接计算总值
  234. List<Color> img2 = lstImages[lstImages.Count - 1]; // 上一幅
  235. for (int i = 0; i < img2.Count; i++)
  236. {
  237. sumR += img2[i].R;
  238. sumG += img2[i].G;
  239. sumB += img2[i].B;
  240. }
  241. /*if (this.IsLog)
  242. {
  243. int tmp = img2.Count;
  244. string strr = string.Format("总值:(R, G, B)({0}, {1}, {2}) 数据个数:{3}", sumR, sumG, sumB, tmp);
  245. swLog.WriteLine(strr);
  246. strr = string.Format("均值:(R, G, B)({0}, {1}, {2})\r\n", (sumR * 1.0 / tmp).ToString("F2"), (sumG * 1.0 / tmp).ToString("F2"), (sumB * 1.0 / tmp).ToString("F2"));
  247. swLog.WriteLine(strr);
  248. swLog.WriteLine();
  249. }*/
  250. #endregion
  251. }
  252. }
  253. /// <summary>
  254. /// 计算后图减前图的过滤图的总值
  255. /// </summary>
  256. /// <param name="sumR"></param>
  257. /// <param name="sumG"></param>
  258. /// <param name="sumB"></param>
  259. protected void CalcAvg(ref int sumR, ref int sumG, ref int sumB)
  260. {
  261. if (lstImages == null)
  262. {
  263. throw new Exception("队列没有数据");
  264. }
  265. if (lstImages.Count >= 2)
  266. {
  267. // 计算滤噪后的总值
  268. List<Color> img1 = lstImages[lstImages.Count - 2]; // 上一幅
  269. List<Color> img2 = lstImages[lstImages.Count - 1]; // 当前幅
  270. #region 计算后图减前图的差值
  271. string strr = "R:\r\n", strg = "G:\r\n", strb = "B:\r\n";
  272. List<IntRGB> imgsub = new List<IntRGB>();
  273. IntRGB val;
  274. for (int i = 0; i < img2.Count; i++)
  275. {
  276. val = new IntRGB(
  277. img2[i].R - img1[i].R,
  278. img2[i].G - img1[i].G,
  279. img2[i].B - img1[i].B);
  280. imgsub.Add(val);
  281. /*if (this.IsLog)
  282. {
  283. strr = string.Format("{0}{1},", strr, val.R.ToString().PadLeft(3, ' '));
  284. strg = string.Format("{0}{1},", strg, val.G.ToString().PadLeft(3, ' '));
  285. strb = string.Format("{0}{1},", strb, val.B.ToString().PadLeft(3, ' '));
  286. if (i % this.LogWidth == LogWidth - 1)
  287. {
  288. strr = string.Format("{0}\r\n", strr);
  289. strg = string.Format("{0}\r\n", strg);
  290. strb = string.Format("{0}\r\n", strb);
  291. }
  292. }*/
  293. }
  294. /*if (this.IsLog)
  295. {
  296. //strr = string.Format("{0}\r\n", strr);
  297. //strg = string.Format("{0}\r\n", strg);
  298. //strb = string.Format("{0}\r\n", strb);
  299. swLog.WriteLine("相减数据:");
  300. swLog.WriteLine(strr);
  301. swLog.WriteLine(strg);
  302. swLog.WriteLine(strb);
  303. swLog.WriteLine();
  304. }*/
  305. #endregion
  306. #region 统计差值,以G值为准
  307. Dictionary<int, int> dicimg = new Dictionary<int, int>();
  308. foreach (IntRGB subval in imgsub)
  309. {
  310. if (dicimg.Keys.Contains(subval.G))
  311. {
  312. dicimg[subval.G] += 1;
  313. }
  314. else
  315. {
  316. dicimg.Add(subval.G, 1);
  317. }
  318. }
  319. List<KeyValuePair<int, int>> lstsub = dicimg.OrderByDescending(c => c.Key).ToList();
  320. #endregion
  321. #region 计算阈值参数
  322. NoiseParam npm = new NoiseParam(NoiseCnt);
  323. npm.Threhold = img2.Count / 10;
  324. int tmp = 0; // 计算上阈值
  325. for (int i = 0; i < lstsub.Count; i++)
  326. {
  327. tmp += lstsub[i].Value;
  328. if (tmp < npm.Threhold)
  329. {
  330. //if (this.IsLog) { swLog.WriteLine("滤除噪点+:" + lstsub[i].Key.ToString() + "[" + lstsub[i].Value.ToString() + "点]"); }
  331. continue; // 未够数,继续下一组
  332. }
  333. else if (tmp == npm.Threhold)
  334. {
  335. // 正好够数,记录阈值,退出循环
  336. npm.Upper = lstsub[i].Key;
  337. npm.UpCnt = lstsub[i].Value; // 该阈值需消除的点数
  338. //if (this.IsLog) { swLog.WriteLine("滤除噪点+:" + lstsub[i].Key.ToString() + "[" + npm.UpCnt.ToString() + "点]"); }
  339. break;
  340. }
  341. else
  342. {
  343. // 过数,计算阈值,退出循环
  344. npm.Upper = lstsub[i].Key;
  345. npm.UpCnt = lstsub[i].Value - (tmp - npm.Threhold);
  346. //if (this.IsLog) { swLog.WriteLine("滤除噪点+:" + lstsub[i].Key.ToString() + "[" + npm.UpCnt.ToString() + "点]"); }
  347. break;
  348. }
  349. }
  350. tmp = 0; // 计算下阈值
  351. for (int i = lstsub.Count - 1; i >= 0; i--)
  352. {
  353. tmp += lstsub[i].Value;
  354. if (tmp < npm.Threhold)
  355. {
  356. //if (this.IsLog) { swLog.WriteLine("滤除噪点-:" + lstsub[i].Key.ToString() + "[" + lstsub[i].Value.ToString() + "点]"); }
  357. continue; // 未够数,继续下一组
  358. }
  359. else if (tmp == npm.Threhold)
  360. {
  361. // 正好够数,记录阈值,退出循环
  362. npm.Lower = lstsub[i].Key;
  363. npm.LowCnt = lstsub[i].Value; // 该阈值需消除的点数
  364. //if (this.IsLog) { swLog.WriteLine("滤除噪点-:" + lstsub[i].Key.ToString() + "[" + npm.LowCnt.ToString() + "点]"); }
  365. break;
  366. }
  367. else
  368. {
  369. // 过数,计算阈值,退出循环
  370. npm.Lower = lstsub[i].Key;
  371. npm.LowCnt = lstsub[i].Value - (tmp - npm.Threhold);
  372. //if (this.IsLog) { swLog.WriteLine("滤除噪点-:" + lstsub[i].Key.ToString() + "[" + npm.LowCnt.ToString() + "点]"); }
  373. break;
  374. }
  375. }
  376. #endregion
  377. #region 滤除噪点后,计算总值
  378. double tmpR = 0.0, tmpG = 0.0, tmpB = 0.0;
  379. double tmpu = (dicimg[npm.Upper] - npm.UpCnt) * 1.0 / dicimg[npm.Upper]; // 部分过滤点保留的比率
  380. /*if (this.IsLog)
  381. {
  382. swLog.WriteLine(string.Format("噪点+:{0}[{1}/{2}]={3}", npm.Upper.ToString(), npm.UpCnt, dicimg[npm.Upper], tmpu.ToString("F2")));
  383. }*/
  384. double tmpd = (dicimg[npm.Lower] - npm.LowCnt) * 1.0 / dicimg[npm.Lower]; // 部分过滤点保留的比率
  385. /* if (this.IsLog)
  386. {
  387. swLog.WriteLine( string.Format("噪点-:{0}[{1}/{2}]={3}", npm.Lower.ToString(), npm.LowCnt, dicimg[npm.Lower], tmpd.ToString("F2")));
  388. }*/
  389. for (int i = 0; i < img2.Count; i++)
  390. {
  391. if (imgsub[i].G > npm.Upper || imgsub[i].G < npm.Lower)
  392. {
  393. continue; // 噪点,滤去
  394. }
  395. else if (imgsub[i].G == npm.Upper && npm.UpCnt > 0)
  396. {
  397. // 部分噪点,滤去部分
  398. tmpR += img2[i].R * tmpu;
  399. tmpG += img2[i].G * tmpu;
  400. tmpB += img2[i].B * tmpu;
  401. continue;
  402. }
  403. else if (imgsub[i].G == npm.Lower && npm.LowCnt > 0)
  404. {
  405. // 部分噪点,滤去部分
  406. tmpR += img2[i].R * tmpd;
  407. tmpG += img2[i].G * tmpd;
  408. tmpB += img2[i].B * tmpd;
  409. continue;
  410. }
  411. else
  412. {
  413. // 非噪点,计算总值
  414. tmpR += img2[i].R;
  415. tmpG += img2[i].G;
  416. tmpB += img2[i].B;
  417. }
  418. }
  419. sumR = Convert.ToInt32(tmpR);
  420. sumG = Convert.ToInt32(tmpG);
  421. sumB = Convert.ToInt32(tmpB);
  422. /*if (this.IsLog)
  423. {
  424. tmp = img2.Count - npm.Threhold * 2;
  425. strr = string.Format("处理总值:(R, G, B)({0}, {1}, {2}), 数据个数:{3}, ", sumR, sumG, sumB, tmp)
  426. + string.Format("均值:(R, G, B)({0}, {1}, {2})", (sumR * 1.0 / tmp).ToString("F2"), (sumG * 1.0 / tmp).ToString("F2"), (sumB * 1.0 / tmp).ToString("F2"));
  427. swLog.WriteLine(strr);
  428. //this.swLog.WriteLine();
  429. }*/
  430. #endregion
  431. }
  432. else
  433. {
  434. #region 仅一图,无法去噪,直接计算总值
  435. List<Color> img2 = lstImages[lstImages.Count - 1]; // 上一幅
  436. for (int i = 0; i < img2.Count; i++)
  437. {
  438. sumR += img2[i].R;
  439. sumG += img2[i].G;
  440. sumB += img2[i].B;
  441. }
  442. /* if (this.IsLog)
  443. {
  444. int tmp = img2.Count;
  445. string strr = string.Format("处理总值:(R, G, B)({0}, {1}, {2}), 数据个数:{3}, ", sumR, sumG, sumB, tmp)
  446. + string.Format("均值:(R, G, B)({0}, {1}, {2})", (sumR * 1.0 / tmp).ToString("F2"), (sumG * 1.0 / tmp).ToString("F2"), (sumB * 1.0 / tmp).ToString("F2"));
  447. swLog.WriteLine(strr);
  448. //this.swLog.WriteLine();
  449. }*/
  450. #endregion
  451. }
  452. }
  453. #endregion
  454. }
  455. /// <summary>
  456. /// 用int类型保存RGB三个值
  457. /// </summary>
  458. public class IntRGB
  459. {
  460. #region properties
  461. public int R { get; set; }
  462. public int G { get; set; }
  463. public int B { get; set; }
  464. #endregion
  465. public IntRGB()
  466. {
  467. R = 0;
  468. G = 0;
  469. B = 0;
  470. }
  471. public IntRGB(int r, int g, int b)
  472. {
  473. R = r;
  474. G = g;
  475. B = b;
  476. }
  477. }
  478. /// <summary>
  479. /// 噪点相关参数
  480. /// </summary>
  481. public class NoiseParam
  482. {
  483. /// <summary>
  484. /// 前后两图相减法,去除上下噪点
  485. /// </summary>
  486. /// <param name="threhold">上下各自噪点数</param>
  487. public NoiseParam(int threhold)
  488. {
  489. Threhold = threhold;
  490. Upper = 0;
  491. UpCnt = 0;
  492. Lower = 0;
  493. LowCnt = 0;
  494. }
  495. #region
  496. /// <summary>
  497. /// 噪点个数
  498. /// </summary>
  499. public int Threhold { get; set; }
  500. /// <summary>
  501. /// 噪点上阈值
  502. /// </summary>
  503. public int Upper { get; set; }
  504. /// <summary>
  505. /// 上阈值噪点数个数
  506. /// </summary>
  507. public int UpCnt { get; set; }
  508. /// <summary>
  509. /// 噪点下阈值
  510. /// </summary>
  511. public int Lower { get; set; }
  512. /// <summary>
  513. /// 下阈值噪点个数
  514. /// </summary>
  515. public int LowCnt { get; set; }
  516. #endregion
  517. }
  518. }