OTSImageProcess.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131
  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. /*COTSImageProcess::BErode3(pSrcImg, pTempImg, 5, nHeightImg, nWidthImg);
  1049. COTSImageProcess::BDilate3(pTempImg, pTempImg2, 5, nHeightImg, nWidthImg);*/
  1050. BYTE* pPixel = new byte[nImgSize];
  1051. long nBGStart;
  1052. long nBGEnd;
  1053. long nNumParticle = 0;
  1054. if (a_pImageProcessParam->GetBGRemoveType() == OTS_BGREMOVE_TYPE::MANUAL)
  1055. {
  1056. nBGStart = a_pImageProcessParam->GetBGGray().GetStart();
  1057. nBGEnd = a_pImageProcessParam->GetBGGray().GetEnd();
  1058. // delete background
  1059. for (unsigned int i = 0; i < nImgSize; i++)
  1060. {
  1061. if (pTempImg2[i] >= nBGStart && pTempImg2[i] <= nBGEnd)
  1062. {
  1063. pPixel[i] = 0;
  1064. }
  1065. else
  1066. {
  1067. pPixel[i] = 255;
  1068. nNumParticle++;
  1069. }
  1070. }
  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. }
  1127. a_pBinImgOut->SetImageData(pPixel,nWidthImg,nHeightImg);
  1128. foundedPixelNum = nNumParticle;
  1129. delete[] pTempImg;
  1130. delete[] pTempImg2;
  1131. return ;
  1132. }
  1133. BOOL COTSImageProcess::GetParticles(long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSParticleList& a_listParticles)
  1134. {
  1135. ASSERT(a_pPixel);
  1136. if (!a_pPixel)
  1137. {
  1138. LogErrorTrace(__FILE__, __LINE__, _T("GetParticles: there is no image data"));
  1139. return FALSE;
  1140. }
  1141. a_listParticles.clear();
  1142. COTSSegmentsList listSegment;
  1143. listSegment.clear();
  1144. //1. get segment line by line
  1145. if (!GetSegmentList(a_nWidth, a_nHeight, a_pPixel, listSegment))
  1146. {
  1147. LogErrorTrace(__FILE__, __LINE__, _T("GetParticles:failed to get segments."));
  1148. return FALSE;
  1149. }
  1150. if ((int)listSegment.size() == 0)
  1151. {
  1152. LogErrorTrace(__FILE__, __LINE__, _T("no particle is found."));
  1153. return FALSE;
  1154. }
  1155. //2. save the temp feature
  1156. COTSFeatureList listFeature;
  1157. listFeature.clear();
  1158. if (!GetFeatureList1(listSegment, listFeature))
  1159. {
  1160. LogErrorTrace(__FILE__, __LINE__, _T("GetParticles:failed to get up down segment list."));
  1161. return FALSE;
  1162. }
  1163. if ((int)listFeature.size() == 0)
  1164. {
  1165. LogErrorTrace(__FILE__, __LINE__, _T("no particle is found."));
  1166. return FALSE;
  1167. }
  1168. COTSParticleList listParticles;
  1169. listParticles.clear();
  1170. if (!ChangeFeaturelist(listFeature, a_listParticles))
  1171. {
  1172. LogErrorTrace(__FILE__, __LINE__, _T("can't change feature to particle."));
  1173. return FALSE;
  1174. }
  1175. return TRUE;
  1176. }
  1177. BOOL COTSImageProcess::GetSegmentList(long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSSegmentsList& a_listSegments)
  1178. {
  1179. ASSERT(a_pPixel);
  1180. if (!a_pPixel)
  1181. {
  1182. LogErrorTrace(__FILE__, __LINE__, _T("GetSegments: there is no image data"));
  1183. return FALSE;
  1184. }
  1185. long nImgSize = a_nWidth * a_nHeight;
  1186. a_listSegments.clear();
  1187. //1. get segment line by line
  1188. long nLine, nm, nn;
  1189. long nStart, nLength;
  1190. for (nLine = 0; nLine < a_nHeight; nLine++)
  1191. {
  1192. for (nm = 0; nm < a_nWidth; nm += (nLength + 1))
  1193. {
  1194. nLength = 0;
  1195. // get start
  1196. if (*(a_pPixel + nLine * a_nWidth + nm) != 0)
  1197. {
  1198. nStart = nm;
  1199. nLength++;
  1200. //get length
  1201. for (nn = nm + 1; nn < a_nWidth; nn++)
  1202. {
  1203. // check if segment is over, break
  1204. if (nLength != 0)
  1205. {
  1206. if (*(a_pPixel + nLine * a_nWidth + nn) == 0)
  1207. break;
  1208. }
  1209. if (*(a_pPixel + nLine * a_nWidth + nn) != 0)
  1210. {
  1211. nLength++;
  1212. }
  1213. }
  1214. // generate segment
  1215. COTSSegmentPtr pSegment = COTSSegmentPtr(new COTSSegment(nLine, nStart, nLength));
  1216. a_listSegments.push_back(pSegment);
  1217. }
  1218. else
  1219. {
  1220. continue;
  1221. }
  1222. }
  1223. }
  1224. if ((int)a_listSegments.size() == 0)
  1225. {
  1226. LogErrorTrace(__FILE__, __LINE__, _T("no particle is found."));
  1227. return FALSE;
  1228. }
  1229. return TRUE;
  1230. }
  1231. BOOL COTSImageProcess::GetFeatureList1(COTSSegmentsList& a_listSegments, COTSFeatureList& a_listFeatures)
  1232. {
  1233. COTSSegmentsList listSegmentNew;
  1234. std::map<long, COTSSegmentsList > mapOneLineSegments;
  1235. for each (auto s in a_listSegments)
  1236. {
  1237. mapOneLineSegments[s->GetHeight()].push_back(s);//sorting all the segments base on the line number.
  1238. }
  1239. std::map<long, COTSSegmentsList >::iterator lineItr = mapOneLineSegments.begin();//find the highest line
  1240. while (lineItr != mapOneLineSegments.end())
  1241. {
  1242. for (auto s = lineItr->second.begin(); s < lineItr->second.end(); )//find one segment of this line.
  1243. {
  1244. COTSSegmentPtr bottomSeg = *s;
  1245. listSegmentNew.clear();
  1246. listSegmentNew.push_back(*s);
  1247. s = lineItr->second.erase(s);
  1248. std::map<long, COTSSegmentsList >::iterator tempItr = lineItr;
  1249. tempItr++;
  1250. for (; tempItr != mapOneLineSegments.end(); tempItr++)//find all other lines of segments
  1251. {
  1252. if (tempItr->first - bottomSeg->GetHeight() > 1)
  1253. {
  1254. break;
  1255. }
  1256. for (auto nextLineSegment = tempItr->second.begin(); nextLineSegment < tempItr->second.end();)//find next line's all segments
  1257. {
  1258. if (((*nextLineSegment)->GetStart() - (bottomSeg->GetStart() + bottomSeg->GetLength())) > 1)
  1259. {
  1260. break;
  1261. }
  1262. if (bottomSeg->UpDownConection(**nextLineSegment))
  1263. {
  1264. listSegmentNew.push_back(*nextLineSegment);
  1265. bottomSeg = *nextLineSegment;
  1266. nextLineSegment = tempItr->second.erase(nextLineSegment);
  1267. break;
  1268. }
  1269. if (tempItr->second.size() > 0)
  1270. {
  1271. nextLineSegment++;
  1272. }
  1273. else
  1274. {
  1275. break;
  1276. }
  1277. }
  1278. }
  1279. COTSFeaturePtr pFeature = COTSFeaturePtr(new COTSFeature());
  1280. pFeature->SetSegmentsList(listSegmentNew);
  1281. //check if this new feature is connected with other found feature.
  1282. COTSSegmentPtr topSeg = listSegmentNew[0];//find the toppest segment of this new feature.
  1283. COTSSegmentPtr bottomSegment = listSegmentNew[listSegmentNew.size() - 1];//find the lowest segment of this new feature.
  1284. bool haveMerged = false;
  1285. for each (auto f in a_listFeatures)
  1286. {
  1287. for (auto seg : f->GetSegmentsList())
  1288. {
  1289. if (bottomSegment->UpDownConection(*seg) || topSeg->UpDownConection(*seg))
  1290. {
  1291. COTSSegmentsList segs = f->GetSegmentsList();
  1292. for (auto s : listSegmentNew)
  1293. {
  1294. segs.push_back(s);
  1295. }
  1296. f->SetSegmentsList(segs);
  1297. haveMerged = true;
  1298. break;
  1299. }
  1300. }
  1301. if (haveMerged)
  1302. {
  1303. break;
  1304. }
  1305. }
  1306. if (!haveMerged)
  1307. {
  1308. a_listFeatures.push_back(pFeature);
  1309. }
  1310. if (lineItr->second.size() == 0)
  1311. {
  1312. break;
  1313. }
  1314. }
  1315. lineItr++;
  1316. }
  1317. return true;
  1318. }
  1319. BOOL COTSImageProcess::ChangeFeaturelist(COTSFeatureList& a_listFeatures, COTSParticleList& a_listParticle)
  1320. {
  1321. if (a_listFeatures.size() == 0)
  1322. {
  1323. LogErrorTrace(__FILE__, __LINE__, _T("ChangeFeaturelist: there is no feature in the list."));
  1324. return FALSE;
  1325. }
  1326. // compute Rect
  1327. for (auto pFeature : a_listFeatures)
  1328. {
  1329. COTSParticlePtr pParticle = COTSParticlePtr(new COTSParticle());
  1330. pParticle->SetFeature(pFeature);
  1331. if (!pParticle->CalCoverRect())
  1332. {
  1333. LogErrorTrace(__FILE__, __LINE__, _T("ChangeFeaturelist: failed to get particle rect."));
  1334. return FALSE;
  1335. }
  1336. a_listParticle.push_back(pParticle);
  1337. }
  1338. if ((int)a_listParticle.size() == 0)
  1339. {
  1340. LogErrorTrace(__FILE__, __LINE__, _T("Can't get particle."));
  1341. return FALSE;
  1342. }
  1343. return TRUE;
  1344. }
  1345. BOOL COTSImageProcess::CalcuParticleImagePropertes(COTSParticlePtr a_pOTSPart, double a_PixelSize)
  1346. {
  1347. //--------- convert this particle data to image data,construct an image only with this particle.------
  1348. const int nExpand_Size = 3;
  1349. const int nWhiteColor = 0;
  1350. const int nThickness = 1;
  1351. // lineType Type of the line
  1352. const int nLineType = 8;
  1353. // get rectangle of the particle
  1354. CRect rect = a_pOTSPart->GetParticleRect();
  1355. 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.
  1356. {
  1357. double w = 0, h = 0;
  1358. w = (double)rect.Width()*a_PixelSize;
  1359. h = (double)rect.Height()*a_PixelSize;
  1360. a_pOTSPart->SetDMax(MAX(w, h));
  1361. a_pOTSPart->SetDMin(MIN(w, h));
  1362. a_pOTSPart->SetDMean((w + h) / 2);
  1363. a_pOTSPart->SetFeretDiameter((w + h) / 2);
  1364. a_pOTSPart->SetDElong(MAX(w, h));
  1365. a_pOTSPart->SetPerimeter((w+h)*2);
  1366. a_pOTSPart->SetDPerp(MIN(w, h));
  1367. a_pOTSPart->SetDInscr(MIN(w, h));
  1368. return true;
  1369. }
  1370. // calculate the particle image data size, expand 3 pixel at the edge
  1371. Mat particleImage = Mat::zeros(rect.Height() + nExpand_Size , rect.Width() + nExpand_Size , CV_8U);
  1372. // get the segment list
  1373. COTSSegmentsList listSegment = a_pOTSPart->GetFeature()->GetSegmentsList();
  1374. for (auto pSegment : listSegment)
  1375. {
  1376. int nStart = pSegment->GetStart() - rect.left + nExpand_Size;
  1377. int nEnd = pSegment->GetStart() + pSegment->GetLength() - rect.left - 1 + nExpand_Size;
  1378. int nHeight = pSegment->GetHeight() - rect.top + nExpand_Size;
  1379. line(particleImage, Point(nStart, nHeight), Point(nEnd, nHeight), Scalar(nBlackColor), nThickness, nLineType);
  1380. }
  1381. //--------abstract the contour of the particle.
  1382. Mat cvcopyImg;
  1383. medianBlur(particleImage, cvcopyImg, 7);//smooth the edge
  1384. Mat cvContourImg = Mat::zeros(rect.Height() + nExpand_Size, rect.Width() + nExpand_Size, CV_8U);
  1385. vector<vector<Point>>contours;
  1386. Canny(cvcopyImg, cvcopyImg, 20, 20 * 2, 3);
  1387. findContours(cvcopyImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
  1388. 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.
  1389. {
  1390. double w = 0, h = 0;
  1391. w = (double)rect.Width()*a_PixelSize;
  1392. h = (double)rect.Height()*a_PixelSize;
  1393. a_pOTSPart->SetDMax(MAX(w, h));
  1394. a_pOTSPart->SetDMin(MIN(w, h));
  1395. a_pOTSPart->SetDMean((w + h) / 2);
  1396. a_pOTSPart->SetFeretDiameter((w + h) / 2);
  1397. a_pOTSPart->SetDElong(MAX(w, h));
  1398. a_pOTSPart->SetPerimeter((w + h) * 2);
  1399. a_pOTSPart->SetDPerp(MIN(w, h));
  1400. a_pOTSPart->SetDInscr(MIN(w, h));
  1401. return true;
  1402. }
  1403. int imaxcontour = 0, imax = 0;
  1404. for (unsigned int i = 0; i < contours.size(); i++) {
  1405. int itmp = contourArea(contours[i]);
  1406. if (imaxcontour < itmp) {
  1407. imax = i;
  1408. imaxcontour = itmp;
  1409. }
  1410. }
  1411. vector<Point > listEdge = contours[imax];
  1412. vector<vector<Point>>Outcontours;
  1413. Outcontours.push_back(listEdge);
  1414. //---------calculate the minimium rectangle
  1415. auto rRect = cv::minAreaRect(listEdge);
  1416. Point2f p[4];
  1417. rRect.points(p);
  1418. int D_MIN =getDistance(p[0], p[1]);
  1419. int D_MinRecLen = 0;//minareaRect's length(the lenger side).
  1420. for (int j = 0; j <= 2; j++)
  1421. {
  1422. //line(cvContourImg, p[j], p[(j + 1) % 4], Scalar(100, 100, 0), 2);
  1423. int d = getDistance(p[j], p[j + 1]);
  1424. if (d < D_MIN)
  1425. {
  1426. D_MIN = d;
  1427. }
  1428. if (d > D_MinRecLen)
  1429. {
  1430. D_MinRecLen = d;
  1431. }
  1432. }
  1433. a_pOTSPart->SetDMin(D_MIN*a_PixelSize);
  1434. a_pOTSPart->SetOrientation(rRect.angle);
  1435. //----------calculate the perimeter
  1436. double d = arcLength(listEdge, true);
  1437. a_pOTSPart->SetPerimeter(d*a_PixelSize);
  1438. //-----------calculate the Max Diameter. Find the min enclosing circle first ,then find the two farthest circle connected point.
  1439. Point2f center; float radius;
  1440. minEnclosingCircle(listEdge, center, radius);
  1441. //circle(cvContourImg, center, radius, Scalar(100), 2);
  1442. std::vector <Point> outContour = listEdge;
  1443. std::vector <Point> rst;
  1444. for (unsigned int k = 0; k < outContour.size(); k++)
  1445. {
  1446. Point p = outContour[k];
  1447. double d = sqrt(pow((p.x - center.x), 2) + pow((p.y - center.y), 2));
  1448. if (fabs(d - radius) < 0.01)
  1449. {
  1450. rst.push_back(p);
  1451. }
  1452. }
  1453. double D_MAX = 0;
  1454. Point lineDmax[2];
  1455. for (unsigned int m = 0; m < rst.size(); m++)
  1456. {
  1457. Point p = rst[m];
  1458. for (unsigned int n = m + 1; n < rst.size(); n++)
  1459. {
  1460. Point p1 = rst[n];
  1461. double d = sqrt(powf((p.x - p1.x), 2) + powf((p.y - p1.y), 2));
  1462. if (d > D_MAX)
  1463. {
  1464. D_MAX = d;
  1465. lineDmax[0] = p;
  1466. lineDmax[1] = p1;
  1467. }
  1468. }
  1469. }
  1470. a_pOTSPart->SetDMax(D_MAX*a_PixelSize);
  1471. //--------calculate the D_PERP property using the D_MAX's two endpoints.
  1472. std::vector<Point> curve1;
  1473. std::vector<Point> curve2;
  1474. for (unsigned int i = 0; i < outContour.size(); i++)
  1475. {
  1476. Point pt = outContour[i];
  1477. bool start = false;
  1478. int clockwise = Side(lineDmax[0], lineDmax[1], pt);// devide these points into two group ,separate into the two sides.
  1479. if (clockwise > 0)
  1480. {
  1481. curve1.push_back(pt);
  1482. }
  1483. else
  1484. {
  1485. curve2.push_back(pt);
  1486. }
  1487. }
  1488. double d_perp1 = 0, d_perp2 = 0;
  1489. for (unsigned int i = 0; i < curve1.size(); i++)
  1490. {
  1491. double d = getDist_P2L(curve1[i], lineDmax[0], lineDmax[1]);
  1492. if (d > d_perp1)
  1493. {
  1494. d_perp1 = d;
  1495. }
  1496. }
  1497. for (unsigned int i = 0; i < curve2.size(); i++)
  1498. {
  1499. double d = getDist_P2L(curve2[i], lineDmax[0], lineDmax[1]);
  1500. if (d > d_perp2)
  1501. {
  1502. d_perp2 = d;
  1503. }
  1504. }
  1505. a_pOTSPart->SetDPerp((d_perp1 + d_perp2)*a_PixelSize);
  1506. //----------find the diameter of max inscribed circle
  1507. int r;
  1508. Point inscribeCirclecenter;
  1509. FindInnerCircleInContour(outContour, inscribeCirclecenter, r);
  1510. //circle(cvContourImg, inscribeCirclecenter, r, Scalar(200));
  1511. a_pOTSPart->SetDInscr(r * 2 * a_PixelSize);
  1512. //---------------calculate the image other caracater: length/width realArea/minRectangeArea etc. we can use these propertes to do forward process.
  1513. double minRectArea = D_MIN * D_MinRecLen*a_PixelSize*a_PixelSize;//最小外接矩形面积
  1514. double fillRatio = a_pOTSPart->GetArea() / minRectArea;//实际面积与最小外接矩形面积比,that's the fill rate.
  1515. double lengthWidthRatio;
  1516. lengthWidthRatio = (double)D_MinRecLen / D_MIN;//长宽比
  1517. //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.
  1518. bool isStripShape = false;
  1519. double curveLength = 0;
  1520. double D_MEAN=0;
  1521. Moments mu;
  1522. mu = moments(listEdge, false);
  1523. int nx = mu.m10 / mu.m00;
  1524. int ny = mu.m01 / mu.m00;
  1525. //circle(cvcopyImg, Point(nx, ny), 1, (255), 1);
  1526. Point ptCenter = Point((int)nx, (int)ny);
  1527. if (pointPolygonTest(outContour, ptCenter, false) != 1)// the center point doesn't contain in the contour, we think it as curve shape.
  1528. {
  1529. isStripShape = true;
  1530. }
  1531. /*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.
  1532. {
  1533. isStripShape = true;
  1534. }*/
  1535. 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.
  1536. {
  1537. isStripShape = true;
  1538. }
  1539. if (isStripShape)
  1540. {
  1541. 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.
  1542. if (curveLength < D_MAX)
  1543. {
  1544. curveLength = D_MAX;
  1545. }
  1546. if (curveLength < MIN_DOUBLE_VALUE || a_pOTSPart->GetArea()<MIN_DOUBLE_VALUE)
  1547. {
  1548. D_MEAN = 0;
  1549. }
  1550. else
  1551. {
  1552. D_MEAN = a_pOTSPart->GetArea() / curveLength;
  1553. }
  1554. a_pOTSPart->SetDMean(D_MEAN*a_PixelSize);
  1555. a_pOTSPart->SetFeretDiameter(D_MEAN*a_PixelSize);
  1556. a_pOTSPart->SetDElong (curveLength*a_PixelSize);
  1557. }
  1558. else//it's a ball shape particle
  1559. {
  1560. curveLength = D_MAX;
  1561. double ftd = 0, maxD = 0, minD = 0, dratio = 0;
  1562. GetParticleAverageChord(outContour, a_PixelSize, ftd);
  1563. a_pOTSPart->SetDMean(ftd);
  1564. a_pOTSPart->SetFeretDiameter(ftd);
  1565. a_pOTSPart->SetDElong(curveLength*a_PixelSize);
  1566. }
  1567. return true;
  1568. }
  1569. BOOL COTSImageProcess::MergeBigBoundaryParticles(COTSFieldDataList allFields,double pixelSize,int scanFieldSize, CSize ResolutionSize, COTSParticleList& mergedParts)
  1570. {
  1571. COTSSegmentsList boarderSegs;
  1572. auto FldMgr = new CFieldMgr(scanFieldSize, ResolutionSize);
  1573. std::map<COTSParticle*, COTSParticleList> mapMergeParticles;//hold up all the boundary connected particles. the pair's first is also the member of these particles.
  1574. std::map<COTSParticle*, COTSSegmentsList> mapMergedSegments;//hold up all the segment's corresponding clone in the connected particles.
  1575. for (auto centerfld : allFields)
  1576. {
  1577. // find neighbor field on the left.
  1578. auto leftFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::LEFT);
  1579. if (leftFld != nullptr)
  1580. {
  1581. auto leftParts = centerfld->GetLeftBorderedBigParticles();
  1582. auto rightParts = leftFld->GetRightBorderedBigParticles();
  1583. for (auto leftp : leftParts)
  1584. {
  1585. for (auto rightp : rightParts)
  1586. {
  1587. if (leftp->IsConnected(rightp.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::LEFT))
  1588. {
  1589. if (leftp->headerParticle != NULL)
  1590. {
  1591. if (rightp->headerParticle == NULL)
  1592. {
  1593. rightp->headerParticle = leftp->headerParticle;
  1594. mapMergeParticles[leftp->headerParticle].push_back(rightp);
  1595. }
  1596. }
  1597. else
  1598. {
  1599. if (rightp->headerParticle != NULL)
  1600. {
  1601. leftp->headerParticle = rightp.get();
  1602. mapMergeParticles[rightp.get()].push_back(leftp);
  1603. }
  1604. else
  1605. {
  1606. leftp->headerParticle = leftp.get();
  1607. rightp->headerParticle = leftp.get();
  1608. mapMergeParticles[leftp.get()].push_back(rightp);
  1609. }
  1610. }
  1611. }
  1612. }
  1613. }
  1614. }
  1615. //find neighbor field on the upward
  1616. auto upFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::UP);
  1617. if (upFld != nullptr)
  1618. {
  1619. auto upParts = centerfld->GetTopBorderedBigParticles();
  1620. auto downParts = upFld->GetBottomBorderedBigParticles();
  1621. for (auto upprt : upParts)
  1622. {
  1623. for (auto downprt : downParts)
  1624. {
  1625. if (upprt->IsConnected(downprt.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::UP))
  1626. {
  1627. if (upprt->headerParticle != NULL)
  1628. {
  1629. if (downprt->headerParticle == NULL)
  1630. {
  1631. downprt->headerParticle = upprt->headerParticle;
  1632. mapMergeParticles[upprt->headerParticle].push_back(downprt);
  1633. }
  1634. }
  1635. else
  1636. {
  1637. if (downprt->headerParticle != NULL)
  1638. {
  1639. upprt->headerParticle = downprt->headerParticle;
  1640. mapMergeParticles[downprt.get()].push_back(upprt);
  1641. }
  1642. else
  1643. {
  1644. upprt->headerParticle = upprt.get();
  1645. downprt->headerParticle = upprt.get();
  1646. mapMergeParticles[upprt.get()].push_back(downprt);
  1647. }
  1648. }
  1649. }
  1650. }
  1651. }
  1652. }
  1653. //find neighbor field on the downward.
  1654. auto downFld = FldMgr->FindNeighborField(allFields, centerfld,SORTING_DIRECTION::DOWN);
  1655. if (downFld != nullptr)
  1656. {
  1657. auto downParts = centerfld->GetBottomBorderedBigParticles();
  1658. auto upParts = downFld->GetTopBorderedBigParticles();
  1659. for (auto downprt : downParts)
  1660. {
  1661. for (auto upprt : upParts)
  1662. {
  1663. if (downprt->IsConnected(upprt.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::DOWN))
  1664. {
  1665. if (downprt->headerParticle != NULL)
  1666. {
  1667. if (upprt->headerParticle == NULL)
  1668. {
  1669. upprt->headerParticle = downprt->headerParticle;
  1670. mapMergeParticles[downprt->headerParticle].push_back(upprt);
  1671. }
  1672. }
  1673. else
  1674. {
  1675. if (upprt->headerParticle != NULL)
  1676. {
  1677. downprt->headerParticle = upprt->headerParticle;
  1678. mapMergeParticles[upprt->headerParticle].push_back(downprt);
  1679. }
  1680. else
  1681. {
  1682. downprt->headerParticle = downprt.get();
  1683. upprt->headerParticle = downprt.get();
  1684. mapMergeParticles[downprt.get()].push_back(upprt);
  1685. }
  1686. }
  1687. }
  1688. }
  1689. }
  1690. }
  1691. //find neighbor field on the right.
  1692. auto rightFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::RIGHT);
  1693. if (rightFld != nullptr)
  1694. {
  1695. auto rightParts = centerfld->GetRightBorderedBigParticles();
  1696. auto leftParts = rightFld->GetLeftBorderedBigParticles();
  1697. for (auto rightprt : rightParts)
  1698. {
  1699. for (auto leftprt : leftParts)
  1700. {
  1701. if (rightprt->IsConnected(leftprt.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::RIGHT))
  1702. {
  1703. if (rightprt->headerParticle != NULL)
  1704. {
  1705. if (leftprt->headerParticle == NULL)
  1706. {
  1707. leftprt->headerParticle = rightprt->headerParticle;
  1708. mapMergeParticles[rightprt->headerParticle].push_back(leftprt);
  1709. }
  1710. }
  1711. else
  1712. {
  1713. if (leftprt->headerParticle != NULL)
  1714. {
  1715. rightprt->headerParticle = leftprt->headerParticle;
  1716. mapMergeParticles[leftprt->headerParticle].push_back(rightprt);
  1717. }
  1718. else
  1719. {
  1720. rightprt->headerParticle = rightprt.get();
  1721. leftprt->headerParticle = rightprt.get();
  1722. mapMergeParticles[rightprt.get()].push_back(leftprt);
  1723. }
  1724. }
  1725. }
  1726. }
  1727. }
  1728. }
  1729. }
  1730. for (auto pair : mapMergeParticles)
  1731. {
  1732. struct EleAreaPercentage
  1733. {
  1734. EleAreaPercentage(double p, CElementChemistryPtr e)
  1735. {
  1736. areaPercentage = p;
  1737. eleData = e;
  1738. }
  1739. double areaPercentage;
  1740. CElementChemistryPtr eleData;
  1741. };
  1742. auto newPart = COTSParticlePtr(new COTSParticle());
  1743. COTSSegmentsList newSegs;
  1744. auto p = pair.first;
  1745. //firstly,we sum up all the merged particles's area and get the represent string.
  1746. std::string partsStr = std::to_string(p->GetFieldId()) + ":" + std::to_string(p->GetAnalysisId());
  1747. double allPartArea = p->GetArea();//Get the first particle's area.
  1748. for (auto other : pair.second)// Get the total area of all these particles for the use of ele calcu.
  1749. {
  1750. partsStr += "," + std::to_string(other->GetFieldId()) + ":" + std::to_string(other->GetAnalysisId());//Get the subparticles string such as "1:1,2:1" etc.
  1751. allPartArea += other->GetArea();//Get other particle's area
  1752. }
  1753. // calculate all the new segment's position.
  1754. std::vector <COTSParticle*> allSubParts;
  1755. allSubParts.push_back(p);
  1756. for (auto other : pair.second)// Get the total area of all these particles for the use of ele calcu.
  1757. {
  1758. allSubParts.push_back(other.get());
  1759. }
  1760. for (auto subp : allSubParts)
  1761. {
  1762. int fid = subp->GetFieldId();
  1763. CPoint myFldPos;
  1764. for (auto f : allFields)//find this particle's filed.
  1765. {
  1766. if (f->GetId() == fid)
  1767. {
  1768. myFldPos = f->GetPosition();
  1769. }
  1770. }
  1771. int fldWidth = allFields[0]->Width;
  1772. int fldHeight = allFields[0]->Height;
  1773. CPoint fldLeftUpPos = CPoint(myFldPos.x + fldWidth / 2 * pixelSize, myFldPos.y + fldHeight / 2 * pixelSize);
  1774. for (auto s : subp->GetFeature()->GetSegmentsList())
  1775. {
  1776. COTSSegmentPtr newseg = COTSSegmentPtr(new COTSSegment());
  1777. newseg->SetStart(s->GetStart() * pixelSize + fldLeftUpPos.x);
  1778. 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.
  1779. newseg->SetLength(s->GetLength());
  1780. newSegs.push_back(newseg);
  1781. }
  1782. }
  1783. COTSFeaturePtr newFeature = COTSFeaturePtr(new COTSFeature());
  1784. newFeature->SetSegmentsList(newSegs);
  1785. newPart->SetFeature(newFeature);
  1786. newPart->CalCoverRect();
  1787. //second, we get all the element data and their area percentage .
  1788. std::map<std::string, std::vector<EleAreaPercentage>> mapEleData;
  1789. CPosXrayPtr pXray1 = p->GetXrayInfo();
  1790. for (auto ele : pXray1->GetElementQuantifyData())
  1791. {
  1792. mapEleData[ele->GetName().GetBuffer()].push_back(EleAreaPercentage(p->GetArea() / allPartArea, ele));
  1793. }
  1794. for (auto other : pair.second)
  1795. {
  1796. auto otherXray = other->GetXrayInfo();
  1797. for (auto eledata : otherXray->GetElementQuantifyData())
  1798. {
  1799. mapEleData[eledata->GetName().GetBuffer()].push_back(EleAreaPercentage(other->GetArea() / allPartArea, eledata));
  1800. }
  1801. }
  1802. // third,we calculate all the element's new percentage data and get a new element chemistry list.
  1803. CElementChemistriesList newCheList;
  1804. for (auto eledata : mapEleData)
  1805. {
  1806. CElementChemistryPtr newEleche = CElementChemistryPtr(new CElementChemistry());
  1807. newEleche->SetName(CString(eledata.first.c_str()));
  1808. double newPercentage = 0;
  1809. for (auto d : eledata.second)
  1810. {
  1811. newPercentage += d.areaPercentage * d.eleData->GetPercentage();
  1812. }
  1813. newEleche->SetPercentage(newPercentage);
  1814. newCheList.push_back(newEleche);
  1815. }
  1816. CPosXrayPtr xray(new CPosXray());
  1817. xray->SetElementQuantifyData(newCheList);
  1818. newPart->SetXrayInfo(xray);
  1819. newPart->SetSubParticles(partsStr);
  1820. newPart->SetArea(allPartArea);
  1821. newPart->SetTagId(mergedParts.size());
  1822. newPart->SetAnalysisId(mergedParts.size());
  1823. std::string name = p->TypeName();
  1824. newPart->TypeName(name);
  1825. newPart->TypeColor(p->TypeColor());
  1826. mergedParts.push_back(newPart);
  1827. }
  1828. return true;
  1829. }
  1830. }