OTSImageProcess.cpp 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695
  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 > 10)
  86. {
  87. interval = edgePointNum / 10;//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. }
  894. else
  895. {
  896. // get the area image
  897. Mat blurImg;
  898. Mat srcImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);
  899. //medianBlur(srcImg, blurImg, 3);//smooth the edge
  900. COTSParticleList listParticleOut;
  901. if (!GetParticles(0,0,nWidthImg, nHeightImg, srcImg.data, listParticleOut))
  902. {
  903. COTSParticleList listParticleEmpty;
  904. listParticleEmpty.clear();
  905. m_pFieldData->SetParticleList(listParticleEmpty);
  906. }
  907. // form a image only have particles on
  908. COTSSegmentsList listImage;
  909. for (auto pParticle : listParticleOut)
  910. {
  911. COTSFeaturePtr pFeature = pParticle->GetFeature();
  912. COTSSegmentsList listSegment = pFeature->GetSegmentsList();
  913. long nPixelNum = 0;
  914. long nPixelAll = 0;
  915. int nStartS = 0;
  916. int nHeightS = 0;
  917. int nLengthS = 0;
  918. for (auto pSegment : listSegment)
  919. {
  920. // update image list
  921. COTSSegmentPtr pSegNew = COTSSegmentPtr(new COTSSegment(*pSegment.get()));
  922. listImage.push_back(pSegNew);
  923. // get particle average gray
  924. nStartS = pSegment->GetStart();
  925. nHeightS = pSegment->GetHeight();
  926. nLengthS = pSegment->GetLength();
  927. nPixelNum += (long)nLengthS;
  928. if (nHeightS > nHeightImg)
  929. {
  930. LogErrorTrace(__FILE__, __LINE__, _T("seg height is wrong."));
  931. return FALSE;
  932. }
  933. if ((nStartS + nLengthS - 1) > nWidthImg)
  934. {
  935. LogErrorTrace(__FILE__, __LINE__, _T("seg starst and length is wrong."));
  936. return FALSE;
  937. }
  938. for (unsigned int i = 0; i < nLengthS; i++)
  939. {
  940. if ((nStartS + i) > nWidthImg)
  941. {
  942. LogErrorTrace(__FILE__, __LINE__, _T("seg start is wrong."));
  943. return FALSE;
  944. }
  945. else if (nHeightS > nHeightImg)
  946. {
  947. LogErrorTrace(__FILE__, __LINE__, _T("seg height is wrong."));
  948. return FALSE;
  949. }
  950. long nValueTemp = (long)*(pSrcImg + nHeightS * nWidthImg + nStartS + i);
  951. nPixelAll += nValueTemp;
  952. }
  953. }
  954. BYTE nAveGray = (BYTE)(nPixelAll / nPixelNum);
  955. pParticle->SetAveGray(nAveGray);
  956. pParticle->SetArea(nPixelNum);
  957. }
  958. m_pFieldData->SetParticleList(listParticleOut);
  959. }
  960. delete[]pTempImg;
  961. return TRUE;
  962. }
  963. BOOL COTSImageProcess::RemoveBGByFindContour(CBSEImgPtr m_pBSEImg, COTSImageProcessParamPtr a_pImageProcessParam, COTSFieldDataPtr m_pFieldData)
  964. {
  965. ASSERT(m_pFieldData);
  966. ASSERT(m_pBSEImg);
  967. ASSERT(a_pImageProcessParam);
  968. int nWidthImg = m_pBSEImg->GetWidth();
  969. int nHeightImg = m_pBSEImg->GetHeight();
  970. m_pFieldData->Width = nWidthImg;
  971. m_pFieldData->Height = nHeightImg;
  972. long nImgSize = nWidthImg * nHeightImg;
  973. BYTE* pSrcImg = m_pBSEImg->GetImageDataPointer();
  974. BYTE* pTempImg = new BYTE[nImgSize];
  975. CBSEImgPtr imgNoBGBinary = CBSEImgPtr(new CBSEImg());
  976. long nNumParticle = 0;
  977. RemoveBackGround(m_pBSEImg, a_pImageProcessParam, imgNoBGBinary, nNumParticle);
  978. BYTE* pPixel = imgNoBGBinary->GetImageDataPointer();
  979. long nPtStart = a_pImageProcessParam->GetParticleGray().GetStart();
  980. long nPtEnd = a_pImageProcessParam->GetParticleGray().GetEnd();
  981. if (nNumParticle == 0)
  982. {
  983. COTSParticleList listParticleEmpty;
  984. listParticleEmpty.clear();
  985. m_pFieldData->SetParticleList(listParticleEmpty);
  986. }
  987. else
  988. {
  989. // get the area image
  990. Mat cvcopyImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);
  991. //Mat blurImg;
  992. //medianBlur(cvcopyImg, blurImg, 5);//smooth the edge
  993. vector<vector<Point>>contours;
  994. findContours(cvcopyImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
  995. COTSParticleList listParticleOut;
  996. for (size_t i = 0; i < contours.size(); i++)
  997. {
  998. Rect rectMax = boundingRect(contours[i]);
  999. Mat rectROI = cvcopyImg(rectMax).clone();
  1000. //exclude the point which intersect into this bounding box but is not in this contour.
  1001. for (int nX = 0; nX < rectROI.rows; nX++)
  1002. {
  1003. for (int nY = 0; nY < rectROI.cols; nY++)
  1004. {
  1005. double localPos = pointPolygonTest(contours[i], Point2f(nX + rectMax.x, nY + rectMax.y), false);
  1006. if (localPos == -1)
  1007. {
  1008. rectROI.data[nX, nY] = 0;//set the value to 0,so we won't consider it when we find segment and feature in this ROI.
  1009. }
  1010. }
  1011. }
  1012. GetParticles(rectMax.x, rectMax.y, rectMax.width, rectMax.height, rectROI.data, listParticleOut);
  1013. }
  1014. // form a image only have particles on
  1015. COTSSegmentsList listImage;
  1016. for (auto pParticle : listParticleOut)
  1017. {
  1018. COTSFeaturePtr pFeature = pParticle->GetFeature();
  1019. COTSSegmentsList listSegment = pFeature->GetSegmentsList();
  1020. long nPixelNum = 0;
  1021. long nPixelAll = 0;
  1022. int nStartS = 0;
  1023. int nHeightS = 0;
  1024. int nLengthS = 0;
  1025. for (auto pSegment : listSegment)
  1026. {
  1027. // update image list
  1028. COTSSegmentPtr pSegNew = COTSSegmentPtr(new COTSSegment(*pSegment.get()));
  1029. listImage.push_back(pSegNew);
  1030. // get particle average gray
  1031. nStartS = pSegment->GetStart();
  1032. nHeightS = pSegment->GetHeight();
  1033. nLengthS = pSegment->GetLength();
  1034. nPixelNum += (long)nLengthS;
  1035. if (nHeightS > nHeightImg)
  1036. {
  1037. //LogErrorTrace(__FILE__, __LINE__, _T("seg height is wrong."));
  1038. return FALSE;
  1039. }
  1040. if ((nStartS + nLengthS - 1) > nWidthImg)
  1041. {
  1042. //LogErrorTrace(__FILE__, __LINE__, _T("seg starst and length is wrong."));
  1043. return FALSE;
  1044. }
  1045. for (unsigned int i = 0; i < nLengthS; i++)
  1046. {
  1047. if ((nStartS + i) > nWidthImg)
  1048. {
  1049. //LogErrorTrace(__FILE__, __LINE__, _T("seg start is wrong."));
  1050. return FALSE;
  1051. }
  1052. else if (nHeightS > nHeightImg)
  1053. {
  1054. //LogErrorTrace(__FILE__, __LINE__, _T("seg height is wrong."));
  1055. return FALSE;
  1056. }
  1057. long nValueTemp = (long)*(pSrcImg + nHeightS * nWidthImg + nStartS + i);
  1058. nPixelAll += nValueTemp;
  1059. }
  1060. }
  1061. BYTE nAveGray = (BYTE)(nPixelAll / nPixelNum);
  1062. pParticle->SetAveGray(nAveGray);
  1063. pParticle->SetArea(nPixelNum);
  1064. }
  1065. m_pFieldData->SetParticleList(listParticleOut);
  1066. }
  1067. delete[]pTempImg;
  1068. return TRUE;
  1069. }
  1070. BOOL COTSImageProcess::RemoveBGByCVconnectivities(CBSEImgPtr m_pBSEImg, COTSImageProcessParamPtr a_pImageProcessParam, COTSFieldDataPtr m_pFieldData)
  1071. {
  1072. ASSERT(m_pFieldData);
  1073. ASSERT(m_pBSEImg);
  1074. ASSERT(a_pImageProcessParam);
  1075. int nWidthImg = m_pBSEImg->GetWidth();
  1076. int nHeightImg = m_pBSEImg->GetHeight();
  1077. m_pFieldData->Width = nWidthImg;
  1078. m_pFieldData->Height = nHeightImg;
  1079. long nImgSize = nWidthImg * nHeightImg;
  1080. BYTE* pSrcImg = m_pBSEImg->GetImageDataPointer();
  1081. BYTE* pTempImg = new BYTE[nImgSize];
  1082. CBSEImgPtr imgNoBGBinary = CBSEImgPtr(new CBSEImg());
  1083. long nNumParticle = 0;
  1084. RemoveBackGround(m_pBSEImg, a_pImageProcessParam, imgNoBGBinary, nNumParticle);
  1085. BYTE* pPixel = imgNoBGBinary->GetImageDataPointer();
  1086. long nPtStart = a_pImageProcessParam->GetParticleGray().GetStart();
  1087. long nPtEnd = a_pImageProcessParam->GetParticleGray().GetEnd();
  1088. if (nNumParticle == 0)
  1089. {
  1090. COTSParticleList listParticleEmpty;
  1091. listParticleEmpty.clear();
  1092. m_pFieldData->SetParticleList(listParticleEmpty);
  1093. }
  1094. else
  1095. {
  1096. // get the area image
  1097. Mat cvcopyImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);
  1098. //Mat blurImg;
  1099. //medianBlur(cvcopyImg, blurImg, 5);//get rid of the noise point.
  1100. Mat labels, stats, centroids;
  1101. int number = connectedComponentsWithStats(cvcopyImg, labels, stats, centroids, 8, CV_16U);
  1102. COTSParticleList listParticleOut;
  1103. for (size_t i = 1; i < number; i++)
  1104. {
  1105. int center_x = centroids.at<double>(i, 0);
  1106. int center_y = centroids.at<double>(i, 1);
  1107. //矩形边框
  1108. int x = stats.at<int>(i, CC_STAT_LEFT);
  1109. int y = stats.at<int>(i, CC_STAT_TOP);
  1110. int w = stats.at<int>(i, CC_STAT_WIDTH);
  1111. int h = stats.at<int>(i, CC_STAT_HEIGHT);
  1112. int area = stats.at<int>(i, CC_STAT_AREA);
  1113. Rect rectMax = Rect(x, y, w, h);
  1114. Mat rectROI = labels(rectMax).clone();
  1115. Mat imageROI = cvcopyImg(rectMax).clone();
  1116. //exclude the point which intersect into this bounding box but is not in this group.
  1117. int curlabel = i;
  1118. for (int nX = 0; nX < rectROI.rows; nX++)
  1119. {
  1120. for (int nY = 0; nY < rectROI.cols; nY++)
  1121. {
  1122. int v = rectROI.data[nX, nY];
  1123. if (v == curlabel)
  1124. {
  1125. imageROI.data[nX, nY] = 255;
  1126. }
  1127. else
  1128. {
  1129. imageROI.data[nX, nY] = 0;//set the value to 0,so we won't consider it when we find segment and feature in this ROI.
  1130. }
  1131. }
  1132. }
  1133. GetParticles(rectMax.x, rectMax.y, rectMax.width, rectMax.height, imageROI.data, listParticleOut);
  1134. }
  1135. // form a image only have particles on
  1136. COTSSegmentsList listImage;
  1137. for (auto pParticle : listParticleOut)
  1138. {
  1139. COTSFeaturePtr pFeature = pParticle->GetFeature();
  1140. COTSSegmentsList listSegment = pFeature->GetSegmentsList();
  1141. long nPixelNum = 0;
  1142. long nPixelAll = 0;
  1143. int nStartS = 0;
  1144. int nHeightS = 0;
  1145. int nLengthS = 0;
  1146. for (auto pSegment : listSegment)
  1147. {
  1148. // update image list
  1149. COTSSegmentPtr pSegNew = COTSSegmentPtr(new COTSSegment(*pSegment.get()));
  1150. listImage.push_back(pSegNew);
  1151. // get particle average gray
  1152. nStartS = pSegment->GetStart();
  1153. nHeightS = pSegment->GetHeight();
  1154. nLengthS = pSegment->GetLength();
  1155. nPixelNum += (long)nLengthS;
  1156. if (nHeightS > nHeightImg)
  1157. {
  1158. //LogErrorTrace(__FILE__, __LINE__, _T("seg height is wrong."));
  1159. return FALSE;
  1160. }
  1161. if ((nStartS + nLengthS - 1) > nWidthImg)
  1162. {
  1163. //LogErrorTrace(__FILE__, __LINE__, _T("seg starst and length is wrong."));
  1164. return FALSE;
  1165. }
  1166. for (unsigned int i = 0; i < nLengthS; i++)
  1167. {
  1168. if ((nStartS + i) > nWidthImg)
  1169. {
  1170. //LogErrorTrace(__FILE__, __LINE__, _T("seg start is wrong."));
  1171. return FALSE;
  1172. }
  1173. else if (nHeightS > nHeightImg)
  1174. {
  1175. //LogErrorTrace(__FILE__, __LINE__, _T("seg height is wrong."));
  1176. return FALSE;
  1177. }
  1178. long nValueTemp = (long)*(pSrcImg + nHeightS * nWidthImg + nStartS + i);
  1179. nPixelAll += nValueTemp;
  1180. }
  1181. }
  1182. BYTE nAveGray = (BYTE)(nPixelAll / nPixelNum);
  1183. pParticle->SetAveGray(nAveGray);
  1184. pParticle->SetArea(nPixelNum);
  1185. }
  1186. m_pFieldData->SetParticleList(listParticleOut);
  1187. }
  1188. delete[]pTempImg;
  1189. return TRUE;
  1190. }
  1191. BOOL COTSImageProcess::GetParticlesBySpecialGrayRange(CBSEImgPtr a_pBSEImg, CIntRangePtr a_grayRange, COTSFieldDataPtr m_pFieldData)
  1192. {
  1193. ASSERT(m_pFieldData);
  1194. ASSERT(a_pBSEImg);
  1195. ASSERT(a_grayRange);
  1196. int nWidthImg = a_pBSEImg->GetWidth();
  1197. int nHeightImg = a_pBSEImg->GetHeight();
  1198. m_pFieldData->Width = nWidthImg;
  1199. m_pFieldData->Height = nHeightImg;
  1200. long nImgSize = nWidthImg * nHeightImg;
  1201. BYTE* pSrcImg = a_pBSEImg->GetImageDataPointer();
  1202. BYTE* pTempImg = new BYTE[nImgSize];
  1203. CBSEImgPtr imgNoBGBinary = CBSEImgPtr(new CBSEImg());
  1204. long nNumParticle = 0;
  1205. GetSpecialGrayRangeImage(a_pBSEImg, a_grayRange, imgNoBGBinary, nNumParticle);
  1206. BYTE* pPixel = imgNoBGBinary->GetImageDataPointer();
  1207. if (nNumParticle == 0)
  1208. {
  1209. COTSParticleList listParticleEmpty;
  1210. listParticleEmpty.clear();
  1211. m_pFieldData->SetParticleList(listParticleEmpty);
  1212. }
  1213. else
  1214. {
  1215. // get the area image
  1216. Mat cvcopyImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);
  1217. //Mat blurImg;
  1218. //medianBlur(cvcopyImg, blurImg, 3);//smooth the edge
  1219. Mat labels, stats, centroids;
  1220. int number = connectedComponentsWithStats(cvcopyImg, labels, stats, centroids, 8, CV_16U);
  1221. COTSParticleList listParticleOut;
  1222. for (size_t i = 1; i < number; i++)
  1223. {
  1224. int center_x = centroids.at<double>(i, 0);
  1225. int center_y = centroids.at<double>(i, 1);
  1226. //矩形边框
  1227. int x = stats.at<int>(i, CC_STAT_LEFT);
  1228. int y = stats.at<int>(i, CC_STAT_TOP);
  1229. int w = stats.at<int>(i, CC_STAT_WIDTH);
  1230. int h = stats.at<int>(i, CC_STAT_HEIGHT);
  1231. int area = stats.at<int>(i, CC_STAT_AREA);
  1232. Rect rectMax = Rect(x, y, w, h);
  1233. Mat rectROI = labels(rectMax).clone();
  1234. Mat imageROI = cvcopyImg(rectMax).clone();
  1235. //exclude the point which intersect into this bounding box but is not in this group.
  1236. int label = i;
  1237. for (int nX = 0; nX < rectROI.rows; nX++)
  1238. {
  1239. for (int nY = 0; nY < rectROI.cols; nY++)
  1240. {
  1241. int v = rectROI.data[nX, nY];
  1242. if (v == label)
  1243. {
  1244. imageROI.data[nX, nY] = 255;
  1245. }
  1246. else
  1247. {
  1248. imageROI.data[nX, nY] = 0;//set the value to 0,so we won't consider it when we find segment and feature in this ROI.
  1249. }
  1250. }
  1251. }
  1252. GetParticles(rectMax.x, rectMax.y, rectMax.width, rectMax.height, imageROI.data, listParticleOut);
  1253. }
  1254. // form a image only have particles on
  1255. COTSSegmentsList listImage;
  1256. for (auto pParticle : listParticleOut)
  1257. {
  1258. COTSFeaturePtr pFeature = pParticle->GetFeature();
  1259. COTSSegmentsList listSegment = pFeature->GetSegmentsList();
  1260. long nPixelNum = 0;
  1261. long nPixelAll = 0;
  1262. int nStartS = 0;
  1263. int nHeightS = 0;
  1264. int nLengthS = 0;
  1265. for (auto pSegment : listSegment)
  1266. {
  1267. // update image list
  1268. COTSSegmentPtr pSegNew = COTSSegmentPtr(new COTSSegment(*pSegment.get()));
  1269. listImage.push_back(pSegNew);
  1270. // get particle average gray
  1271. nStartS = pSegment->GetStart();
  1272. nHeightS = pSegment->GetHeight();
  1273. nLengthS = pSegment->GetLength();
  1274. nPixelNum += (long)nLengthS;
  1275. if (nHeightS > nHeightImg)
  1276. {
  1277. //LogErrorTrace(__FILE__, __LINE__, _T("seg height is wrong."));
  1278. return FALSE;
  1279. }
  1280. if ((nStartS + nLengthS - 1) > nWidthImg)
  1281. {
  1282. //LogErrorTrace(__FILE__, __LINE__, _T("seg starst and length is wrong."));
  1283. return FALSE;
  1284. }
  1285. for (unsigned int i = 0; i < nLengthS; i++)
  1286. {
  1287. if ((nStartS + i) > nWidthImg)
  1288. {
  1289. //LogErrorTrace(__FILE__, __LINE__, _T("seg start is wrong."));
  1290. return FALSE;
  1291. }
  1292. else if (nHeightS > nHeightImg)
  1293. {
  1294. //LogErrorTrace(__FILE__, __LINE__, _T("seg height is wrong."));
  1295. return FALSE;
  1296. }
  1297. long nValueTemp = (long)*(pSrcImg + nHeightS * nWidthImg + nStartS + i);
  1298. nPixelAll += nValueTemp;
  1299. }
  1300. }
  1301. BYTE nAveGray = (BYTE)(nPixelAll / nPixelNum);
  1302. pParticle->SetAveGray(nAveGray);
  1303. pParticle->SetArea(nPixelNum);
  1304. }
  1305. m_pFieldData->SetParticleList(listParticleOut);
  1306. }
  1307. delete[]pTempImg;
  1308. return TRUE;
  1309. }
  1310. CIntRangePtr COTSImageProcess::CalBackground(CBSEImgPtr m_pBSEImg)
  1311. {
  1312. CIntRangePtr pBackground = CIntRangePtr(new CIntRange());
  1313. WORD nBSEChart[MAXBYTE];
  1314. //1. get chart data
  1315. m_pBSEImg->SetChartData();
  1316. linearSmooth5(m_pBSEImg->GetBSEChart(), nBSEChart,MAXBYTE);
  1317. //2. get down edge
  1318. int nLengthEdge = MAXBYTE + 2;
  1319. WORD n_aBSEChart[MAXBYTE + 2];
  1320. memset(n_aBSEChart, 0, sizeof(WORD) * nLengthEdge);
  1321. std::map<long, std::vector <int>> upEdgeSeries;
  1322. std::map<long, std::vector<int>> downEdgeSeries;
  1323. std::vector<int> currentUpSeries;
  1324. std::vector<int> currentDownSeries;
  1325. // make sure the wave begin with up edge and end with down edge
  1326. n_aBSEChart[0] = 0;
  1327. n_aBSEChart[nLengthEdge - 1] = 0;
  1328. memcpy(&n_aBSEChart[1], &nBSEChart, sizeof(WORD) * MAXBYTE);
  1329. int nLengthCom = MAXBYTE + 1;
  1330. // up edge
  1331. for (int i = 0; i < nLengthCom; i++)
  1332. {
  1333. if (n_aBSEChart[i] <= n_aBSEChart[i + 1])
  1334. {
  1335. if (currentDownSeries.size() > 0)
  1336. {
  1337. int seriesSize = currentDownSeries.size();
  1338. long area = 0;
  1339. for (int i = 0; i < seriesSize; i++)
  1340. {
  1341. area = area + n_aBSEChart[currentDownSeries[i]];
  1342. }
  1343. downEdgeSeries[area] = currentDownSeries;
  1344. currentDownSeries.clear();
  1345. }
  1346. currentUpSeries.push_back(i);
  1347. }
  1348. else
  1349. {
  1350. if (currentUpSeries.size() > 0)
  1351. {
  1352. int seriesSize = currentUpSeries.size();
  1353. long area = 0;
  1354. for (int i = 0; i < seriesSize; i++)
  1355. {
  1356. area = area + n_aBSEChart[currentUpSeries[i]];
  1357. }
  1358. upEdgeSeries[area] = currentUpSeries;
  1359. currentUpSeries.clear();
  1360. }
  1361. currentDownSeries.push_back(i);
  1362. }
  1363. }
  1364. CIntRangePtr pRangeFirst = CIntRangePtr(new CIntRange());
  1365. if (upEdgeSeries.size() > 0)
  1366. {
  1367. pRangeFirst->SetStart((upEdgeSeries.rbegin())->second[0]);
  1368. }
  1369. if (downEdgeSeries.size() > 0)
  1370. {
  1371. auto s = downEdgeSeries.rbegin()->second;
  1372. pRangeFirst->SetEnd((long)(s[s.size() - 1]));
  1373. }
  1374. return pRangeFirst;
  1375. }
  1376. void COTSImageProcess::GetSpecialGrayRangeImage(CBSEImgPtr a_pImgIn, CIntRangePtr a_SpecialGrayRange, CBSEImgPtr a_pBinImgOut, long& foundedPixelNum)
  1377. {
  1378. // the background pixel will be 0,and the other part will be 255.
  1379. ASSERT(a_pImgIn);
  1380. int nWidthImg = a_pImgIn->GetWidth();
  1381. int nHeightImg = a_pImgIn->GetHeight();
  1382. long nImgSize = nWidthImg * nHeightImg;
  1383. BYTE* pTempImg = new BYTE[nImgSize];
  1384. BYTE* pSrcImg = a_pImgIn->GetImageDataPointer();
  1385. BYTE* pPixel = new byte[nImgSize];
  1386. long nBGStart;
  1387. long nBGEnd;
  1388. long nNumParticle = 0;
  1389. nBGStart = a_SpecialGrayRange->GetStart();
  1390. nBGEnd = a_SpecialGrayRange->GetEnd();
  1391. // delete background
  1392. for (unsigned int i = 0; i < nImgSize; i++)
  1393. {
  1394. if (pSrcImg[i] >= nBGStart && pSrcImg[i] <= nBGEnd)
  1395. {
  1396. pPixel[i] = 255;
  1397. nNumParticle++;
  1398. }
  1399. else
  1400. {
  1401. pPixel[i] = 0;
  1402. }
  1403. }
  1404. /*COTSImageProcess::BErode3(pPixel, pTempImg, 5, nHeightImg, nWidthImg);
  1405. COTSImageProcess::BDilate3(pTempImg, pPixel, 5, nHeightImg, nWidthImg);*/
  1406. Mat cvcopyImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);// use the medianblur method to achieve the same effect as open morphology(errod and dialate).
  1407. medianBlur(cvcopyImg, cvcopyImg, 5);
  1408. pPixel = cvcopyImg.data;
  1409. a_pBinImgOut->SetImageData(pPixel, nWidthImg, nHeightImg);
  1410. foundedPixelNum = nNumParticle;
  1411. delete[] pTempImg;
  1412. return;
  1413. }
  1414. void COTSImageProcess::RemoveBackGround(CBSEImgPtr a_pImgIn, COTSImageProcessParamPtr a_pImageProcessParam, CBSEImgPtr a_pBinImgOut,long& foundedPixelNum)
  1415. {
  1416. // the background pixel will be 0,and the other part will be 255.
  1417. ASSERT(a_pImgIn);
  1418. ASSERT(a_pImageProcessParam);
  1419. int nWidthImg = a_pImgIn->GetWidth();
  1420. int nHeightImg = a_pImgIn->GetHeight();
  1421. long nImgSize = nWidthImg * nHeightImg;
  1422. BYTE* pTempImg = new BYTE[nImgSize];
  1423. BYTE* pSrcImg = a_pImgIn->GetImageDataPointer();
  1424. BYTE* pPixel = new byte[nImgSize];
  1425. long nBGStart;
  1426. long nBGEnd;
  1427. long nPartStart;
  1428. long nPartEnd;
  1429. long nNumParticle = 0;
  1430. if (a_pImageProcessParam->GetBGRemoveType() == OTS_BGREMOVE_TYPE::MANUAL)
  1431. {
  1432. nBGStart = a_pImageProcessParam->GetBGGray().GetStart();
  1433. nBGEnd = a_pImageProcessParam->GetBGGray().GetEnd();
  1434. nPartStart = a_pImageProcessParam->GetParticleGray().GetStart();
  1435. nPartEnd = a_pImageProcessParam->GetParticleGray().GetEnd();
  1436. // delete background
  1437. for (unsigned int i = 0; i < nImgSize; i++)
  1438. {
  1439. if (pSrcImg[i] >= nBGStart && pSrcImg[i] <= nBGEnd)
  1440. {
  1441. pPixel[i] = 0;
  1442. }
  1443. else
  1444. {
  1445. pPixel[i] = 255;
  1446. nNumParticle++;
  1447. }
  1448. if (pSrcImg[i]<nPartStart || pSrcImg[i]>nPartEnd)
  1449. {
  1450. pPixel[i] = 0;
  1451. }
  1452. }
  1453. COTSImageProcess::BErode3(pPixel, pTempImg, 5, nHeightImg, nWidthImg);
  1454. COTSImageProcess::BDilate3(pTempImg, pPixel, 5, nHeightImg, nWidthImg);
  1455. Mat cvcopyImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);// use the medianblur method to achieve the same effect as open morphology(errod and dialate).
  1456. medianBlur(cvcopyImg, cvcopyImg, 5);
  1457. pPixel = cvcopyImg.data;
  1458. }
  1459. else
  1460. {
  1461. auto range = CalBackground(a_pImgIn);
  1462. nBGStart = range->GetStart();
  1463. nBGEnd = range->GetEnd();
  1464. switch (a_pImageProcessParam->GetAutoBGRemoveType())
  1465. {
  1466. case OTS_AUTOBGREMOVE_TYPE::DOWNWARD:
  1467. for (unsigned int i = 0; i < nImgSize; i++)
  1468. {
  1469. if (pSrcImg[i] <= nBGEnd)
  1470. {
  1471. pPixel[i] = 0;
  1472. }
  1473. else
  1474. {
  1475. pPixel[i] = 255;
  1476. nNumParticle++;
  1477. }
  1478. }
  1479. break;
  1480. case OTS_AUTOBGREMOVE_TYPE::UPWARD:
  1481. for (unsigned int i = 0; i < nImgSize; i++)
  1482. {
  1483. if (pSrcImg[i] >= nBGStart)
  1484. {
  1485. pPixel[i] = 0;
  1486. }
  1487. else
  1488. {
  1489. pPixel[i] = 255;
  1490. nNumParticle++;
  1491. }
  1492. }
  1493. break;
  1494. case OTS_AUTOBGREMOVE_TYPE::MIDDLE:
  1495. for (unsigned int i = 0; i < nImgSize; i++)
  1496. {
  1497. if (pSrcImg[i] >= nBGStart && pSrcImg[i] <= nBGEnd)
  1498. {
  1499. pPixel[i] = 0;
  1500. }
  1501. else
  1502. {
  1503. pPixel[i] = 255;
  1504. nNumParticle++;
  1505. }
  1506. }
  1507. break;
  1508. default:
  1509. break;
  1510. }
  1511. COTSImageProcess::BErode3(pPixel, pTempImg, 5, nHeightImg, nWidthImg);
  1512. COTSImageProcess::BDilate3(pTempImg, pPixel, 5, nHeightImg, nWidthImg);
  1513. Mat cvcopyImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);
  1514. medianBlur(cvcopyImg, cvcopyImg, 5);
  1515. pPixel = cvcopyImg.data;
  1516. }
  1517. a_pBinImgOut->SetImageData(pPixel,nWidthImg,nHeightImg);
  1518. foundedPixelNum = nNumParticle;
  1519. delete[] pTempImg;
  1520. return ;
  1521. }
  1522. BOOL COTSImageProcess::GetParticles(long left, long top, long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSParticleList& a_listParticles)
  1523. {
  1524. ASSERT(a_pPixel);
  1525. if (!a_pPixel)
  1526. {
  1527. return FALSE;
  1528. }
  1529. //a_listParticles.clear();
  1530. COTSParticleList findedParts;
  1531. COTSSegmentsList listSegment;
  1532. listSegment.clear();
  1533. //1. get segment line by line
  1534. if (!GetSegmentList(left, top, a_nWidth, a_nHeight, a_pPixel, listSegment))
  1535. {
  1536. return FALSE;
  1537. }
  1538. if ((int)listSegment.size() == 0)
  1539. {
  1540. return FALSE;
  1541. }
  1542. //2. save the temp feature
  1543. COTSFeatureList listFeature;
  1544. listFeature.clear();
  1545. if (!GetFeatureList(listSegment, listFeature))//get every feature for all the particle,the complete feature.
  1546. {
  1547. return FALSE;
  1548. }
  1549. if ((int)listFeature.size() == 0)
  1550. {
  1551. return FALSE;
  1552. }
  1553. COTSParticleList listParticles;
  1554. listParticles.clear();
  1555. if (!ChangeFeaturelist(listFeature, findedParts))
  1556. {
  1557. return FALSE;
  1558. }
  1559. for (auto f : findedParts)
  1560. {
  1561. a_listParticles.push_back(f);
  1562. }
  1563. return TRUE;
  1564. }
  1565. BOOL COTSImageProcess::GetSegmentList(long left, long top, long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSSegmentsList& a_listSegments)
  1566. {
  1567. ASSERT(a_pPixel);
  1568. if (!a_pPixel)
  1569. {
  1570. //LogErrorTrace(__FILE__, __LINE__, _T("GetSegments: there is no image data"));
  1571. return FALSE;
  1572. }
  1573. long nImgSize = a_nWidth * a_nHeight;
  1574. a_listSegments.clear();
  1575. //1. get segment line by line
  1576. long nLine, nm, nn;
  1577. long nStart = 0, nLength = 0;
  1578. for (nLine = 0; nLine < a_nHeight; nLine++)
  1579. {
  1580. for (nm = 0; nm < a_nWidth; nm += (nLength + 1))
  1581. {
  1582. nLength = 0;
  1583. // get start
  1584. if (*(a_pPixel + nLine * a_nWidth + nm) != 0)
  1585. {
  1586. nStart = nm;
  1587. nLength++;
  1588. //get length
  1589. for (nn = nm + 1; nn < a_nWidth; nn++)
  1590. {
  1591. // check if segment is over, break
  1592. if (nLength != 0)
  1593. {
  1594. if (*(a_pPixel + nLine * a_nWidth + nn) == 0)
  1595. break;
  1596. }
  1597. if (*(a_pPixel + nLine * a_nWidth + nn) != 0)
  1598. {
  1599. nLength++;
  1600. }
  1601. }
  1602. // generate segment
  1603. COTSSegmentPtr pSegment = COTSSegmentPtr(new COTSSegment(nLine + top, nStart + left, nLength));
  1604. a_listSegments.push_back(pSegment);
  1605. }
  1606. else
  1607. {
  1608. continue;
  1609. }
  1610. }
  1611. }
  1612. if ((int)a_listSegments.size() == 0)
  1613. {
  1614. //LogErrorTrace(__FILE__, __LINE__, _T("no particle is found."));
  1615. return FALSE;
  1616. }
  1617. return TRUE;
  1618. }
  1619. BOOL COTSImageProcess::GetFeatureList(COTSSegmentsList& a_listSegments, COTSFeatureList& a_listFeatures)
  1620. {
  1621. COTSSegmentsList listSegmentNew;
  1622. std::map<long, COTSSegmentsList > mapOneLineSegments;
  1623. for each (auto s in a_listSegments)
  1624. {
  1625. mapOneLineSegments[s->GetHeight()].push_back(s);//sorting all the segments base on the line number.
  1626. }
  1627. std::map<long, COTSSegmentsList >::iterator lineItr = mapOneLineSegments.begin();//find the highest line
  1628. while (lineItr != mapOneLineSegments.end())
  1629. {
  1630. for (auto s = lineItr->second.begin(); s < lineItr->second.end(); )//find one segment of this line.
  1631. {
  1632. COTSSegmentPtr bottomSeg = *s;
  1633. listSegmentNew.clear();
  1634. listSegmentNew.push_back(*s);
  1635. s = lineItr->second.erase(s);
  1636. std::map<long, COTSSegmentsList >::iterator tempItr = lineItr;
  1637. tempItr++;
  1638. for (; tempItr != mapOneLineSegments.end(); tempItr++)//find all other lines of segments
  1639. {
  1640. if (tempItr->first - bottomSeg->GetHeight() > 1)
  1641. {
  1642. break;
  1643. }
  1644. for (auto nextLineSegment = tempItr->second.begin(); nextLineSegment < tempItr->second.end();)//find next line's all segments
  1645. {
  1646. if (((*nextLineSegment)->GetStart() - (bottomSeg->GetStart() + bottomSeg->GetLength())) > 1)
  1647. {
  1648. break;
  1649. }
  1650. if (bottomSeg->UpDownConection(**nextLineSegment))
  1651. {
  1652. listSegmentNew.push_back(*nextLineSegment);
  1653. bottomSeg = *nextLineSegment;
  1654. nextLineSegment = tempItr->second.erase(nextLineSegment);
  1655. break;
  1656. }
  1657. if (tempItr->second.size() > 0)
  1658. {
  1659. nextLineSegment++;
  1660. }
  1661. else
  1662. {
  1663. break;
  1664. }
  1665. }
  1666. }
  1667. COTSFeaturePtr pFeature = COTSFeaturePtr(new COTSFeature());
  1668. pFeature->SetSegmentsList(listSegmentNew);
  1669. //check if this new feature is connected with other found feature.
  1670. COTSSegmentPtr topSeg = listSegmentNew[0];//find the toppest segment of this new feature.
  1671. COTSSegmentPtr bottomSegment = listSegmentNew[listSegmentNew.size() - 1];//find the lowest segment of this new feature.
  1672. bool haveMerged = false;
  1673. for each (auto f in a_listFeatures)
  1674. {
  1675. for (auto seg : f->GetSegmentsList())
  1676. {
  1677. if (bottomSegment->UpDownConection(*seg) || topSeg->UpDownConection(*seg))
  1678. {
  1679. COTSSegmentsList segs = f->GetSegmentsList();
  1680. for (auto s : listSegmentNew)
  1681. {
  1682. segs.push_back(s);
  1683. }
  1684. f->SetSegmentsList(segs);
  1685. haveMerged = true;
  1686. break;
  1687. }
  1688. }
  1689. if (haveMerged)
  1690. {
  1691. break;
  1692. }
  1693. }
  1694. if (!haveMerged)
  1695. {
  1696. a_listFeatures.push_back(pFeature);
  1697. }
  1698. if (lineItr->second.size() == 0)
  1699. {
  1700. break;
  1701. }
  1702. }
  1703. lineItr++;
  1704. }
  1705. return true;
  1706. }
  1707. BOOL COTSImageProcess::ChangeFeaturelist(COTSFeatureList& a_listFeatures, COTSParticleList& a_listParticle)
  1708. {
  1709. if (a_listFeatures.size() == 0)
  1710. {
  1711. return FALSE;
  1712. }
  1713. // compute Rect
  1714. for (auto pFeature : a_listFeatures)
  1715. {
  1716. COTSParticlePtr pParticle = COTSParticlePtr(new COTSParticle());
  1717. pParticle->SetFeature(pFeature);
  1718. if (!pParticle->CalCoverRect())
  1719. {
  1720. return FALSE;
  1721. }
  1722. a_listParticle.push_back(pParticle);
  1723. }
  1724. if ((int)a_listParticle.size() == 0)
  1725. {
  1726. return FALSE;
  1727. }
  1728. return TRUE;
  1729. }
  1730. BOOL COTSImageProcess::CalcuParticleImagePropertes(COTSParticlePtr a_pOTSPart, double a_PixelSize)
  1731. {
  1732. //--------- convert this particle data to image data,construct an image only with this particle.------
  1733. const int nExpand_Size = 3;
  1734. const int nWhiteColor = 0;
  1735. const int nThickness = 1;
  1736. // lineType Type of the line
  1737. const int nLineType = 8;
  1738. // get rectangle of the particle
  1739. CRect rect = a_pOTSPart->GetParticleRect();
  1740. if (a_pOTSPart->GetArea() < 30 * 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.
  1741. {
  1742. double w = 0, h = 0;
  1743. w = (double)rect.Width()*a_PixelSize;
  1744. h = (double)rect.Height()*a_PixelSize;
  1745. a_pOTSPart->SetDMax(MAX(w, h));
  1746. a_pOTSPart->SetDMin(MIN(w, h));
  1747. a_pOTSPart->SetDMean((w + h) / 2);
  1748. a_pOTSPart->SetFeretDiameter((w + h) / 2);
  1749. a_pOTSPart->SetDElong(MAX(w, h));
  1750. a_pOTSPart->SetPerimeter((w+h)*2);
  1751. a_pOTSPart->SetDPerp(MIN(w, h));
  1752. a_pOTSPart->SetDInscr(MIN(w, h));
  1753. return true;
  1754. }
  1755. // calculate the particle image data size, expand 3 pixel at the edge
  1756. Mat particleImage = Mat::zeros(rect.Height() + nExpand_Size , rect.Width() + nExpand_Size , CV_8U);
  1757. // get the segment list
  1758. COTSSegmentsList listSegment = a_pOTSPart->GetFeature()->GetSegmentsList();
  1759. for (auto pSegment : listSegment)
  1760. {
  1761. int nStart = pSegment->GetStart() - rect.left + nExpand_Size;
  1762. int nEnd = pSegment->GetStart() + pSegment->GetLength() - rect.left - 1 + nExpand_Size;
  1763. int nHeight = pSegment->GetHeight() - rect.top + nExpand_Size;
  1764. line(particleImage, Point(nStart, nHeight), Point(nEnd, nHeight), Scalar(nBlackColor), nThickness, nLineType);
  1765. }
  1766. //--------abstract the contour of the particle.
  1767. Mat cvcopyImg;
  1768. medianBlur(particleImage, cvcopyImg, 5);//smooth the edge
  1769. vector<vector<Point>>contours;
  1770. findContours(cvcopyImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
  1771. 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.
  1772. {
  1773. double w = 0, h = 0;
  1774. w = (double)rect.Width()*a_PixelSize;
  1775. h = (double)rect.Height()*a_PixelSize;
  1776. a_pOTSPart->SetDMax(MAX(w, h));
  1777. a_pOTSPart->SetDMin(MIN(w, h));
  1778. a_pOTSPart->SetDMean((w + h) / 2);
  1779. a_pOTSPart->SetFeretDiameter((w + h) / 2);
  1780. a_pOTSPart->SetDElong(MAX(w, h));
  1781. a_pOTSPart->SetPerimeter((w + h) * 2);
  1782. a_pOTSPart->SetDPerp(MIN(w, h));
  1783. a_pOTSPart->SetDInscr(MIN(w, h));
  1784. return true;
  1785. }
  1786. int imaxcontour = 0, imax = 0;
  1787. for (unsigned int i = 0; i < contours.size(); i++) {
  1788. int itmp = contourArea(contours[i]);
  1789. if (imaxcontour < itmp) {
  1790. imax = i;
  1791. imaxcontour = itmp;
  1792. }
  1793. }
  1794. vector<Point > listEdge = contours[imax];
  1795. vector<vector<Point>>Outcontours;
  1796. Outcontours.push_back(listEdge);
  1797. //---------calculate the minimium rectangle
  1798. auto rRect = cv::minAreaRect(listEdge);
  1799. Point2f p[4];
  1800. rRect.points(p);
  1801. int D_MIN =getDistance(p[0], p[1]);
  1802. int D_MinRecLen = 0;//minareaRect's length(the lenger side).
  1803. for (int j = 0; j <= 2; j++)
  1804. {
  1805. //line(cvContourImg, p[j], p[(j + 1) % 4], Scalar(100, 100, 0), 2);
  1806. int d = getDistance(p[j], p[j + 1]);
  1807. if (d < D_MIN)
  1808. {
  1809. D_MIN = d;
  1810. }
  1811. if (d > D_MinRecLen)
  1812. {
  1813. D_MinRecLen = d;
  1814. }
  1815. }
  1816. a_pOTSPart->SetDMin(D_MIN*a_PixelSize);
  1817. a_pOTSPart->SetOrientation(rRect.angle);
  1818. //----------calculate the perimeter
  1819. double d = arcLength(listEdge, true);
  1820. a_pOTSPart->SetPerimeter(d*a_PixelSize);
  1821. //-----------calculate the Max Diameter. Find the min enclosing circle first ,then find the two farthest circle connected point.
  1822. Point2f center; float radius;
  1823. minEnclosingCircle(listEdge, center, radius);
  1824. //circle(cvContourImg, center, radius, Scalar(100), 2);
  1825. std::vector <Point> outContour = listEdge;
  1826. std::vector <Point> rst;
  1827. for (unsigned int k = 0; k < outContour.size(); k++)
  1828. {
  1829. Point p = outContour[k];
  1830. double d = sqrt(pow((p.x - center.x), 2) + pow((p.y - center.y), 2));
  1831. if (fabs(d - radius) < 0.01)
  1832. {
  1833. rst.push_back(p);
  1834. }
  1835. }
  1836. double D_MAX = 0;
  1837. Point lineDmax[2];
  1838. for (unsigned int m = 0; m < rst.size(); m++)
  1839. {
  1840. Point p = rst[m];
  1841. for (unsigned int n = m + 1; n < rst.size(); n++)
  1842. {
  1843. Point p1 = rst[n];
  1844. double d = sqrt(powf((p.x - p1.x), 2) + powf((p.y - p1.y), 2));
  1845. if (d > D_MAX)
  1846. {
  1847. D_MAX = d;
  1848. lineDmax[0] = p;
  1849. lineDmax[1] = p1;
  1850. }
  1851. }
  1852. }
  1853. a_pOTSPart->SetDMax(D_MAX*a_PixelSize);
  1854. //--------calculate the D_PERP property using the D_MAX's two endpoints.
  1855. std::vector<Point> curve1;
  1856. std::vector<Point> curve2;
  1857. for (unsigned int i = 0; i < outContour.size(); i++)
  1858. {
  1859. Point pt = outContour[i];
  1860. bool start = false;
  1861. int clockwise = Side(lineDmax[0], lineDmax[1], pt);// devide these points into two group ,separate into the two sides.
  1862. if (clockwise > 0)
  1863. {
  1864. curve1.push_back(pt);
  1865. }
  1866. else
  1867. {
  1868. curve2.push_back(pt);
  1869. }
  1870. }
  1871. double d_perp1 = 0, d_perp2 = 0;
  1872. for (unsigned int i = 0; i < curve1.size(); i++)
  1873. {
  1874. double d = getDist_P2L(curve1[i], lineDmax[0], lineDmax[1]);
  1875. if (d > d_perp1)
  1876. {
  1877. d_perp1 = d;
  1878. }
  1879. }
  1880. for (unsigned int i = 0; i < curve2.size(); i++)
  1881. {
  1882. double d = getDist_P2L(curve2[i], lineDmax[0], lineDmax[1]);
  1883. if (d > d_perp2)
  1884. {
  1885. d_perp2 = d;
  1886. }
  1887. }
  1888. a_pOTSPart->SetDPerp((d_perp1 + d_perp2)*a_PixelSize);
  1889. //----------find the diameter of max inscribed circle
  1890. int r;
  1891. Point inscribeCirclecenter;
  1892. FindInnerCircleInContour(outContour, inscribeCirclecenter, r);
  1893. //circle(cvContourImg, inscribeCirclecenter, r, Scalar(200));
  1894. a_pOTSPart->SetDInscr(r * 2 * a_PixelSize);
  1895. //---------------calculate the image other caracater: length/width realArea/minRectangeArea etc. we can use these propertes to do forward process.
  1896. double minRectArea = D_MIN * D_MinRecLen*a_PixelSize*a_PixelSize;//最小外接矩形面积
  1897. double fillRatio = a_pOTSPart->GetArea() / minRectArea;//实际面积与最小外接矩形面积比,that's the fill rate.
  1898. double lengthWidthRatio;
  1899. lengthWidthRatio = (double)D_MinRecLen / D_MIN;//长宽比
  1900. //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.
  1901. bool isStripShape = false;
  1902. double curveLength = 0;
  1903. double D_MEAN=0;
  1904. Moments mu;
  1905. mu = moments(listEdge, false);
  1906. int nx = mu.m10 / mu.m00;
  1907. int ny = mu.m01 / mu.m00;
  1908. //circle(cvcopyImg, Point(nx, ny), 1, (255), 1);
  1909. Point ptCenter = Point((int)nx, (int)ny);
  1910. if (pointPolygonTest(outContour, ptCenter, false) != 1)// the center point doesn't contain in the contour, we think it as curve shape.
  1911. {
  1912. isStripShape = true;
  1913. }
  1914. /*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.
  1915. {
  1916. isStripShape = true;
  1917. }*/
  1918. 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.
  1919. {
  1920. isStripShape = true;
  1921. }
  1922. if (isStripShape)
  1923. {
  1924. 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.
  1925. if (curveLength < D_MAX)
  1926. {
  1927. curveLength = D_MAX;
  1928. }
  1929. if (curveLength < MIN_DOUBLE_VALUE || a_pOTSPart->GetArea()<MIN_DOUBLE_VALUE)
  1930. {
  1931. D_MEAN = 0;
  1932. }
  1933. else
  1934. {
  1935. D_MEAN = a_pOTSPart->GetArea() / curveLength;
  1936. }
  1937. a_pOTSPart->SetDMean(D_MEAN*a_PixelSize);
  1938. a_pOTSPart->SetFeretDiameter(D_MEAN*a_PixelSize);
  1939. a_pOTSPart->SetDElong (curveLength*a_PixelSize);
  1940. }
  1941. else//it's a ball shape particle
  1942. {
  1943. curveLength = D_MAX;
  1944. double ftd = 0, maxD = 0, minD = 0, dratio = 0;
  1945. GetParticleAverageChord(outContour, a_PixelSize, ftd);
  1946. a_pOTSPart->SetDMean(ftd);
  1947. a_pOTSPart->SetFeretDiameter(ftd);
  1948. a_pOTSPart->SetDElong(curveLength*a_PixelSize);
  1949. }
  1950. return true;
  1951. }
  1952. BOOL COTSImageProcess::MergeBigBoundaryParticles(COTSFieldDataList allFields,double pixelSize,int scanFieldSize, CSize ResolutionSize, COTSParticleList& mergedParts)
  1953. {
  1954. COTSSegmentsList boarderSegs;
  1955. auto FldMgr = new CFieldMgr(scanFieldSize, ResolutionSize);
  1956. std::map<COTSParticle*, COTSParticleList> mapMergeParticles;//hold up all the boundary connected particles. the pair's first is also the member of these particles.
  1957. std::map<COTSParticle*, COTSSegmentsList> mapMergedSegments;//hold up all the segment's corresponding clone in the connected particles.
  1958. for (auto centerfld : allFields)
  1959. {
  1960. // find neighbor field on the left.
  1961. auto leftFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::LEFT);
  1962. if (leftFld != nullptr)
  1963. {
  1964. auto leftParts = centerfld->GetLeftBorderedBigParticles();
  1965. auto rightParts = leftFld->GetRightBorderedBigParticles();
  1966. for (auto leftp : leftParts)
  1967. {
  1968. for (auto rightp : rightParts)
  1969. {
  1970. if (leftp->IsConnected(rightp.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::LEFT))
  1971. {
  1972. if (leftp->headerParticle != NULL)
  1973. {
  1974. if (rightp->headerParticle == NULL)
  1975. {
  1976. rightp->headerParticle = leftp->headerParticle;
  1977. mapMergeParticles[leftp->headerParticle].push_back(rightp);
  1978. }
  1979. }
  1980. else
  1981. {
  1982. if (rightp->headerParticle != NULL)
  1983. {
  1984. leftp->headerParticle = rightp.get();
  1985. mapMergeParticles[rightp.get()].push_back(leftp);
  1986. }
  1987. else
  1988. {
  1989. leftp->headerParticle = leftp.get();
  1990. rightp->headerParticle = leftp.get();
  1991. mapMergeParticles[leftp.get()].push_back(rightp);
  1992. }
  1993. }
  1994. }
  1995. }
  1996. }
  1997. }
  1998. //find neighbor field on the upward
  1999. auto upFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::UP);
  2000. if (upFld != nullptr)
  2001. {
  2002. auto upParts = centerfld->GetTopBorderedBigParticles();
  2003. auto downParts = upFld->GetBottomBorderedBigParticles();
  2004. for (auto upprt : upParts)
  2005. {
  2006. for (auto downprt : downParts)
  2007. {
  2008. if (upprt->IsConnected(downprt.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::UP))
  2009. {
  2010. if (upprt->headerParticle != NULL)
  2011. {
  2012. if (downprt->headerParticle == NULL)
  2013. {
  2014. downprt->headerParticle = upprt->headerParticle;
  2015. mapMergeParticles[upprt->headerParticle].push_back(downprt);
  2016. }
  2017. }
  2018. else
  2019. {
  2020. if (downprt->headerParticle != NULL)
  2021. {
  2022. upprt->headerParticle = downprt->headerParticle;
  2023. mapMergeParticles[downprt.get()].push_back(upprt);
  2024. }
  2025. else
  2026. {
  2027. upprt->headerParticle = upprt.get();
  2028. downprt->headerParticle = upprt.get();
  2029. mapMergeParticles[upprt.get()].push_back(downprt);
  2030. }
  2031. }
  2032. }
  2033. }
  2034. }
  2035. }
  2036. //find neighbor field on the downward.
  2037. auto downFld = FldMgr->FindNeighborField(allFields, centerfld,SORTING_DIRECTION::DOWN);
  2038. if (downFld != nullptr)
  2039. {
  2040. auto downParts = centerfld->GetBottomBorderedBigParticles();
  2041. auto upParts = downFld->GetTopBorderedBigParticles();
  2042. for (auto downprt : downParts)
  2043. {
  2044. for (auto upprt : upParts)
  2045. {
  2046. if (downprt->IsConnected(upprt.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::DOWN))
  2047. {
  2048. if (downprt->headerParticle != NULL)
  2049. {
  2050. if (upprt->headerParticle == NULL)
  2051. {
  2052. upprt->headerParticle = downprt->headerParticle;
  2053. mapMergeParticles[downprt->headerParticle].push_back(upprt);
  2054. }
  2055. }
  2056. else
  2057. {
  2058. if (upprt->headerParticle != NULL)
  2059. {
  2060. downprt->headerParticle = upprt->headerParticle;
  2061. mapMergeParticles[upprt->headerParticle].push_back(downprt);
  2062. }
  2063. else
  2064. {
  2065. downprt->headerParticle = downprt.get();
  2066. upprt->headerParticle = downprt.get();
  2067. mapMergeParticles[downprt.get()].push_back(upprt);
  2068. }
  2069. }
  2070. }
  2071. }
  2072. }
  2073. }
  2074. //find neighbor field on the right.
  2075. auto rightFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::RIGHT);
  2076. if (rightFld != nullptr)
  2077. {
  2078. auto rightParts = centerfld->GetRightBorderedBigParticles();
  2079. auto leftParts = rightFld->GetLeftBorderedBigParticles();
  2080. for (auto rightprt : rightParts)
  2081. {
  2082. for (auto leftprt : leftParts)
  2083. {
  2084. if (rightprt->IsConnected(leftprt.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::RIGHT))
  2085. {
  2086. if (rightprt->headerParticle != NULL)
  2087. {
  2088. if (leftprt->headerParticle == NULL)
  2089. {
  2090. leftprt->headerParticle = rightprt->headerParticle;
  2091. mapMergeParticles[rightprt->headerParticle].push_back(leftprt);
  2092. }
  2093. }
  2094. else
  2095. {
  2096. if (leftprt->headerParticle != NULL)
  2097. {
  2098. rightprt->headerParticle = leftprt->headerParticle;
  2099. mapMergeParticles[leftprt->headerParticle].push_back(rightprt);
  2100. }
  2101. else
  2102. {
  2103. rightprt->headerParticle = rightprt.get();
  2104. leftprt->headerParticle = rightprt.get();
  2105. mapMergeParticles[rightprt.get()].push_back(leftprt);
  2106. }
  2107. }
  2108. }
  2109. }
  2110. }
  2111. }
  2112. }
  2113. for (auto pair : mapMergeParticles)
  2114. {
  2115. struct EleAreaPercentage
  2116. {
  2117. EleAreaPercentage(double p, CElementChemistryPtr e)
  2118. {
  2119. areaPercentage = p;
  2120. eleData = e;
  2121. }
  2122. double areaPercentage;
  2123. CElementChemistryPtr eleData;
  2124. };
  2125. auto newPart = COTSParticlePtr(new COTSParticle());
  2126. COTSSegmentsList newSegs;
  2127. auto p = pair.first;
  2128. newPart->SetAbsolutePos(p->GetAbsolutPos());
  2129. //firstly,we sum up all the merged particles's area and get the represent string.
  2130. std::string partsStr = std::to_string(p->GetFieldId()) + ":" + std::to_string(p->GetAnalysisId());
  2131. double allPartArea = p->GetArea();//Get the first particle's area.
  2132. for (auto other : pair.second)// Get the total area of all these particles for the use of ele calcu.
  2133. {
  2134. partsStr += "," + std::to_string(other->GetFieldId()) + ":" + std::to_string(other->GetAnalysisId());//Get the subparticles string such as "1:1,2:1" etc.
  2135. allPartArea += other->GetArea();//Get other particle's area
  2136. }
  2137. // calculate all the new segment's position.
  2138. std::vector <COTSParticle*> allSubParts;
  2139. allSubParts.push_back(p);
  2140. for (auto other : pair.second)// Get the total area of all these particles for the use of ele calcu.
  2141. {
  2142. allSubParts.push_back(other.get());
  2143. }
  2144. for (auto subp : allSubParts)
  2145. {
  2146. int fid = subp->GetFieldId();
  2147. CPoint myFldPos;
  2148. for (auto f : allFields)//find this particle's filed.
  2149. {
  2150. if (f->GetId() == fid)
  2151. {
  2152. myFldPos = f->GetPosition();
  2153. }
  2154. }
  2155. int fldWidth = allFields[0]->Width;
  2156. int fldHeight = allFields[0]->Height;
  2157. CPoint fldLeftUpPos = CPoint(myFldPos.x + fldWidth / 2 , myFldPos.y + fldHeight / 2 );
  2158. for (auto s : subp->GetFeature()->GetSegmentsList())
  2159. {
  2160. COTSSegmentPtr newseg = COTSSegmentPtr(new COTSSegment());
  2161. newseg->SetStart(s->GetStart() + fldLeftUpPos.x);
  2162. newseg->SetHeight((0 - s->GetHeight()) + 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.
  2163. newseg->SetLength(s->GetLength());
  2164. newSegs.push_back(newseg);
  2165. }
  2166. }
  2167. COTSFeaturePtr newFeature = COTSFeaturePtr(new COTSFeature());
  2168. newFeature->SetSegmentsList(newSegs);
  2169. newPart->SetFeature(newFeature);
  2170. newPart->CalCoverRect();
  2171. //second, we get all the element data and their area percentage .
  2172. std::map<std::string, std::vector<EleAreaPercentage>> mapEleData;
  2173. CPosXrayPtr pXray1 = p->GetXrayInfo();
  2174. if (pXray1 != nullptr)
  2175. {
  2176. for (auto ele : pXray1->GetElementQuantifyData())
  2177. {
  2178. mapEleData[ele->GetName().GetBuffer()].push_back(EleAreaPercentage(p->GetArea() / allPartArea, ele));
  2179. }
  2180. }
  2181. for (auto other : pair.second)
  2182. {
  2183. auto otherXray = other->GetXrayInfo();
  2184. if (otherXray != nullptr)
  2185. {
  2186. for (auto eledata : otherXray->GetElementQuantifyData())
  2187. {
  2188. mapEleData[eledata->GetName().GetBuffer()].push_back(EleAreaPercentage(other->GetArea() / allPartArea, eledata));
  2189. }
  2190. }
  2191. }
  2192. // third,we calculate all the element's new percentage data and get a new element chemistry list.
  2193. CElementChemistriesList newCheList;
  2194. for (auto eledata : mapEleData)
  2195. {
  2196. CElementChemistryPtr newEleche = CElementChemistryPtr(new CElementChemistry());
  2197. newEleche->SetName(CString(eledata.first.c_str()));
  2198. double newPercentage = 0;
  2199. for (auto d : eledata.second)
  2200. {
  2201. newPercentage += d.areaPercentage * d.eleData->GetPercentage();
  2202. }
  2203. newEleche->SetPercentage(newPercentage);
  2204. newCheList.push_back(newEleche);
  2205. }
  2206. CPosXrayPtr xray(new CPosXray());
  2207. xray->SetElementQuantifyData(newCheList);
  2208. newPart->SetXrayInfo(xray);
  2209. newPart->SetSubParticles(partsStr);
  2210. newPart->SetArea(allPartArea);
  2211. newPart->SetTagId(mergedParts.size());
  2212. newPart->SetAnalysisId(mergedParts.size());
  2213. std::string name = p->TypeName();
  2214. newPart->TypeName(name);
  2215. newPart->TypeColor(p->TypeColor());
  2216. mergedParts.push_back(newPart);
  2217. }
  2218. return true;
  2219. }
  2220. }