OTSImageProcess.cpp 67 KB

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