zimIdentifyEdge.cs 26 KB

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