OTSImageProcess.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133
  1. #pragma once
  2. #include "stdafx.h"
  3. #include <opencv2/core/core.hpp>
  4. #include <opencv2/highgui/highgui.hpp>
  5. #include <opencv2/opencv.hpp>
  6. #include "OTSImageProcess.h"
  7. #include "OTSImageProcessParam.h"
  8. #include <OTSFieldData.h>
  9. #include "../OTSLog/COTSUtilityDllFunExport.h"
  10. #include "FieldMgr.h"
  11. using namespace cv;
  12. using namespace std;
  13. namespace OTSIMGPROC
  14. {
  15. namespace
  16. {
  17. /***** 求两点间距离*****/
  18. float getDistance(Point pointO, Point pointA)
  19. {
  20. float distance;
  21. distance = powf((pointO.x - pointA.x), 2) + powf((pointO.y - pointA.y), 2);
  22. distance = sqrtf(distance);
  23. return distance;
  24. }
  25. /***** 点到直线的距离:P到AB的距离*****/
  26. //P为线外一点,AB为线段两个端点
  27. float getDist_P2L(Point pointP, Point pointA, Point pointB)
  28. {
  29. //求直线方程
  30. int A = 0, B = 0, C = 0;
  31. A = pointA.y - pointB.y;
  32. B = pointB.x - pointA.x;
  33. C = pointA.x*pointB.y - pointA.y*pointB.x;
  34. //代入点到直线距离公式
  35. float distance = 0;
  36. distance = ((float)abs(A*pointP.x + B * pointP.y + C)) / ((float)sqrtf(A*A + B * B));
  37. return distance;
  38. }
  39. int Side(Point P1, Point P2, Point point)
  40. {
  41. /*Point P1 = line.P1;
  42. Point P2 = line.P2;*/
  43. return ((P2.y - P1.y) * point.x + (P1.x - P2.x) * point.y + (P2.x*P1.y - P1.x*P2.y));
  44. }
  45. void FindInnerCircleInContour(vector<Point> contour, Point &center, int &radius)
  46. {
  47. Rect r = boundingRect(contour);
  48. int nL = r.x, nR = r.br().x; //轮廓左右边界
  49. int nT = r.y, nB = r.br().y; //轮廓上下边界
  50. double dist = 0;
  51. double maxdist = 0;
  52. for (int i = nL; i < nR; i++) //列
  53. {
  54. for (int j = nT; j < nB; j++) //行
  55. {
  56. //计算轮廓内部各点到最近轮廓点的距离
  57. dist = pointPolygonTest(contour, Point(i, j), true);
  58. if (dist > maxdist)
  59. {
  60. //求最大距离,只有轮廓最中心的点才距离最大
  61. maxdist = dist;
  62. center = Point(i, j);
  63. }
  64. }
  65. }
  66. radius = maxdist; //圆半径
  67. }
  68. BOOL GetParticleAverageChord(std::vector<Point> listEdge, double a_PixelSize, double &dPartFTD)
  69. {
  70. // safety check
  71. double nx = 0, ny = 0;
  72. Moments mu;
  73. mu = moments(listEdge, false);
  74. nx = mu.m10 / mu.m00;
  75. ny = mu.m01 / mu.m00;
  76. //circle(cvcopyImg, Point(nx, ny), 1, (255), 1);
  77. Point ptCenter = Point((int)nx, (int)ny);
  78. // coordinate transformation
  79. Point ptPosition;
  80. int radiusNum = 0;
  81. // get ferret diameter
  82. double sumFltDiameter = 0;
  83. int interval;
  84. int edgePointNum = listEdge.size();
  85. if (edgePointNum > 100)
  86. {
  87. interval = edgePointNum / 100;//get one line per 10 degree aproxemately
  88. }
  89. else
  90. {
  91. interval = 1;
  92. }
  93. for (int i = 0; i < edgePointNum; i++)
  94. {
  95. Point pt = listEdge[i];
  96. ptPosition.x = abs(pt.x - ptCenter.x);
  97. ptPosition.y = abs(pt.y - ptCenter.y);
  98. if (i % interval == 0)//calculate one line per 10 point ,so to speed up.don't calculate all the diameter.
  99. {
  100. double r1 = sqrt(pow(ptPosition.x, 2) + pow(ptPosition.y, 2));
  101. sumFltDiameter += r1;
  102. radiusNum += 1;
  103. //line(cvImageData, ptCenter, pt, Scalar(nBlackColor), nThickness, nLineType);
  104. }
  105. }
  106. if (radiusNum == 0)
  107. {
  108. dPartFTD = 0;
  109. }
  110. else
  111. {
  112. dPartFTD = a_PixelSize * sumFltDiameter / radiusNum * 2;
  113. }
  114. //imshow("feret center", cvImageData);
  115. return TRUE;
  116. }
  117. void linearSmooth5(WORD wordIn[], WORD wordOut[], int N = 255)//smooth algorithm
  118. {
  119. double in[256];
  120. double out[256];
  121. double smoothCurveData[256];
  122. for (int i = 0; i < 256; i++)
  123. {
  124. in[i] = (double)wordIn[i];
  125. }
  126. int i;
  127. if (N < 5)
  128. {
  129. for (i = 0; i <= N - 1; i++)
  130. {
  131. out[i] = in[i];
  132. }
  133. }
  134. else
  135. {
  136. out[0] = (3.0 * in[0] + 2.0 * in[1] + in[2] - in[4]) / 5.0;
  137. out[1] = (4.0 * in[0] + 3.0 * in[1] + 2 * in[2] + in[3]) / 10.0;
  138. for (i = 2; i <= N - 3; i++)
  139. {
  140. out[i] = (in[i - 2] + in[i - 1] + in[i] + in[i + 1] + in[i + 2]) / 5.0;
  141. }
  142. out[N - 2] = (4.0 * in[N - 1] + 3.0 * in[N - 2] + 2 * in[N - 3] + in[N - 4]) / 10.0;
  143. out[N - 1] = (3.0 * in[N - 1] + 2.0 * in[N - 2] + in[N - 3] - in[N - 5]) / 5.0;
  144. }
  145. for (int i = 0; i < N; i++)
  146. {
  147. wordOut[i] = (WORD)out[i];
  148. }
  149. }
  150. }
  151. COTSImageProcess::COTSImageProcess()
  152. {
  153. }
  154. COTSImageProcess::~COTSImageProcess()
  155. {
  156. }
  157. // use verticl line of 3 pixel to erode a image
  158. void COTSImageProcess::BErodeVertical3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  159. {
  160. WORD x, y, wcounts;
  161. if (rows <= 2 || columns <= 2)return;
  162. // top line
  163. for (x = 0; x < columns; x++)
  164. {
  165. *(target + x) = 0;
  166. }
  167. // bottom line
  168. for (x = 0; x < columns; x++)
  169. {
  170. *(target + (DWORD)(rows - 1)*columns + x) = 0;
  171. }
  172. for (y = 1; y<rows - 1; y++)
  173. {
  174. for (x = 0; x<columns; x++)
  175. {
  176. if (*(source + (DWORD)y*columns + x) == 0)
  177. {
  178. *(target + (DWORD)y*columns + x) = 0;
  179. continue;
  180. }
  181. wcounts = 0;
  182. if (*(source + (DWORD)(y - 1)*columns + x) == 255)
  183. {
  184. wcounts++;
  185. }
  186. if (*(source + (DWORD)(y + 1)*columns + x) == 255)
  187. {
  188. wcounts++;
  189. }
  190. if (wcounts == 2) *(target + (DWORD)y*columns + x) = 255;
  191. else *(target + (DWORD)y*columns + x) = 0;
  192. }
  193. }
  194. }
  195. // use left 45 degree line of 3 pixel to erode a image
  196. void COTSImageProcess::BErodeLeft45Degree3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  197. {
  198. WORD x, y, wcounts;
  199. if (rows <= 2 || columns <= 2)return;
  200. // top line
  201. for (x = 0; x < columns; x++)
  202. {
  203. *(target + x) = 0;
  204. }
  205. // bottom line
  206. for (x = 0; x < columns; x++)
  207. {
  208. *(target + (DWORD)(rows - 1)*columns + x) = 0;
  209. }
  210. // left line
  211. for (y = 0; y<rows; y++)
  212. {
  213. *(target + (DWORD)y*columns) = 0;
  214. }
  215. // right line
  216. for (y = 0; y<rows; y++)
  217. {
  218. *(target + (DWORD)y*columns + columns - 1) = 0;
  219. }
  220. for (y = 1; y < rows - 1; y++)
  221. {
  222. for (x = 1; x < columns - 1; x++)
  223. {
  224. if (*(source + (DWORD)y*columns + x) == 0)
  225. {
  226. *(target + (DWORD)y*columns + x) = 0;
  227. continue;
  228. }
  229. wcounts = 0;
  230. if (*(source + (DWORD)(y - 1)*columns + x - 1) == 255)
  231. {
  232. wcounts++;
  233. }
  234. if (*(source + (DWORD)(y + 1)*columns + x + 1) == 255)
  235. {
  236. wcounts++;
  237. }
  238. if (wcounts == 2) *(target + (DWORD)y*columns + x) = 255;
  239. else *(target + (DWORD)y*columns + x) = 0;
  240. }
  241. }
  242. }
  243. // use horizoontal line of 3 pixel to erode a image
  244. void COTSImageProcess::BErodeHorizontal3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  245. {
  246. WORD x, y, wcounts;
  247. if (rows <= 2 || columns <= 2)return;
  248. // left line
  249. for (y = 0; y<rows; y++)
  250. {
  251. *(target + (DWORD)y*columns) = 0;
  252. }
  253. // right line
  254. for (y = 0; y<rows; y++)
  255. {
  256. *(target + (DWORD)y*columns + columns - 1) = 0;
  257. }
  258. for (y = 0; y<rows; y++)
  259. {
  260. for (x = 1; x<columns - 1; x++)
  261. {
  262. if (*(source + (DWORD)y*columns + x) == 0)
  263. {
  264. *(target + (DWORD)y*columns + x) = 0;
  265. continue;
  266. }
  267. wcounts = 0;
  268. if (*(source + (DWORD)y*columns + x - 1) == 255)
  269. {
  270. wcounts++;
  271. }
  272. if (*(source + (DWORD)y*columns + x + 1) == 255)
  273. {
  274. wcounts++;
  275. }
  276. if (wcounts == 2) *(target + (DWORD)y*columns + x) = 255;
  277. else *(target + (DWORD)y*columns + x) = 0;
  278. }
  279. }
  280. }
  281. // use right 45 degree line of 3 pixel to erode a image
  282. void COTSImageProcess::BErodeRight45Degree3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  283. {
  284. WORD x, y, wcounts;
  285. if (rows <= 2 || columns <= 2)return;
  286. // top line
  287. for (x = 0; x < columns; x++)
  288. {
  289. *(target + x) = 0;
  290. }
  291. // bottom line
  292. for (x = 0; x < columns; x++)
  293. {
  294. *(target + (DWORD)(rows - 1)*columns + x) = 0;
  295. }
  296. // left line
  297. for (y = 0; y<rows; y++)
  298. {
  299. *(target + (DWORD)y*columns) = 0;
  300. }
  301. // right line
  302. for (y = 0; y<rows; y++)
  303. {
  304. *(target + (DWORD)y*columns + columns - 1) = 0;
  305. }
  306. for (y = 1; y<rows - 1; y++)
  307. {
  308. for (x = 1; x<columns - 1; x++)
  309. {
  310. if (*(source + (DWORD)y*columns + x) == 0)
  311. {
  312. *(target + (DWORD)y*columns + x) = 0;
  313. continue;
  314. }
  315. wcounts = 0;
  316. if (*(source + (DWORD)(y - 1)*columns + x + 1) == 255)
  317. {
  318. wcounts++;
  319. }
  320. if (*(source + (DWORD)(y + 1)*columns + x - 1) == 255)
  321. {
  322. wcounts++;
  323. }
  324. if (wcounts == 2) *(target + (DWORD)y*columns + x) = 255;
  325. else *(target + (DWORD)y*columns + x) = 0;
  326. }
  327. }
  328. }
  329. void COTSImageProcess::BDilateVertical3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  330. {
  331. WORD x, y;
  332. if (rows <= 2 || columns <= 2)return;
  333. // top line
  334. for (x = 0; x<columns; x++)
  335. {
  336. if (*(source + x) != 0)
  337. {
  338. *(target + x) = 0xff;
  339. }
  340. }
  341. // bottom line
  342. for (x = 0; x<columns; x++)
  343. {
  344. if (*(source + (DWORD)(rows - 1)*columns + x) != 0)
  345. {
  346. *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
  347. }
  348. }
  349. for (y = 1; y<rows - 1; y++)
  350. {
  351. for (x = 1; x<columns - 1; x++)
  352. {
  353. if (*(source + (DWORD)y*columns + x) != 0)
  354. {
  355. *(target + (DWORD)y*columns + x) = 0xff;
  356. *(target + (DWORD)(y - 1)*columns + x) = 255;
  357. *(target + (DWORD)(y + 1)*columns + x) = 255;
  358. }
  359. else *(target + (DWORD)y*columns + x) = 0;
  360. }
  361. }
  362. }
  363. void COTSImageProcess::BDilateLeft45Degree3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  364. {
  365. WORD x, y;
  366. if (rows <= 2 || columns <= 2)return;
  367. // top line
  368. for (x = 0; x<columns; x++)
  369. {
  370. if (*(source + x) != 0)
  371. {
  372. *(target + x) = 0xff;
  373. }
  374. }
  375. // bottom line
  376. for (x = 0; x<columns; x++)
  377. {
  378. if (*(source + (DWORD)(rows - 1)*columns + x) != 0)
  379. {
  380. *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
  381. }
  382. }
  383. // left line
  384. for (y = 0; y<rows; y++)
  385. {
  386. if (*(source + (DWORD)y*columns) != 0)
  387. {
  388. *(target + (DWORD)y*columns) = 0xff;
  389. }
  390. }
  391. // right line
  392. for (y = 0; y<rows; y++)
  393. {
  394. if (*(source + (DWORD)y*columns + columns - 1) != 0)
  395. {
  396. *(target + (DWORD)y*columns + columns - 1) = 0xff;
  397. }
  398. }
  399. for (y = 1; y<rows - 1; y++)
  400. {
  401. for (x = 1; x<columns - 1; x++)
  402. {
  403. if (*(source + (DWORD)y*columns + x) != 0)
  404. {
  405. *(target + (DWORD)y*columns + x) = 0xff;
  406. *(target + (DWORD)(y - 1)*columns + x - 1) = 255;
  407. *(target + (DWORD)(y + 1)*columns + x + 1) = 255;
  408. }
  409. else *(target + (DWORD)y*columns + x) = 0;
  410. }
  411. }
  412. }
  413. void COTSImageProcess::BDilateHorizontal3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  414. {
  415. WORD x, y;
  416. if (rows <= 2 || columns <= 2)return;
  417. // left line
  418. for (y = 0; y<rows; y++)
  419. {
  420. if (*(source + (DWORD)y*columns) != 0)
  421. {
  422. *(target + (DWORD)y*columns) = 0xff;
  423. }
  424. }
  425. // right line
  426. for (y = 0; y<rows; y++)
  427. {
  428. if (*(source + (DWORD)y*columns + columns - 1) != 0)
  429. {
  430. *(target + (DWORD)y*columns + columns - 1) = 0xff;
  431. }
  432. }
  433. for (y = 1; y<rows - 1; y++)
  434. {
  435. for (x = 1; x<columns - 1; x++)
  436. {
  437. if (*(source + (DWORD)y*columns + x) != 0)
  438. {
  439. *(target + (DWORD)y*columns + x) = 0xff;
  440. *(target + (DWORD)y*columns + x - 1) = 255;
  441. *(target + (DWORD)y*columns + x + 1) = 255;
  442. }
  443. else *(target + (DWORD)y*columns + x) = 0;
  444. }
  445. }
  446. }
  447. void COTSImageProcess::BDilateRight45Degree3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  448. {
  449. WORD x, y;
  450. if (rows <= 2 || columns <= 2)return;
  451. // top line
  452. for (x = 0; x<columns; x++)
  453. {
  454. if (*(source + x) != 0)
  455. {
  456. *(target + x) = 0xff;
  457. }
  458. }
  459. // bottom line
  460. for (x = 0; x<columns; x++)
  461. {
  462. if (*(source + (DWORD)(rows - 1)*columns + x) != 0)
  463. {
  464. *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
  465. }
  466. }
  467. // left line
  468. for (y = 0; y<rows; y++)
  469. {
  470. if (*(source + (DWORD)y*columns) != 0)
  471. {
  472. *(target + (DWORD)y*columns) = 0xff;
  473. }
  474. }
  475. // right line
  476. for (y = 0; y<rows; y++)
  477. {
  478. if (*(source + (DWORD)y*columns + columns - 1) != 0)
  479. {
  480. *(target + (DWORD)y*columns + columns - 1) = 0xff;
  481. }
  482. }
  483. for (y = 1; y<rows - 1; y++)
  484. {
  485. for (x = 1; x<columns - 1; x++)
  486. {
  487. if (*(source + (DWORD)y*columns + x) != 0)
  488. {
  489. *(target + (DWORD)y*columns + x) = 0xff;
  490. *(target + (DWORD)(y - 1)*columns + x + 1) = 255;
  491. *(target + (DWORD)(y + 1)*columns + x - 1) = 255;
  492. }
  493. else *(target + (DWORD)y*columns + x) = 0;
  494. }
  495. }
  496. }
  497. void COTSImageProcess::BErode3(LPBYTE source, LPBYTE target, WORD wDegree, WORD rows, WORD columns)
  498. {
  499. WORD x, y, i, j, wcounts;
  500. if (rows == 1 || columns == 1)return;
  501. for (y = 1; y<rows - 1; y++)
  502. {
  503. for (x = 1; x<columns - 1; x++)
  504. {
  505. if (*(source + (DWORD)y*columns + x) == 0)
  506. {
  507. *(target + (DWORD)y*columns + x) = 0;
  508. continue;
  509. }
  510. wcounts = 0;
  511. for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
  512. {
  513. for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
  514. {
  515. if (*(source + (DWORD)i*columns + j) == 0)
  516. {
  517. wcounts++;
  518. }
  519. }
  520. }
  521. if (wcounts >= wDegree) *(target + (DWORD)y*columns + x) = 0;
  522. else *(target + (DWORD)y*columns + x) = 0xff;
  523. }
  524. }
  525. // top line
  526. for (x = 1; x<columns - (WORD)1; x++)
  527. {
  528. if (*(source + x) == 0)
  529. {
  530. *(target + x) = 0;
  531. continue;
  532. }
  533. wcounts = 0;
  534. for (i = 0; i <= 1; i++)
  535. {
  536. for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
  537. {
  538. if (*(source + (DWORD)i*columns + j) == 0) wcounts++;
  539. }
  540. }
  541. if (wcounts >= wDegree * 5 / 8) *(target + x) = 0;
  542. else *(target + x) = 0xff;
  543. }
  544. // bottom line
  545. for (x = 1; x<columns - 1; x++)
  546. {
  547. if (*(source + (DWORD)(rows - 1)*columns + x) == 0)
  548. {
  549. *(target + (DWORD)(rows - 1)*columns + x) = 0;
  550. continue;
  551. }
  552. wcounts = 0;
  553. for (i = (WORD)(rows - 2); i <= (WORD)(rows - 1); i++)
  554. {
  555. for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
  556. {
  557. if (*(source + (DWORD)i*columns + j) == 0) wcounts++;
  558. }
  559. }
  560. if (wcounts >= wDegree * 5 / 8) *(target + (DWORD)(rows - 1)*columns + x) = 0;
  561. else *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
  562. }
  563. // left line
  564. for (y = 1; y<rows - 1; y++)
  565. {
  566. if (*(source + (DWORD)y*columns) == 0)
  567. {
  568. *(target + (DWORD)y*columns) = 0;
  569. continue;
  570. }
  571. wcounts = 0;
  572. for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
  573. {
  574. for (j = 0; j <= 1; j++)
  575. {
  576. if (*(source + (DWORD)i*columns + j) == 0) wcounts++;
  577. }
  578. }
  579. if (wcounts >= wDegree * 5 / 8) *(target + (DWORD)y*columns) = 0;
  580. else *(target + (DWORD)y*columns) = 0xff;
  581. }
  582. // right line
  583. for (y = 1; y<rows - 1; y++)
  584. {
  585. if (*(source + (DWORD)y*columns + columns - 1) == 0)
  586. {
  587. *(target + (DWORD)y*columns + columns - 1) = 0;
  588. continue;
  589. }
  590. wcounts = 0;
  591. for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
  592. {
  593. for (j = (WORD)(columns - 2); j <= (WORD)(columns - 1); j++)
  594. {
  595. if (*(source + (DWORD)i*columns + j) == 0) wcounts++;
  596. }
  597. }
  598. if (wcounts >= wDegree * 5 / 8) *(target + (DWORD)y*columns + columns - 1) = 0;
  599. else *(target + (DWORD)y*columns + columns - 1) = 0xff;
  600. }
  601. return;
  602. }
  603. void COTSImageProcess::BDilate3(LPBYTE source, LPBYTE target, WORD wDegree, WORD rows, WORD columns)
  604. {
  605. WORD x, y, i, j, wcounts;
  606. for (y = 1; y<rows - 1; y++)
  607. {
  608. for (x = 1; x<columns - 1; x++)
  609. {
  610. if (*(source + (DWORD)y*columns + x) != 0)
  611. {
  612. *(target + (DWORD)y*columns + x) = 0xff;
  613. continue;
  614. }
  615. wcounts = 0;
  616. for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
  617. {
  618. for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
  619. {
  620. if (*(source + (DWORD)i*columns + j) != 0) wcounts++;
  621. }
  622. }
  623. if (wcounts >= wDegree) *(target + (DWORD)y*columns + x) = 0xff;
  624. else *(target + (DWORD)y*columns + x) = 0;
  625. }
  626. }
  627. // top line
  628. for (x = 1; x<columns - 1; x++)
  629. {
  630. if (*(source + x) != 0)
  631. {
  632. *(target + x) = 0xff;
  633. continue;
  634. }
  635. wcounts = 0;
  636. for (i = 0; i <= 1; i++)
  637. {
  638. for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
  639. {
  640. if (*(source + (DWORD)i*columns + j) != 0) wcounts++;
  641. }
  642. }
  643. if (wcounts >= wDegree * 5 / 8) // but does not mater, as we have border of 2 now
  644. {
  645. *(target + x) = 0xff;
  646. }
  647. else { *(target + x) = 0; }
  648. }
  649. // bottom line
  650. for (x = 1; x<columns - 1; x++)
  651. {
  652. if (*(source + (DWORD)(rows - 1)*columns + x) != 0)
  653. {
  654. *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
  655. continue;
  656. }
  657. wcounts = 0;
  658. for (i = (WORD)(rows - 2); i <= (WORD)(rows - 1); i++)
  659. {
  660. for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
  661. {
  662. if (*(source + (DWORD)i*columns + j) != 0) wcounts++;
  663. }
  664. }
  665. if (wcounts > wDegree * 5 / 8)
  666. {
  667. *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
  668. }
  669. else
  670. {
  671. *(target + (DWORD)(rows - 1)*columns + x) = 0;
  672. }
  673. }
  674. // left line
  675. for (y = 1; y<rows - 1; y++)
  676. {
  677. if (*(source + (DWORD)y*columns) != 0)
  678. {
  679. *(target + (DWORD)y*columns) = 0xff;
  680. continue;
  681. }
  682. wcounts = 0;
  683. for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
  684. {
  685. for (j = 0; j <= (WORD)1; j++)
  686. {
  687. if (*(source + (DWORD)i*columns + j) != 0) wcounts++;
  688. }
  689. }
  690. if (wcounts >= wDegree * 5 / 8)
  691. {
  692. *(target + (DWORD)y*columns) = 0xff;
  693. }
  694. else
  695. {
  696. *(target + (DWORD)y*columns) = 0;
  697. }
  698. }
  699. // right line
  700. for (y = 1; y<rows - 1; y++)
  701. {
  702. if (*(source + (DWORD)y*columns + columns - 1) != 0)
  703. {
  704. *(target + (DWORD)y*columns + columns - 1) = 0xff;
  705. continue;
  706. }
  707. wcounts = 0;
  708. for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
  709. {
  710. for (j = (WORD)(columns - 2); j <= (WORD)(columns - 1); j++)
  711. {
  712. if (*(source + (DWORD)i*columns + j) != 0) wcounts++;
  713. }
  714. }
  715. if (wcounts >= wDegree * 5 / 8)
  716. {
  717. *(target + (DWORD)y*columns + columns - 1) = 0xff;
  718. }
  719. else
  720. {
  721. *(target + (DWORD)y*columns + columns - 1) = 0;
  722. }
  723. }
  724. // four cornor points treated separately here
  725. // top-left
  726. if (*(source) != 0)
  727. {
  728. *target = 0xff;
  729. }
  730. else
  731. {
  732. wcounts = 0;
  733. if (*(source + 1) != 0) wcounts++;
  734. if (*(source + columns) != 0) wcounts++;
  735. if (*(source + columns + 1) != 0) wcounts++;
  736. // if (wcounts >= wDegree*3/8) // this is a bug here - interger division
  737. if (wcounts * 8 >= wDegree * 3)
  738. {
  739. *target = 0xff;
  740. }
  741. else
  742. {
  743. *target = 0;
  744. }
  745. }
  746. //top-right
  747. if (*(source + columns - 1) != 0)
  748. {
  749. *(target + columns - 1) = 0xff;
  750. }
  751. else
  752. {
  753. wcounts = 0;
  754. if (*(source + columns - 2) != 0) wcounts++;
  755. if (*(source + columns * 2 - 1) != 0) wcounts++;
  756. if (*(source + columns * 2 - 2) != 0) wcounts++;
  757. // if (wcounts >= wDegree*3/8) // this is a bug here - interger division
  758. if (wcounts * 8 >= wDegree * 3)
  759. {
  760. *(target + columns - 1) = 0xff;
  761. }
  762. else
  763. {
  764. *(target + columns - 1) = 0;
  765. }
  766. }
  767. //bottom-left
  768. if (*(source + (DWORD)columns * (rows - 1)) != 0)
  769. {
  770. *(target + (DWORD)columns * (rows - 1)) = 0xff;
  771. }
  772. else
  773. {
  774. wcounts = 0;
  775. if (*(source + (DWORD)columns * (rows - 1) + 1) != 0) wcounts++;
  776. if (*(source + (DWORD)columns * (rows - 2)) != 0) wcounts++;
  777. if (*(source + (DWORD)columns * (rows - 2) + 1) != 0) wcounts++;
  778. // if (wcounts >= wDegree*3/8) // this is a bug here - interger division
  779. if (wcounts * 8 >= wDegree * 3)
  780. {
  781. *(target + (DWORD)columns * (rows - 1)) = 0xff;
  782. }
  783. else
  784. {
  785. *(target + (DWORD)columns * (rows - 1)) = 0;
  786. }
  787. }
  788. //bottom-right
  789. if (*(source + (DWORD)columns * rows - 1) != 0)
  790. {
  791. *(target + (DWORD)columns * rows - 1) = 0xff;
  792. }
  793. else
  794. {
  795. wcounts = 0;
  796. if (*(source + (DWORD)columns * rows - 2) != 0) wcounts++;
  797. if (*(source + (DWORD)columns * (rows - 1) - 2) != 0) wcounts++;
  798. if (*(source + (DWORD)columns * (rows - 1) - 1) != 0) wcounts++;
  799. // if (wcounts >= wDegree*3/8) // this is a bug here - interger division
  800. if (wcounts * 8 >= wDegree * 3)
  801. {
  802. *(target + (DWORD)columns * rows - 1) = 0xff;
  803. }
  804. else
  805. {
  806. *(target + (DWORD)columns * rows - 1) = 0;
  807. }
  808. }
  809. return;
  810. }
  811. // ReZoom the picture with re-magnification
  812. BOOL COTSImageProcess::ReZoom(CString InPutPath, CString OutPutPath)
  813. {
  814. Mat cvSrcImg;
  815. string strInputPath;
  816. strInputPath = CStringA(InPutPath);
  817. // Pictures loop in folder
  818. std::vector<cv::String> ImageFolder;
  819. cv::glob(strInputPath, ImageFolder);
  820. if (ImageFolder.size() == 0)
  821. {
  822. return FALSE;
  823. }
  824. for (unsigned int nImgNum = 0; nImgNum < ImageFolder.size(); ++nImgNum) {
  825. cvSrcImg = cv::imread(ImageFolder[nImgNum], CV_LOAD_IMAGE_GRAYSCALE);
  826. // Image convolution operation
  827. //// convolution kernel
  828. float kernel[] = { -1, -1 , -1, -1 , 0, -1, -1 , -1 , -1 };
  829. cv::Mat ker = cv::Mat(nImage_Size, nImage_Size, CV_32F, &kernel);
  830. cv::Mat cvDstImg = cv::Mat(cvSrcImg.size(), cvSrcImg.type());
  831. // anchor of the kernel
  832. cv::Point anchor(-1, -1);
  833. cv::filter2D(cvSrcImg, cvDstImg, CV_32F, ker, anchor, delta, cv::THRESH_TRUNC);
  834. // Maximum Pixel Value
  835. cvDstImg = abs(cvDstImg);
  836. double minVal, maxVal;
  837. minMaxLoc(cvDstImg, &minVal, &maxVal);
  838. // Grayscale image
  839. int nReduce;
  840. Mat onesImg = Mat::ones(cvDstImg.rows, cvDstImg.cols, CV_32F) * (int)minVal;
  841. absdiff(cvDstImg, onesImg, cvDstImg);
  842. nReduce = (int)maxVal - minVal;
  843. cvDstImg = cvDstImg * nBlackColor / nReduce;
  844. // Output image convert data to int
  845. cvDstImg.convertTo(cvDstImg, CV_8U);
  846. // Process the picture to 128 pixels
  847. resize(cvDstImg, cvDstImg, Size(nPictureSize, nPictureSize));
  848. threshold(cvDstImg, cvDstImg, nProcessParam, nBlackColor, CV_THRESH_BINARY);
  849. string strOutPutPath;
  850. strOutPutPath = CStringA(OutPutPath);
  851. imwrite(strOutPutPath , cvDstImg);
  852. }
  853. return TRUE;
  854. }
  855. BOOL COTSImageProcess::RemoveBSEImageBG(CBSEImgPtr m_pBSEImg, COTSImageProcessParamPtr a_pImgProcessParam,COTSFieldDataPtr m_pFieldData)
  856. {
  857. ASSERT(m_pFieldData);
  858. if (!m_pFieldData)
  859. {
  860. LogErrorTrace(__FILE__, __LINE__, _T("RemoveBSEImageBG: there is no field data"));
  861. return FALSE;
  862. }
  863. ASSERT(m_pBSEImg);
  864. if (!m_pBSEImg)
  865. {
  866. LogErrorTrace(__FILE__, __LINE__, _T("RemoveBSEImageBG: there is no image data"));
  867. return FALSE;
  868. }
  869. ASSERT(a_pImgProcessParam);
  870. if (!a_pImgProcessParam)
  871. {
  872. LogErrorTrace(__FILE__, __LINE__, _T("RemoveBSEImageBG: there is no image process data"));
  873. return FALSE;
  874. }
  875. int nWidthImg = m_pBSEImg->GetWidth();
  876. int nHeightImg = m_pBSEImg->GetHeight();
  877. m_pFieldData->Width = nWidthImg;
  878. m_pFieldData->Height = nHeightImg;
  879. long nImgSize = nWidthImg * nHeightImg;
  880. BYTE* pSrcImg = m_pBSEImg->GetImageDataPointer();
  881. BYTE* pTempImg = new BYTE[nImgSize];
  882. CBSEImgPtr imgNoBGBinary = CBSEImgPtr(new CBSEImg());
  883. long nNumParticle = 0;
  884. RemoveBackGround(m_pBSEImg, a_pImgProcessParam, imgNoBGBinary,nNumParticle);
  885. BYTE* pPixel = imgNoBGBinary->GetImageDataPointer();
  886. long nPtStart = a_pImgProcessParam->GetParticleGray().GetStart();
  887. long nPtEnd = a_pImgProcessParam->GetParticleGray().GetEnd();
  888. if (nNumParticle == 0)
  889. {
  890. COTSParticleList listParticleEmpty;
  891. listParticleEmpty.clear();
  892. m_pFieldData->SetParticleList(listParticleEmpty);
  893. //memset(pPixel, 0, nImgSize);
  894. LogInfoTrace(__FILE__, __LINE__, _T("RemoveBSEImageBG: no particle is found."));
  895. }
  896. else
  897. {
  898. //memcpy(pTempImg, pPixel, nImgSize);
  899. // get the area image
  900. COTSImageProcess::BErode3(pPixel, pTempImg, 5, nHeightImg, nWidthImg);
  901. COTSImageProcess::BDilate3(pTempImg, pPixel, 5, nHeightImg, nWidthImg);
  902. COTSParticleList listParticleOut;
  903. if (!GetParticles(nWidthImg, nHeightImg, pPixel, listParticleOut))
  904. {
  905. COTSParticleList listParticleEmpty;
  906. listParticleEmpty.clear();
  907. m_pFieldData->SetParticleList(listParticleEmpty);
  908. //memset(pPixel, 0, nImgSize);
  909. }
  910. // form a image only have particles on
  911. COTSSegmentsList listImage;
  912. for (auto pParticle : listParticleOut)
  913. {
  914. COTSFeaturePtr pFeature = pParticle->GetFeature();
  915. COTSSegmentsList listSegment = pFeature->GetSegmentsList();
  916. long nPixelNum = 0;
  917. long nPixelAll = 0;
  918. int nStartS = 0;
  919. int nHeightS = 0;
  920. int nLengthS = 0;
  921. for (auto pSegment : listSegment)
  922. {
  923. // update image list
  924. COTSSegmentPtr pSegNew = COTSSegmentPtr(new COTSSegment(*pSegment.get()));
  925. listImage.push_back(pSegNew);
  926. // get particle average gray
  927. nStartS = pSegment->GetStart();
  928. nHeightS = pSegment->GetHeight();
  929. nLengthS = pSegment->GetLength();
  930. nPixelNum += (long)nLengthS;
  931. if (nHeightS > nHeightImg)
  932. {
  933. LogErrorTrace(__FILE__, __LINE__, _T("seg height is wrong."));
  934. return FALSE;
  935. }
  936. if ((nStartS + nLengthS - 1) > nWidthImg)
  937. {
  938. LogErrorTrace(__FILE__, __LINE__, _T("seg starst and length is wrong."));
  939. return FALSE;
  940. }
  941. for (unsigned int i = 0; i < nLengthS; i++)
  942. {
  943. if ((nStartS + i) > nWidthImg)
  944. {
  945. LogErrorTrace(__FILE__, __LINE__, _T("seg start is wrong."));
  946. return FALSE;
  947. }
  948. else if (nHeightS > nHeightImg)
  949. {
  950. LogErrorTrace(__FILE__, __LINE__, _T("seg height is wrong."));
  951. return FALSE;
  952. }
  953. long nValueTemp = (long)*(pSrcImg + nHeightS * nWidthImg + nStartS + i);
  954. nPixelAll += nValueTemp;
  955. }
  956. }
  957. BYTE nAveGray = (BYTE)(nPixelAll / nPixelNum);
  958. pParticle->SetAveGray(nAveGray);
  959. pParticle->SetArea(nPixelNum);
  960. }
  961. m_pFieldData->SetParticleList(listParticleOut);
  962. }
  963. delete[]pTempImg;
  964. //delete[]pTempImg;
  965. return TRUE;
  966. }
  967. CIntRangePtr COTSImageProcess::CalBackground(CBSEImgPtr m_pBSEImg)
  968. {
  969. CIntRangePtr pBackground = CIntRangePtr(new CIntRange());
  970. WORD nBSEChart[MAXBYTE];
  971. //1. get chart data
  972. m_pBSEImg->SetChartData();
  973. linearSmooth5(m_pBSEImg->GetBSEChart(), nBSEChart,MAXBYTE);
  974. //2. get down edge
  975. int nLengthEdge = MAXBYTE + 2;
  976. WORD n_aBSEChart[MAXBYTE + 2];
  977. memset(n_aBSEChart, 0, sizeof(WORD) * nLengthEdge);
  978. std::map<long, std::vector <int>> upEdgeSeries;
  979. std::map<long, std::vector<int>> downEdgeSeries;
  980. std::vector<int> currentUpSeries;
  981. std::vector<int> currentDownSeries;
  982. // make sure the wave begin with up edge and end with down edge
  983. n_aBSEChart[0] = 0;
  984. n_aBSEChart[nLengthEdge - 1] = 0;
  985. memcpy(&n_aBSEChart[1], &nBSEChart, sizeof(WORD) * MAXBYTE);
  986. int nLengthCom = MAXBYTE + 1;
  987. // up edge
  988. for (int i = 0; i < nLengthCom; i++)
  989. {
  990. if (n_aBSEChart[i] <= n_aBSEChart[i + 1])
  991. {
  992. if (currentDownSeries.size() > 0)
  993. {
  994. int seriesSize = currentDownSeries.size();
  995. long area = 0;
  996. for (int i = 0; i < seriesSize; i++)
  997. {
  998. area = area + n_aBSEChart[currentDownSeries[i]];
  999. }
  1000. downEdgeSeries[area] = currentDownSeries;
  1001. currentDownSeries.clear();
  1002. }
  1003. currentUpSeries.push_back(i);
  1004. }
  1005. else
  1006. {
  1007. if (currentUpSeries.size() > 0)
  1008. {
  1009. int seriesSize = currentUpSeries.size();
  1010. long area = 0;
  1011. for (int i = 0; i < seriesSize; i++)
  1012. {
  1013. area = area + n_aBSEChart[currentUpSeries[i]];
  1014. }
  1015. upEdgeSeries[area] = currentUpSeries;
  1016. currentUpSeries.clear();
  1017. }
  1018. currentDownSeries.push_back(i);
  1019. }
  1020. }
  1021. CIntRangePtr pRangeFirst = CIntRangePtr(new CIntRange());
  1022. if (upEdgeSeries.size() > 0)
  1023. {
  1024. pRangeFirst->SetStart((upEdgeSeries.rbegin())->second[0]);
  1025. }
  1026. if (downEdgeSeries.size() > 0)
  1027. {
  1028. auto s = downEdgeSeries.rbegin()->second;
  1029. pRangeFirst->SetEnd((long)(s[s.size() - 1]));
  1030. }
  1031. /*delete[] nBSEChart;
  1032. delete[] n_aBSEChart;*/
  1033. return pRangeFirst;
  1034. }
  1035. void COTSImageProcess::RemoveBackGround(CBSEImgPtr a_pImgIn, COTSImageProcessParamPtr a_pImageProcessParam, CBSEImgPtr a_pBinImgOut,long& foundedPixelNum)
  1036. {
  1037. // the background pixel will be 0,and the other part will be 255.
  1038. ASSERT(a_pImgIn);
  1039. ASSERT(a_pImageProcessParam);
  1040. int nWidthImg = a_pImgIn->GetWidth();
  1041. int nHeightImg = a_pImgIn->GetHeight();
  1042. long nImgSize = nWidthImg * nHeightImg;
  1043. BYTE* pTempImg = new BYTE[nImgSize];
  1044. BYTE* pTempImg2 = new BYTE[nImgSize];
  1045. BYTE* pSrcImg = a_pImgIn->GetImageDataPointer();
  1046. //memcpy(pTempImg2, pSrcImg, nImgSize);
  1047. //cv::blur(pSrcImg, pTempImg2, nImgSize);
  1048. BYTE* pPixel = new byte[nImgSize];
  1049. long nBGStart;
  1050. long nBGEnd;
  1051. long nNumParticle = 0;
  1052. if (a_pImageProcessParam->GetBGRemoveType() == OTS_BGREMOVE_TYPE::MANUAL)
  1053. {
  1054. nBGStart = a_pImageProcessParam->GetBGGray().GetStart();
  1055. nBGEnd = a_pImageProcessParam->GetBGGray().GetEnd();
  1056. // delete background
  1057. for (unsigned int i = 0; i < nImgSize; i++)
  1058. {
  1059. if (pTempImg2[i] >= nBGStart && pTempImg2[i] <= nBGEnd)
  1060. {
  1061. pPixel[i] = 0;
  1062. }
  1063. else
  1064. {
  1065. pPixel[i] = 255;
  1066. nNumParticle++;
  1067. }
  1068. }
  1069. COTSImageProcess::BErode3(pPixel, pTempImg, 5, nHeightImg, nWidthImg);
  1070. COTSImageProcess::BDilate3(pTempImg, pPixel, 5, nHeightImg, nWidthImg);
  1071. }
  1072. else
  1073. {
  1074. /*CBSEImgPtr srcImg2 = CBSEImgPtr(new CBSEImg());
  1075. srcImg2->SetImageData(a_pImgIn, nWidthImg, nHeightImg);*/
  1076. auto range = CalBackground(a_pImgIn);
  1077. nBGStart = range->GetStart();
  1078. nBGEnd = range->GetEnd();
  1079. switch (a_pImageProcessParam->GetAutoBGRemoveType())
  1080. {
  1081. case OTS_AUTOBGREMOVE_TYPE::DOWNWARD:
  1082. for (unsigned int i = 0; i < nImgSize; i++)
  1083. {
  1084. if (pTempImg2[i] <= nBGEnd)
  1085. {
  1086. pPixel[i] = 0;
  1087. }
  1088. else
  1089. {
  1090. pPixel[i] = 255;
  1091. nNumParticle++;
  1092. }
  1093. }
  1094. break;
  1095. case OTS_AUTOBGREMOVE_TYPE::UPWARD:
  1096. for (unsigned int i = 0; i < nImgSize; i++)
  1097. {
  1098. if (pTempImg2[i] >= nBGStart)
  1099. {
  1100. pPixel[i] = 0;
  1101. }
  1102. else
  1103. {
  1104. pPixel[i] = 255;
  1105. nNumParticle++;
  1106. }
  1107. }
  1108. break;
  1109. case OTS_AUTOBGREMOVE_TYPE::MIDDLE:
  1110. for (unsigned int i = 0; i < nImgSize; i++)
  1111. {
  1112. if (pTempImg2[i] >= nBGStart && pTempImg2[i] <= nBGEnd)
  1113. {
  1114. pPixel[i] = 0;
  1115. }
  1116. else
  1117. {
  1118. pPixel[i] = 255;
  1119. nNumParticle++;
  1120. }
  1121. }
  1122. break;
  1123. default:
  1124. break;
  1125. }
  1126. COTSImageProcess::BErode3(pPixel, pTempImg, 5, nHeightImg, nWidthImg);
  1127. COTSImageProcess::BDilate3(pTempImg, pPixel, 5, nHeightImg, nWidthImg);
  1128. }
  1129. a_pBinImgOut->SetImageData(pPixel,nWidthImg,nHeightImg);
  1130. foundedPixelNum = nNumParticle;
  1131. delete[] pTempImg;
  1132. delete[] pTempImg2;
  1133. return ;
  1134. }
  1135. BOOL COTSImageProcess::GetParticles(long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSParticleList& a_listParticles)
  1136. {
  1137. ASSERT(a_pPixel);
  1138. if (!a_pPixel)
  1139. {
  1140. LogErrorTrace(__FILE__, __LINE__, _T("GetParticles: there is no image data"));
  1141. return FALSE;
  1142. }
  1143. a_listParticles.clear();
  1144. COTSSegmentsList listSegment;
  1145. listSegment.clear();
  1146. //1. get segment line by line
  1147. if (!GetSegmentList(a_nWidth, a_nHeight, a_pPixel, listSegment))
  1148. {
  1149. LogErrorTrace(__FILE__, __LINE__, _T("GetParticles:failed to get segments."));
  1150. return FALSE;
  1151. }
  1152. if ((int)listSegment.size() == 0)
  1153. {
  1154. LogErrorTrace(__FILE__, __LINE__, _T("no particle is found."));
  1155. return FALSE;
  1156. }
  1157. //2. save the temp feature
  1158. COTSFeatureList listFeature;
  1159. listFeature.clear();
  1160. if (!GetFeatureList1(listSegment, listFeature))
  1161. {
  1162. LogErrorTrace(__FILE__, __LINE__, _T("GetParticles:failed to get up down segment list."));
  1163. return FALSE;
  1164. }
  1165. if ((int)listFeature.size() == 0)
  1166. {
  1167. LogErrorTrace(__FILE__, __LINE__, _T("no particle is found."));
  1168. return FALSE;
  1169. }
  1170. COTSParticleList listParticles;
  1171. listParticles.clear();
  1172. if (!ChangeFeaturelist(listFeature, a_listParticles))
  1173. {
  1174. LogErrorTrace(__FILE__, __LINE__, _T("can't change feature to particle."));
  1175. return FALSE;
  1176. }
  1177. return TRUE;
  1178. }
  1179. BOOL COTSImageProcess::GetSegmentList(long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSSegmentsList& a_listSegments)
  1180. {
  1181. ASSERT(a_pPixel);
  1182. if (!a_pPixel)
  1183. {
  1184. LogErrorTrace(__FILE__, __LINE__, _T("GetSegments: there is no image data"));
  1185. return FALSE;
  1186. }
  1187. long nImgSize = a_nWidth * a_nHeight;
  1188. a_listSegments.clear();
  1189. //1. get segment line by line
  1190. long nLine, nm, nn;
  1191. long nStart, nLength;
  1192. for (nLine = 0; nLine < a_nHeight; nLine++)
  1193. {
  1194. for (nm = 0; nm < a_nWidth; nm += (nLength + 1))
  1195. {
  1196. nLength = 0;
  1197. // get start
  1198. if (*(a_pPixel + nLine * a_nWidth + nm) != 0)
  1199. {
  1200. nStart = nm;
  1201. nLength++;
  1202. //get length
  1203. for (nn = nm + 1; nn < a_nWidth; nn++)
  1204. {
  1205. // check if segment is over, break
  1206. if (nLength != 0)
  1207. {
  1208. if (*(a_pPixel + nLine * a_nWidth + nn) == 0)
  1209. break;
  1210. }
  1211. if (*(a_pPixel + nLine * a_nWidth + nn) != 0)
  1212. {
  1213. nLength++;
  1214. }
  1215. }
  1216. // generate segment
  1217. COTSSegmentPtr pSegment = COTSSegmentPtr(new COTSSegment(nLine, nStart, nLength));
  1218. a_listSegments.push_back(pSegment);
  1219. }
  1220. else
  1221. {
  1222. continue;
  1223. }
  1224. }
  1225. }
  1226. if ((int)a_listSegments.size() == 0)
  1227. {
  1228. LogErrorTrace(__FILE__, __LINE__, _T("no particle is found."));
  1229. return FALSE;
  1230. }
  1231. return TRUE;
  1232. }
  1233. BOOL COTSImageProcess::GetFeatureList1(COTSSegmentsList& a_listSegments, COTSFeatureList& a_listFeatures)
  1234. {
  1235. COTSSegmentsList listSegmentNew;
  1236. std::map<long, COTSSegmentsList > mapOneLineSegments;
  1237. for each (auto s in a_listSegments)
  1238. {
  1239. mapOneLineSegments[s->GetHeight()].push_back(s);//sorting all the segments base on the line number.
  1240. }
  1241. std::map<long, COTSSegmentsList >::iterator lineItr = mapOneLineSegments.begin();//find the highest line
  1242. while (lineItr != mapOneLineSegments.end())
  1243. {
  1244. for (auto s = lineItr->second.begin(); s < lineItr->second.end(); )//find one segment of this line.
  1245. {
  1246. COTSSegmentPtr bottomSeg = *s;
  1247. listSegmentNew.clear();
  1248. listSegmentNew.push_back(*s);
  1249. s = lineItr->second.erase(s);
  1250. std::map<long, COTSSegmentsList >::iterator tempItr = lineItr;
  1251. tempItr++;
  1252. for (; tempItr != mapOneLineSegments.end(); tempItr++)//find all other lines of segments
  1253. {
  1254. if (tempItr->first - bottomSeg->GetHeight() > 1)
  1255. {
  1256. break;
  1257. }
  1258. for (auto nextLineSegment = tempItr->second.begin(); nextLineSegment < tempItr->second.end();)//find next line's all segments
  1259. {
  1260. if (((*nextLineSegment)->GetStart() - (bottomSeg->GetStart() + bottomSeg->GetLength())) > 1)
  1261. {
  1262. break;
  1263. }
  1264. if (bottomSeg->UpDownConection(**nextLineSegment))
  1265. {
  1266. listSegmentNew.push_back(*nextLineSegment);
  1267. bottomSeg = *nextLineSegment;
  1268. nextLineSegment = tempItr->second.erase(nextLineSegment);
  1269. break;
  1270. }
  1271. if (tempItr->second.size() > 0)
  1272. {
  1273. nextLineSegment++;
  1274. }
  1275. else
  1276. {
  1277. break;
  1278. }
  1279. }
  1280. }
  1281. COTSFeaturePtr pFeature = COTSFeaturePtr(new COTSFeature());
  1282. pFeature->SetSegmentsList(listSegmentNew);
  1283. //check if this new feature is connected with other found feature.
  1284. COTSSegmentPtr topSeg = listSegmentNew[0];//find the toppest segment of this new feature.
  1285. COTSSegmentPtr bottomSegment = listSegmentNew[listSegmentNew.size() - 1];//find the lowest segment of this new feature.
  1286. bool haveMerged = false;
  1287. for each (auto f in a_listFeatures)
  1288. {
  1289. for (auto seg : f->GetSegmentsList())
  1290. {
  1291. if (bottomSegment->UpDownConection(*seg) || topSeg->UpDownConection(*seg))
  1292. {
  1293. COTSSegmentsList segs = f->GetSegmentsList();
  1294. for (auto s : listSegmentNew)
  1295. {
  1296. segs.push_back(s);
  1297. }
  1298. f->SetSegmentsList(segs);
  1299. haveMerged = true;
  1300. break;
  1301. }
  1302. }
  1303. if (haveMerged)
  1304. {
  1305. break;
  1306. }
  1307. }
  1308. if (!haveMerged)
  1309. {
  1310. a_listFeatures.push_back(pFeature);
  1311. }
  1312. if (lineItr->second.size() == 0)
  1313. {
  1314. break;
  1315. }
  1316. }
  1317. lineItr++;
  1318. }
  1319. return true;
  1320. }
  1321. BOOL COTSImageProcess::ChangeFeaturelist(COTSFeatureList& a_listFeatures, COTSParticleList& a_listParticle)
  1322. {
  1323. if (a_listFeatures.size() == 0)
  1324. {
  1325. LogErrorTrace(__FILE__, __LINE__, _T("ChangeFeaturelist: there is no feature in the list."));
  1326. return FALSE;
  1327. }
  1328. // compute Rect
  1329. for (auto pFeature : a_listFeatures)
  1330. {
  1331. COTSParticlePtr pParticle = COTSParticlePtr(new COTSParticle());
  1332. pParticle->SetFeature(pFeature);
  1333. if (!pParticle->CalCoverRect())
  1334. {
  1335. LogErrorTrace(__FILE__, __LINE__, _T("ChangeFeaturelist: failed to get particle rect."));
  1336. return FALSE;
  1337. }
  1338. a_listParticle.push_back(pParticle);
  1339. }
  1340. if ((int)a_listParticle.size() == 0)
  1341. {
  1342. LogErrorTrace(__FILE__, __LINE__, _T("Can't get particle."));
  1343. return FALSE;
  1344. }
  1345. return TRUE;
  1346. }
  1347. BOOL COTSImageProcess::CalcuParticleImagePropertes(COTSParticlePtr a_pOTSPart, double a_PixelSize)
  1348. {
  1349. //--------- convert this particle data to image data,construct an image only with this particle.------
  1350. const int nExpand_Size = 3;
  1351. const int nWhiteColor = 0;
  1352. const int nThickness = 1;
  1353. // lineType Type of the line
  1354. const int nLineType = 8;
  1355. // get rectangle of the particle
  1356. CRect rect = a_pOTSPart->GetParticleRect();
  1357. if (a_pOTSPart->GetArea() < 80 * a_PixelSize)// the particle is too small that openCV can't calculate a width value of it. Then we take the upright rect of the particle as it's minArea rect.
  1358. {
  1359. double w = 0, h = 0;
  1360. w = (double)rect.Width()*a_PixelSize;
  1361. h = (double)rect.Height()*a_PixelSize;
  1362. a_pOTSPart->SetDMax(MAX(w, h));
  1363. a_pOTSPart->SetDMin(MIN(w, h));
  1364. a_pOTSPart->SetDMean((w + h) / 2);
  1365. a_pOTSPart->SetFeretDiameter((w + h) / 2);
  1366. a_pOTSPart->SetDElong(MAX(w, h));
  1367. a_pOTSPart->SetPerimeter((w+h)*2);
  1368. a_pOTSPart->SetDPerp(MIN(w, h));
  1369. a_pOTSPart->SetDInscr(MIN(w, h));
  1370. return true;
  1371. }
  1372. // calculate the particle image data size, expand 3 pixel at the edge
  1373. Mat particleImage = Mat::zeros(rect.Height() + nExpand_Size , rect.Width() + nExpand_Size , CV_8U);
  1374. // get the segment list
  1375. COTSSegmentsList listSegment = a_pOTSPart->GetFeature()->GetSegmentsList();
  1376. for (auto pSegment : listSegment)
  1377. {
  1378. int nStart = pSegment->GetStart() - rect.left + nExpand_Size;
  1379. int nEnd = pSegment->GetStart() + pSegment->GetLength() - rect.left - 1 + nExpand_Size;
  1380. int nHeight = pSegment->GetHeight() - rect.top + nExpand_Size;
  1381. line(particleImage, Point(nStart, nHeight), Point(nEnd, nHeight), Scalar(nBlackColor), nThickness, nLineType);
  1382. }
  1383. //--------abstract the contour of the particle.
  1384. Mat cvcopyImg;
  1385. medianBlur(particleImage, cvcopyImg, 7);//smooth the edge
  1386. Mat cvContourImg = Mat::zeros(rect.Height() + nExpand_Size, rect.Width() + nExpand_Size, CV_8U);
  1387. vector<vector<Point>>contours;
  1388. Canny(cvcopyImg, cvcopyImg, 20, 20 * 2, 3);
  1389. findContours(cvcopyImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
  1390. if (contours.size()==0)// the particle is too odd that openCV can't find a contour of it. Then we take the upright rect of the particle as it's minArea rect.
  1391. {
  1392. double w = 0, h = 0;
  1393. w = (double)rect.Width()*a_PixelSize;
  1394. h = (double)rect.Height()*a_PixelSize;
  1395. a_pOTSPart->SetDMax(MAX(w, h));
  1396. a_pOTSPart->SetDMin(MIN(w, h));
  1397. a_pOTSPart->SetDMean((w + h) / 2);
  1398. a_pOTSPart->SetFeretDiameter((w + h) / 2);
  1399. a_pOTSPart->SetDElong(MAX(w, h));
  1400. a_pOTSPart->SetPerimeter((w + h) * 2);
  1401. a_pOTSPart->SetDPerp(MIN(w, h));
  1402. a_pOTSPart->SetDInscr(MIN(w, h));
  1403. return true;
  1404. }
  1405. int imaxcontour = 0, imax = 0;
  1406. for (unsigned int i = 0; i < contours.size(); i++) {
  1407. int itmp = contourArea(contours[i]);
  1408. if (imaxcontour < itmp) {
  1409. imax = i;
  1410. imaxcontour = itmp;
  1411. }
  1412. }
  1413. vector<Point > listEdge = contours[imax];
  1414. vector<vector<Point>>Outcontours;
  1415. Outcontours.push_back(listEdge);
  1416. //---------calculate the minimium rectangle
  1417. auto rRect = cv::minAreaRect(listEdge);
  1418. Point2f p[4];
  1419. rRect.points(p);
  1420. int D_MIN =getDistance(p[0], p[1]);
  1421. int D_MinRecLen = 0;//minareaRect's length(the lenger side).
  1422. for (int j = 0; j <= 2; j++)
  1423. {
  1424. //line(cvContourImg, p[j], p[(j + 1) % 4], Scalar(100, 100, 0), 2);
  1425. int d = getDistance(p[j], p[j + 1]);
  1426. if (d < D_MIN)
  1427. {
  1428. D_MIN = d;
  1429. }
  1430. if (d > D_MinRecLen)
  1431. {
  1432. D_MinRecLen = d;
  1433. }
  1434. }
  1435. a_pOTSPart->SetDMin(D_MIN*a_PixelSize);
  1436. a_pOTSPart->SetOrientation(rRect.angle);
  1437. //----------calculate the perimeter
  1438. double d = arcLength(listEdge, true);
  1439. a_pOTSPart->SetPerimeter(d*a_PixelSize);
  1440. //-----------calculate the Max Diameter. Find the min enclosing circle first ,then find the two farthest circle connected point.
  1441. Point2f center; float radius;
  1442. minEnclosingCircle(listEdge, center, radius);
  1443. //circle(cvContourImg, center, radius, Scalar(100), 2);
  1444. std::vector <Point> outContour = listEdge;
  1445. std::vector <Point> rst;
  1446. for (unsigned int k = 0; k < outContour.size(); k++)
  1447. {
  1448. Point p = outContour[k];
  1449. double d = sqrt(pow((p.x - center.x), 2) + pow((p.y - center.y), 2));
  1450. if (fabs(d - radius) < 0.01)
  1451. {
  1452. rst.push_back(p);
  1453. }
  1454. }
  1455. double D_MAX = 0;
  1456. Point lineDmax[2];
  1457. for (unsigned int m = 0; m < rst.size(); m++)
  1458. {
  1459. Point p = rst[m];
  1460. for (unsigned int n = m + 1; n < rst.size(); n++)
  1461. {
  1462. Point p1 = rst[n];
  1463. double d = sqrt(powf((p.x - p1.x), 2) + powf((p.y - p1.y), 2));
  1464. if (d > D_MAX)
  1465. {
  1466. D_MAX = d;
  1467. lineDmax[0] = p;
  1468. lineDmax[1] = p1;
  1469. }
  1470. }
  1471. }
  1472. a_pOTSPart->SetDMax(D_MAX*a_PixelSize);
  1473. //--------calculate the D_PERP property using the D_MAX's two endpoints.
  1474. std::vector<Point> curve1;
  1475. std::vector<Point> curve2;
  1476. for (unsigned int i = 0; i < outContour.size(); i++)
  1477. {
  1478. Point pt = outContour[i];
  1479. bool start = false;
  1480. int clockwise = Side(lineDmax[0], lineDmax[1], pt);// devide these points into two group ,separate into the two sides.
  1481. if (clockwise > 0)
  1482. {
  1483. curve1.push_back(pt);
  1484. }
  1485. else
  1486. {
  1487. curve2.push_back(pt);
  1488. }
  1489. }
  1490. double d_perp1 = 0, d_perp2 = 0;
  1491. for (unsigned int i = 0; i < curve1.size(); i++)
  1492. {
  1493. double d = getDist_P2L(curve1[i], lineDmax[0], lineDmax[1]);
  1494. if (d > d_perp1)
  1495. {
  1496. d_perp1 = d;
  1497. }
  1498. }
  1499. for (unsigned int i = 0; i < curve2.size(); i++)
  1500. {
  1501. double d = getDist_P2L(curve2[i], lineDmax[0], lineDmax[1]);
  1502. if (d > d_perp2)
  1503. {
  1504. d_perp2 = d;
  1505. }
  1506. }
  1507. a_pOTSPart->SetDPerp((d_perp1 + d_perp2)*a_PixelSize);
  1508. //----------find the diameter of max inscribed circle
  1509. int r;
  1510. Point inscribeCirclecenter;
  1511. FindInnerCircleInContour(outContour, inscribeCirclecenter, r);
  1512. //circle(cvContourImg, inscribeCirclecenter, r, Scalar(200));
  1513. a_pOTSPart->SetDInscr(r * 2 * a_PixelSize);
  1514. //---------------calculate the image other caracater: length/width realArea/minRectangeArea etc. we can use these propertes to do forward process.
  1515. double minRectArea = D_MIN * D_MinRecLen*a_PixelSize*a_PixelSize;//最小外接矩形面积
  1516. double fillRatio = a_pOTSPart->GetArea() / minRectArea;//实际面积与最小外接矩形面积比,that's the fill rate.
  1517. double lengthWidthRatio;
  1518. lengthWidthRatio = (double)D_MinRecLen / D_MIN;//长宽比
  1519. //decide if this shape is a strip shape :if the lenthWidthRatio>2 then it is. if the lengthWidthRatio<2 and the areaRatio<0.5 then it is.
  1520. bool isStripShape = false;
  1521. double curveLength = 0;
  1522. double D_MEAN=0;
  1523. Moments mu;
  1524. mu = moments(listEdge, false);
  1525. int nx = mu.m10 / mu.m00;
  1526. int ny = mu.m01 / mu.m00;
  1527. //circle(cvcopyImg, Point(nx, ny), 1, (255), 1);
  1528. Point ptCenter = Point((int)nx, (int)ny);
  1529. if (pointPolygonTest(outContour, ptCenter, false) != 1)// the center point doesn't contain in the contour, we think it as curve shape.
  1530. {
  1531. isStripShape = true;
  1532. }
  1533. /*if (lengthWidthRatio >= 2 )// in PartA software this is true,but IncA because of the GB definition the everage feret diameter is always the mean value of all the chord.
  1534. {
  1535. isStripShape = true;
  1536. }*/
  1537. if (fillRatio <= 0.4)// only when the fill rate is very low,we think it as a curve shape,then we choose the mean width as the feret diameter.
  1538. {
  1539. isStripShape = true;
  1540. }
  1541. if (isStripShape)
  1542. {
  1543. curveLength = a_pOTSPart->GetPerimeter()/2 - a_pOTSPart->GetDInscr()/2;// thinking this particle as a strip rectangle.the width is the max inscribe circle diameter/2.
  1544. if (curveLength < D_MAX)
  1545. {
  1546. curveLength = D_MAX;
  1547. }
  1548. if (curveLength < MIN_DOUBLE_VALUE || a_pOTSPart->GetArea()<MIN_DOUBLE_VALUE)
  1549. {
  1550. D_MEAN = 0;
  1551. }
  1552. else
  1553. {
  1554. D_MEAN = a_pOTSPart->GetArea() / curveLength;
  1555. }
  1556. a_pOTSPart->SetDMean(D_MEAN*a_PixelSize);
  1557. a_pOTSPart->SetFeretDiameter(D_MEAN*a_PixelSize);
  1558. a_pOTSPart->SetDElong (curveLength*a_PixelSize);
  1559. }
  1560. else//it's a ball shape particle
  1561. {
  1562. curveLength = D_MAX;
  1563. double ftd = 0, maxD = 0, minD = 0, dratio = 0;
  1564. GetParticleAverageChord(outContour, a_PixelSize, ftd);
  1565. a_pOTSPart->SetDMean(ftd);
  1566. a_pOTSPart->SetFeretDiameter(ftd);
  1567. a_pOTSPart->SetDElong(curveLength*a_PixelSize);
  1568. }
  1569. return true;
  1570. }
  1571. BOOL COTSImageProcess::MergeBigBoundaryParticles(COTSFieldDataList allFields,double pixelSize,int scanFieldSize, CSize ResolutionSize, COTSParticleList& mergedParts)
  1572. {
  1573. COTSSegmentsList boarderSegs;
  1574. auto FldMgr = new CFieldMgr(scanFieldSize, ResolutionSize);
  1575. std::map<COTSParticle*, COTSParticleList> mapMergeParticles;//hold up all the boundary connected particles. the pair's first is also the member of these particles.
  1576. std::map<COTSParticle*, COTSSegmentsList> mapMergedSegments;//hold up all the segment's corresponding clone in the connected particles.
  1577. for (auto centerfld : allFields)
  1578. {
  1579. // find neighbor field on the left.
  1580. auto leftFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::LEFT);
  1581. if (leftFld != nullptr)
  1582. {
  1583. auto leftParts = centerfld->GetLeftBorderedBigParticles();
  1584. auto rightParts = leftFld->GetRightBorderedBigParticles();
  1585. for (auto leftp : leftParts)
  1586. {
  1587. for (auto rightp : rightParts)
  1588. {
  1589. if (leftp->IsConnected(rightp.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::LEFT))
  1590. {
  1591. if (leftp->headerParticle != NULL)
  1592. {
  1593. if (rightp->headerParticle == NULL)
  1594. {
  1595. rightp->headerParticle = leftp->headerParticle;
  1596. mapMergeParticles[leftp->headerParticle].push_back(rightp);
  1597. }
  1598. }
  1599. else
  1600. {
  1601. if (rightp->headerParticle != NULL)
  1602. {
  1603. leftp->headerParticle = rightp.get();
  1604. mapMergeParticles[rightp.get()].push_back(leftp);
  1605. }
  1606. else
  1607. {
  1608. leftp->headerParticle = leftp.get();
  1609. rightp->headerParticle = leftp.get();
  1610. mapMergeParticles[leftp.get()].push_back(rightp);
  1611. }
  1612. }
  1613. }
  1614. }
  1615. }
  1616. }
  1617. //find neighbor field on the upward
  1618. auto upFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::UP);
  1619. if (upFld != nullptr)
  1620. {
  1621. auto upParts = centerfld->GetTopBorderedBigParticles();
  1622. auto downParts = upFld->GetBottomBorderedBigParticles();
  1623. for (auto upprt : upParts)
  1624. {
  1625. for (auto downprt : downParts)
  1626. {
  1627. if (upprt->IsConnected(downprt.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::UP))
  1628. {
  1629. if (upprt->headerParticle != NULL)
  1630. {
  1631. if (downprt->headerParticle == NULL)
  1632. {
  1633. downprt->headerParticle = upprt->headerParticle;
  1634. mapMergeParticles[upprt->headerParticle].push_back(downprt);
  1635. }
  1636. }
  1637. else
  1638. {
  1639. if (downprt->headerParticle != NULL)
  1640. {
  1641. upprt->headerParticle = downprt->headerParticle;
  1642. mapMergeParticles[downprt.get()].push_back(upprt);
  1643. }
  1644. else
  1645. {
  1646. upprt->headerParticle = upprt.get();
  1647. downprt->headerParticle = upprt.get();
  1648. mapMergeParticles[upprt.get()].push_back(downprt);
  1649. }
  1650. }
  1651. }
  1652. }
  1653. }
  1654. }
  1655. //find neighbor field on the downward.
  1656. auto downFld = FldMgr->FindNeighborField(allFields, centerfld,SORTING_DIRECTION::DOWN);
  1657. if (downFld != nullptr)
  1658. {
  1659. auto downParts = centerfld->GetBottomBorderedBigParticles();
  1660. auto upParts = downFld->GetTopBorderedBigParticles();
  1661. for (auto downprt : downParts)
  1662. {
  1663. for (auto upprt : upParts)
  1664. {
  1665. if (downprt->IsConnected(upprt.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::DOWN))
  1666. {
  1667. if (downprt->headerParticle != NULL)
  1668. {
  1669. if (upprt->headerParticle == NULL)
  1670. {
  1671. upprt->headerParticle = downprt->headerParticle;
  1672. mapMergeParticles[downprt->headerParticle].push_back(upprt);
  1673. }
  1674. }
  1675. else
  1676. {
  1677. if (upprt->headerParticle != NULL)
  1678. {
  1679. downprt->headerParticle = upprt->headerParticle;
  1680. mapMergeParticles[upprt->headerParticle].push_back(downprt);
  1681. }
  1682. else
  1683. {
  1684. downprt->headerParticle = downprt.get();
  1685. upprt->headerParticle = downprt.get();
  1686. mapMergeParticles[downprt.get()].push_back(upprt);
  1687. }
  1688. }
  1689. }
  1690. }
  1691. }
  1692. }
  1693. //find neighbor field on the right.
  1694. auto rightFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::RIGHT);
  1695. if (rightFld != nullptr)
  1696. {
  1697. auto rightParts = centerfld->GetRightBorderedBigParticles();
  1698. auto leftParts = rightFld->GetLeftBorderedBigParticles();
  1699. for (auto rightprt : rightParts)
  1700. {
  1701. for (auto leftprt : leftParts)
  1702. {
  1703. if (rightprt->IsConnected(leftprt.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::RIGHT))
  1704. {
  1705. if (rightprt->headerParticle != NULL)
  1706. {
  1707. if (leftprt->headerParticle == NULL)
  1708. {
  1709. leftprt->headerParticle = rightprt->headerParticle;
  1710. mapMergeParticles[rightprt->headerParticle].push_back(leftprt);
  1711. }
  1712. }
  1713. else
  1714. {
  1715. if (leftprt->headerParticle != NULL)
  1716. {
  1717. rightprt->headerParticle = leftprt->headerParticle;
  1718. mapMergeParticles[leftprt->headerParticle].push_back(rightprt);
  1719. }
  1720. else
  1721. {
  1722. rightprt->headerParticle = rightprt.get();
  1723. leftprt->headerParticle = rightprt.get();
  1724. mapMergeParticles[rightprt.get()].push_back(leftprt);
  1725. }
  1726. }
  1727. }
  1728. }
  1729. }
  1730. }
  1731. }
  1732. for (auto pair : mapMergeParticles)
  1733. {
  1734. struct EleAreaPercentage
  1735. {
  1736. EleAreaPercentage(double p, CElementChemistryPtr e)
  1737. {
  1738. areaPercentage = p;
  1739. eleData = e;
  1740. }
  1741. double areaPercentage;
  1742. CElementChemistryPtr eleData;
  1743. };
  1744. auto newPart = COTSParticlePtr(new COTSParticle());
  1745. COTSSegmentsList newSegs;
  1746. auto p = pair.first;
  1747. //firstly,we sum up all the merged particles's area and get the represent string.
  1748. std::string partsStr = std::to_string(p->GetFieldId()) + ":" + std::to_string(p->GetAnalysisId());
  1749. double allPartArea = p->GetArea();//Get the first particle's area.
  1750. for (auto other : pair.second)// Get the total area of all these particles for the use of ele calcu.
  1751. {
  1752. partsStr += "," + std::to_string(other->GetFieldId()) + ":" + std::to_string(other->GetAnalysisId());//Get the subparticles string such as "1:1,2:1" etc.
  1753. allPartArea += other->GetArea();//Get other particle's area
  1754. }
  1755. // calculate all the new segment's position.
  1756. std::vector <COTSParticle*> allSubParts;
  1757. allSubParts.push_back(p);
  1758. for (auto other : pair.second)// Get the total area of all these particles for the use of ele calcu.
  1759. {
  1760. allSubParts.push_back(other.get());
  1761. }
  1762. for (auto subp : allSubParts)
  1763. {
  1764. int fid = subp->GetFieldId();
  1765. CPoint myFldPos;
  1766. for (auto f : allFields)//find this particle's filed.
  1767. {
  1768. if (f->GetId() == fid)
  1769. {
  1770. myFldPos = f->GetPosition();
  1771. }
  1772. }
  1773. int fldWidth = allFields[0]->Width;
  1774. int fldHeight = allFields[0]->Height;
  1775. CPoint fldLeftUpPos = CPoint(myFldPos.x + fldWidth / 2 * pixelSize, myFldPos.y + fldHeight / 2 * pixelSize);
  1776. for (auto s : subp->GetFeature()->GetSegmentsList())
  1777. {
  1778. COTSSegmentPtr newseg = COTSSegmentPtr(new COTSSegment());
  1779. newseg->SetStart(s->GetStart() * pixelSize + fldLeftUpPos.x);
  1780. newseg->SetHeight((0 - s->GetHeight() * pixelSize) + fldLeftUpPos.y);//the coordinate system of segment in a field is different with the OTS coordinate system.OTS system's y axis is upward positive ,yet the field is downward positive.
  1781. newseg->SetLength(s->GetLength());
  1782. newSegs.push_back(newseg);
  1783. }
  1784. }
  1785. COTSFeaturePtr newFeature = COTSFeaturePtr(new COTSFeature());
  1786. newFeature->SetSegmentsList(newSegs);
  1787. newPart->SetFeature(newFeature);
  1788. newPart->CalCoverRect();
  1789. //second, we get all the element data and their area percentage .
  1790. std::map<std::string, std::vector<EleAreaPercentage>> mapEleData;
  1791. CPosXrayPtr pXray1 = p->GetXrayInfo();
  1792. for (auto ele : pXray1->GetElementQuantifyData())
  1793. {
  1794. mapEleData[ele->GetName().GetBuffer()].push_back(EleAreaPercentage(p->GetArea() / allPartArea, ele));
  1795. }
  1796. for (auto other : pair.second)
  1797. {
  1798. auto otherXray = other->GetXrayInfo();
  1799. for (auto eledata : otherXray->GetElementQuantifyData())
  1800. {
  1801. mapEleData[eledata->GetName().GetBuffer()].push_back(EleAreaPercentage(other->GetArea() / allPartArea, eledata));
  1802. }
  1803. }
  1804. // third,we calculate all the element's new percentage data and get a new element chemistry list.
  1805. CElementChemistriesList newCheList;
  1806. for (auto eledata : mapEleData)
  1807. {
  1808. CElementChemistryPtr newEleche = CElementChemistryPtr(new CElementChemistry());
  1809. newEleche->SetName(CString(eledata.first.c_str()));
  1810. double newPercentage = 0;
  1811. for (auto d : eledata.second)
  1812. {
  1813. newPercentage += d.areaPercentage * d.eleData->GetPercentage();
  1814. }
  1815. newEleche->SetPercentage(newPercentage);
  1816. newCheList.push_back(newEleche);
  1817. }
  1818. CPosXrayPtr xray(new CPosXray());
  1819. xray->SetElementQuantifyData(newCheList);
  1820. newPart->SetXrayInfo(xray);
  1821. newPart->SetSubParticles(partsStr);
  1822. newPart->SetArea(allPartArea);
  1823. newPart->SetTagId(mergedParts.size());
  1824. newPart->SetAnalysisId(mergedParts.size());
  1825. std::string name = p->TypeName();
  1826. newPart->TypeName(name);
  1827. newPart->TypeColor(p->TypeColor());
  1828. mergedParts.push_back(newPart);
  1829. }
  1830. return true;
  1831. }
  1832. }