HistTools.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using OpenCvSharp;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace SmartCoalApplication.Base.CommTool
  8. {
  9. public class HistTools
  10. {
  11. /// <summary>
  12. /// 灰度图
  13. /// </summary>
  14. /// <param name="mat"></param>
  15. public static float[] CalcHist(Mat gray)
  16. {
  17. float[] hist_int = new float[255];
  18. Mat hist = new Mat();
  19. //设置计算直方图的维度
  20. int dims = 1;
  21. int[] histSize = { 255 };
  22. Rangef[] pranges = new Rangef[1];
  23. pranges[0] = new Rangef(0.0F, 256.0F);
  24. //计算直方图
  25. Cv2.CalcHist(new Mat[1] { gray }, new int[] { 0 }, new Mat(), hist, dims, histSize, pranges);
  26. //创建数据,便于计算
  27. for (int w = 0; w < hist.Height; w++)
  28. {
  29. hist_int[w] = hist.At<float>(0, w);
  30. }
  31. return hist_int;
  32. }
  33. /// <summary>
  34. /// 直方图平滑
  35. /// </summary>
  36. /// <param name="arr"></param>
  37. /// <param name="step"></param>
  38. /// <returns></returns>
  39. public static int[] Smooth(float[] arr, int step, int num=256)
  40. {
  41. int start = step - step / 2;
  42. int end = num - step / 2;
  43. double temp = 0;
  44. int[] res = new int[num];
  45. for (int i = start; i < end; i++)
  46. {
  47. temp = 0;
  48. for (int j = 0 - step / 2; j < step / 2; j++)
  49. {
  50. temp += arr[i + j];
  51. }
  52. temp /= step;
  53. res[i] = (int)temp;
  54. }
  55. return res;
  56. }
  57. /// <summary>
  58. /// 寻找峰
  59. /// </summary>
  60. /// <param name="hists_new"></param>
  61. /// <param name="leftValue"></param>
  62. /// <param name="rightValue"></param>
  63. /// <param name="mountainsList"></param>
  64. /// <param name="mountainsArrList"></param>
  65. /// <param name="mountainsArrListBackUp"></param>
  66. /// <param name="str"></param>
  67. /// <param name="smoothStep"></param>
  68. public static void FindTopAndBetweenWithOutWT123(int[] hists_new, int leftValue, int rightValue, List<int> mountainsList, List<int[]> mountainsArrList, List<int[]> mountainsArrListBackUp, List<string> str, int smoothStep)
  69. {
  70. //获得去除左右值的最高波峰
  71. int topValue = leftValue;
  72. for (int y = leftValue; y < rightValue; y++)
  73. {
  74. if (hists_new[y] > hists_new[topValue])
  75. {
  76. topValue = y;
  77. }
  78. }
  79. if (topValue == 0 || topValue == leftValue || topValue == rightValue)
  80. return;
  81. mountainsList.Add(topValue);
  82. //获得波峰左右的波谷
  83. int leftTrough = topValue;
  84. int leftTroughBackUp = 0;
  85. //从波峰点向左侧找波谷点
  86. for (int y = topValue - 1; y > leftValue; y--)
  87. {
  88. if (hists_new[y] >= 0 && hists_new[y] <= hists_new[leftTrough])
  89. {
  90. leftTrough = y;
  91. }
  92. else
  93. {
  94. break;
  95. }
  96. }
  97. //微调
  98. //if (smoothStep >= 30)
  99. {
  100. for (int y = leftTrough + 1; y < topValue; y++)
  101. {
  102. if (hists_new[y] == hists_new[leftTrough])// || hists_new[y] < hists_new[topValue] / 10
  103. {
  104. leftTroughBackUp = y;
  105. }
  106. }
  107. }
  108. if (leftTroughBackUp == 0) leftTroughBackUp = leftTrough;
  109. int rightTrough = topValue;
  110. int rightTroughBackUp = 0;
  111. //从波峰点向右侧找波谷点
  112. for (int y = topValue + 1; y < rightValue; y++)
  113. {
  114. if (hists_new[y] >= 0 && hists_new[y] <= hists_new[rightTrough])
  115. {
  116. rightTrough = y;
  117. }
  118. else
  119. {
  120. break;
  121. }
  122. }
  123. //微调
  124. //if (smoothStep >= 30)
  125. {
  126. for (int y = rightTrough - 1; y > topValue; y--)
  127. {
  128. if (hists_new[y] == hists_new[rightTrough])// || hists_new[y] < hists_new[topValue] / 10
  129. {
  130. rightTroughBackUp = y;
  131. }
  132. }
  133. }
  134. if (rightTroughBackUp == 0) rightTroughBackUp = rightTrough;
  135. int[] arr = new int[2];
  136. arr[0] = leftTroughBackUp;
  137. arr[1] = rightTroughBackUp;
  138. mountainsArrList.Add(arr);
  139. int[] arr1 = new int[2];
  140. arr1[0] = leftTroughBackUp > 0 ? leftTroughBackUp : leftTrough;
  141. arr1[1] = rightTroughBackUp > 0 ? rightTroughBackUp : rightTrough;
  142. mountainsArrListBackUp.Add(arr1);
  143. if (leftValue < leftTrough - 1)
  144. {
  145. if (!str.Contains(leftValue + "|" + leftTrough))
  146. {
  147. str.Add(leftValue + "|" + leftTrough);
  148. FindTopAndBetweenWithOutWT123(hists_new, leftValue, leftTrough, mountainsList, mountainsArrList, mountainsArrListBackUp, str, smoothStep);
  149. }
  150. }
  151. if (rightValue > rightTrough + 1)
  152. {
  153. if (!str.Contains(rightValue + "|" + rightTrough))
  154. {
  155. str.Add(rightValue + "|" + rightTrough);
  156. FindTopAndBetweenWithOutWT123(hists_new, rightTrough, rightValue, mountainsList, mountainsArrList, mountainsArrListBackUp, str, smoothStep);
  157. }
  158. }
  159. }
  160. }
  161. }