OTSImageProcess.cpp 66 KB

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