OTSImageProcess.cpp 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694
  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, blurImg.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(blurImg, 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, 3, nHeightImg, nWidthImg);
  1405. COTSImageProcess::BDilate3(pTempImg, pPixel, 3, nHeightImg, nWidthImg);
  1406. a_pBinImgOut->SetImageData(pPixel, nWidthImg, nHeightImg);
  1407. foundedPixelNum = nNumParticle;
  1408. delete[] pTempImg;
  1409. return;
  1410. }
  1411. void COTSImageProcess::RemoveBackGround(CBSEImgPtr a_pImgIn, COTSImageProcessParamPtr a_pImageProcessParam, CBSEImgPtr a_pBinImgOut,long& foundedPixelNum)
  1412. {
  1413. // the background pixel will be 0,and the other part will be 255.
  1414. ASSERT(a_pImgIn);
  1415. ASSERT(a_pImageProcessParam);
  1416. int nWidthImg = a_pImgIn->GetWidth();
  1417. int nHeightImg = a_pImgIn->GetHeight();
  1418. long nImgSize = nWidthImg * nHeightImg;
  1419. BYTE* pTempImg = new BYTE[nImgSize];
  1420. BYTE* pSrcImg = a_pImgIn->GetImageDataPointer();
  1421. BYTE* pPixel = new byte[nImgSize];
  1422. long nBGStart;
  1423. long nBGEnd;
  1424. long nPartStart;
  1425. long nPartEnd;
  1426. long nNumParticle = 0;
  1427. if (a_pImageProcessParam->GetBGRemoveType() == OTS_BGREMOVE_TYPE::MANUAL)
  1428. {
  1429. nBGStart = a_pImageProcessParam->GetBGGray().GetStart();
  1430. nBGEnd = a_pImageProcessParam->GetBGGray().GetEnd();
  1431. nPartStart = a_pImageProcessParam->GetParticleGray().GetStart();
  1432. nPartEnd = a_pImageProcessParam->GetParticleGray().GetEnd();
  1433. // delete background
  1434. for (unsigned int i = 0; i < nImgSize; i++)
  1435. {
  1436. if (pSrcImg[i] >= nBGStart && pSrcImg[i] <= nBGEnd)
  1437. {
  1438. pPixel[i] = 0;
  1439. }
  1440. else
  1441. {
  1442. pPixel[i] = 255;
  1443. nNumParticle++;
  1444. }
  1445. if (pSrcImg[i]<nPartStart || pSrcImg[i]>nPartEnd)
  1446. {
  1447. pPixel[i] = 0;
  1448. }
  1449. }
  1450. /*COTSImageProcess::BErode3(pPixel, pTempImg, 3, nHeightImg, nWidthImg);
  1451. COTSImageProcess::BDilate3(pTempImg, pPixel, 3, nHeightImg, nWidthImg);*/
  1452. Mat cvcopyImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);// use the medianblur method to achieve the same effect as open morphology(errod and dialate).
  1453. medianBlur(cvcopyImg, cvcopyImg, 5);
  1454. pPixel = cvcopyImg.data;
  1455. }
  1456. else
  1457. {
  1458. auto range = CalBackground(a_pImgIn);
  1459. nBGStart = range->GetStart();
  1460. nBGEnd = range->GetEnd();
  1461. switch (a_pImageProcessParam->GetAutoBGRemoveType())
  1462. {
  1463. case OTS_AUTOBGREMOVE_TYPE::DOWNWARD:
  1464. for (unsigned int i = 0; i < nImgSize; i++)
  1465. {
  1466. if (pSrcImg[i] <= nBGEnd)
  1467. {
  1468. pPixel[i] = 0;
  1469. }
  1470. else
  1471. {
  1472. pPixel[i] = 255;
  1473. nNumParticle++;
  1474. }
  1475. }
  1476. break;
  1477. case OTS_AUTOBGREMOVE_TYPE::UPWARD:
  1478. for (unsigned int i = 0; i < nImgSize; i++)
  1479. {
  1480. if (pSrcImg[i] >= nBGStart)
  1481. {
  1482. pPixel[i] = 0;
  1483. }
  1484. else
  1485. {
  1486. pPixel[i] = 255;
  1487. nNumParticle++;
  1488. }
  1489. }
  1490. break;
  1491. case OTS_AUTOBGREMOVE_TYPE::MIDDLE:
  1492. for (unsigned int i = 0; i < nImgSize; i++)
  1493. {
  1494. if (pSrcImg[i] >= nBGStart && pSrcImg[i] <= nBGEnd)
  1495. {
  1496. pPixel[i] = 0;
  1497. }
  1498. else
  1499. {
  1500. pPixel[i] = 255;
  1501. nNumParticle++;
  1502. }
  1503. }
  1504. break;
  1505. default:
  1506. break;
  1507. }
  1508. /*COTSImageProcess::BErode3(pPixel, pTempImg, 3, nHeightImg, nWidthImg);
  1509. COTSImageProcess::BDilate3(pTempImg, pPixel, 3, nHeightImg, nWidthImg);*/
  1510. Mat cvcopyImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);
  1511. medianBlur(cvcopyImg, cvcopyImg, 5);
  1512. pPixel = cvcopyImg.data;
  1513. }
  1514. a_pBinImgOut->SetImageData(pPixel,nWidthImg,nHeightImg);
  1515. foundedPixelNum = nNumParticle;
  1516. delete[] pTempImg;
  1517. return ;
  1518. }
  1519. BOOL COTSImageProcess::GetParticles(long left, long top, long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSParticleList& a_listParticles)
  1520. {
  1521. ASSERT(a_pPixel);
  1522. if (!a_pPixel)
  1523. {
  1524. return FALSE;
  1525. }
  1526. //a_listParticles.clear();
  1527. COTSParticleList findedParts;
  1528. COTSSegmentsList listSegment;
  1529. listSegment.clear();
  1530. //1. get segment line by line
  1531. if (!GetSegmentList(left, top, a_nWidth, a_nHeight, a_pPixel, listSegment))
  1532. {
  1533. return FALSE;
  1534. }
  1535. if ((int)listSegment.size() == 0)
  1536. {
  1537. return FALSE;
  1538. }
  1539. //2. save the temp feature
  1540. COTSFeatureList listFeature;
  1541. listFeature.clear();
  1542. if (!GetFeatureList(listSegment, listFeature))//get every feature for all the particle,the complete feature.
  1543. {
  1544. return FALSE;
  1545. }
  1546. if ((int)listFeature.size() == 0)
  1547. {
  1548. return FALSE;
  1549. }
  1550. COTSParticleList listParticles;
  1551. listParticles.clear();
  1552. if (!ChangeFeaturelist(listFeature, findedParts))
  1553. {
  1554. return FALSE;
  1555. }
  1556. for (auto f : findedParts)
  1557. {
  1558. a_listParticles.push_back(f);
  1559. }
  1560. return TRUE;
  1561. }
  1562. BOOL COTSImageProcess::GetSegmentList(long left, long top, long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSSegmentsList& a_listSegments)
  1563. {
  1564. ASSERT(a_pPixel);
  1565. if (!a_pPixel)
  1566. {
  1567. //LogErrorTrace(__FILE__, __LINE__, _T("GetSegments: there is no image data"));
  1568. return FALSE;
  1569. }
  1570. long nImgSize = a_nWidth * a_nHeight;
  1571. a_listSegments.clear();
  1572. //1. get segment line by line
  1573. long nLine, nm, nn;
  1574. long nStart = 0, nLength = 0;
  1575. for (nLine = 0; nLine < a_nHeight; nLine++)
  1576. {
  1577. for (nm = 0; nm < a_nWidth; nm += (nLength + 1))
  1578. {
  1579. nLength = 0;
  1580. // get start
  1581. if (*(a_pPixel + nLine * a_nWidth + nm) != 0)
  1582. {
  1583. nStart = nm;
  1584. nLength++;
  1585. //get length
  1586. for (nn = nm + 1; nn < a_nWidth; nn++)
  1587. {
  1588. // check if segment is over, break
  1589. if (nLength != 0)
  1590. {
  1591. if (*(a_pPixel + nLine * a_nWidth + nn) == 0)
  1592. break;
  1593. }
  1594. if (*(a_pPixel + nLine * a_nWidth + nn) != 0)
  1595. {
  1596. nLength++;
  1597. }
  1598. }
  1599. // generate segment
  1600. COTSSegmentPtr pSegment = COTSSegmentPtr(new COTSSegment(nLine + top, nStart + left, nLength));
  1601. a_listSegments.push_back(pSegment);
  1602. }
  1603. else
  1604. {
  1605. continue;
  1606. }
  1607. }
  1608. }
  1609. if ((int)a_listSegments.size() == 0)
  1610. {
  1611. //LogErrorTrace(__FILE__, __LINE__, _T("no particle is found."));
  1612. return FALSE;
  1613. }
  1614. return TRUE;
  1615. }
  1616. BOOL COTSImageProcess::GetFeatureList(COTSSegmentsList& a_listSegments, COTSFeatureList& a_listFeatures)
  1617. {
  1618. COTSSegmentsList listSegmentNew;
  1619. std::map<long, COTSSegmentsList > mapOneLineSegments;
  1620. for each (auto s in a_listSegments)
  1621. {
  1622. mapOneLineSegments[s->GetHeight()].push_back(s);//sorting all the segments base on the line number.
  1623. }
  1624. std::map<long, COTSSegmentsList >::iterator lineItr = mapOneLineSegments.begin();//find the highest line
  1625. while (lineItr != mapOneLineSegments.end())
  1626. {
  1627. for (auto s = lineItr->second.begin(); s < lineItr->second.end(); )//find one segment of this line.
  1628. {
  1629. COTSSegmentPtr bottomSeg = *s;
  1630. listSegmentNew.clear();
  1631. listSegmentNew.push_back(*s);
  1632. s = lineItr->second.erase(s);
  1633. std::map<long, COTSSegmentsList >::iterator tempItr = lineItr;
  1634. tempItr++;
  1635. for (; tempItr != mapOneLineSegments.end(); tempItr++)//find all other lines of segments
  1636. {
  1637. if (tempItr->first - bottomSeg->GetHeight() > 1)
  1638. {
  1639. break;
  1640. }
  1641. for (auto nextLineSegment = tempItr->second.begin(); nextLineSegment < tempItr->second.end();)//find next line's all segments
  1642. {
  1643. if (((*nextLineSegment)->GetStart() - (bottomSeg->GetStart() + bottomSeg->GetLength())) > 1)
  1644. {
  1645. break;
  1646. }
  1647. if (bottomSeg->UpDownConection(**nextLineSegment))
  1648. {
  1649. listSegmentNew.push_back(*nextLineSegment);
  1650. bottomSeg = *nextLineSegment;
  1651. nextLineSegment = tempItr->second.erase(nextLineSegment);
  1652. break;
  1653. }
  1654. if (tempItr->second.size() > 0)
  1655. {
  1656. nextLineSegment++;
  1657. }
  1658. else
  1659. {
  1660. break;
  1661. }
  1662. }
  1663. }
  1664. COTSFeaturePtr pFeature = COTSFeaturePtr(new COTSFeature());
  1665. pFeature->SetSegmentsList(listSegmentNew);
  1666. //check if this new feature is connected with other found feature.
  1667. COTSSegmentPtr topSeg = listSegmentNew[0];//find the toppest segment of this new feature.
  1668. COTSSegmentPtr bottomSegment = listSegmentNew[listSegmentNew.size() - 1];//find the lowest segment of this new feature.
  1669. bool haveMerged = false;
  1670. for each (auto f in a_listFeatures)
  1671. {
  1672. for (auto seg : f->GetSegmentsList())
  1673. {
  1674. if (bottomSegment->UpDownConection(*seg) || topSeg->UpDownConection(*seg))
  1675. {
  1676. COTSSegmentsList segs = f->GetSegmentsList();
  1677. for (auto s : listSegmentNew)
  1678. {
  1679. segs.push_back(s);
  1680. }
  1681. f->SetSegmentsList(segs);
  1682. haveMerged = true;
  1683. break;
  1684. }
  1685. }
  1686. if (haveMerged)
  1687. {
  1688. break;
  1689. }
  1690. }
  1691. if (!haveMerged)
  1692. {
  1693. a_listFeatures.push_back(pFeature);
  1694. }
  1695. if (lineItr->second.size() == 0)
  1696. {
  1697. break;
  1698. }
  1699. }
  1700. lineItr++;
  1701. }
  1702. return true;
  1703. }
  1704. BOOL COTSImageProcess::ChangeFeaturelist(COTSFeatureList& a_listFeatures, COTSParticleList& a_listParticle)
  1705. {
  1706. if (a_listFeatures.size() == 0)
  1707. {
  1708. return FALSE;
  1709. }
  1710. // compute Rect
  1711. for (auto pFeature : a_listFeatures)
  1712. {
  1713. COTSParticlePtr pParticle = COTSParticlePtr(new COTSParticle());
  1714. pParticle->SetFeature(pFeature);
  1715. if (!pParticle->CalCoverRect())
  1716. {
  1717. return FALSE;
  1718. }
  1719. a_listParticle.push_back(pParticle);
  1720. }
  1721. if ((int)a_listParticle.size() == 0)
  1722. {
  1723. return FALSE;
  1724. }
  1725. return TRUE;
  1726. }
  1727. BOOL COTSImageProcess::CalcuParticleImagePropertes(COTSParticlePtr a_pOTSPart, double a_PixelSize)
  1728. {
  1729. //--------- convert this particle data to image data,construct an image only with this particle.------
  1730. const int nExpand_Size = 3;
  1731. const int nWhiteColor = 0;
  1732. const int nThickness = 1;
  1733. // lineType Type of the line
  1734. const int nLineType = 8;
  1735. // get rectangle of the particle
  1736. CRect rect = a_pOTSPart->GetParticleRect();
  1737. 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.
  1738. {
  1739. double w = 0, h = 0;
  1740. w = (double)rect.Width()*a_PixelSize;
  1741. h = (double)rect.Height()*a_PixelSize;
  1742. a_pOTSPart->SetDMax(MAX(w, h));
  1743. a_pOTSPart->SetDMin(MIN(w, h));
  1744. a_pOTSPart->SetDMean((w + h) / 2);
  1745. a_pOTSPart->SetFeretDiameter((w + h) / 2);
  1746. a_pOTSPart->SetDElong(MAX(w, h));
  1747. a_pOTSPart->SetPerimeter((w+h)*2);
  1748. a_pOTSPart->SetDPerp(MIN(w, h));
  1749. a_pOTSPart->SetDInscr(MIN(w, h));
  1750. return true;
  1751. }
  1752. // calculate the particle image data size, expand 3 pixel at the edge
  1753. Mat particleImage = Mat::zeros(rect.Height() + nExpand_Size , rect.Width() + nExpand_Size , CV_8U);
  1754. // get the segment list
  1755. COTSSegmentsList listSegment = a_pOTSPart->GetFeature()->GetSegmentsList();
  1756. for (auto pSegment : listSegment)
  1757. {
  1758. int nStart = pSegment->GetStart() - rect.left + nExpand_Size;
  1759. int nEnd = pSegment->GetStart() + pSegment->GetLength() - rect.left - 1 + nExpand_Size;
  1760. int nHeight = pSegment->GetHeight() - rect.top + nExpand_Size;
  1761. line(particleImage, Point(nStart, nHeight), Point(nEnd, nHeight), Scalar(nBlackColor), nThickness, nLineType);
  1762. }
  1763. //--------abstract the contour of the particle.
  1764. Mat cvcopyImg;
  1765. medianBlur(particleImage, cvcopyImg, 5);//smooth the edge
  1766. vector<vector<Point>>contours;
  1767. findContours(cvcopyImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
  1768. 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.
  1769. {
  1770. double w = 0, h = 0;
  1771. w = (double)rect.Width()*a_PixelSize;
  1772. h = (double)rect.Height()*a_PixelSize;
  1773. a_pOTSPart->SetDMax(MAX(w, h));
  1774. a_pOTSPart->SetDMin(MIN(w, h));
  1775. a_pOTSPart->SetDMean((w + h) / 2);
  1776. a_pOTSPart->SetFeretDiameter((w + h) / 2);
  1777. a_pOTSPart->SetDElong(MAX(w, h));
  1778. a_pOTSPart->SetPerimeter((w + h) * 2);
  1779. a_pOTSPart->SetDPerp(MIN(w, h));
  1780. a_pOTSPart->SetDInscr(MIN(w, h));
  1781. return true;
  1782. }
  1783. int imaxcontour = 0, imax = 0;
  1784. for (unsigned int i = 0; i < contours.size(); i++) {
  1785. int itmp = contourArea(contours[i]);
  1786. if (imaxcontour < itmp) {
  1787. imax = i;
  1788. imaxcontour = itmp;
  1789. }
  1790. }
  1791. vector<Point > listEdge = contours[imax];
  1792. vector<vector<Point>>Outcontours;
  1793. Outcontours.push_back(listEdge);
  1794. //---------calculate the minimium rectangle
  1795. auto rRect = cv::minAreaRect(listEdge);
  1796. Point2f p[4];
  1797. rRect.points(p);
  1798. int D_MIN =getDistance(p[0], p[1]);
  1799. int D_MinRecLen = 0;//minareaRect's length(the lenger side).
  1800. for (int j = 0; j <= 2; j++)
  1801. {
  1802. //line(cvContourImg, p[j], p[(j + 1) % 4], Scalar(100, 100, 0), 2);
  1803. int d = getDistance(p[j], p[j + 1]);
  1804. if (d < D_MIN)
  1805. {
  1806. D_MIN = d;
  1807. }
  1808. if (d > D_MinRecLen)
  1809. {
  1810. D_MinRecLen = d;
  1811. }
  1812. }
  1813. a_pOTSPart->SetDMin(D_MIN*a_PixelSize);
  1814. a_pOTSPart->SetOrientation(rRect.angle);
  1815. //----------calculate the perimeter
  1816. double d = arcLength(listEdge, true);
  1817. a_pOTSPart->SetPerimeter(d*a_PixelSize);
  1818. //-----------calculate the Max Diameter. Find the min enclosing circle first ,then find the two farthest circle connected point.
  1819. Point2f center; float radius;
  1820. minEnclosingCircle(listEdge, center, radius);
  1821. //circle(cvContourImg, center, radius, Scalar(100), 2);
  1822. std::vector <Point> outContour = listEdge;
  1823. std::vector <Point> rst;
  1824. for (unsigned int k = 0; k < outContour.size(); k++)
  1825. {
  1826. Point p = outContour[k];
  1827. double d = sqrt(pow((p.x - center.x), 2) + pow((p.y - center.y), 2));
  1828. if (fabs(d - radius) < 0.01)
  1829. {
  1830. rst.push_back(p);
  1831. }
  1832. }
  1833. double D_MAX = 0;
  1834. Point lineDmax[2];
  1835. for (unsigned int m = 0; m < rst.size(); m++)
  1836. {
  1837. Point p = rst[m];
  1838. for (unsigned int n = m + 1; n < rst.size(); n++)
  1839. {
  1840. Point p1 = rst[n];
  1841. double d = sqrt(powf((p.x - p1.x), 2) + powf((p.y - p1.y), 2));
  1842. if (d > D_MAX)
  1843. {
  1844. D_MAX = d;
  1845. lineDmax[0] = p;
  1846. lineDmax[1] = p1;
  1847. }
  1848. }
  1849. }
  1850. a_pOTSPart->SetDMax(D_MAX*a_PixelSize);
  1851. //--------calculate the D_PERP property using the D_MAX's two endpoints.
  1852. std::vector<Point> curve1;
  1853. std::vector<Point> curve2;
  1854. for (unsigned int i = 0; i < outContour.size(); i++)
  1855. {
  1856. Point pt = outContour[i];
  1857. bool start = false;
  1858. int clockwise = Side(lineDmax[0], lineDmax[1], pt);// devide these points into two group ,separate into the two sides.
  1859. if (clockwise > 0)
  1860. {
  1861. curve1.push_back(pt);
  1862. }
  1863. else
  1864. {
  1865. curve2.push_back(pt);
  1866. }
  1867. }
  1868. double d_perp1 = 0, d_perp2 = 0;
  1869. for (unsigned int i = 0; i < curve1.size(); i++)
  1870. {
  1871. double d = getDist_P2L(curve1[i], lineDmax[0], lineDmax[1]);
  1872. if (d > d_perp1)
  1873. {
  1874. d_perp1 = d;
  1875. }
  1876. }
  1877. for (unsigned int i = 0; i < curve2.size(); i++)
  1878. {
  1879. double d = getDist_P2L(curve2[i], lineDmax[0], lineDmax[1]);
  1880. if (d > d_perp2)
  1881. {
  1882. d_perp2 = d;
  1883. }
  1884. }
  1885. a_pOTSPart->SetDPerp((d_perp1 + d_perp2)*a_PixelSize);
  1886. //----------find the diameter of max inscribed circle
  1887. int r;
  1888. Point inscribeCirclecenter;
  1889. FindInnerCircleInContour(outContour, inscribeCirclecenter, r);
  1890. //circle(cvContourImg, inscribeCirclecenter, r, Scalar(200));
  1891. a_pOTSPart->SetDInscr(r * 2 * a_PixelSize);
  1892. //---------------calculate the image other caracater: length/width realArea/minRectangeArea etc. we can use these propertes to do forward process.
  1893. double minRectArea = D_MIN * D_MinRecLen*a_PixelSize*a_PixelSize;//最小外接矩形面积
  1894. double fillRatio = a_pOTSPart->GetArea() / minRectArea;//实际面积与最小外接矩形面积比,that's the fill rate.
  1895. double lengthWidthRatio;
  1896. lengthWidthRatio = (double)D_MinRecLen / D_MIN;//长宽比
  1897. //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.
  1898. bool isStripShape = false;
  1899. double curveLength = 0;
  1900. double D_MEAN=0;
  1901. Moments mu;
  1902. mu = moments(listEdge, false);
  1903. int nx = mu.m10 / mu.m00;
  1904. int ny = mu.m01 / mu.m00;
  1905. //circle(cvcopyImg, Point(nx, ny), 1, (255), 1);
  1906. Point ptCenter = Point((int)nx, (int)ny);
  1907. if (pointPolygonTest(outContour, ptCenter, false) != 1)// the center point doesn't contain in the contour, we think it as curve shape.
  1908. {
  1909. isStripShape = true;
  1910. }
  1911. /*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.
  1912. {
  1913. isStripShape = true;
  1914. }*/
  1915. 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.
  1916. {
  1917. isStripShape = true;
  1918. }
  1919. if (isStripShape)
  1920. {
  1921. 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.
  1922. if (curveLength < D_MAX)
  1923. {
  1924. curveLength = D_MAX;
  1925. }
  1926. if (curveLength < MIN_DOUBLE_VALUE || a_pOTSPart->GetArea()<MIN_DOUBLE_VALUE)
  1927. {
  1928. D_MEAN = 0;
  1929. }
  1930. else
  1931. {
  1932. D_MEAN = a_pOTSPart->GetArea() / curveLength;
  1933. }
  1934. a_pOTSPart->SetDMean(D_MEAN*a_PixelSize);
  1935. a_pOTSPart->SetFeretDiameter(D_MEAN*a_PixelSize);
  1936. a_pOTSPart->SetDElong (curveLength*a_PixelSize);
  1937. }
  1938. else//it's a ball shape particle
  1939. {
  1940. curveLength = D_MAX;
  1941. double ftd = 0, maxD = 0, minD = 0, dratio = 0;
  1942. GetParticleAverageChord(outContour, a_PixelSize, ftd);
  1943. a_pOTSPart->SetDMean(ftd);
  1944. a_pOTSPart->SetFeretDiameter(ftd);
  1945. a_pOTSPart->SetDElong(curveLength*a_PixelSize);
  1946. }
  1947. return true;
  1948. }
  1949. BOOL COTSImageProcess::MergeBigBoundaryParticles(COTSFieldDataList allFields,double pixelSize,int scanFieldSize, CSize ResolutionSize, COTSParticleList& mergedParts)
  1950. {
  1951. COTSSegmentsList boarderSegs;
  1952. auto FldMgr = new CFieldMgr(scanFieldSize, ResolutionSize);
  1953. std::map<COTSParticle*, COTSParticleList> mapMergeParticles;//hold up all the boundary connected particles. the pair's first is also the member of these particles.
  1954. std::map<COTSParticle*, COTSSegmentsList> mapMergedSegments;//hold up all the segment's corresponding clone in the connected particles.
  1955. for (auto centerfld : allFields)
  1956. {
  1957. // find neighbor field on the left.
  1958. auto leftFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::LEFT);
  1959. if (leftFld != nullptr)
  1960. {
  1961. auto leftParts = centerfld->GetLeftBorderedBigParticles();
  1962. auto rightParts = leftFld->GetRightBorderedBigParticles();
  1963. for (auto leftp : leftParts)
  1964. {
  1965. for (auto rightp : rightParts)
  1966. {
  1967. if (leftp->IsConnected(rightp.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::LEFT))
  1968. {
  1969. if (leftp->headerParticle != NULL)
  1970. {
  1971. if (rightp->headerParticle == NULL)
  1972. {
  1973. rightp->headerParticle = leftp->headerParticle;
  1974. mapMergeParticles[leftp->headerParticle].push_back(rightp);
  1975. }
  1976. }
  1977. else
  1978. {
  1979. if (rightp->headerParticle != NULL)
  1980. {
  1981. leftp->headerParticle = rightp.get();
  1982. mapMergeParticles[rightp.get()].push_back(leftp);
  1983. }
  1984. else
  1985. {
  1986. leftp->headerParticle = leftp.get();
  1987. rightp->headerParticle = leftp.get();
  1988. mapMergeParticles[leftp.get()].push_back(rightp);
  1989. }
  1990. }
  1991. }
  1992. }
  1993. }
  1994. }
  1995. //find neighbor field on the upward
  1996. auto upFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::UP);
  1997. if (upFld != nullptr)
  1998. {
  1999. auto upParts = centerfld->GetTopBorderedBigParticles();
  2000. auto downParts = upFld->GetBottomBorderedBigParticles();
  2001. for (auto upprt : upParts)
  2002. {
  2003. for (auto downprt : downParts)
  2004. {
  2005. if (upprt->IsConnected(downprt.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::UP))
  2006. {
  2007. if (upprt->headerParticle != NULL)
  2008. {
  2009. if (downprt->headerParticle == NULL)
  2010. {
  2011. downprt->headerParticle = upprt->headerParticle;
  2012. mapMergeParticles[upprt->headerParticle].push_back(downprt);
  2013. }
  2014. }
  2015. else
  2016. {
  2017. if (downprt->headerParticle != NULL)
  2018. {
  2019. upprt->headerParticle = downprt->headerParticle;
  2020. mapMergeParticles[downprt.get()].push_back(upprt);
  2021. }
  2022. else
  2023. {
  2024. upprt->headerParticle = upprt.get();
  2025. downprt->headerParticle = upprt.get();
  2026. mapMergeParticles[upprt.get()].push_back(downprt);
  2027. }
  2028. }
  2029. }
  2030. }
  2031. }
  2032. }
  2033. //find neighbor field on the downward.
  2034. auto downFld = FldMgr->FindNeighborField(allFields, centerfld,SORTING_DIRECTION::DOWN);
  2035. if (downFld != nullptr)
  2036. {
  2037. auto downParts = centerfld->GetBottomBorderedBigParticles();
  2038. auto upParts = downFld->GetTopBorderedBigParticles();
  2039. for (auto downprt : downParts)
  2040. {
  2041. for (auto upprt : upParts)
  2042. {
  2043. if (downprt->IsConnected(upprt.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::DOWN))
  2044. {
  2045. if (downprt->headerParticle != NULL)
  2046. {
  2047. if (upprt->headerParticle == NULL)
  2048. {
  2049. upprt->headerParticle = downprt->headerParticle;
  2050. mapMergeParticles[downprt->headerParticle].push_back(upprt);
  2051. }
  2052. }
  2053. else
  2054. {
  2055. if (upprt->headerParticle != NULL)
  2056. {
  2057. downprt->headerParticle = upprt->headerParticle;
  2058. mapMergeParticles[upprt->headerParticle].push_back(downprt);
  2059. }
  2060. else
  2061. {
  2062. downprt->headerParticle = downprt.get();
  2063. upprt->headerParticle = downprt.get();
  2064. mapMergeParticles[downprt.get()].push_back(upprt);
  2065. }
  2066. }
  2067. }
  2068. }
  2069. }
  2070. }
  2071. //find neighbor field on the right.
  2072. auto rightFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::RIGHT);
  2073. if (rightFld != nullptr)
  2074. {
  2075. auto rightParts = centerfld->GetRightBorderedBigParticles();
  2076. auto leftParts = rightFld->GetLeftBorderedBigParticles();
  2077. for (auto rightprt : rightParts)
  2078. {
  2079. for (auto leftprt : leftParts)
  2080. {
  2081. if (rightprt->IsConnected(leftprt.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::RIGHT))
  2082. {
  2083. if (rightprt->headerParticle != NULL)
  2084. {
  2085. if (leftprt->headerParticle == NULL)
  2086. {
  2087. leftprt->headerParticle = rightprt->headerParticle;
  2088. mapMergeParticles[rightprt->headerParticle].push_back(leftprt);
  2089. }
  2090. }
  2091. else
  2092. {
  2093. if (leftprt->headerParticle != NULL)
  2094. {
  2095. rightprt->headerParticle = leftprt->headerParticle;
  2096. mapMergeParticles[leftprt->headerParticle].push_back(rightprt);
  2097. }
  2098. else
  2099. {
  2100. rightprt->headerParticle = rightprt.get();
  2101. leftprt->headerParticle = rightprt.get();
  2102. mapMergeParticles[rightprt.get()].push_back(leftprt);
  2103. }
  2104. }
  2105. }
  2106. }
  2107. }
  2108. }
  2109. }
  2110. for (auto pair : mapMergeParticles)
  2111. {
  2112. struct EleAreaPercentage
  2113. {
  2114. EleAreaPercentage(double p, CElementChemistryPtr e)
  2115. {
  2116. areaPercentage = p;
  2117. eleData = e;
  2118. }
  2119. double areaPercentage;
  2120. CElementChemistryPtr eleData;
  2121. };
  2122. auto newPart = COTSParticlePtr(new COTSParticle());
  2123. COTSSegmentsList newSegs;
  2124. auto p = pair.first;
  2125. newPart->SetAbsolutePos(p->GetAbsolutPos());
  2126. //firstly,we sum up all the merged particles's area and get the represent string.
  2127. std::string partsStr = std::to_string(p->GetFieldId()) + ":" + std::to_string(p->GetAnalysisId());
  2128. double allPartArea = p->GetArea();//Get the first particle's area.
  2129. for (auto other : pair.second)// Get the total area of all these particles for the use of ele calcu.
  2130. {
  2131. partsStr += "," + std::to_string(other->GetFieldId()) + ":" + std::to_string(other->GetAnalysisId());//Get the subparticles string such as "1:1,2:1" etc.
  2132. allPartArea += other->GetArea();//Get other particle's area
  2133. }
  2134. // calculate all the new segment's position.
  2135. std::vector <COTSParticle*> allSubParts;
  2136. allSubParts.push_back(p);
  2137. for (auto other : pair.second)// Get the total area of all these particles for the use of ele calcu.
  2138. {
  2139. allSubParts.push_back(other.get());
  2140. }
  2141. for (auto subp : allSubParts)
  2142. {
  2143. int fid = subp->GetFieldId();
  2144. CPoint myFldPos;
  2145. for (auto f : allFields)//find this particle's filed.
  2146. {
  2147. if (f->GetId() == fid)
  2148. {
  2149. myFldPos = f->GetPosition();
  2150. }
  2151. }
  2152. int fldWidth = allFields[0]->Width;
  2153. int fldHeight = allFields[0]->Height;
  2154. CPoint fldLeftUpPos = CPoint(myFldPos.x + fldWidth / 2 , myFldPos.y + fldHeight / 2 );
  2155. for (auto s : subp->GetFeature()->GetSegmentsList())
  2156. {
  2157. COTSSegmentPtr newseg = COTSSegmentPtr(new COTSSegment());
  2158. newseg->SetStart(s->GetStart() + fldLeftUpPos.x);
  2159. 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.
  2160. newseg->SetLength(s->GetLength());
  2161. newSegs.push_back(newseg);
  2162. }
  2163. }
  2164. COTSFeaturePtr newFeature = COTSFeaturePtr(new COTSFeature());
  2165. newFeature->SetSegmentsList(newSegs);
  2166. newPart->SetFeature(newFeature);
  2167. newPart->CalCoverRect();
  2168. //second, we get all the element data and their area percentage .
  2169. std::map<std::string, std::vector<EleAreaPercentage>> mapEleData;
  2170. CPosXrayPtr pXray1 = p->GetXrayInfo();
  2171. if (pXray1 != nullptr)
  2172. {
  2173. for (auto ele : pXray1->GetElementQuantifyData())
  2174. {
  2175. mapEleData[ele->GetName().GetBuffer()].push_back(EleAreaPercentage(p->GetArea() / allPartArea, ele));
  2176. }
  2177. }
  2178. for (auto other : pair.second)
  2179. {
  2180. auto otherXray = other->GetXrayInfo();
  2181. if (otherXray != nullptr)
  2182. {
  2183. for (auto eledata : otherXray->GetElementQuantifyData())
  2184. {
  2185. mapEleData[eledata->GetName().GetBuffer()].push_back(EleAreaPercentage(other->GetArea() / allPartArea, eledata));
  2186. }
  2187. }
  2188. }
  2189. // third,we calculate all the element's new percentage data and get a new element chemistry list.
  2190. CElementChemistriesList newCheList;
  2191. for (auto eledata : mapEleData)
  2192. {
  2193. CElementChemistryPtr newEleche = CElementChemistryPtr(new CElementChemistry());
  2194. newEleche->SetName(CString(eledata.first.c_str()));
  2195. double newPercentage = 0;
  2196. for (auto d : eledata.second)
  2197. {
  2198. newPercentage += d.areaPercentage * d.eleData->GetPercentage();
  2199. }
  2200. newEleche->SetPercentage(newPercentage);
  2201. newCheList.push_back(newEleche);
  2202. }
  2203. CPosXrayPtr xray(new CPosXray());
  2204. xray->SetElementQuantifyData(newCheList);
  2205. newPart->SetXrayInfo(xray);
  2206. newPart->SetSubParticles(partsStr);
  2207. newPart->SetArea(allPartArea);
  2208. newPart->SetTagId(mergedParts.size());
  2209. newPart->SetAnalysisId(mergedParts.size());
  2210. std::string name = p->TypeName();
  2211. newPart->TypeName(name);
  2212. newPart->TypeColor(p->TypeColor());
  2213. mergedParts.push_back(newPart);
  2214. }
  2215. return true;
  2216. }
  2217. }