OLDcellsegm.cs 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using OpenCvSharp;
  9. using Point = System.Drawing.Point;
  10. using Size = OpenCvSharp.Size;
  11. namespace PaintDotNet.DedicatedAnalysis.GrainSizeStandard.IntegrationClass
  12. {
  13. internal class InParameter
  14. {
  15. public int iMethod;
  16. public Mat MGryImg;
  17. public Mat MHesOutImage;
  18. public Mat MValley;
  19. public int iDimA;
  20. public int iDimB;
  21. public int iXiHua;
  22. public int iLianJie;
  23. public Mat MEdgeOutImage;
  24. }
  25. public class NewSeg
  26. {
  27. private Mat m_MOrgImg;
  28. private Mat m_MOutImg;
  29. private double[] m_adOutParam;
  30. private InParameter m_InParam;
  31. public int DoSeg(Mat _MOrgImg, Mat _MOutImg, double[] _adOutParam, Vec4b value)
  32. {
  33. m_MOrgImg = _MOrgImg;
  34. m_MOutImg = _MOutImg;
  35. m_adOutParam = _adOutParam;
  36. PrepareParameter();
  37. if (2 == m_InParam.iMethod)
  38. {
  39. DetectEdge(value);
  40. return 2;
  41. }
  42. ImgHessian();
  43. FindValley();
  44. FindShed(value);
  45. return 1;
  46. }
  47. private int DetectEdge(Vec4b value)
  48. {
  49. Mat mat = new Mat();
  50. Cv2.Resize(m_InParam.MGryImg, mat, new Size((double)m_InParam.MGryImg.Cols * 0.2, (double)m_InParam.MGryImg.Rows * 0.2));
  51. Cv2.Sobel(mat, m_InParam.MEdgeOutImage, -1, 1, 1);
  52. Cv2.Threshold(m_InParam.MEdgeOutImage, m_InParam.MEdgeOutImage, 20.0, 255.0, ThresholdTypes.Binary);
  53. Mat mat2 = new Mat();
  54. Cv2.Canny(mat, mat2, 50.0, 50.0);
  55. m_InParam.MEdgeOutImage = (Mat)(m_InParam.MEdgeOutImage | mat2);
  56. Mat structuringElement = Cv2.GetStructuringElement(MorphShapes.Ellipse, new Size(7, 7), new OpenCvSharp.Point(-1, -1));
  57. Cv2.MorphologyEx(m_InParam.MEdgeOutImage, m_InParam.MEdgeOutImage, MorphTypes.Close, (InputArray)structuringElement);
  58. Mat mat3 = new Mat();
  59. Cv2.Threshold(mat, mat3, 150.0, 255.0, ThresholdTypes.BinaryInv);
  60. m_InParam.MEdgeOutImage = (Mat)(m_InParam.MEdgeOutImage & mat3);
  61. ConnectionBoundary(m_InParam.MEdgeOutImage, 108);
  62. Mat mat4 = new Mat();
  63. Cv2.BitwiseNot(m_InParam.MEdgeOutImage, mat4);
  64. structuringElement = Cv2.GetStructuringElement(MorphShapes.Cross, new Size(7, 7), new OpenCvSharp.Point(-1, -1));
  65. Cv2.MorphologyEx(mat4, mat4, MorphTypes.Erode, (InputArray)structuringElement);
  66. Mat mat5 = new Mat();
  67. Cv2.Resize(m_MOrgImg, mat5, new Size(mat4.Cols, mat4.Rows));
  68. Mat mat6 = new Mat(mat4.Size(), 4);
  69. Cv2.FindContours(mat4, out OpenCvSharp.Point[][] contours, out HierarchyIndex[] hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxSimple);
  70. for (int i = 0; i < hierarchy.Length; i++)
  71. {
  72. Cv2.DrawContours(mat6, contours, i, Scalar.All(i + 1), 1, LineTypes.Link8, hierarchy);
  73. }
  74. Cv2.Watershed(mat5, mat6);
  75. Mat mat7 = new Mat(mat5.Rows, mat5.Cols, MatType.CV_8UC1, Scalar.All(0.0));
  76. int rows = mat6.Rows;
  77. int cols = mat6.Cols;
  78. int[] array = new int[2]
  79. {
  80. 0,
  81. 0
  82. };
  83. while (array[0] < rows)
  84. {
  85. array[1] = 0;
  86. while (array[1] < cols)
  87. {
  88. if (0 >= mat6.At<int>(array[0], array[1]))
  89. {
  90. mat7.Set(array, byte.MaxValue);
  91. }
  92. array[1]++;
  93. }
  94. array[0]++;
  95. }
  96. Cv2.Resize(mat7, mat7, new Size(m_InParam.MGryImg.Cols, m_InParam.MGryImg.Rows));
  97. Cv2.Resize(mat7, mat7, new Size(m_InParam.MGryImg.Cols, m_InParam.MGryImg.Rows));
  98. //Vec3b value = new Vec3b(0, 0, byte.MaxValue);
  99. rows = m_MOutImg.Rows;
  100. cols = m_MOutImg.Cols;
  101. array[0] = 0;
  102. while (array[0] < rows)
  103. {
  104. array[1] = 0;
  105. while (array[1] < cols)
  106. {
  107. if (120 < mat7.At<byte>(array[0], array[1]))
  108. {
  109. m_MOutImg.Set(array, value);
  110. }
  111. array[1]++;
  112. }
  113. array[0]++;
  114. }
  115. return 1;
  116. }
  117. public int LTQY()
  118. {
  119. Mat mat = new Mat();
  120. Mat mat2 = new Mat();
  121. Mat mat3 = new Mat();
  122. Mat mEdgeOutImage = m_InParam.MEdgeOutImage;
  123. int num = Cv2.ConnectedComponentsWithStats(mEdgeOutImage, mat, mat2, mat3);
  124. int[] array = new int[num];
  125. for (int i = 1; i < num; i++)
  126. {
  127. array[i] = 255;
  128. if (mat2.At<int>(i, 4) < 20)
  129. {
  130. array[i] = 0;
  131. }
  132. }
  133. Mat mat4 = new Mat(mEdgeOutImage.Rows, mEdgeOutImage.Cols, MatType.CV_8UC1, Scalar.All(0.0));
  134. for (int j = 0; j < mat4.Rows; j++)
  135. {
  136. for (int k = 0; k < mEdgeOutImage.Cols; k++)
  137. {
  138. int num2 = mat.At<int>(j, k);
  139. mat4.Set(j, k, (byte)array[num2]);
  140. }
  141. }
  142. return 0;
  143. }
  144. public void ImgThin(Mat src, int maxIterations = -1)
  145. {
  146. Cv2.Threshold(src, src, 100.0, 1.0, ThresholdTypes.Binary);
  147. int rows = src.Rows;
  148. int cols = src.Cols;
  149. int num = 0;
  150. while (true)
  151. {
  152. num++;
  153. if (maxIterations != -1 && num > maxIterations)
  154. {
  155. break;
  156. }
  157. List<int[]> list = new List<int[]>();
  158. for (int i = 0; i < rows; i++)
  159. {
  160. for (int j = 0; j < cols; j++)
  161. {
  162. src.At<byte>(i, j);
  163. int num2 = (i != 0) ? src.At<byte>(i - 1, j) : 0;
  164. int num3 = (i != 0 && j != cols - 1) ? src.At<byte>(i - 1, j + 1) : 0;
  165. int num4 = (j != cols - 1) ? src.At<byte>(i, j + 1) : 0;
  166. int num5 = (i != rows - 1 && j != cols - 1) ? src.At<byte>(i + 1, j + 1) : 0;
  167. int num6 = (i != rows - 1) ? src.At<byte>(i + 1, j) : 0;
  168. int num7 = (i != rows - 1 && j != 0) ? src.At<byte>(i + 1, j - 1) : 0;
  169. int num8 = (j != 0) ? src.At<byte>(i, j - 1) : 0;
  170. int num9 = (i != 0 && j != 0) ? src.At<byte>(i - 1, j - 1) : 0;
  171. if (num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 >= 2 && num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 <= 6)
  172. {
  173. int num10 = 0;
  174. if (num2 == 0 && num3 == 1)
  175. {
  176. num10++;
  177. }
  178. if (num3 == 0 && num4 == 1)
  179. {
  180. num10++;
  181. }
  182. if (num4 == 0 && num5 == 1)
  183. {
  184. num10++;
  185. }
  186. if (num5 == 0 && num6 == 1)
  187. {
  188. num10++;
  189. }
  190. if (num6 == 0 && num7 == 1)
  191. {
  192. num10++;
  193. }
  194. if (num7 == 0 && num8 == 1)
  195. {
  196. num10++;
  197. }
  198. if (num8 == 0 && num9 == 1)
  199. {
  200. num10++;
  201. }
  202. if (num9 == 0 && num2 == 1)
  203. {
  204. num10++;
  205. }
  206. if (num10 == 1 && num2 * num4 * num6 == 0 && num4 * num6 * num8 == 0)
  207. {
  208. list.Add(new int[2]
  209. {
  210. i,
  211. j
  212. });
  213. }
  214. }
  215. }
  216. }
  217. list.ForEach(delegate (int[] aiP)
  218. {
  219. src.Set(aiP, (byte)0);
  220. });
  221. if (list.Count() == 0)
  222. {
  223. break;
  224. }
  225. list.Clear();
  226. for (int k = 0; k < rows; k++)
  227. {
  228. for (int l = 0; l < cols; l++)
  229. {
  230. int num11 = src.At<byte>(k, l);
  231. if (num11 != 1)
  232. {
  233. continue;
  234. }
  235. int num12 = (k != 0) ? src.At<byte>(k - 1, l) : 0;
  236. int num13 = (k != 0 && l != cols - 1) ? src.At<byte>(k - 1, l + 1) : 0;
  237. int num14 = (l != cols - 1) ? src.At<byte>(k, l + 1) : 0;
  238. int num15 = (k != rows - 1 && l != cols - 1) ? src.At<byte>(k + 1, l + 1) : 0;
  239. int num16 = (k != rows - 1) ? src.At<byte>(k + 1, l) : 0;
  240. int num17 = (k != rows - 1 && l != 0) ? src.At<byte>(k + 1, l - 1) : 0;
  241. int num18 = (l != 0) ? src.At<byte>(k, l - 1) : 0;
  242. int num19 = (k != 0 && l != 0) ? src.At<byte>(k - 1, l - 1) : 0;
  243. if (num12 + num13 + num14 + num15 + num16 + num17 + num18 + num19 >= 2 && num12 + num13 + num14 + num15 + num16 + num17 + num18 + num19 <= 6)
  244. {
  245. int num20 = 0;
  246. if (num12 == 0 && num13 == 1)
  247. {
  248. num20++;
  249. }
  250. if (num13 == 0 && num14 == 1)
  251. {
  252. num20++;
  253. }
  254. if (num14 == 0 && num15 == 1)
  255. {
  256. num20++;
  257. }
  258. if (num15 == 0 && num16 == 1)
  259. {
  260. num20++;
  261. }
  262. if (num16 == 0 && num17 == 1)
  263. {
  264. num20++;
  265. }
  266. if (num17 == 0 && num18 == 1)
  267. {
  268. num20++;
  269. }
  270. if (num18 == 0 && num19 == 1)
  271. {
  272. num20++;
  273. }
  274. if (num19 == 0 && num12 == 1)
  275. {
  276. num20++;
  277. }
  278. if (num20 == 1 && num12 * num14 * num18 == 0 && num12 * num16 * num18 == 0)
  279. {
  280. list.Add(new int[2]
  281. {
  282. k,
  283. l
  284. });
  285. }
  286. }
  287. }
  288. }
  289. list.ForEach(delegate (int[] aiP)
  290. {
  291. src.Set(aiP, (byte)0);
  292. });
  293. if (list.Count() == 0)
  294. {
  295. break;
  296. }
  297. list.Clear();
  298. }
  299. Cv2.Threshold(src, src, 0.0, 255.0, ThresholdTypes.Binary);
  300. }
  301. public void ConnectionBoundary(Mat Img, int iDistance)
  302. {
  303. Cv2.Threshold(Img, Img, 100.0, 1.0, ThresholdTypes.Binary);
  304. Mat mat = Connectivity(Img);
  305. List<int[]> list = new List<int[]>();
  306. List<int[]> list2 = new List<int[]>();
  307. int[] array = new int[2]
  308. {
  309. 1,
  310. 0
  311. };
  312. while (array[0] < Img.Rows - 1)
  313. {
  314. array[1] = 1;
  315. while (array[1] < Img.Cols - 1)
  316. {
  317. if (1 <= Img.At<byte>(array[0], array[1]) && mat.At<byte>(array[0], array[1]) == 0)
  318. {
  319. Img.Set(array, (byte)0);
  320. }
  321. if (1 <= Img.At<byte>(array[0], array[1]) && 1 == mat.At<byte>(array[0], array[1]))
  322. {
  323. list.Add(new int[2]
  324. {
  325. array[0],
  326. array[1]
  327. });
  328. int num = 0;
  329. int i = 0;
  330. for (num = -1; num <= 1; num++)
  331. {
  332. bool flag = false;
  333. for (i = -1; i <= 1; i++)
  334. {
  335. if ((num != 0 || i != 0) && Img.At<byte>(array[0] + num, array[1] + i) >= 1)
  336. {
  337. flag = true;
  338. break;
  339. }
  340. }
  341. if (flag)
  342. {
  343. break;
  344. }
  345. }
  346. list2.Add(new int[2]
  347. {
  348. -num,
  349. -i
  350. });
  351. }
  352. array[1]++;
  353. }
  354. array[0]++;
  355. }
  356. int num2 = list.Count();
  357. List<int[]>[] array2 = new List<int[]>[num2];
  358. List<int> list3 = new List<int>();
  359. List<int> list4 = new List<int>();
  360. for (int j = 0; j < num2; j++)
  361. {
  362. list3.Add(j);
  363. int[] array3 = list[j];
  364. array2[j] = new List<int[]>();
  365. array2[j].Add(new int[2]
  366. {
  367. array3[0],
  368. array3[1]
  369. });
  370. }
  371. Mat mat2 = new Mat(Img.Size(), MatType.CV_32SC1);
  372. array[0] = 0;
  373. while (array[0] < Img.Rows)
  374. {
  375. array[1] = 0;
  376. while (array[1] < Img.Cols)
  377. {
  378. int value = Img.At<byte>(array[0], array[1]);
  379. mat2.Set(array, value);
  380. array[1]++;
  381. }
  382. array[0]++;
  383. }
  384. int[,] array4 = new int[260, 2]
  385. {
  386. {
  387. -1,
  388. 0
  389. },
  390. {
  391. 0,
  392. 1
  393. },
  394. {
  395. 1,
  396. 0
  397. },
  398. {
  399. 0,
  400. 1
  401. },
  402. {
  403. -1,
  404. 1
  405. },
  406. {
  407. 1,
  408. 1
  409. },
  410. {
  411. 1,
  412. -1
  413. },
  414. {
  415. -1,
  416. -1
  417. },
  418. {
  419. -2,
  420. 0
  421. },
  422. {
  423. 0,
  424. 2
  425. },
  426. {
  427. 2,
  428. 0
  429. },
  430. {
  431. 0,
  432. -2
  433. },
  434. {
  435. -2,
  436. 1
  437. },
  438. {
  439. -1,
  440. 2
  441. },
  442. {
  443. 1,
  444. 2
  445. },
  446. {
  447. 2,
  448. 1
  449. },
  450. {
  451. 2,
  452. -1
  453. },
  454. {
  455. 1,
  456. -2
  457. },
  458. {
  459. -1,
  460. -2
  461. },
  462. {
  463. -2,
  464. -1
  465. },
  466. {
  467. -2,
  468. 2
  469. },
  470. {
  471. 2,
  472. 2
  473. },
  474. {
  475. 2,
  476. -2
  477. },
  478. {
  479. -2,
  480. -2
  481. },
  482. {
  483. -3,
  484. 0
  485. },
  486. {
  487. 0,
  488. 3
  489. },
  490. {
  491. 3,
  492. 0
  493. },
  494. {
  495. 0,
  496. -3
  497. },
  498. {
  499. -3,
  500. 1
  501. },
  502. {
  503. -1,
  504. 3
  505. },
  506. {
  507. 1,
  508. 3
  509. },
  510. {
  511. 3,
  512. 1
  513. },
  514. {
  515. 3,
  516. -1
  517. },
  518. {
  519. 1,
  520. -3
  521. },
  522. {
  523. -1,
  524. -3
  525. },
  526. {
  527. -3,
  528. -1
  529. },
  530. {
  531. 2,
  532. 3
  533. },
  534. {
  535. -3,
  536. 2
  537. },
  538. {
  539. -2,
  540. -3
  541. },
  542. {
  543. -2,
  544. 3
  545. },
  546. {
  547. -3,
  548. -2
  549. },
  550. {
  551. 3,
  552. 2
  553. },
  554. {
  555. 3,
  556. -2
  557. },
  558. {
  559. 2,
  560. -3
  561. },
  562. {
  563. 4,
  564. 0
  565. },
  566. {
  567. 0,
  568. 4
  569. },
  570. {
  571. -4,
  572. 0
  573. },
  574. {
  575. 0,
  576. -4
  577. },
  578. {
  579. 1,
  580. 4
  581. },
  582. {
  583. 4,
  584. -1
  585. },
  586. {
  587. 4,
  588. 1
  589. },
  590. {
  591. -1,
  592. -4
  593. },
  594. {
  595. -4,
  596. 1
  597. },
  598. {
  599. -4,
  600. -1
  601. },
  602. {
  603. -1,
  604. 4
  605. },
  606. {
  607. 1,
  608. -4
  609. },
  610. {
  611. 3,
  612. 3
  613. },
  614. {
  615. 3,
  616. -3
  617. },
  618. {
  619. -3,
  620. 3
  621. },
  622. {
  623. -3,
  624. -3
  625. },
  626. {
  627. -4,
  628. -2
  629. },
  630. {
  631. -2,
  632. -4
  633. },
  634. {
  635. -4,
  636. 2
  637. },
  638. {
  639. 4,
  640. 2
  641. },
  642. {
  643. -2,
  644. 4
  645. },
  646. {
  647. 4,
  648. -2
  649. },
  650. {
  651. 2,
  652. -4
  653. },
  654. {
  655. 2,
  656. 4
  657. },
  658. {
  659. 4,
  660. -3
  661. },
  662. {
  663. -3,
  664. 4
  665. },
  666. {
  667. 4,
  668. 3
  669. },
  670. {
  671. 5,
  672. 0
  673. },
  674. {
  675. 0,
  676. 5
  677. },
  678. {
  679. 0,
  680. -5
  681. },
  682. {
  683. -5,
  684. 0
  685. },
  686. {
  687. 3,
  688. 4
  689. },
  690. {
  691. -3,
  692. -4
  693. },
  694. {
  695. -4,
  696. -3
  697. },
  698. {
  699. 3,
  700. -4
  701. },
  702. {
  703. -4,
  704. 3
  705. },
  706. {
  707. 5,
  708. -1
  709. },
  710. {
  711. -1,
  712. 5
  713. },
  714. {
  715. -1,
  716. -5
  717. },
  718. {
  719. 5,
  720. 1
  721. },
  722. {
  723. 1,
  724. -5
  725. },
  726. {
  727. -5,
  728. 1
  729. },
  730. {
  731. -5,
  732. -1
  733. },
  734. {
  735. 1,
  736. 5
  737. },
  738. {
  739. -2,
  740. 5
  741. },
  742. {
  743. 2,
  744. -5
  745. },
  746. {
  747. 2,
  748. 5
  749. },
  750. {
  751. 5,
  752. 2
  753. },
  754. {
  755. -5,
  756. 2
  757. },
  758. {
  759. -2,
  760. -5
  761. },
  762. {
  763. 5,
  764. -2
  765. },
  766. {
  767. -5,
  768. -2
  769. },
  770. {
  771. -4,
  772. 4
  773. },
  774. {
  775. 4,
  776. -4
  777. },
  778. {
  779. 4,
  780. 4
  781. },
  782. {
  783. -4,
  784. -4
  785. },
  786. {
  787. 3,
  788. -5
  789. },
  790. {
  791. -5,
  792. 3
  793. },
  794. {
  795. -3,
  796. 5
  797. },
  798. {
  799. 5,
  800. -3
  801. },
  802. {
  803. 5,
  804. 3
  805. },
  806. {
  807. -5,
  808. -3
  809. },
  810. {
  811. -3,
  812. -5
  813. },
  814. {
  815. 3,
  816. 5
  817. },
  818. {
  819. 0,
  820. 6
  821. },
  822. {
  823. 0,
  824. -6
  825. },
  826. {
  827. -6,
  828. 0
  829. },
  830. {
  831. 6,
  832. 0
  833. },
  834. {
  835. -6,
  836. 1
  837. },
  838. {
  839. 1,
  840. 6
  841. },
  842. {
  843. -6,
  844. -1
  845. },
  846. {
  847. 1,
  848. -6
  849. },
  850. {
  851. -1,
  852. 6
  853. },
  854. {
  855. -1,
  856. -6
  857. },
  858. {
  859. 6,
  860. -1
  861. },
  862. {
  863. 6,
  864. 1
  865. },
  866. {
  867. 2,
  868. 6
  869. },
  870. {
  871. -2,
  872. -6
  873. },
  874. {
  875. 6,
  876. 2
  877. },
  878. {
  879. -6,
  880. 2
  881. },
  882. {
  883. 2,
  884. -6
  885. },
  886. {
  887. -6,
  888. -2
  889. },
  890. {
  891. -2,
  892. 6
  893. },
  894. {
  895. 6,
  896. -2
  897. },
  898. {
  899. -5,
  900. -4
  901. },
  902. {
  903. 5,
  904. 4
  905. },
  906. {
  907. 4,
  908. 5
  909. },
  910. {
  911. -4,
  912. 5
  913. },
  914. {
  915. 4,
  916. -5
  917. },
  918. {
  919. -5,
  920. 4
  921. },
  922. {
  923. 5,
  924. -4
  925. },
  926. {
  927. -4,
  928. -5
  929. },
  930. {
  931. -3,
  932. 6
  933. },
  934. {
  935. -6,
  936. 3
  937. },
  938. {
  939. -3,
  940. -6
  941. },
  942. {
  943. -6,
  944. -3
  945. },
  946. {
  947. 3,
  948. -6
  949. },
  950. {
  951. 6,
  952. 3
  953. },
  954. {
  955. 3,
  956. 6
  957. },
  958. {
  959. 6,
  960. -3
  961. },
  962. {
  963. 0,
  964. 7
  965. },
  966. {
  967. 7,
  968. 0
  969. },
  970. {
  971. 0,
  972. -7
  973. },
  974. {
  975. -7,
  976. 0
  977. },
  978. {
  979. 5,
  980. -5
  981. },
  982. {
  983. 1,
  984. 7
  985. },
  986. {
  987. -1,
  988. 7
  989. },
  990. {
  991. -5,
  992. -5
  993. },
  994. {
  995. -7,
  996. -1
  997. },
  998. {
  999. -7,
  1000. 1
  1001. },
  1002. {
  1003. -1,
  1004. -7
  1005. },
  1006. {
  1007. 1,
  1008. -7
  1009. },
  1010. {
  1011. 7,
  1012. 1
  1013. },
  1014. {
  1015. -5,
  1016. 5
  1017. },
  1018. {
  1019. 5,
  1020. 5
  1021. },
  1022. {
  1023. 7,
  1024. -1
  1025. },
  1026. {
  1027. 6,
  1028. 4
  1029. },
  1030. {
  1031. -6,
  1032. 4
  1033. },
  1034. {
  1035. 6,
  1036. -4
  1037. },
  1038. {
  1039. -6,
  1040. -4
  1041. },
  1042. {
  1043. -4,
  1044. -6
  1045. },
  1046. {
  1047. 4,
  1048. 6
  1049. },
  1050. {
  1051. 4,
  1052. -6
  1053. },
  1054. {
  1055. -4,
  1056. 6
  1057. },
  1058. {
  1059. 7,
  1060. -2
  1061. },
  1062. {
  1063. 7,
  1064. 2
  1065. },
  1066. {
  1067. 2,
  1068. 7
  1069. },
  1070. {
  1071. -7,
  1072. -2
  1073. },
  1074. {
  1075. -2,
  1076. -7
  1077. },
  1078. {
  1079. -2,
  1080. 7
  1081. },
  1082. {
  1083. 2,
  1084. -7
  1085. },
  1086. {
  1087. -7,
  1088. 2
  1089. },
  1090. {
  1091. 7,
  1092. -3
  1093. },
  1094. {
  1095. -3,
  1096. 7
  1097. },
  1098. {
  1099. -3,
  1100. -7
  1101. },
  1102. {
  1103. 3,
  1104. -7
  1105. },
  1106. {
  1107. -7,
  1108. 3
  1109. },
  1110. {
  1111. 3,
  1112. 7
  1113. },
  1114. {
  1115. 7,
  1116. 3
  1117. },
  1118. {
  1119. -7,
  1120. -3
  1121. },
  1122. {
  1123. 6,
  1124. -5
  1125. },
  1126. {
  1127. 5,
  1128. 6
  1129. },
  1130. {
  1131. 5,
  1132. -6
  1133. },
  1134. {
  1135. 6,
  1136. 5
  1137. },
  1138. {
  1139. -5,
  1140. 6
  1141. },
  1142. {
  1143. -5,
  1144. -6
  1145. },
  1146. {
  1147. -6,
  1148. 5
  1149. },
  1150. {
  1151. -6,
  1152. -5
  1153. },
  1154. {
  1155. 8,
  1156. 0
  1157. },
  1158. {
  1159. 0,
  1160. -8
  1161. },
  1162. {
  1163. -8,
  1164. 0
  1165. },
  1166. {
  1167. 0,
  1168. 8
  1169. },
  1170. {
  1171. -4,
  1172. 7
  1173. },
  1174. {
  1175. 4,
  1176. -7
  1177. },
  1178. {
  1179. -8,
  1180. -1
  1181. },
  1182. {
  1183. -1,
  1184. 8
  1185. },
  1186. {
  1187. -7,
  1188. 4
  1189. },
  1190. {
  1191. -1,
  1192. -8
  1193. },
  1194. {
  1195. 7,
  1196. 4
  1197. },
  1198. {
  1199. 1,
  1200. -8
  1201. },
  1202. {
  1203. 1,
  1204. 8
  1205. },
  1206. {
  1207. 8,
  1208. 1
  1209. },
  1210. {
  1211. 8,
  1212. -1
  1213. },
  1214. {
  1215. -4,
  1216. -7
  1217. },
  1218. {
  1219. -7,
  1220. -4
  1221. },
  1222. {
  1223. 7,
  1224. -4
  1225. },
  1226. {
  1227. -8,
  1228. 1
  1229. },
  1230. {
  1231. 4,
  1232. 7
  1233. },
  1234. {
  1235. -2,
  1236. 8
  1237. },
  1238. {
  1239. 2,
  1240. -8
  1241. },
  1242. {
  1243. -2,
  1244. -8
  1245. },
  1246. {
  1247. 8,
  1248. 2
  1249. },
  1250. {
  1251. -8,
  1252. 2
  1253. },
  1254. {
  1255. -8,
  1256. -2
  1257. },
  1258. {
  1259. 2,
  1260. 8
  1261. },
  1262. {
  1263. 8,
  1264. -2
  1265. },
  1266. {
  1267. -6,
  1268. 6
  1269. },
  1270. {
  1271. 6,
  1272. 6
  1273. },
  1274. {
  1275. 6,
  1276. -6
  1277. },
  1278. {
  1279. -6,
  1280. -6
  1281. },
  1282. {
  1283. 3,
  1284. -8
  1285. },
  1286. {
  1287. -3,
  1288. 8
  1289. },
  1290. {
  1291. 8,
  1292. 3
  1293. },
  1294. {
  1295. 8,
  1296. -3
  1297. },
  1298. {
  1299. 3,
  1300. 8
  1301. },
  1302. {
  1303. -3,
  1304. -8
  1305. },
  1306. {
  1307. -8,
  1308. 3
  1309. },
  1310. {
  1311. -8,
  1312. -3
  1313. },
  1314. {
  1315. 7,
  1316. -5
  1317. },
  1318. {
  1319. -7,
  1320. -5
  1321. },
  1322. {
  1323. 5,
  1324. 7
  1325. },
  1326. {
  1327. 7,
  1328. 5
  1329. },
  1330. {
  1331. 5,
  1332. -7
  1333. },
  1334. {
  1335. -7,
  1336. 5
  1337. },
  1338. {
  1339. -5,
  1340. -7
  1341. },
  1342. {
  1343. -5,
  1344. 7
  1345. },
  1346. {
  1347. 8,
  1348. -4
  1349. },
  1350. {
  1351. -4,
  1352. -8
  1353. },
  1354. {
  1355. -8,
  1356. -4
  1357. },
  1358. {
  1359. 8,
  1360. 4
  1361. },
  1362. {
  1363. 4,
  1364. 8
  1365. },
  1366. {
  1367. -8,
  1368. 4
  1369. },
  1370. {
  1371. 4,
  1372. -8
  1373. },
  1374. {
  1375. -4,
  1376. 8
  1377. },
  1378. {
  1379. 0,
  1380. -9
  1381. },
  1382. {
  1383. 0,
  1384. 9
  1385. },
  1386. {
  1387. -9,
  1388. 0
  1389. },
  1390. {
  1391. 9,
  1392. 0
  1393. },
  1394. {
  1395. 9,
  1396. -1
  1397. },
  1398. {
  1399. -9,
  1400. -1
  1401. },
  1402. {
  1403. 1,
  1404. -9
  1405. },
  1406. {
  1407. -1,
  1408. 9
  1409. },
  1410. {
  1411. -1,
  1412. -9
  1413. },
  1414. {
  1415. -9,
  1416. 1
  1417. },
  1418. {
  1419. 1,
  1420. 9
  1421. },
  1422. {
  1423. 9,
  1424. 1
  1425. }
  1426. };
  1427. while (list3.Count() != 0)
  1428. {
  1429. int num3 = list3[0];
  1430. list3.RemoveAt(0);
  1431. int index = array2[num3].Count() - 1;
  1432. int[] array5 = array2[num3][index];
  1433. int[] pnt = new int[2]
  1434. {
  1435. array5[0],
  1436. array5[1]
  1437. };
  1438. int[] array6 = list2[num3];
  1439. bool flag2 = false;
  1440. for (int k = 0; k < iDistance; k++)
  1441. {
  1442. if (0 > array6[0] * array4[k, 0] || 0 > array6[1] * array4[k, 1])
  1443. {
  1444. continue;
  1445. }
  1446. pnt[0] = array5[0] + array4[k, 0];
  1447. pnt[1] = array5[1] + array4[k, 1];
  1448. if (0 > pnt[0] || Img.Rows <= pnt[0] || 0 > pnt[1] || Img.Cols <= pnt[1])
  1449. {
  1450. continue;
  1451. }
  1452. int num4 = mat2.At<int>(pnt[0], pnt[1]);
  1453. if (1 < num4 && num3 != num4)
  1454. {
  1455. list3.Remove(num4);
  1456. if (!list4.Contains(num4))
  1457. {
  1458. int num5 = array2[num4].FindIndex((int[] t) => t[0] == pnt[0] && t[1] == pnt[1]);
  1459. int num6 = array2[num4].Count() - 1;
  1460. if (1 <= num6 - num5)
  1461. {
  1462. for (int l = num5 + 1; l <= num6; l++)
  1463. {
  1464. mat2.Set(array2[num4][l], 0);
  1465. }
  1466. array2[num4].RemoveRange(num5 + 1, num6 - num5);
  1467. }
  1468. list4.Add(num4);
  1469. list4.Add(num3);
  1470. }
  1471. ConnectTwoPnt(array5, pnt, array2[num3]);
  1472. flag2 = true;
  1473. break;
  1474. }
  1475. if (1 == num4)
  1476. {
  1477. ConnectTwoPnt(array5, pnt, array2[num3]);
  1478. flag2 = true;
  1479. break;
  1480. }
  1481. }
  1482. if (!flag2)
  1483. {
  1484. pnt[0] = array5[0] + array6[0];
  1485. pnt[1] = array5[1] + array6[1];
  1486. if (0 <= pnt[0] && Img.Rows > pnt[0] && 0 <= pnt[1] && Img.Cols > pnt[1])
  1487. {
  1488. array2[num3].Add(pnt);
  1489. mat2.Set(pnt, num3);
  1490. list3.Add(num3);
  1491. }
  1492. }
  1493. }
  1494. for (int m = 0; m < num2; m++)
  1495. {
  1496. array2[m].ForEach(delegate (int[] aiP)
  1497. {
  1498. Img.Set(aiP, (byte)1);
  1499. });
  1500. }
  1501. Cv2.Threshold(Img, Img, 0.0, 255.0, ThresholdTypes.Binary);
  1502. }
  1503. public void ConnectTwoPnt(int[] pntA, int[] pntB, List<int[]> DevelopmentA)
  1504. {
  1505. int num = pntA[0];
  1506. int num2 = pntA[1];
  1507. int num3 = pntB[0] - num;
  1508. int num4 = pntB[1] - num2;
  1509. int num5 = (num3 != 0) ? (num3 / Math.Abs(num3)) : 0;
  1510. int num6 = (num4 != 0) ? (num4 / Math.Abs(num4)) : 0;
  1511. while (num3 != 0 || num4 != 0)
  1512. {
  1513. if (Math.Abs(num3) > Math.Abs(num4))
  1514. {
  1515. num += num5;
  1516. }
  1517. else
  1518. {
  1519. num2 += num6;
  1520. }
  1521. DevelopmentA.Add(new int[2]
  1522. {
  1523. num,
  1524. num2
  1525. });
  1526. num3 = pntB[0] - num;
  1527. num4 = pntB[1] - num2;
  1528. }
  1529. }
  1530. public Mat Connectivity(Mat Img)
  1531. {
  1532. Mat mat = new Mat(Img.Size(), MatType.CV_8UC1);
  1533. int[] array = new int[2]
  1534. {
  1535. 1,
  1536. 0
  1537. };
  1538. while (array[0] < Img.Rows - 1)
  1539. {
  1540. array[1] = 0;
  1541. while (array[1] < Img.Cols)
  1542. {
  1543. byte b = Img.At<byte>(array[0] - 1, array[1]);
  1544. b = (byte)(b + Img.At<byte>(array[0], array[1]));
  1545. b = (byte)(b + Img.At<byte>(array[0] + 1, array[1]));
  1546. mat.Set(array, b);
  1547. array[1]++;
  1548. }
  1549. array[0]++;
  1550. }
  1551. Mat mat2 = new Mat(Img.Size(), MatType.CV_8UC1);
  1552. array[0] = 1;
  1553. while (array[0] < Img.Rows - 1)
  1554. {
  1555. array[1] = 1;
  1556. while (array[1] < Img.Cols - 1)
  1557. {
  1558. byte b2 = mat.At<byte>(array[0], array[1] - 1);
  1559. b2 = (byte)(b2 + mat.At<byte>(array[0], array[1]));
  1560. b2 = (byte)(b2 + mat.At<byte>(array[0], array[1] + 1));
  1561. b2 = (byte)(b2 - Img.At<byte>(array[0], array[1]));
  1562. mat2.Set(array, b2);
  1563. array[1]++;
  1564. }
  1565. array[0]++;
  1566. }
  1567. mat = null;
  1568. return mat2;
  1569. }
  1570. private int FindShed(Vec4b value)
  1571. {
  1572. Mat mat = new Mat(m_InParam.MValley.Size(), 4);
  1573. Cv2.FindContours(m_InParam.MValley, out OpenCvSharp.Point[][] contours, out HierarchyIndex[] hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxSimple);
  1574. for (int i = 0; i < hierarchy.Length; i++)//# 相同的条件,这里比demo慢?
  1575. {
  1576. Cv2.DrawContours(mat, contours, i, Scalar.All(i + 1), 1, LineTypes.Link8, hierarchy);
  1577. }
  1578. Cv2.Watershed(m_MOrgImg, mat);
  1579. int rows = mat.Rows;
  1580. int cols = mat.Cols;
  1581. int[] array = new int[2];
  1582. //Vec3b value = new Vec3b(0, 0, byte.MaxValue);
  1583. array[0] = 0;
  1584. while (array[0] < rows)
  1585. {
  1586. array[1] = 0;
  1587. while (array[1] < cols)
  1588. {
  1589. if (0 >= mat.At<int>(array[0], array[1]))
  1590. {
  1591. m_MOutImg.Set(array, value);
  1592. }
  1593. array[1]++;
  1594. }
  1595. array[0]++;
  1596. }
  1597. return 1;
  1598. }
  1599. private int FindValley()
  1600. {
  1601. Mat structuringElement = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(m_InParam.iDimA, m_InParam.iDimA), new OpenCvSharp.Point(-1, -1));
  1602. Cv2.MorphologyEx(m_InParam.MHesOutImage, m_InParam.MHesOutImage, MorphTypes.Close, (InputArray)structuringElement);
  1603. Cv2.BitwiseNot(m_InParam.MHesOutImage, m_InParam.MValley);
  1604. Size size = m_InParam.MValley.Size();
  1605. Mat mat = (Mat)Mat.Zeros(size.Height + 2, size.Width + 2, m_InParam.MValley.Type());
  1606. m_InParam.MValley.CopyTo(mat.SubMat(new Range(1, size.Height + 1), new Range(1, size.Width + 1)));
  1607. Cv2.FloodFill(mat, new OpenCvSharp.Point(0, 0), new Scalar(255.0));
  1608. Mat mat2 = new Mat();
  1609. mat.SubMat(new Range(1, size.Height + 1), new Range(1, size.Width + 1)).CopyTo(mat2);
  1610. Cv2.BitwiseNot(mat2, mat2);
  1611. m_InParam.MValley = (Mat)(m_InParam.MValley | mat2);
  1612. return 1;
  1613. }
  1614. private int PrepareParameter()
  1615. {
  1616. m_InParam = new InParameter();
  1617. m_InParam.MGryImg = new Mat();
  1618. if (3 == m_MOrgImg.Channels())
  1619. {
  1620. Cv2.CvtColor(m_MOrgImg, m_InParam.MGryImg, ColorConversionCodes.BGR2GRAY/*RGB2GRAY*/);
  1621. }
  1622. else
  1623. {
  1624. if (1 != m_MOrgImg.Channels())
  1625. {
  1626. return -1;
  1627. }
  1628. m_InParam.MGryImg = m_MOrgImg.Clone();
  1629. }
  1630. m_InParam.iMethod = 1;
  1631. m_InParam.iDimA = 3;
  1632. m_InParam.iDimB = 1;
  1633. bool flag = false;
  1634. int num = 0;
  1635. double num2 = 1.0;
  1636. double num3 = 1.0;
  1637. double num4 = 0.0;
  1638. m_InParam.iXiHua = 0;
  1639. m_InParam.iLianJie = 0;
  1640. if (m_adOutParam != null)
  1641. {
  1642. if (m_adOutParam[0] > 1.5 && m_adOutParam[0] < 2.5)
  1643. {
  1644. m_InParam.iMethod = 2;
  1645. }
  1646. if (m_adOutParam[1] > 1.5 && m_adOutParam[1] < 2.5)
  1647. {
  1648. flag = true;
  1649. }
  1650. if (m_adOutParam[2] > 0.5 && m_adOutParam[2] < 100.5)
  1651. {
  1652. m_InParam.iDimA = (int)(m_adOutParam[2] + 0.5);
  1653. }
  1654. if (m_adOutParam[3] > 0.5 && m_adOutParam[3] < 100.5)
  1655. {
  1656. m_InParam.iDimB = (int)(m_adOutParam[3] + 0.5);
  1657. }
  1658. if (m_adOutParam[4] > 1.5 && m_adOutParam[4] < 2.5)
  1659. {
  1660. num = 2;
  1661. num2 = Math.Max(m_adOutParam[5], 1.0);
  1662. }
  1663. else if (m_adOutParam[4] > 2.5 && m_adOutParam[4] < 3.5)
  1664. {
  1665. num = 3;
  1666. num2 = Math.Max(m_adOutParam[5], 1.0);
  1667. }
  1668. else if (m_adOutParam[4] > 3.5 && m_adOutParam[4] < 4.5)
  1669. {
  1670. num = 4;
  1671. num3 = m_adOutParam[6];
  1672. num4 = m_adOutParam[7];
  1673. }
  1674. if (m_adOutParam[8] > 1.5 && m_adOutParam[8] < 2.5)
  1675. {
  1676. m_InParam.iXiHua = 2;
  1677. }
  1678. if (m_adOutParam[9] > 1.5 && m_adOutParam[9] < 2.5)
  1679. {
  1680. m_InParam.iLianJie = 2;
  1681. }
  1682. }
  1683. if (flag || num > 1)
  1684. {
  1685. int rows = m_InParam.MGryImg.Rows;
  1686. int cols = m_InParam.MGryImg.Cols;
  1687. int[] array = new int[2]
  1688. {
  1689. 0,
  1690. 0
  1691. };
  1692. while (array[0] < rows)
  1693. {
  1694. array[1] = 0;
  1695. while (array[1] < cols)
  1696. {
  1697. byte b = m_InParam.MGryImg.At<byte>(array);
  1698. if (flag)
  1699. {
  1700. b = (byte)(255 - b);
  1701. }
  1702. if (2 == num)
  1703. {
  1704. b = (byte)((double)(int)b / num2 + 0.5);
  1705. }
  1706. if (3 == num)
  1707. {
  1708. double num5 = 255.0 - (double)(int)b;
  1709. num5 /= num2;
  1710. b = (byte)(255.0 - num5 + 0.5);
  1711. }
  1712. if (4 == num)
  1713. {
  1714. double val = num3 * (double)(int)b + num4;
  1715. val = Math.Min(Math.Max(val, 0.0), 255.0);
  1716. b = (byte)(val + 0.5);
  1717. }
  1718. m_InParam.MGryImg.Set(array, b);
  1719. array[1]++;
  1720. }
  1721. array[0]++;
  1722. }
  1723. }
  1724. m_InParam.MHesOutImage = new Mat();
  1725. m_InParam.MValley = new Mat();
  1726. m_InParam.MEdgeOutImage = new Mat();
  1727. return 1;
  1728. }
  1729. private int ImgHessian()
  1730. {
  1731. Mat mat = new Mat();
  1732. m_InParam.MGryImg.ConvertTo(mat, 5);
  1733. int rows = mat.Rows;
  1734. int cols = mat.Cols;
  1735. int num = 5;
  1736. double num2 = 1.2;
  1737. Mat mat2 = new Mat(2 * num + 1, 2 * num + 1, MatType.CV_32FC1, Scalar.All(0.0));
  1738. Mat mat3 = new Mat(2 * num + 1, 2 * num + 1, MatType.CV_32FC1, Scalar.All(0.0));
  1739. Mat mat4 = new Mat(2 * num + 1, 2 * num + 1, MatType.CV_32FC1, Scalar.All(0.0));
  1740. for (int i = -num; i <= num; i++)
  1741. {
  1742. for (int j = -num; j <= num; j++)
  1743. {
  1744. double num3 = 1.0 - (double)(i * i) / (num2 * num2);
  1745. double num4 = Math.Exp((double)(-1 * (i * i + j * j)) / (2.0 * num2 * num2));
  1746. double num5 = -1.0 / (Math.PI * 2.0 * Math.Pow(num2, 4.0));
  1747. double num6 = num3 * num4 * num5;
  1748. float value = (float)num6;
  1749. mat2.Set(i + num, j + num, value);
  1750. mat4.Set(i + num, j + num, (float)((1.0 - (double)(j * j) / (num2 * num2)) * Math.Exp((double)(-1 * (i * i + j * j)) / (2.0 * num2 * num2)) * (-1.0 / (Math.PI * 2.0 * Math.Pow(num2, 4.0)))));
  1751. mat3.Set(i + num, j + num, (float)((double)(i * j) * Math.Exp((double)(-1 * (i * i + j * j)) / (2.0 * num2 * num2)) * (1.0 / (Math.PI * 2.0 * Math.Pow(num2, 6.0)))));
  1752. }
  1753. }
  1754. Mat mat5 = new Mat(rows, cols, MatType.CV_32FC1, Scalar.All(0.0));
  1755. Mat mat6 = new Mat(rows, cols, MatType.CV_32FC1, Scalar.All(0.0));
  1756. Mat mat7 = new Mat(rows, cols, MatType.CV_32FC1, Scalar.All(0.0));
  1757. Cv2.Filter2D(mat, mat5, mat5.Depth(), mat2);//# 相同的条件,这里比demo慢
  1758. Cv2.Filter2D(mat, mat6, mat6.Depth(), mat4);
  1759. Cv2.Filter2D(mat, mat7, mat7.Depth(), mat3);
  1760. byte[,] array = new byte[rows, cols];
  1761. float[] array2 = new float[rows * cols];
  1762. float[] array3 = new float[rows * cols];
  1763. float[] array4 = new float[rows * cols];
  1764. Marshal.Copy(mat5.Data, array2, 0, rows * cols);
  1765. Marshal.Copy(mat6.Data, array3, 0, rows * cols);
  1766. Marshal.Copy(mat7.Data, array4, 0, rows * cols);
  1767. for (int k = 0; k < rows; k++)
  1768. {
  1769. for (int l = 0; l < cols; l++)
  1770. {
  1771. float num7 = array2[k * cols + l];
  1772. float num8 = array3[k * cols + l];
  1773. float num9 = array4[k * cols + l];
  1774. double num10 = (num7 + num8) * (num7 + num8) - 4f * (num7 * num8 - num9 * num9);
  1775. if (!(num10 < 0.0))
  1776. {
  1777. num10 = Math.Sqrt(num10);
  1778. double num11 = ((double)(num7 + num8) + num10) / 2.0;
  1779. double value2 = ((double)(num7 + num8) - num10) / 2.0;
  1780. if (num11 > 0.0 && Math.Abs(num11) > 1.0 + Math.Abs(value2))
  1781. {
  1782. array[k, l] = byte.MaxValue;
  1783. }
  1784. }
  1785. }
  1786. }
  1787. m_InParam.MHesOutImage = new Mat(rows, cols, MatType.CV_8UC1, array, 0L);
  1788. return 0;
  1789. }
  1790. }
  1791. /////
  1792. /////
  1793. class OUTsegmsurf
  1794. {
  1795. public Mat cellbw;
  1796. public Mat wat;
  1797. public Mat imsegm;
  1798. public Mat minima;
  1799. public Mat minimacell;
  1800. //kkkkk public NEW_TYPE_SegInf info;
  1801. }
  1802. class In5segmsurf
  1803. {
  1804. public string smoothim_method;
  1805. public int filterridges;
  1806. public double gaussian_stdev;
  1807. public string LEVEL;
  1808. public double classifycells_convexarea;
  1809. public double classifycells_convexperim;
  1810. public Mat OrgImg;
  1811. public In5segmsurf Clone()
  1812. {
  1813. return this.MemberwiseClone() as In5segmsurf;
  1814. }
  1815. }
  1816. class In6segmsurf
  1817. {
  1818. public string prmfile;//Path to parameter file
  1819. public Mat minima;//All markers, for instance from manual drawings
  1820. public Mat minimacell;//; for cells, for instance from manual drawings
  1821. public Mat imnucl;//Nucleus image for defining markers
  1822. }
  1823. class PMergefragments//区域合并
  1824. {
  1825. public int optlog;
  1826. public int optint;
  1827. public double intint;
  1828. public double conv;
  1829. public PMergefragments Clone()
  1830. {
  1831. return this.MemberwiseClone() as PMergefragments;
  1832. }
  1833. }
  1834. class PSmoothim//平滑
  1835. {
  1836. public string method;
  1837. public double[] h;
  1838. public int gpu;
  1839. public int dim;
  1840. public int ced_maxniter;
  1841. public PSmoothim Clone()
  1842. {
  1843. return this.MemberwiseClone() as PSmoothim;
  1844. }
  1845. }
  1846. class PClassifycells//区域分类
  1847. {
  1848. public double convexarea;
  1849. public double convexperim;
  1850. public double[] h;
  1851. public double minvolfull;
  1852. public double maxvolfull;
  1853. public string method;
  1854. public PClassifycells Clone()
  1855. {
  1856. return this.MemberwiseClone() as PClassifycells;
  1857. }
  1858. }
  1859. class In4smoothim
  1860. {
  1861. public int filterridges;
  1862. public string LEVEL;
  1863. public double gaussian_stdev;
  1864. public double[] getminima_h;
  1865. public double getminima_minvolfull;
  1866. public double getminima_maxvolfull;
  1867. public double getminima_level;
  1868. //public string getminima_method;
  1869. public PMergefragments mergefragments;
  1870. public PSmoothim smoothim;
  1871. public PClassifycells classifycells;
  1872. public int[] dim;
  1873. public double minvol;
  1874. public double minvolvox;
  1875. public double maxvol;
  1876. public double maxvolvox;
  1877. public double watminvolfull;
  1878. public double watmaxvolfull;
  1879. public double minvolfull;
  1880. public double maxvolfull;
  1881. public double[] h;
  1882. public double just;
  1883. public int gpu;
  1884. public int illum;
  1885. public double illumdiameter;
  1886. public int merge;
  1887. public int iInterpolationMethod;//zzzzz new, 1 for Cubic spline interpolation, 2 for Bilinear interpolation, 3 for Nearest neighbor interpolation
  1888. }
  1889. class OUTgetminima
  1890. {
  1891. public Mat minimacell;
  1892. public Mat minima;
  1893. }
  1894. class OUTclassifycells
  1895. {
  1896. public Mat cellbw;
  1897. public PClassifycells classifycells;
  1898. }
  1899. class OUTmaxfilt3
  1900. {
  1901. public List<float>[,] maxim;
  1902. public float[,] sumim;
  1903. public int numpoints;
  1904. public float[][][] minim;
  1905. }
  1906. class MyFilter
  1907. {
  1908. public double[] x;
  1909. public double[] y;
  1910. public double[] z;
  1911. }
  1912. class OLDcellsegm
  1913. {
  1914. public const double m_PI = 3.1415926535897931;
  1915. void mf_mergeinputpar(In4smoothim prm, In5segmsurf prmin)
  1916. {
  1917. prm.smoothim.method = prmin.smoothim_method;
  1918. prm.filterridges = prmin.filterridges;
  1919. prm.gaussian_stdev = prmin.gaussian_stdev;
  1920. prm.LEVEL = (string)prmin.LEVEL.Clone();
  1921. prm.classifycells.convexarea = prmin.classifycells_convexarea;
  1922. prm.classifycells.convexperim = prmin.classifycells_convexperim;
  1923. }
  1924. void mf_cellsize(double minvol, double maxvol, In4smoothim prm, int ThreeDimNO)
  1925. {
  1926. if (0 == prm.h[0] * prm.h[1] * prm.h[2])
  1927. {
  1928. Console.WriteLine("There is zero voxel size");
  1929. }
  1930. double minr = Math.Pow((3 * minvol) / (4 * m_PI), 1 / 3);
  1931. //maxr = ((3*maxvol)/(4*pi))^(1/3);
  1932. //maxz = maxr/h(3);
  1933. double minz = minr / prm.h[2];
  1934. if (ThreeDimNO < minz)
  1935. {
  1936. //shrinink a sphere to a circle
  1937. double r = Math.Pow((3 * maxvol) / (4 * m_PI), 1 / 3);
  1938. maxvol = Math.Pow(ThreeDimNO, prm.just) * m_PI * r * r;
  1939. r = Math.Pow((3 * minvol) / (4 * m_PI), 1 / 3);
  1940. minvol = Math.Pow(ThreeDimNO, prm.just) * m_PI * r * r;
  1941. }
  1942. double voxelvol = prm.h[0] * prm.h[1] * prm.h[2];
  1943. double minvolvox = Math.Round(minvol / voxelvol, MidpointRounding.AwayFromZero);
  1944. double maxvolvox = Math.Round(maxvol / voxelvol, MidpointRounding.AwayFromZero);
  1945. prm.minvol = minvol;
  1946. prm.minvolvox = minvolvox;
  1947. prm.maxvol = maxvol;
  1948. prm.maxvolvox = maxvolvox;
  1949. }
  1950. public OUTsegmsurf mf_segmsurf_progress_auto(Mat GryImg, Color phaseColor, int adOutParam0, int adOutParam1, int adOutParam2, int adOutParam3, int adOutParam4, double adOutParam5, int adOutParam6)
  1951. {
  1952. long start = Cv2.GetTickCount();
  1953. OUTsegmsurf rtn = new OUTsegmsurf();
  1954. //ttttt Cv2.FindContours(watermark, out contour, out hier, RetrievalModes.CComp, ContourApproximationModes.ApproxSimple, null);
  1955. rtn.wat = new Mat(GryImg.Size(), MatType.CV_8UC4, new Scalar(phaseColor.B, phaseColor.G, phaseColor.R, 0));
  1956. //Mat OutImg = new Mat(GryImg.Rows, GryImg.Cols, MatType.CV_8UC3, Scalar.All(0));
  1957. NewSeg NSeg = new NewSeg();
  1958. //转为为灰度?
  1959. if (/*3 == GryImg.Channels() || */4 == GryImg.Channels())
  1960. {
  1961. Cv2.CvtColor(GryImg, GryImg, ColorConversionCodes.BGRA2BGR);
  1962. }
  1963. else if (1 == GryImg.Channels())
  1964. {
  1965. Cv2.CvtColor(GryImg, GryImg, ColorConversionCodes.GRAY2BGR);
  1966. }
  1967. //Cv2.ImShow("oriImg", GryImg);
  1968. //Cv2.WaitKey();
  1969. double time_v2 = (Cv2.GetTickCount() - start) / Cv2.GetTickFrequency();
  1970. System.Console.WriteLine("晶界重现_V2_执行时间1:" + time_v2);
  1971. double[] adOutParam = new double[10];
  1972. adOutParam[0] = adOutParam0;// 2;//方法1原方法,方法2模拟软件方法
  1973. adOutParam[1] = adOutParam1;// 0;//默认为暗晶界,为2时为亮晶界
  1974. adOutParam[2] = adOutParam2;// 3;//尺度系数,默认为3
  1975. adOutParam[3] = adOutParam3;// 1;//晶界宽度系数,默认为1
  1976. adOutParam[4] = adOutParam4;// 0;//亮度调整,为0不调整,为1自动调整,为2整体调暗,为3整体调亮,为4线性调整
  1977. adOutParam[5] = adOutParam5;// 1;//adOutParam[4]为2或3时adOutParam[5]有意义,表示调整强度,必须大于1,等于1无变化
  1978. adOutParam[6] = adOutParam6==0 ? 1 : 0;// 1;//adOutParam[4]为4时adOutParam[6]有意义,线性变换斜率。
  1979. adOutParam[7] = adOutParam6 == 1 ? 1 : 0;// 0;//adOutParam[4]为4时adOutParam[7]有意义,线性变换截距。
  1980. adOutParam[8] = 0;//为2时表示边界必须细化
  1981. adOutParam[9] = 0;//为2时表示边界必须连接
  1982. NSeg.DoSeg(GryImg.Clone()/*OrgImg*/, rtn.wat, adOutParam, new Vec4b(phaseColor.B, phaseColor.G, phaseColor.R, 255));
  1983. ////ttttt Cv2.DrawContours(rtn.wat, contour, -1, Scalar.Red);
  1984. //int Rows = OutImg.Rows;
  1985. //int Cols = OutImg/*watermark*/.Cols;
  1986. //int[] point = new int[2];
  1987. //Vec4b redcol;
  1988. //if (phaseColor != null)
  1989. //{
  1990. // redcol = new Vec4b(phaseColor.B, phaseColor.G, phaseColor.R, 255);
  1991. //}
  1992. //else
  1993. // redcol = new Vec4b(0, 0, 255/*255*/, 255);
  1994. //for (point[0] = 0; point[0] < Rows; point[0]++)
  1995. //{
  1996. // for (point[1] = 0; point[1] < Cols; point[1]++)
  1997. // {
  1998. // if (0 >= watermark.At<int>(point[0], point[1]))
  1999. // {
  2000. // rtn.wat.Set<Vec4b>(point[0], point[1], redcol);//###
  2001. // }
  2002. // }
  2003. //}
  2004. time_v2 = (Cv2.GetTickCount() - start) / Cv2.GetTickFrequency();
  2005. System.Console.WriteLine("晶界重现_V2_执行时间2:" + time_v2);
  2006. ////Cv2.ImShow("contour", rtn.wat);//kkkkk
  2007. ////Cv2.WaitKey(0);//kkkkk
  2008. //control.Cursor = cursor;// System.Windows.Forms.Cursors.Default;
  2009. return rtn;
  2010. }
  2011. OUTclassifycells classifycells(Mat watermark, Mat imsegmini, PClassifycells prmclf, Mat minimacell = null)
  2012. {
  2013. OUTclassifycells rtn = new OUTclassifycells();
  2014. return rtn;
  2015. }
  2016. OUTgetminima getminima(Mat imsegm, Mat minimacell, In5segmsurf prmin)
  2017. {
  2018. OUTgetminima rtn = new OUTgetminima();
  2019. rtn.minima = null;
  2020. rtn.minimacell = null;
  2021. if (null == minimacell)
  2022. {
  2023. rtn.minima = automated(imsegm, prmin);
  2024. }
  2025. return rtn;
  2026. }
  2027. Mat automated(Mat imsegm, In5segmsurf prmin)
  2028. {
  2029. Mat mat_mean = new Mat(), mat_stddev = new Mat();
  2030. double m, s, d;
  2031. //Cv2.MeanStdDev(imsegm, mat_mean, mat_stddev);
  2032. //m = mat_mean.At<double>(0, 0);
  2033. //s = mat_stddev.At<double>(0, 0);
  2034. //d = 0;//mmmmm
  2035. double th;
  2036. //th = Math.Min(Math.Max(m+d*s, m/2),1.5*m);
  2037. Mat minima = new Mat(), minimacell = new Mat(imsegm.Size(), MatType.CV_8UC3);
  2038. //Cv2.Threshold(imsegm, minimacell, th, 255, ThresholdTypes.Binary);
  2039. int iRad = 39;
  2040. Mat matTempA = new Mat();
  2041. Mat matTempB = new Mat();
  2042. Cv2.MedianBlur(imsegm, matTempA, iRad);
  2043. Cv2.AddWeighted(imsegm, 1, matTempA, -1, 0, matTempB);
  2044. Cv2.MeanStdDev(matTempB, mat_mean, mat_stddev);
  2045. m = mat_mean.At<double>(0, 0);
  2046. s = mat_stddev.At<double>(0, 0);
  2047. d = 0;//mmmmm
  2048. th = m + d * s;
  2049. Cv2.Threshold(matTempB, minimacell, th, 255, ThresholdTypes.Binary);
  2050. int diam;//mmmmm
  2051. Mat kernel;
  2052. diam = 9;
  2053. kernel = Cv2.GetStructuringElement(MorphShapes.Cross, new Size(diam, diam), new OpenCvSharp.Point(-1, -1));
  2054. Cv2.Dilate(minimacell, minimacell, kernel);
  2055. diam = 3;
  2056. kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(diam, diam), new OpenCvSharp.Point(-1, -1));
  2057. Cv2.Erode(minimacell, minimacell, kernel);
  2058. Cv2.MorphologyEx(minimacell, minimacell, MorphTypes.Open, kernel);
  2059. //zzzzz Cv2.ImShow("bianjie", minimacell);
  2060. //zzzzz Cv2.WaitKey(0);
  2061. Cv2.BitwiseNot(minimacell, minima);
  2062. Size bianjieSize = minima.Size();
  2063. Mat Temp = Mat.Zeros(bianjieSize.Height + 2, bianjieSize.Width + 2, minimacell.Type());//延展图像
  2064. minima.CopyTo(Temp.SubMat(new Range(1, bianjieSize.Height + 1), new Range(1, bianjieSize.Width + 1)));
  2065. Cv2.FloodFill(Temp, new OpenCvSharp.Point(0, 0), new Scalar(255));
  2066. Mat cutImg = new Mat();
  2067. Temp.SubMat(new Range(1, bianjieSize.Height + 1), new Range(1, bianjieSize.Width + 1)).CopyTo(cutImg);
  2068. Cv2.BitwiseNot(cutImg, cutImg);
  2069. minima = minima | cutImg;
  2070. diam = 9;
  2071. kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(diam, diam), new OpenCvSharp.Point(-1, -1));
  2072. Cv2.MorphologyEx(minima, minima, MorphTypes.Open, kernel);
  2073. //zzzzz Cv2.ImShow("neibu", minima);//kkkkk
  2074. //zzzzz Cv2.WaitKey(0);//kkkkk
  2075. return minima;
  2076. }
  2077. Mat smoothim(Mat im, string method, string prmflg, In4smoothim prm)
  2078. {
  2079. //diameter of filter
  2080. int iPrmD = 13;
  2081. Mat rst = new Mat();
  2082. Cv2.ConvertScaleAbs(dircohenh(im, iPrmD, prm.h, prm.iInterpolationMethod), rst);
  2083. return rst;
  2084. }
  2085. Mat dircohenh(Mat im, int iPrmD, double[] adPrmH, int iInterpolationMethod)
  2086. {
  2087. int[] dim = new int[3] { im.Rows, im.Cols, 1 };
  2088. if (3 == im.Dims)
  2089. {
  2090. dim[2] = im.Height;
  2091. }
  2092. //number of elements to remove before one takes the Olympic average. Set to
  2093. //1-->3
  2094. int numremele = 2;
  2095. int stepxy = 30;
  2096. int numdeghere = (180 - stepxy) / stepxy + 1;
  2097. int[] deghere = new int[numdeghere];
  2098. deghere[0] = 0;
  2099. for (int i = 1; i < numdeghere; i++)
  2100. {
  2101. deghere[i] = deghere[i - 1] + stepxy;
  2102. }
  2103. double[,] deg2D = new double[numdeghere, 2];
  2104. for (int i = 1; i < numdeghere; i++)
  2105. {
  2106. deg2D[i, 0] = (double)(deghere[i]);
  2107. deg2D[i, 1] = 90;
  2108. }
  2109. double[,] deg;
  2110. deg = deg2D;
  2111. if (1 != dim[2])
  2112. {
  2113. double[,] deg3D = new double[numdeghere + 1, 2];
  2114. for (int i = 1; i < numdeghere; i++)
  2115. {
  2116. deg3D[i, 0] = deg2D[i, 0];
  2117. deg3D[i, 1] = deg2D[i, 1];
  2118. }
  2119. deg3D[numdeghere, 0] = 0;
  2120. deg3D[numdeghere, 1] = 0;
  2121. deg = deg3D;
  2122. }
  2123. //the half filter size
  2124. int p = iPrmD / 2;
  2125. //make filter
  2126. MyFilter[] c = makefilter(deg, p);
  2127. //number of directions
  2128. int numdir = c.Length;
  2129. //inisization
  2130. Mat filtim = new Mat(im.Rows, im.Cols, MatType.CV_32F, 0.0);
  2131. //zzzzz
  2132. //201118 for Acceleration Mat imRs = new Mat();
  2133. Mat imRs = null;
  2134. float[] afImRs = null;
  2135. int iProportion = 5;
  2136. if (1 == iInterpolationMethod)
  2137. {
  2138. imRs = new Mat();//201118 for Acceleration
  2139. Mat imFt = new Mat();
  2140. im.ConvertTo(imFt, MatType.CV_32F);
  2141. Cv2.Resize(imFt, imRs, new Size(imFt.Cols * iProportion, imFt.Rows * iProportion), 0, 0, InterpolationFlags.Cubic);
  2142. //afImRs = new float[imRs.Rows, imRs.Cols];
  2143. afImRs = new float[imRs.Rows * imRs.Cols];
  2144. Marshal.Copy(imRs.Data, afImRs, 0, imRs.Rows * imRs.Cols);
  2145. /*
  2146. for (int i = 0; i < imRs.Rows; i++)
  2147. {
  2148. for (int j = 0; j < imRs.Cols; j++)
  2149. {
  2150. afImRs[i, j] = imRs.At<float>(i, j);
  2151. }
  2152. */
  2153. imRs = null;
  2154. }
  2155. for (int i = 0; i < numdir; i++)
  2156. {
  2157. //coordinates of points in this direction after rotation of filter
  2158. MyFilter chere = c[i];
  2159. //filter image for max value
  2160. //zzzzz
  2161. OUTmaxfilt3 fltrst;
  2162. if (1 == iInterpolationMethod)
  2163. {
  2164. //201118 for Acceleration fltrst = maxfilt3ByCubicSpline(im, chere, numremele/*, dim*/, imRs, iProportion);
  2165. fltrst = maxfilt3ByCubicSpline(im, chere, numremele/*, dim*/, afImRs, iProportion);
  2166. }
  2167. else if (2 == iInterpolationMethod)
  2168. {
  2169. fltrst = maxfilt3ByBilinear(im, chere, numremele/*, dim*/);
  2170. }
  2171. else
  2172. {
  2173. fltrst = maxfilt3ByNearest(im, chere, numremele/*, dim*/);
  2174. }
  2175. //structural filtering
  2176. //filtim = max(filtim,(sumim - sum(maxim,4))/ (numpoints-numremele));
  2177. int[] point = new int[2];
  2178. for (point[0] = 0; point[0] < filtim.Rows; point[0]++)
  2179. {
  2180. for (point[1] = 0; point[1] < filtim.Cols; point[1]++)
  2181. {
  2182. float fTemp = filtim.At<float>(point[0], point[1]);
  2183. /*201118 for Acceleration
  2184. fTemp = Math.Max(fTemp,
  2185. (fltrst.sumim[point[0], point[1]] - (fltrst.maxim[point[0], point[1]]).Sum()) / (fltrst.numpoints - numremele)
  2186. );
  2187. */
  2188. //(fltrst.minim[point[0], point[1]]).Sum()
  2189. fTemp = fTemp = Math.Max(fTemp, (fltrst.minim[point[0]][point[1]].Take<float>(fltrst.numpoints - numremele)).Sum() / (fltrst.numpoints - numremele));
  2190. filtim.Set<float>(point, fTemp);
  2191. }
  2192. }
  2193. }
  2194. return filtim;
  2195. }
  2196. OUTmaxfilt3 maxfilt3ByNearest(Mat im, MyFilter chere, int numremele/*, int[] dim*/)
  2197. {
  2198. /////Mat imRs = new Mat();
  2199. /////im.ConvertTo(imRs, MatType.CV_32F);
  2200. int BigRows = im.Rows;
  2201. int BigCols = im.Cols;
  2202. OUTmaxfilt3 rst = new OUTmaxfilt3();
  2203. rst.sumim = new float[im.Rows, im.Cols];
  2204. rst.maxim = new List<float>[im.Rows, im.Cols];
  2205. for (int i = 0; i < im.Rows; i++)
  2206. {
  2207. for (int j = 0; j < im.Cols; j++)
  2208. {
  2209. (rst.sumim)[i, j] = 0;
  2210. List<float> lisTmp = new List<float>();
  2211. for (int k = 0; k < numremele; k++)
  2212. {
  2213. lisTmp.Add(0);
  2214. }
  2215. (rst.maxim)[i, j] = lisTmp;
  2216. }
  2217. }
  2218. rst.numpoints = chere.x.Length;
  2219. for (int i = 0; i < rst.numpoints; i++)
  2220. {
  2221. double xhere;
  2222. double yhere;
  2223. int[] point = new int[2];
  2224. for (point[0] = 0; point[0] < im.Rows; point[0]++)
  2225. {
  2226. for (point[1] = 0; point[1] < im.Cols; point[1]++)
  2227. {
  2228. xhere = (point[0] + (chere.x)[i]);
  2229. yhere = (point[1] + (chere.y)[i]);
  2230. int iX = Math.Min(Math.Max((int)(xhere + 0.5), 0), BigRows - 1);
  2231. int iY = Math.Min(Math.Max((int)(yhere + 0.5), 0), BigCols - 1);
  2232. float fTemp = im.At<byte>(iX, iY);
  2233. (rst.sumim)[point[0], point[1]] += fTemp;
  2234. (rst.maxim)[point[0], point[1]].Add(fTemp);
  2235. (rst.maxim)[point[0], point[1]].Sort();
  2236. (rst.maxim)[point[0], point[1]].RemoveAt(0);
  2237. (rst.maxim)[point[0], point[1]].Reverse();
  2238. }
  2239. }
  2240. }
  2241. return rst;
  2242. }
  2243. OUTmaxfilt3 maxfilt3ByBilinear(Mat im, MyFilter chere, int numremele/*, int[] dim*/)
  2244. {
  2245. /////Mat imRs = new Mat();
  2246. /////im.ConvertTo(imRs, MatType.CV_32F);
  2247. int BigRows = im.Rows;
  2248. int BigCols = im.Cols;
  2249. OUTmaxfilt3 rst = new OUTmaxfilt3();
  2250. rst.sumim = new float[im.Rows, im.Cols];
  2251. rst.maxim = new List<float>[im.Rows, im.Cols];
  2252. for (int i = 0; i < im.Rows; i++)
  2253. {
  2254. for (int j = 0; j < im.Cols; j++)
  2255. {
  2256. (rst.sumim)[i, j] = 0;
  2257. List<float> lisTmp = new List<float>();
  2258. for (int k = 0; k < numremele; k++)
  2259. {
  2260. lisTmp.Add(0);
  2261. }
  2262. (rst.maxim)[i, j] = lisTmp;
  2263. }
  2264. }
  2265. rst.numpoints = chere.x.Length;
  2266. for (int i = 0; i < rst.numpoints; i++)
  2267. {
  2268. double xhere;
  2269. double yhere;
  2270. int[] point = new int[2];
  2271. for (point[0] = 0; point[0] < im.Rows; point[0]++)
  2272. {
  2273. for (point[1] = 0; point[1] < im.Cols; point[1]++)
  2274. {
  2275. xhere = point[0] + (chere.x)[i];
  2276. yhere = point[1] + (chere.y)[i];
  2277. int iXS = Math.Min(Math.Max((int)(xhere), 0), BigRows - 1);
  2278. int iYS = Math.Min(Math.Max((int)(yhere), 0), BigCols - 1);
  2279. int iXB = Math.Min(Math.Max((int)(xhere + 1.0), 0), BigRows - 1);
  2280. int iYB = Math.Min(Math.Max((int)(yhere + 1.0), 0), BigCols - 1);
  2281. double dWXB = Math.Min(Math.Max((xhere - (double)iXS), 0.0), 1.0);
  2282. double dWXS = 1.0 - dWXB;
  2283. double dWYB = Math.Min(Math.Max((yhere - (double)iYS), 0.0), 1.0);
  2284. double dWYS = 1.0 - dWYB;
  2285. double dTempA = im.At<byte>(iXS, iYS);
  2286. double dTempB = im.At<byte>(iXS, iYB);
  2287. double dTempC = im.At<byte>(iXB, iYS);
  2288. double dTempD = im.At<byte>(iXB, iYB);
  2289. double dTempAB = dTempA * dWYS + dTempB * dWYB;
  2290. double dTempCD = dTempC * dWYS + dTempD * dWYB;
  2291. float fTemp = (float)(dTempAB * dWXS + dTempCD * dWXB);
  2292. (rst.sumim)[point[0], point[1]] += fTemp;
  2293. (rst.maxim)[point[0], point[1]].Add(fTemp);
  2294. (rst.maxim)[point[0], point[1]].Sort();
  2295. (rst.maxim)[point[0], point[1]].RemoveAt(0);
  2296. (rst.maxim)[point[0], point[1]].Reverse();
  2297. }
  2298. }
  2299. }
  2300. return rst;
  2301. }
  2302. //201118 for Acceleration OUTmaxfilt3 maxfilt3ByCubicSpline(Mat im, MyFilter chere, int numremele/*, int[] dim*/, Mat imRs, int iProportion)
  2303. OUTmaxfilt3 maxfilt3ByCubicSpline(Mat im, MyFilter chere, int numremele, float[] afImRs, int iProportion)
  2304. {
  2305. /////Mat imFt = new Mat();
  2306. /////Mat imRs = new Mat();
  2307. /////im.ConvertTo(imFt, MatType.CV_32F);
  2308. /////int iProportion = 5;
  2309. /////Cv2.Resize(imFt, imRs, new Size(imFt.Cols * iProportion, imFt.Rows * iProportion), 0, 0, InterpolationFlags.Cubic);
  2310. //201118 for Acceleration int BigRows = imRs.Rows;
  2311. //201118 for Acceleration int BigCols = imRs.Cols;
  2312. int BigRows = im.Rows * iProportion;
  2313. int BigCols = im.Cols * iProportion;
  2314. /*
  2315. double[,] x = new double[dim[0], dim[1]];
  2316. double[,] y = new double[dim[0], dim[1]];
  2317. for (int i = 0; i < dim[0]; i++)
  2318. {
  2319. for (int j = 0; j < dim[1]; j++)
  2320. {
  2321. x[i, j] = (double)i;
  2322. y[i, j] = (double)j;
  2323. }
  2324. }
  2325. */
  2326. OUTmaxfilt3 rst = new OUTmaxfilt3();
  2327. //rst.maxim = new Mat[numremele];
  2328. //201118 for Acceleration rst.sumim = new float[im.Rows, im.Cols];
  2329. //201118 for Acceleration rst.maxim = new List<float>[im.Rows, im.Cols];
  2330. rst.numpoints = chere.x.Length;
  2331. rst.minim = new float[im.Rows][][];
  2332. for (int i = 0; i < im.Rows; i++)
  2333. {
  2334. rst.minim[i] = new float[im.Cols][];
  2335. for (int j = 0; j < im.Cols; j++)
  2336. {
  2337. rst.minim[i][j] = new float[rst.numpoints];
  2338. //201118 for Acceleration (rst.sumim)[i, j] = 0;
  2339. //201118 for Acceleration List<float> lisTmp = new List<float>();
  2340. //201118 for Acceleration for (int k = 0; k < numremele; k++)
  2341. //201118 for Acceleration {
  2342. //201118 for Acceleration lisTmp.Add(0);
  2343. //201118 for Acceleration }
  2344. //201118 for Acceleration (rst.maxim)[i, j] = lisTmp;
  2345. //201118 for Acceleration List<float> lisTmpMin = new List<float>();
  2346. //201118 for Acceleration (rst.minim)[i, j] = lisTmpMin;
  2347. }
  2348. }
  2349. //Mat imhere = new Mat(imFt.Rows, imFt.Cols, MatType.CV_32F, 0.0);
  2350. /* 201118 for Acceleration
  2351. for (int i = 0; i < rst.numpoints; i++)
  2352. {
  2353. //the coordinate where we want to find the values
  2354. //NB!!!!!!!!!
  2355. //Must not do this for z direction, then it goes wrong.
  2356. //Do NOOOOOT replacestr any Inf or NaN by image values, then the filter
  2357. //does not work anymore!!!!!!
  2358. //xhere(xhere < h(1)) = h(1);xhere(xhere > dim(1)*h(1)) = dim(1)*h(1);
  2359. //yhere(yhere < h(1)) = h(1);yhere(yhere > dim(2)*h(1)) = dim(2)*h(2);
  2360. //double[,] xhere = new double[dim[0], dim[1]];
  2361. //double[,] yhere = new double[dim[0], dim[1]];
  2362. double xhere;
  2363. double yhere;
  2364. int[] point = new int[2];
  2365. for (point[0] = 0; point[0] < im.Rows; point[0]++)
  2366. {
  2367. for (point[1] = 0; point[1] < im.Cols; point[1]++)
  2368. {
  2369. //xhere[i, j] = Math.Min(Math.Max(x[i, j]+(chere.x)[t],1),dim[0]);
  2370. //yhere[i, j] = Math.Min(Math.Max(y[i, j]+(chere.y)[t],1),dim[1]);
  2371. xhere = (point[0] + (chere.x)[i]) * iProportion;
  2372. yhere = (point[1] + (chere.y)[i]) * iProportion;
  2373. int iX = Math.Min(Math.Max((int)(xhere + 0.5), 0), BigRows - 1);
  2374. int iY = Math.Min(Math.Max((int)(yhere + 0.5), 0), BigCols - 1);
  2375. float fTemp = imRs.At<float>(iX, iY);
  2376. //imhere.Set<float>(point, fTemp);
  2377. (rst.sumim)[point[0], point[1]] += fTemp;
  2378. (rst.maxim)[point[0], point[1]].Add(fTemp);
  2379. (rst.maxim)[point[0], point[1]].Sort();
  2380. (rst.maxim)[point[0], point[1]].RemoveAt(0);
  2381. (rst.maxim)[point[0], point[1]].Reverse();
  2382. }
  2383. }
  2384. }
  2385. */
  2386. for (int ir = 0; ir < im.Rows; ir++)
  2387. {
  2388. for (int ic = 0; ic < im.Cols; ic++)
  2389. {
  2390. double xhere;
  2391. double yhere;
  2392. for (int ip = 0; ip < rst.numpoints; ip++)
  2393. {
  2394. xhere = (ir + (chere.x)[ip]) * iProportion;
  2395. yhere = (ic + (chere.y)[ip]) * iProportion;
  2396. int iX = Math.Min(Math.Max((int)(xhere + 0.5), 0), BigRows - 1);
  2397. int iY = Math.Min(Math.Max((int)(yhere + 0.5), 0), BigCols - 1);
  2398. //201118 for Acceleration float fTemp = imRs.At<float>(iX, iY);
  2399. float fTemp = afImRs[iX * BigCols + iY];
  2400. //201118 for Acceleration (rst.sumim)[ir, ic] += fTemp;
  2401. //201118 for Acceleration (rst.maxim)[ir, ic].Add(fTemp);
  2402. (rst.minim)[ir][ic][ip] = fTemp;
  2403. }
  2404. //201118 for Acceleration (rst.maxim)[ir, ic].Sort((x, y) => -x.CompareTo(y));
  2405. //201118 for Acceleration (rst.maxim)[ir, ic].RemoveRange(numremele, rst.numpoints - numremele);
  2406. Array.Sort((rst.minim)[ir][ic]);
  2407. //201118 for Acceleration (rst.minim)[ir, ic].Sort();
  2408. //201118 for Acceleration (rst.minim)[ir, ic].RemoveRange(rst.numpoints - numremele,numremele);
  2409. }
  2410. }
  2411. return rst;
  2412. }
  2413. MyFilter[] makefilter(double[,] deg, int p)
  2414. {
  2415. int iNum = deg.GetLength(0);
  2416. MyFilter[] c = new MyFilter[iNum];
  2417. //r = linspace(-p,p,7);
  2418. int iArrLength = 7;
  2419. double[] r = new double[iArrLength];
  2420. double dStep = p * 2.0 / (iArrLength - 1.0);
  2421. for (int i = 0; i < 7; i++)
  2422. {
  2423. r[i] = -1.0 * p + i * dStep;
  2424. }
  2425. /*
  2426. for i = 1 : size(deg,1)
  2427. % the degrees
  2428. theta = deg(i,1);
  2429. phi = deg(i,2);
  2430. % the positions
  2431. c(i).x = r*sind(phi)*cosd(theta);
  2432. c(i).y = r*sind(phi)*sind(theta);
  2433. c(i).z = r*cosd(phi);
  2434. end;
  2435. */
  2436. for (int i = 0; i < iNum; i++)
  2437. {
  2438. double theta = deg[i, 0];
  2439. double phi = deg[i, 1];
  2440. c[i] = new MyFilter();
  2441. c[i].x = new double[iArrLength];
  2442. c[i].y = new double[iArrLength];
  2443. c[i].z = new double[iArrLength];
  2444. for (int j = 0; j < iArrLength; j++)
  2445. {
  2446. (c[i].x)[j] = r[j] * Math.Sin(phi) * Math.Cos(theta); ;
  2447. (c[i].y)[j] = r[j] * Math.Sin(phi) * Math.Sin(theta); ;
  2448. (c[i].z)[j] = r[j] * Math.Cos(phi);
  2449. }
  2450. }
  2451. return c;
  2452. }
  2453. public Mat ProFroStandardImage(Mat GryImg, int iConnect, Color phaseColor)
  2454. {
  2455. //Mat GryImg = new Mat();
  2456. ////转为为灰度?
  2457. //if (3 == OrgImg.Channels())
  2458. //{
  2459. // Cv2.CvtColor(OrgImg, GryImg, ColorConversionCodes.RGB2GRAY);
  2460. //}
  2461. //else
  2462. //{
  2463. // if (1 == OrgImg.Channels())
  2464. // {
  2465. // GryImg = OrgImg.Clone();
  2466. // }
  2467. // else
  2468. // {
  2469. // return null;
  2470. // }
  2471. //}
  2472. Mat CrystalImg = new Mat();
  2473. Mat BoundaryImg = new Mat();
  2474. Mat mat_mean = new Mat(), mat_stddev = new Mat();
  2475. double m, s, d = 0.5;
  2476. Cv2.MeanStdDev(GryImg, mat_mean, mat_stddev);
  2477. m = mat_mean.At<double>(0, 0);
  2478. s = mat_stddev.At<double>(0, 0);
  2479. //Cv2.Threshold(GryImg, CrystalImg, (int)Math.Min(m+d*s,230), 255, ThresholdTypes.Binary);
  2480. //Cv2.Threshold(GryImg, CrystalImg, 0, 1, ThresholdTypes.Otsu);
  2481. Cv2.Threshold(GryImg, CrystalImg, m, 255, ThresholdTypes.Binary);
  2482. //Cv2.ImShow("src", CrystalImg);//kkkkk
  2483. //Cv2.WaitKey(0);//kkkkk
  2484. int diam = 3;//mmmmm
  2485. Mat kernel = Cv2.GetStructuringElement(MorphShapes.Ellipse, new Size(diam, diam), new OpenCvSharp.Point(-1, -1));
  2486. //Cv2.Erode(CrystalImg, CrystalImg, kernel);
  2487. Cv2.MorphologyEx(CrystalImg, CrystalImg, MorphTypes.Open, kernel);
  2488. Cv2.BitwiseNot(CrystalImg, BoundaryImg);
  2489. //kernel = Cv2.GetStructuringElement(MorphShapes.Ellipse, new Size(diam, diam), new Point(-1, -1));
  2490. //Cv2.MorphologyEx(BoundaryImg, BoundaryImg, MorphTypes.Open, kernel);
  2491. //Cv2.MorphologyEx(BoundaryImg, BoundaryImg, MorphTypes.Close, kernel);
  2492. //Cv2.Dilate(BoundaryImg, BoundaryImg, kernel);
  2493. Class12 TCAlgo = new Class12();
  2494. TCAlgo.ImgThin(BoundaryImg);
  2495. //string filename = "D:\\temp.jpg";
  2496. //Cv2.ImWrite(filename, BoundaryImg);
  2497. if (1 == iConnect)
  2498. {
  2499. TCAlgo.ConnectionBoundary(BoundaryImg, 108);
  2500. }
  2501. //Cv2.ImShow("wai", BoundaryImg);//kkkkk
  2502. //Cv2.WaitKey(0);//kkkkk
  2503. //Cv2.BitwiseNot(BoundaryImg, CrystalImg);
  2504. //diam = 9;
  2505. //kernel = Cv2.GetStructuringElement(MorphShapes.Cross, new Size(diam, diam), new Point(-1, -1));
  2506. //Cv2.MorphologyEx(BoundaryImg, BoundaryImg, MorphTypes.Close, kernel);
  2507. //Cv2.Erode(CrystalImg, CrystalImg, kernel);
  2508. //Cv2.MorphologyEx(CrystalImg, CrystalImg, MorphTypes.Open, kernel);
  2509. //Cv2.MorphologyEx(CrystalImg, CrystalImg, MorphTypes.Close, kernel);
  2510. //Cv2.ImShow("nei", CrystalImg);//kkkkk
  2511. //Cv2.WaitKey(0);//kkkkk
  2512. /*
  2513. Mat watermark = new Mat(CrystalImg.Size(), MatType.CV_32S);
  2514. Point[][] contour;
  2515. HierarchyIndex[] hier;
  2516. Cv2.FindContours(CrystalImg, out contour, out hier, RetrievalModes.CComp, ContourApproximationModes.ApproxSimple, null);
  2517. for (int i = 0; i < hier.Length; i++)
  2518. {
  2519. Cv2.DrawContours(watermark, contour, i, Scalar.All(i + 1), 1, LineTypes.Link8, hier);
  2520. }
  2521. Cv2.Watershed(OrgImg, watermark);
  2522. BoundaryImg = new Mat(watermark.Size(), MatType.CV_8UC3);
  2523. //ttttt Cv2.DrawContours(rtn.wat, contour, -1, Scalar.Red);
  2524. */
  2525. /*
  2526. Mat NewWat = new Mat();
  2527. Cv2.BitwiseNot(CrystalImg, NewWat);
  2528. Cv2.ImShow("bian", NewWat);//kkkkk
  2529. Cv2.WaitKey(0);//kkkkk
  2530. ImgThin(NewWat);
  2531. int Rows = NewWat.Rows;
  2532. int Cols = NewWat.Cols;
  2533. */
  2534. int[] point = new int[2];
  2535. Vec4b redcol;
  2536. if (phaseColor != null)
  2537. {
  2538. redcol = new Vec4b(phaseColor.B, phaseColor.G, phaseColor.R, 255);
  2539. }
  2540. else
  2541. redcol = new Vec4b(0, 0, 255/*255*/, 255);
  2542. Mat WAT = new Mat(BoundaryImg.Size(), MatType.CV_8UC4);
  2543. for (point[0] = 0; point[0] < WAT.Rows; point[0]++)
  2544. {
  2545. for (point[1] = 0; point[1] < WAT.Cols; point[1]++)
  2546. {
  2547. if (0 < BoundaryImg.At<byte>(point[0], point[1]))
  2548. {
  2549. WAT.Set<Vec4b>(point, redcol);
  2550. }
  2551. }
  2552. }
  2553. return WAT;
  2554. }
  2555. }
  2556. }