zimIdentifyEdge.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. using o0.Geometry2D.Float;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7. namespace o0.Project
  8. {
  9. public static partial class Extension
  10. {
  11. static public (Matrix, Matrix) zimIdentifyEdge55GradientFull(this Matrix mat)
  12. {
  13. var conSize = 5;
  14. var conArea = conSize * conSize;
  15. var convolution = new Matrix(new Geometry2D.Vector<int>(mat.Size.x - (conSize - 1), mat.Size.y - (conSize - 1)), Tiling: true);
  16. var pixelC = convolution.Element;
  17. var dirMat = new Matrix(convolution.Size, Tiling: true);
  18. var pixelDir = dirMat.Element;
  19. var convolutionWidth = convolution.Size.x;
  20. Parallel.For(0, convolution.Size.x * convolution.Size.y, i => {
  21. var iPx = i % convolutionWidth;
  22. var iPy = i / convolutionWidth;
  23. var sum = 0.0f;
  24. for (var jX = 0; jX < conSize; ++jX)
  25. for (var jY = 0; jY < conSize; ++jY)
  26. sum += mat.Element[(iPx + jX) + (iPy + jY) * mat.Size.x];
  27. if (sum == 0.0f)
  28. return;
  29. var factor0 = new float[] { 1, 1.5f, 2 };
  30. var factor1 = new float[] { 0.94868329805051379959966806332982f, 1.3416407864998738178455042012388f, 1.7888543819998317571273389349851f };
  31. var factor2 = new float[] { 0.70710678118654752440084436210485f, 1.0606601717798212866012665431573f, 1.4142135623730950488016887242097f };
  32. //float root2 = 1.4142135623730950488016887242097f; // 根号2
  33. //float root5d4 = 1.1180339887498948482045868343656f; // 根号5/4
  34. //float root10d3 = 1.0540925533894597773329645148109f; // 根号10/9
  35. var g0 =
  36. mat.Element[(iPx + 4) + (iPy + 0) * mat.Size.x] * factor0[0]
  37. + mat.Element[(iPx + 4) + (iPy + 1) * mat.Size.x] * factor0[1]
  38. + mat.Element[(iPx + 4) + (iPy + 2) * mat.Size.x] * factor0[2]
  39. + mat.Element[(iPx + 4) + (iPy + 3) * mat.Size.x] * factor0[1]
  40. + mat.Element[(iPx + 4) + (iPy + 4) * mat.Size.x] * factor0[0]
  41. - mat.Element[(iPx + 0) + (iPy + 0) * mat.Size.x] * factor0[0]
  42. - mat.Element[(iPx + 0) + (iPy + 1) * mat.Size.x] * factor0[1]
  43. - mat.Element[(iPx + 0) + (iPy + 2) * mat.Size.x] * factor0[2]
  44. - mat.Element[(iPx + 0) + (iPy + 3) * mat.Size.x] * factor0[1]
  45. - mat.Element[(iPx + 0) + (iPy + 4) * mat.Size.x] * factor0[0];
  46. var g26 = //梯度26.565度
  47. mat.Element[(iPx + 4) + (iPy + 1) * mat.Size.x] * factor1[0]
  48. + mat.Element[(iPx + 4) + (iPy + 2) * mat.Size.x] * factor1[1]
  49. + mat.Element[(iPx + 4) + (iPy + 3) * mat.Size.x] * factor1[2]
  50. + mat.Element[(iPx + 4) + (iPy + 4) * mat.Size.x] * factor1[1]
  51. + mat.Element[(iPx + 3) + (iPy + 4) * mat.Size.x] * factor1[0]
  52. - mat.Element[(iPx + 0) + (iPy + 3) * mat.Size.x] * factor1[0]
  53. - mat.Element[(iPx + 0) + (iPy + 2) * mat.Size.x] * factor1[1]
  54. - mat.Element[(iPx + 0) + (iPy + 1) * mat.Size.x] * factor1[2]
  55. - mat.Element[(iPx + 0) + (iPy + 0) * mat.Size.x] * factor1[1]
  56. - mat.Element[(iPx + 1) + (iPy + 0) * mat.Size.x] * factor1[0];
  57. var g45 =
  58. mat.Element[(iPx + 4) + (iPy + 2) * mat.Size.x] * factor2[0]
  59. + mat.Element[(iPx + 4) + (iPy + 3) * mat.Size.x] * factor2[1]
  60. + mat.Element[(iPx + 4) + (iPy + 4) * mat.Size.x] * factor2[2]
  61. + mat.Element[(iPx + 3) + (iPy + 4) * mat.Size.x] * factor2[1]
  62. + mat.Element[(iPx + 2) + (iPy + 4) * mat.Size.x] * factor2[0]
  63. - mat.Element[(iPx + 0) + (iPy + 2) * mat.Size.x] * factor2[0]
  64. - mat.Element[(iPx + 0) + (iPy + 1) * mat.Size.x] * factor2[1]
  65. - mat.Element[(iPx + 0) + (iPy + 0) * mat.Size.x] * factor2[2]
  66. - mat.Element[(iPx + 1) + (iPy + 0) * mat.Size.x] * factor2[1]
  67. - mat.Element[(iPx + 2) + (iPy + 0) * mat.Size.x] * factor2[0];
  68. var g63 = //梯度63.435度
  69. mat.Element[(iPx + 4) + (iPy + 3) * mat.Size.x] * factor1[0]
  70. + mat.Element[(iPx + 4) + (iPy + 4) * mat.Size.x] * factor1[1]
  71. + mat.Element[(iPx + 3) + (iPy + 4) * mat.Size.x] * factor1[2]
  72. + mat.Element[(iPx + 2) + (iPy + 4) * mat.Size.x] * factor1[1]
  73. + mat.Element[(iPx + 1) + (iPy + 4) * mat.Size.x] * factor1[0]
  74. - mat.Element[(iPx + 0) + (iPy + 1) * mat.Size.x] * factor1[0]
  75. - mat.Element[(iPx + 0) + (iPy + 0) * mat.Size.x] * factor1[1]
  76. - mat.Element[(iPx + 1) + (iPy + 0) * mat.Size.x] * factor1[2]
  77. - mat.Element[(iPx + 2) + (iPy + 0) * mat.Size.x] * factor1[1]
  78. - mat.Element[(iPx + 3) + (iPy + 0) * mat.Size.x] * factor1[0];
  79. var g90 =
  80. mat.Element[(iPx + 4) + (iPy + 4) * mat.Size.x] * factor0[0]
  81. + mat.Element[(iPx + 3) + (iPy + 4) * mat.Size.x] * factor0[1]
  82. + mat.Element[(iPx + 2) + (iPy + 4) * mat.Size.x] * factor0[2]
  83. + mat.Element[(iPx + 1) + (iPy + 4) * mat.Size.x] * factor0[1]
  84. + mat.Element[(iPx + 0) + (iPy + 4) * mat.Size.x] * factor0[0]
  85. - mat.Element[(iPx + 0) + (iPy + 0) * mat.Size.x] * factor0[0]
  86. - mat.Element[(iPx + 1) + (iPy + 0) * mat.Size.x] * factor0[1]
  87. - mat.Element[(iPx + 2) + (iPy + 0) * mat.Size.x] * factor0[2]
  88. - mat.Element[(iPx + 3) + (iPy + 0) * mat.Size.x] * factor0[1]
  89. - mat.Element[(iPx + 4) + (iPy + 0) * mat.Size.x] * factor0[0];
  90. var g116 = //梯度116.565度
  91. mat.Element[(iPx + 3) + (iPy + 4) * mat.Size.x] * factor1[0]
  92. + mat.Element[(iPx + 2) + (iPy + 4) * mat.Size.x] * factor1[1]
  93. + mat.Element[(iPx + 1) + (iPy + 4) * mat.Size.x] * factor1[2]
  94. + mat.Element[(iPx + 0) + (iPy + 4) * mat.Size.x] * factor1[1]
  95. + mat.Element[(iPx + 0) + (iPy + 3) * mat.Size.x] * factor1[0]
  96. - mat.Element[(iPx + 1) + (iPy + 0) * mat.Size.x] * factor1[0]
  97. - mat.Element[(iPx + 2) + (iPy + 0) * mat.Size.x] * factor1[1]
  98. - mat.Element[(iPx + 3) + (iPy + 0) * mat.Size.x] * factor1[2]
  99. - mat.Element[(iPx + 4) + (iPy + 0) * mat.Size.x] * factor1[1]
  100. - mat.Element[(iPx + 4) + (iPy + 1) * mat.Size.x] * factor1[0];
  101. var g135 =
  102. mat.Element[(iPx + 2) + (iPy + 4) * mat.Size.x] * factor2[0]
  103. + mat.Element[(iPx + 1) + (iPy + 4) * mat.Size.x] * factor2[1]
  104. + mat.Element[(iPx + 0) + (iPy + 4) * mat.Size.x] * factor2[2]
  105. + mat.Element[(iPx + 0) + (iPy + 3) * mat.Size.x] * factor2[1]
  106. + mat.Element[(iPx + 0) + (iPy + 2) * mat.Size.x] * factor2[0]
  107. - mat.Element[(iPx + 2) + (iPy + 0) * mat.Size.x] * factor2[0]
  108. - mat.Element[(iPx + 3) + (iPy + 0) * mat.Size.x] * factor2[1]
  109. - mat.Element[(iPx + 4) + (iPy + 0) * mat.Size.x] * factor2[2]
  110. - mat.Element[(iPx + 4) + (iPy + 1) * mat.Size.x] * factor2[1]
  111. - mat.Element[(iPx + 4) + (iPy + 2) * mat.Size.x] * factor2[0];
  112. var g153 = // 梯度153.435度
  113. mat.Element[(iPx + 1) + (iPy + 4) * mat.Size.x] * factor1[0]
  114. + mat.Element[(iPx + 0) + (iPy + 4) * mat.Size.x] * factor1[1]
  115. + mat.Element[(iPx + 0) + (iPy + 3) * mat.Size.x] * factor1[2]
  116. + mat.Element[(iPx + 0) + (iPy + 2) * mat.Size.x] * factor1[1]
  117. + mat.Element[(iPx + 0) + (iPy + 1) * mat.Size.x] * factor1[0]
  118. - mat.Element[(iPx + 3) + (iPy + 0) * mat.Size.x] * factor1[0]
  119. - mat.Element[(iPx + 4) + (iPy + 0) * mat.Size.x] * factor1[1]
  120. - mat.Element[(iPx + 4) + (iPy + 1) * mat.Size.x] * factor1[2]
  121. - mat.Element[(iPx + 4) + (iPy + 2) * mat.Size.x] * factor1[1]
  122. - mat.Element[(iPx + 4) + (iPy + 3) * mat.Size.x] * factor1[0];
  123. float[] gra = new float[] { MathF.Abs(g0), MathF.Abs(g26), MathF.Abs(g45), MathF.Abs(g63), MathF.Abs(g90), MathF.Abs(g116), MathF.Abs(g135), MathF.Abs(g153) };
  124. var maxIndex = o0.MaxIndex(gra);
  125. pixelC[i] = gra[maxIndex];
  126. switch (maxIndex)
  127. {
  128. case 0:
  129. pixelDir[i] = g0 > 0 ? 0 : 180;
  130. break;
  131. case 1:
  132. pixelDir[i] = g26 > 0 ? 26.565f : 206.565f;
  133. break;
  134. case 2:
  135. pixelDir[i] = g45 > 0 ? 45 : 225;
  136. break;
  137. case 3:
  138. pixelDir[i] = g63 > 0 ? 63.435f : 243.435f;
  139. break;
  140. case 4:
  141. pixelDir[i] = g90 > 0 ? 90 : 270;
  142. break;
  143. case 5:
  144. pixelDir[i] = g116 > 0 ? 116.565f : 296.565f;
  145. break;
  146. case 6:
  147. pixelDir[i] = g135 > 0 ? 135 : 315;
  148. break;
  149. case 7:
  150. pixelDir[i] = g153 > 0 ? 153.435f : 333.435f;
  151. break;
  152. }
  153. //pixelC[i] = MathF.Abs(gX) + MathF.Abs(gY) + MathF.Abs(gXY) + MathF.Abs(gYX);
  154. /*
  155. if (pixelC[i] / 9f < 1.0f / 256f)
  156. {
  157. pixelC[i] = 0;
  158. return;
  159. }/**/
  160. /*
  161. if (sum < 100f/256f)
  162. {
  163. pixelC[i] = 0;
  164. return;
  165. }/**/
  166. //pixelC[i] = o0.Max(MathF.Abs(gX), MathF.Abs(gY), MathF.Abs(gXY), MathF.Abs(gYX));
  167. //pixelC[i] /= MathF.Pow(sum / 9, 0.1f);
  168. //pixelC[i] /= min + 0.5f;
  169. //pixelC[i] /= min + 0.1f;
  170. pixelC[i] /= sum / 25f + 1f;//+1是为了防止除数太小,结果不合理地过大
  171. });
  172. return (convolution.Map(ignoreExtremum: 10), dirMat);
  173. }
  174. static public (Matrix, Matrix) zimIdentifyEdgeGradient45(this Matrix mat, int conSizeLevel = 2)
  175. {
  176. var conSize = 4 * conSizeLevel + 1; // conSize一定 = 4 * n + 1
  177. var conArea = conSize * conSize;
  178. var convolution = new Matrix(new Geometry2D.Vector<int>(mat.Size.x - (conSize - 1), mat.Size.y - (conSize - 1)), Tiling: true);
  179. var pixelC = convolution.Element;
  180. var dirMat = new Matrix(convolution.Size, Tiling: true);
  181. var pixelDir = dirMat.Element;
  182. var convolutionWidth = convolution.Size.x;
  183. Parallel.For(0, convolution.Size.x * convolution.Size.y, i => {
  184. var iPx = i % convolutionWidth;
  185. var iPy = i / convolutionWidth;
  186. var sum = 0.0f;
  187. for (var jX = 0; jX < conSize; ++jX)
  188. for (var jY = 0; jY < conSize; ++jY)
  189. sum += mat.Element[(iPx + jX) + (iPy + jY) * mat.Size.x];
  190. if (sum == 0.0f)
  191. return;
  192. float root2 = 1.4142135623730950488016887242097f; // 根号2
  193. float root5d4 = 1.1180339887498948482045868343656f; // 根号5/4
  194. //float root10d3 = 1.0540925533894597773329645148109f; // 根号10/9
  195. var g0 =
  196. mat.Element[(iPx + 4 * conSizeLevel) + (iPy + 1 * conSizeLevel) * mat.Size.x] * 1f
  197. + mat.Element[(iPx + 4 * conSizeLevel) + (iPy + 2 * conSizeLevel) * mat.Size.x] * 1.5f
  198. + mat.Element[(iPx + 4 * conSizeLevel) + (iPy + 3 * conSizeLevel) * mat.Size.x] * 1f
  199. - mat.Element[(iPx + 0 * conSizeLevel) + (iPy + 3 * conSizeLevel) * mat.Size.x] * 1f
  200. - mat.Element[(iPx + 0 * conSizeLevel) + (iPy + 2 * conSizeLevel) * mat.Size.x] * 1.5f
  201. - mat.Element[(iPx + 0 * conSizeLevel) + (iPy + 1 * conSizeLevel) * mat.Size.x] * 1f;
  202. var g26 = //梯度26.565度
  203. mat.Element[(iPx + 4 * conSizeLevel) + (iPy + 2 * conSizeLevel) * mat.Size.x] * 1f
  204. + mat.Element[(iPx + 4 * conSizeLevel) + (iPy + 3 * conSizeLevel) * mat.Size.x] * 1.5f
  205. + mat.Element[(iPx + 4 * conSizeLevel) + (iPy + 4 * conSizeLevel) * mat.Size.x] * 1f
  206. - mat.Element[(iPx + 0 * conSizeLevel) + (iPy + 2) * mat.Size.x] * 1f
  207. - mat.Element[(iPx + 0 * conSizeLevel) + (iPy + 1 * conSizeLevel) * mat.Size.x] * 1.5f
  208. - mat.Element[(iPx + 0 * conSizeLevel) + (iPy + 0 * conSizeLevel) * mat.Size.x] * 1f;
  209. g26 /= root5d4;
  210. var g45 =
  211. mat.Element[(iPx + 4 * conSizeLevel) + (iPy + 3 * conSizeLevel) * mat.Size.x] * 1f
  212. + mat.Element[(iPx + 4 * conSizeLevel) + (iPy + 4 * conSizeLevel) * mat.Size.x] * 1.5f
  213. + mat.Element[(iPx + 3 * conSizeLevel) + (iPy + 4 * conSizeLevel) * mat.Size.x] * 1f
  214. - mat.Element[(iPx + 0 * conSizeLevel) + (iPy + 1 * conSizeLevel) * mat.Size.x] * 1f
  215. - mat.Element[(iPx + 0 * conSizeLevel) + (iPy + 0 * conSizeLevel) * mat.Size.x] * 1.5f
  216. - mat.Element[(iPx + 1 * conSizeLevel) + (iPy + 0 * conSizeLevel) * mat.Size.x] * 1f;
  217. g45 /= root2;
  218. var g63 = //梯度63.435度
  219. mat.Element[(iPx + 4 * conSizeLevel) + (iPy + 4 * conSizeLevel) * mat.Size.x] * 1f
  220. + mat.Element[(iPx + 3 * conSizeLevel) + (iPy + 4 * conSizeLevel) * mat.Size.x] * 1.5f
  221. + mat.Element[(iPx + 2 * conSizeLevel) + (iPy + 4 * conSizeLevel) * mat.Size.x] * 1f
  222. - mat.Element[(iPx + 0 * conSizeLevel) + (iPy + 0 * conSizeLevel) * mat.Size.x] * 1f
  223. - mat.Element[(iPx + 1 * conSizeLevel) + (iPy + 0 * conSizeLevel) * mat.Size.x] * 1.5f
  224. - mat.Element[(iPx + 2 * conSizeLevel) + (iPy + 0 * conSizeLevel) * mat.Size.x] * 1f;
  225. g63 /= root5d4;
  226. var g90 =
  227. mat.Element[(iPx + 3 * conSizeLevel) + (iPy + 4 * conSizeLevel) * mat.Size.x] * 1f
  228. + mat.Element[(iPx + 2 * conSizeLevel) + (iPy + 4 * conSizeLevel) * mat.Size.x] * 1.5f
  229. + mat.Element[(iPx + 1 * conSizeLevel) + (iPy + 4 * conSizeLevel) * mat.Size.x] * 1f
  230. - mat.Element[(iPx + 1 * conSizeLevel) + (iPy + 0 * conSizeLevel) * mat.Size.x] * 1f
  231. - mat.Element[(iPx + 2 * conSizeLevel) + (iPy + 0 * conSizeLevel) * mat.Size.x] * 1.5f
  232. - mat.Element[(iPx + 3 * conSizeLevel) + (iPy + 0 * conSizeLevel) * mat.Size.x] * 1f;
  233. var g116 = //梯度116.565度
  234. mat.Element[(iPx + 2 * conSizeLevel) + (iPy + 4 * conSizeLevel) * mat.Size.x] * 1f
  235. + mat.Element[(iPx + 1 * conSizeLevel) + (iPy + 4 * conSizeLevel) * mat.Size.x] * 1.5f
  236. + mat.Element[(iPx + 0 * conSizeLevel) + (iPy + 4 * conSizeLevel) * mat.Size.x] * 1f
  237. - mat.Element[(iPx + 2 * conSizeLevel) + (iPy + 0 * conSizeLevel) * mat.Size.x] * 1f
  238. - mat.Element[(iPx + 3 * conSizeLevel) + (iPy + 0 * conSizeLevel) * mat.Size.x] * 1.5f
  239. - mat.Element[(iPx + 4 * conSizeLevel) + (iPy + 0 * conSizeLevel) * mat.Size.x] * 1f;
  240. g116 /= root5d4;
  241. var g135 =
  242. mat.Element[(iPx + 1 * conSizeLevel) + (iPy + 4 * conSizeLevel) * mat.Size.x] * 1f
  243. + mat.Element[(iPx + 0 * conSizeLevel) + (iPy + 4 * conSizeLevel) * mat.Size.x] * 1.5f
  244. + mat.Element[(iPx + 0 * conSizeLevel) + (iPy + 3 * conSizeLevel) * mat.Size.x] * 1f
  245. - mat.Element[(iPx + 3 * conSizeLevel) + (iPy + 0 * conSizeLevel) * mat.Size.x] * 1f
  246. - mat.Element[(iPx + 4 * conSizeLevel) + (iPy + 0 * conSizeLevel) * mat.Size.x] * 1.5f
  247. - mat.Element[(iPx + 4 * conSizeLevel) + (iPy + 1 * conSizeLevel) * mat.Size.x] * 1f;
  248. g135 /= root2;
  249. var g153 = // 梯度153.435度
  250. mat.Element[(iPx + 0 * conSizeLevel) + (iPy + 4 * conSizeLevel) * mat.Size.x] * 1f
  251. + mat.Element[(iPx + 0 * conSizeLevel) + (iPy + 3 * conSizeLevel) * mat.Size.x] * 1.5f
  252. + mat.Element[(iPx + 0 * conSizeLevel) + (iPy + 2 * conSizeLevel) * mat.Size.x] * 1f
  253. - mat.Element[(iPx + 4 * conSizeLevel) + (iPy + 0 * conSizeLevel) * mat.Size.x] * 1f
  254. - mat.Element[(iPx + 4 * conSizeLevel) + (iPy + 1 * conSizeLevel) * mat.Size.x] * 1.5f
  255. - mat.Element[(iPx + 4 * conSizeLevel) + (iPy + 2 * conSizeLevel) * mat.Size.x] * 1f;
  256. g153 /= root5d4;
  257. float[] gra = new float[] { MathF.Abs(g0), MathF.Abs(g26), MathF.Abs(g45), MathF.Abs(g63), MathF.Abs(g90), MathF.Abs(g116), MathF.Abs(g135), MathF.Abs(g153) };
  258. var maxIndex = o0.MaxIndex(gra);
  259. pixelC[i] = gra[maxIndex];
  260. switch (maxIndex)
  261. {
  262. case 0:
  263. pixelDir[i] = g0 > 0 ? 0 : 180;
  264. break;
  265. case 1:
  266. pixelDir[i] = g26 > 0 ? 26.565f : 206.565f;
  267. break;
  268. case 2:
  269. pixelDir[i] = g45 > 0 ? 45 : 225;
  270. break;
  271. case 3:
  272. pixelDir[i] = g63 > 0 ? 63.435f : 243.435f;
  273. break;
  274. case 4:
  275. pixelDir[i] = g90 > 0 ? 90 : 270;
  276. break;
  277. case 5:
  278. pixelDir[i] = g116 > 0 ? 116.565f : 296.565f;
  279. break;
  280. case 6:
  281. pixelDir[i] = g135 > 0 ? 135 : 315;
  282. break;
  283. case 7:
  284. pixelDir[i] = g153 > 0 ? 153.435f : 333.435f;
  285. break;
  286. }
  287. //pixelC[i] = MathF.Abs(gX) + MathF.Abs(gY) + MathF.Abs(gXY) + MathF.Abs(gYX);
  288. /*
  289. if (pixelC[i] / 9f < 1.0f / 256f)
  290. {
  291. pixelC[i] = 0;
  292. return;
  293. }/**/
  294. /*
  295. if (sum < 100f/256f)
  296. {
  297. pixelC[i] = 0;
  298. return;
  299. }/**/
  300. //pixelC[i] = o0.Max(MathF.Abs(gX), MathF.Abs(gY), MathF.Abs(gXY), MathF.Abs(gYX));
  301. //pixelC[i] /= MathF.Pow(sum / 9, 0.1f);
  302. //pixelC[i] /= min + 0.5f;
  303. //pixelC[i] /= min + 0.1f;
  304. pixelC[i] /= sum / 25f + 1f;//+1是为了防止除数太小,结果不合理地过大
  305. });
  306. return (convolution.Map(ignoreExtremum: 10), dirMat);
  307. }
  308. // conSize 应 ≥ 5
  309. static public (Matrix, Matrix) zimIdentifyEdgeGradientAny(this Matrix mat, int conSize = 7)
  310. {
  311. var convolution = new Matrix(new Geometry2D.Vector<int>(mat.Size.x - (conSize - 1), mat.Size.y - (conSize - 1)), Tiling: true);
  312. var pixelC = convolution.Element;
  313. var dirMat = new Matrix(convolution.Size, Tiling: true);
  314. var pixelDir = dirMat.Element;
  315. var convolutionWidth = convolution.Size.x;
  316. var sampleCount = conSize + conSize - 6;
  317. (Vector a, Vector b)[] samples = new (Vector, Vector)[sampleCount];
  318. int[] angles = new int[sampleCount];
  319. float[] scales = new float[sampleCount];
  320. int ax = conSize - 2, ay = (conSize - 1) / 2, bx, by;
  321. for (int i = 0; i < samples.Length; i++)
  322. {
  323. bx = conSize - ax - 1;
  324. by = conSize - ay - 1;
  325. samples[i] = (new Vector(ax, ay), new Vector(bx, by));
  326. var dir = new Vector(ax - bx, ay - by);
  327. scales[i] = dir.Length;
  328. if (ax > bx)
  329. angles[i] = (int)(Math.Atan(dir.y / dir.x) * 180 / Math.PI);
  330. else if (ax < bx)
  331. angles[i] = 180 + (int)(Math.Atan(dir.y / dir.x) * 180 / Math.PI);
  332. else
  333. angles[i] = 90;
  334. if (ax == 1)
  335. ay--;
  336. else if (ay == conSize - 2)
  337. ax--;
  338. else
  339. ay++;
  340. }
  341. //Debug.Log(angles.ToJson() + "\r\n" + samples.ToJson() +"\r\n" + scales.ToJson());
  342. Parallel.For(0, convolution.Size.x * convolution.Size.y, i => {
  343. var iPx = i % convolutionWidth;
  344. var iPy = i / convolutionWidth;
  345. var avg = 0.0f;
  346. for (var jX = 0; jX < conSize; ++jX)
  347. for (var jY = 0; jY < conSize; ++jY)
  348. avg += mat.Element[(iPx + jX) + (iPy + jY) * mat.Size.x];
  349. if (avg == 0.0f)
  350. return;
  351. avg /= conSize * conSize;
  352. var gra = new float[sampleCount];
  353. //var gAvg = new Vector();
  354. for (int j = 0; j < samples.Length; j++)
  355. {
  356. var a = samples[j].a;
  357. var b = samples[j].b;
  358. float aAvg = 0, bAvg = 0;
  359. for (int m = -1; m <= 1; m++) // 取像素点周围9格平均值
  360. {
  361. for (int n = -1; n <= 1; n++)
  362. {
  363. aAvg += mat.Element[(iPx + (int)a.x + m) + (iPy + (int)a.y + n) * mat.Size.x];
  364. bAvg += mat.Element[(iPx + (int)b.x + m) + (iPy + (int)b.y + n) * mat.Size.x];
  365. }
  366. }
  367. aAvg /= 9;
  368. bAvg /= 9;
  369. gra[j] = (aAvg - bAvg) / scales[j];
  370. //gAvg += (a - b) * gra[j] / scales[j];
  371. }
  372. //gAvg /= samples.Length;
  373. int maxIndex = 0;
  374. float gMax = gra[0];
  375. for (int k = 1; k < gra.Length; k++)
  376. {
  377. var gAbs = Math.Abs(gra[k]);
  378. if (gMax < gAbs)
  379. {
  380. maxIndex = k;
  381. gMax = gAbs;
  382. }
  383. }
  384. pixelC[i] = gMax / (avg + 1f); // +1是防止结果过大
  385. pixelDir[i] = gra[maxIndex] > 0 ? angles[maxIndex] : angles[maxIndex] + 180;
  386. });
  387. return (convolution.Map(ignoreExtremum: 10), dirMat);
  388. }
  389. static public (Matrix, Matrix) zimIdentifyEdgeGradientGroup(this Matrix mat, int conSize = 5)
  390. {
  391. var conArea = conSize * conSize;
  392. var convolution = new Matrix(new Geometry2D.Vector<int>(mat.Size.x - (conSize - 1), mat.Size.y - (conSize - 1)), Tiling: true);
  393. var pixelC = convolution.Element;
  394. var dirMat = new Matrix(convolution.Size, Tiling: true);
  395. var pixelDir = dirMat.Element;
  396. var convolutionWidth = convolution.Size.x;
  397. Parallel.For(0, convolution.Size.x * convolution.Size.y, i => {
  398. var iPx = i % convolutionWidth;
  399. var iPy = i / convolutionWidth;
  400. var sum = 0.0f;
  401. for (var jX = 0; jX < conSize; ++jX)
  402. for (var jY = 0; jY < conSize; ++jY)
  403. sum += mat.Element[(iPx + jX) + (iPy + jY) * mat.Size.x];
  404. if (sum == 0.0f)
  405. return;
  406. var convList = new List<(float, Vector)>();
  407. for (var jX = 0; jX < conSize; ++jX)
  408. for (var jY = 0; jY < conSize; ++jY)
  409. convList.Add((mat.Element[(iPx + jX) + (iPy + jY) * mat.Size.x], new Vector(jX, jY))); ;
  410. convList.Sort((a, b) => a.Item1.CompareTo(b.Item1));
  411. var vMean0 = Vector.Zero; // 暗区
  412. var vMean1 = Vector.Zero; // 明区
  413. for (int k = 0; k < convList.Count / 2; k++)
  414. {
  415. vMean0 += convList[k].Item2;
  416. pixelC[i] -= convList[k].Item1;
  417. }
  418. vMean0 /= convList.Count / 2;
  419. for (int k = convList.Count / 2; k < convList.Count; k++)
  420. {
  421. vMean1 += convList[k].Item2;
  422. pixelC[i] += convList[k].Item1;
  423. }
  424. vMean1 /= convList.Count - convList.Count / 2;
  425. pixelC[i] /= sum / convList.Count + 1f;
  426. var dir = vMean1 - vMean0;
  427. //pixelDir[i] = new Vector(dir.y, -dir.x);
  428. pixelDir[i] = dir.y > 0 ? (float)Math.Atan((double)(dir.y / dir.x)) : -(float)Math.Atan((double)(dir.y / dir.x));
  429. pixelDir[i] *= 180 / (float)Math.PI;
  430. });
  431. return (convolution.Map(ignoreExtremum: 10), dirMat);
  432. }
  433. }
  434. }