OTSImageProcess.cpp 66 KB

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