OTSImageProcess.cpp 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936
  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. void GetMatricsParticlesFromRawParticle(COTSParticlePtr a_pOTSPart,int imageWidth,int imageHeight, double a_PixelSize, int xrayStep, COTSParticleList& matricsParts)
  151. {
  152. auto originalSegs = a_pOTSPart->GetFeature()->GetSegmentsList();
  153. std::map<int, COTSSegmentsList> segsOnTheSameHeight;
  154. for (auto s : originalSegs)
  155. {
  156. segsOnTheSameHeight[s->GetHeight()].push_back(s);
  157. }
  158. auto rect = a_pOTSPart->GetParticleRect();
  159. std::vector<CPoint> matrixPs;
  160. CPoint theFirst = CPoint(rect.left + xrayStep / 2, rect.top + xrayStep / 2);
  161. int colnum = ceil((double)rect.Width() / xrayStep + 0.5);
  162. int rownum = ceil((double)rect.Height() / xrayStep + 0.5);
  163. for (int i = 0; i < rownum; i++)
  164. {
  165. for (int j = 0; j < colnum; j++)
  166. {
  167. double x =(double) theFirst.x + (double)j * xrayStep;
  168. double y = (double)theFirst.y + (double)i * xrayStep;
  169. CPoint thePoint = CPoint(x, y);
  170. Point cvP = Point(x, y);
  171. matrixPs.push_back(thePoint);
  172. }
  173. }
  174. a_pOTSPart->SetXrayMatrixPoints(matrixPs);
  175. for (auto point : matrixPs)
  176. {
  177. COTSParticlePtr part = COTSParticlePtr(new COTSParticle());
  178. COTSSegmentsList segs;
  179. for (int i = 0; i < xrayStep; i++)
  180. {
  181. COTSSegmentPtr seg = COTSSegmentPtr(new COTSSegment());
  182. seg->SetStart(point.x - xrayStep / 2);
  183. seg->SetLength(xrayStep);
  184. seg->SetHeight(point.y - xrayStep / 2 + i);
  185. auto originalSegs = segsOnTheSameHeight[seg->GetHeight()];
  186. int currentH = seg->GetHeight();
  187. int segStart = seg->GetStart();
  188. int segEnd = seg->GetEnd();
  189. for (int i = 0; i < originalSegs.size();i++)//judge if the seg is in the original particle scope.
  190. {
  191. auto rseg = originalSegs[i];
  192. int rsegStart = rseg->GetStart();
  193. int rsegEnd = rseg->GetEnd();
  194. if (segStart > rsegEnd || rsegStart > segEnd)//there's no intersection.is not a valid seg for this original segment.
  195. {
  196. continue;
  197. }
  198. if (segStart>=rsegStart && segEnd <= rsegEnd)//contained in the original segment,is a valid seg.
  199. {
  200. segs.push_back(seg);
  201. break;
  202. }
  203. else if (segStart>= rsegStart&& segEnd >= rsegEnd)// intersect in the head end.Modify the end of the seg .
  204. {
  205. COTSSegmentPtr newseg = COTSSegmentPtr(new COTSSegment());
  206. newseg->SetStart(segStart);
  207. newseg->SetEnd(rsegEnd);
  208. newseg->SetHeight(currentH);
  209. segs.push_back(newseg);
  210. continue;
  211. }
  212. else if (segStart<= rsegStart&& segEnd >= rsegEnd)
  213. {
  214. COTSSegmentPtr newseg = COTSSegmentPtr(new COTSSegment());
  215. newseg->SetStart(rsegStart);
  216. newseg->SetEnd(rsegEnd);
  217. newseg->SetHeight(currentH);
  218. segs.push_back(newseg);
  219. continue;
  220. }
  221. else if (segStart<= rsegStart&& rsegEnd >= segEnd)
  222. {
  223. COTSSegmentPtr newseg = COTSSegmentPtr(new COTSSegment());
  224. newseg->SetStart(rsegStart);
  225. newseg->SetEnd(segEnd);
  226. newseg->SetHeight(currentH);
  227. segs.push_back(newseg);
  228. continue;
  229. }
  230. }
  231. }
  232. part->GetFeature()->SetSegmentsList(segs);
  233. part->CalXRayPos();
  234. part->SetFieldId(a_pOTSPart->GetFieldId());
  235. part->SetAnalysisId(a_pOTSPart->GetAnalysisId());
  236. matricsParts.push_back(part);
  237. }
  238. }
  239. }
  240. COTSImageProcess::COTSImageProcess()
  241. {
  242. }
  243. COTSImageProcess::~COTSImageProcess()
  244. {
  245. }
  246. // use verticl line of 3 pixel to erode a image
  247. void COTSImageProcess::BErodeVertical3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  248. {
  249. WORD x, y, wcounts;
  250. if (rows <= 2 || columns <= 2)return;
  251. // top line
  252. for (x = 0; x < columns; x++)
  253. {
  254. *(target + x) = 0;
  255. }
  256. // bottom line
  257. for (x = 0; x < columns; x++)
  258. {
  259. *(target + (DWORD)(rows - 1)*columns + x) = 0;
  260. }
  261. for (y = 1; y<rows - 1; y++)
  262. {
  263. for (x = 0; x<columns; x++)
  264. {
  265. if (*(source + (DWORD)y*columns + x) == 0)
  266. {
  267. *(target + (DWORD)y*columns + x) = 0;
  268. continue;
  269. }
  270. wcounts = 0;
  271. if (*(source + (DWORD)(y - 1)*columns + x) == 255)
  272. {
  273. wcounts++;
  274. }
  275. if (*(source + (DWORD)(y + 1)*columns + x) == 255)
  276. {
  277. wcounts++;
  278. }
  279. if (wcounts == 2) *(target + (DWORD)y*columns + x) = 255;
  280. else *(target + (DWORD)y*columns + x) = 0;
  281. }
  282. }
  283. }
  284. // use left 45 degree line of 3 pixel to erode a image
  285. void COTSImageProcess::BErodeLeft45Degree3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  286. {
  287. WORD x, y, wcounts;
  288. if (rows <= 2 || columns <= 2)return;
  289. // top line
  290. for (x = 0; x < columns; x++)
  291. {
  292. *(target + x) = 0;
  293. }
  294. // bottom line
  295. for (x = 0; x < columns; x++)
  296. {
  297. *(target + (DWORD)(rows - 1)*columns + x) = 0;
  298. }
  299. // left line
  300. for (y = 0; y<rows; y++)
  301. {
  302. *(target + (DWORD)y*columns) = 0;
  303. }
  304. // right line
  305. for (y = 0; y<rows; y++)
  306. {
  307. *(target + (DWORD)y*columns + columns - 1) = 0;
  308. }
  309. for (y = 1; y < rows - 1; y++)
  310. {
  311. for (x = 1; x < columns - 1; x++)
  312. {
  313. if (*(source + (DWORD)y*columns + x) == 0)
  314. {
  315. *(target + (DWORD)y*columns + x) = 0;
  316. continue;
  317. }
  318. wcounts = 0;
  319. if (*(source + (DWORD)(y - 1)*columns + x - 1) == 255)
  320. {
  321. wcounts++;
  322. }
  323. if (*(source + (DWORD)(y + 1)*columns + x + 1) == 255)
  324. {
  325. wcounts++;
  326. }
  327. if (wcounts == 2) *(target + (DWORD)y*columns + x) = 255;
  328. else *(target + (DWORD)y*columns + x) = 0;
  329. }
  330. }
  331. }
  332. // use horizoontal line of 3 pixel to erode a image
  333. void COTSImageProcess::BErodeHorizontal3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  334. {
  335. WORD x, y, wcounts;
  336. if (rows <= 2 || columns <= 2)return;
  337. // left line
  338. for (y = 0; y<rows; y++)
  339. {
  340. *(target + (DWORD)y*columns) = 0;
  341. }
  342. // right line
  343. for (y = 0; y<rows; y++)
  344. {
  345. *(target + (DWORD)y*columns + columns - 1) = 0;
  346. }
  347. for (y = 0; y<rows; y++)
  348. {
  349. for (x = 1; x<columns - 1; x++)
  350. {
  351. if (*(source + (DWORD)y*columns + x) == 0)
  352. {
  353. *(target + (DWORD)y*columns + x) = 0;
  354. continue;
  355. }
  356. wcounts = 0;
  357. if (*(source + (DWORD)y*columns + x - 1) == 255)
  358. {
  359. wcounts++;
  360. }
  361. if (*(source + (DWORD)y*columns + x + 1) == 255)
  362. {
  363. wcounts++;
  364. }
  365. if (wcounts == 2) *(target + (DWORD)y*columns + x) = 255;
  366. else *(target + (DWORD)y*columns + x) = 0;
  367. }
  368. }
  369. }
  370. // use right 45 degree line of 3 pixel to erode a image
  371. void COTSImageProcess::BErodeRight45Degree3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  372. {
  373. WORD x, y, wcounts;
  374. if (rows <= 2 || columns <= 2)return;
  375. // top line
  376. for (x = 0; x < columns; x++)
  377. {
  378. *(target + x) = 0;
  379. }
  380. // bottom line
  381. for (x = 0; x < columns; x++)
  382. {
  383. *(target + (DWORD)(rows - 1)*columns + x) = 0;
  384. }
  385. // left line
  386. for (y = 0; y<rows; y++)
  387. {
  388. *(target + (DWORD)y*columns) = 0;
  389. }
  390. // right line
  391. for (y = 0; y<rows; y++)
  392. {
  393. *(target + (DWORD)y*columns + columns - 1) = 0;
  394. }
  395. for (y = 1; y<rows - 1; y++)
  396. {
  397. for (x = 1; x<columns - 1; x++)
  398. {
  399. if (*(source + (DWORD)y*columns + x) == 0)
  400. {
  401. *(target + (DWORD)y*columns + x) = 0;
  402. continue;
  403. }
  404. wcounts = 0;
  405. if (*(source + (DWORD)(y - 1)*columns + x + 1) == 255)
  406. {
  407. wcounts++;
  408. }
  409. if (*(source + (DWORD)(y + 1)*columns + x - 1) == 255)
  410. {
  411. wcounts++;
  412. }
  413. if (wcounts == 2) *(target + (DWORD)y*columns + x) = 255;
  414. else *(target + (DWORD)y*columns + x) = 0;
  415. }
  416. }
  417. }
  418. void COTSImageProcess::BDilateVertical3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  419. {
  420. WORD x, y;
  421. if (rows <= 2 || columns <= 2)return;
  422. // top line
  423. for (x = 0; x<columns; x++)
  424. {
  425. if (*(source + x) != 0)
  426. {
  427. *(target + x) = 0xff;
  428. }
  429. }
  430. // bottom line
  431. for (x = 0; x<columns; x++)
  432. {
  433. if (*(source + (DWORD)(rows - 1)*columns + x) != 0)
  434. {
  435. *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
  436. }
  437. }
  438. for (y = 1; y<rows - 1; y++)
  439. {
  440. for (x = 1; x<columns - 1; x++)
  441. {
  442. if (*(source + (DWORD)y*columns + x) != 0)
  443. {
  444. *(target + (DWORD)y*columns + x) = 0xff;
  445. *(target + (DWORD)(y - 1)*columns + x) = 255;
  446. *(target + (DWORD)(y + 1)*columns + x) = 255;
  447. }
  448. else *(target + (DWORD)y*columns + x) = 0;
  449. }
  450. }
  451. }
  452. void COTSImageProcess::BDilateLeft45Degree3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  453. {
  454. WORD x, y;
  455. if (rows <= 2 || columns <= 2)return;
  456. // top line
  457. for (x = 0; x<columns; x++)
  458. {
  459. if (*(source + x) != 0)
  460. {
  461. *(target + x) = 0xff;
  462. }
  463. }
  464. // bottom line
  465. for (x = 0; x<columns; x++)
  466. {
  467. if (*(source + (DWORD)(rows - 1)*columns + x) != 0)
  468. {
  469. *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
  470. }
  471. }
  472. // left line
  473. for (y = 0; y<rows; y++)
  474. {
  475. if (*(source + (DWORD)y*columns) != 0)
  476. {
  477. *(target + (DWORD)y*columns) = 0xff;
  478. }
  479. }
  480. // right line
  481. for (y = 0; y<rows; y++)
  482. {
  483. if (*(source + (DWORD)y*columns + columns - 1) != 0)
  484. {
  485. *(target + (DWORD)y*columns + columns - 1) = 0xff;
  486. }
  487. }
  488. for (y = 1; y<rows - 1; y++)
  489. {
  490. for (x = 1; x<columns - 1; x++)
  491. {
  492. if (*(source + (DWORD)y*columns + x) != 0)
  493. {
  494. *(target + (DWORD)y*columns + x) = 0xff;
  495. *(target + (DWORD)(y - 1)*columns + x - 1) = 255;
  496. *(target + (DWORD)(y + 1)*columns + x + 1) = 255;
  497. }
  498. else *(target + (DWORD)y*columns + x) = 0;
  499. }
  500. }
  501. }
  502. void COTSImageProcess::BDilateHorizontal3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  503. {
  504. WORD x, y;
  505. if (rows <= 2 || columns <= 2)return;
  506. // left line
  507. for (y = 0; y<rows; y++)
  508. {
  509. if (*(source + (DWORD)y*columns) != 0)
  510. {
  511. *(target + (DWORD)y*columns) = 0xff;
  512. }
  513. }
  514. // right line
  515. for (y = 0; y<rows; y++)
  516. {
  517. if (*(source + (DWORD)y*columns + columns - 1) != 0)
  518. {
  519. *(target + (DWORD)y*columns + columns - 1) = 0xff;
  520. }
  521. }
  522. for (y = 1; y<rows - 1; y++)
  523. {
  524. for (x = 1; x<columns - 1; x++)
  525. {
  526. if (*(source + (DWORD)y*columns + x) != 0)
  527. {
  528. *(target + (DWORD)y*columns + x) = 0xff;
  529. *(target + (DWORD)y*columns + x - 1) = 255;
  530. *(target + (DWORD)y*columns + x + 1) = 255;
  531. }
  532. else *(target + (DWORD)y*columns + x) = 0;
  533. }
  534. }
  535. }
  536. void COTSImageProcess::BDilateRight45Degree3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  537. {
  538. WORD x, y;
  539. if (rows <= 2 || columns <= 2)return;
  540. // top line
  541. for (x = 0; x<columns; x++)
  542. {
  543. if (*(source + x) != 0)
  544. {
  545. *(target + x) = 0xff;
  546. }
  547. }
  548. // bottom line
  549. for (x = 0; x<columns; x++)
  550. {
  551. if (*(source + (DWORD)(rows - 1)*columns + x) != 0)
  552. {
  553. *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
  554. }
  555. }
  556. // left line
  557. for (y = 0; y<rows; y++)
  558. {
  559. if (*(source + (DWORD)y*columns) != 0)
  560. {
  561. *(target + (DWORD)y*columns) = 0xff;
  562. }
  563. }
  564. // right line
  565. for (y = 0; y<rows; y++)
  566. {
  567. if (*(source + (DWORD)y*columns + columns - 1) != 0)
  568. {
  569. *(target + (DWORD)y*columns + columns - 1) = 0xff;
  570. }
  571. }
  572. for (y = 1; y<rows - 1; y++)
  573. {
  574. for (x = 1; x<columns - 1; x++)
  575. {
  576. if (*(source + (DWORD)y*columns + x) != 0)
  577. {
  578. *(target + (DWORD)y*columns + x) = 0xff;
  579. *(target + (DWORD)(y - 1)*columns + x + 1) = 255;
  580. *(target + (DWORD)(y + 1)*columns + x - 1) = 255;
  581. }
  582. else *(target + (DWORD)y*columns + x) = 0;
  583. }
  584. }
  585. }
  586. void COTSImageProcess::BErode3(LPBYTE source, LPBYTE target, WORD wDegree, WORD rows, WORD columns)
  587. {
  588. WORD x, y, i, j, wcounts;
  589. if (rows == 1 || columns == 1)return;
  590. for (y = 1; y<rows - 1; y++)
  591. {
  592. for (x = 1; x<columns - 1; x++)
  593. {
  594. if (*(source + (DWORD)y*columns + x) == 0)
  595. {
  596. *(target + (DWORD)y*columns + x) = 0;
  597. continue;
  598. }
  599. wcounts = 0;
  600. for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
  601. {
  602. for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
  603. {
  604. if (*(source + (DWORD)i*columns + j) == 0)
  605. {
  606. wcounts++;
  607. }
  608. }
  609. }
  610. if (wcounts >= wDegree) *(target + (DWORD)y*columns + x) = 0;
  611. else *(target + (DWORD)y*columns + x) = 0xff;
  612. }
  613. }
  614. // top line
  615. for (x = 1; x<columns - (WORD)1; x++)
  616. {
  617. if (*(source + x) == 0)
  618. {
  619. *(target + x) = 0;
  620. continue;
  621. }
  622. wcounts = 0;
  623. for (i = 0; i <= 1; i++)
  624. {
  625. for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
  626. {
  627. if (*(source + (DWORD)i*columns + j) == 0) wcounts++;
  628. }
  629. }
  630. if (wcounts >= wDegree * 5 / 8) *(target + x) = 0;
  631. else *(target + x) = 0xff;
  632. }
  633. // bottom line
  634. for (x = 1; x<columns - 1; x++)
  635. {
  636. if (*(source + (DWORD)(rows - 1)*columns + x) == 0)
  637. {
  638. *(target + (DWORD)(rows - 1)*columns + x) = 0;
  639. continue;
  640. }
  641. wcounts = 0;
  642. for (i = (WORD)(rows - 2); i <= (WORD)(rows - 1); i++)
  643. {
  644. for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
  645. {
  646. if (*(source + (DWORD)i*columns + j) == 0) wcounts++;
  647. }
  648. }
  649. if (wcounts >= wDegree * 5 / 8) *(target + (DWORD)(rows - 1)*columns + x) = 0;
  650. else *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
  651. }
  652. // left line
  653. for (y = 1; y<rows - 1; y++)
  654. {
  655. if (*(source + (DWORD)y*columns) == 0)
  656. {
  657. *(target + (DWORD)y*columns) = 0;
  658. continue;
  659. }
  660. wcounts = 0;
  661. for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
  662. {
  663. for (j = 0; j <= 1; j++)
  664. {
  665. if (*(source + (DWORD)i*columns + j) == 0) wcounts++;
  666. }
  667. }
  668. if (wcounts >= wDegree * 5 / 8) *(target + (DWORD)y*columns) = 0;
  669. else *(target + (DWORD)y*columns) = 0xff;
  670. }
  671. // right line
  672. for (y = 1; y<rows - 1; y++)
  673. {
  674. if (*(source + (DWORD)y*columns + columns - 1) == 0)
  675. {
  676. *(target + (DWORD)y*columns + columns - 1) = 0;
  677. continue;
  678. }
  679. wcounts = 0;
  680. for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
  681. {
  682. for (j = (WORD)(columns - 2); j <= (WORD)(columns - 1); j++)
  683. {
  684. if (*(source + (DWORD)i*columns + j) == 0) wcounts++;
  685. }
  686. }
  687. if (wcounts >= wDegree * 5 / 8) *(target + (DWORD)y*columns + columns - 1) = 0;
  688. else *(target + (DWORD)y*columns + columns - 1) = 0xff;
  689. }
  690. return;
  691. }
  692. void COTSImageProcess::BDilate3(LPBYTE source, LPBYTE target, WORD wDegree, WORD rows, WORD columns)
  693. {
  694. WORD x, y, i, j, wcounts;
  695. for (y = 1; y<rows - 1; y++)
  696. {
  697. for (x = 1; x<columns - 1; x++)
  698. {
  699. if (*(source + (DWORD)y*columns + x) != 0)
  700. {
  701. *(target + (DWORD)y*columns + x) = 0xff;
  702. continue;
  703. }
  704. wcounts = 0;
  705. for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
  706. {
  707. for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
  708. {
  709. if (*(source + (DWORD)i*columns + j) != 0) wcounts++;
  710. }
  711. }
  712. if (wcounts >= wDegree) *(target + (DWORD)y*columns + x) = 0xff;
  713. else *(target + (DWORD)y*columns + x) = 0;
  714. }
  715. }
  716. // top line
  717. for (x = 1; x<columns - 1; x++)
  718. {
  719. if (*(source + x) != 0)
  720. {
  721. *(target + x) = 0xff;
  722. continue;
  723. }
  724. wcounts = 0;
  725. for (i = 0; i <= 1; i++)
  726. {
  727. for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
  728. {
  729. if (*(source + (DWORD)i*columns + j) != 0) wcounts++;
  730. }
  731. }
  732. if (wcounts >= wDegree * 5 / 8) // but does not mater, as we have border of 2 now
  733. {
  734. *(target + x) = 0xff;
  735. }
  736. else { *(target + x) = 0; }
  737. }
  738. // bottom line
  739. for (x = 1; x<columns - 1; x++)
  740. {
  741. if (*(source + (DWORD)(rows - 1)*columns + x) != 0)
  742. {
  743. *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
  744. continue;
  745. }
  746. wcounts = 0;
  747. for (i = (WORD)(rows - 2); i <= (WORD)(rows - 1); i++)
  748. {
  749. for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
  750. {
  751. if (*(source + (DWORD)i*columns + j) != 0) wcounts++;
  752. }
  753. }
  754. if (wcounts > wDegree * 5 / 8)
  755. {
  756. *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
  757. }
  758. else
  759. {
  760. *(target + (DWORD)(rows - 1)*columns + x) = 0;
  761. }
  762. }
  763. // left line
  764. for (y = 1; y<rows - 1; y++)
  765. {
  766. if (*(source + (DWORD)y*columns) != 0)
  767. {
  768. *(target + (DWORD)y*columns) = 0xff;
  769. continue;
  770. }
  771. wcounts = 0;
  772. for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
  773. {
  774. for (j = 0; j <= (WORD)1; j++)
  775. {
  776. if (*(source + (DWORD)i*columns + j) != 0) wcounts++;
  777. }
  778. }
  779. if (wcounts >= wDegree * 5 / 8)
  780. {
  781. *(target + (DWORD)y*columns) = 0xff;
  782. }
  783. else
  784. {
  785. *(target + (DWORD)y*columns) = 0;
  786. }
  787. }
  788. // right line
  789. for (y = 1; y<rows - 1; y++)
  790. {
  791. if (*(source + (DWORD)y*columns + columns - 1) != 0)
  792. {
  793. *(target + (DWORD)y*columns + columns - 1) = 0xff;
  794. continue;
  795. }
  796. wcounts = 0;
  797. for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
  798. {
  799. for (j = (WORD)(columns - 2); j <= (WORD)(columns - 1); j++)
  800. {
  801. if (*(source + (DWORD)i*columns + j) != 0) wcounts++;
  802. }
  803. }
  804. if (wcounts >= wDegree * 5 / 8)
  805. {
  806. *(target + (DWORD)y*columns + columns - 1) = 0xff;
  807. }
  808. else
  809. {
  810. *(target + (DWORD)y*columns + columns - 1) = 0;
  811. }
  812. }
  813. // four cornor points treated separately here
  814. // top-left
  815. if (*(source) != 0)
  816. {
  817. *target = 0xff;
  818. }
  819. else
  820. {
  821. wcounts = 0;
  822. if (*(source + 1) != 0) wcounts++;
  823. if (*(source + columns) != 0) wcounts++;
  824. if (*(source + columns + 1) != 0) wcounts++;
  825. // if (wcounts >= wDegree*3/8) // this is a bug here - interger division
  826. if (wcounts * 8 >= wDegree * 3)
  827. {
  828. *target = 0xff;
  829. }
  830. else
  831. {
  832. *target = 0;
  833. }
  834. }
  835. //top-right
  836. if (*(source + columns - 1) != 0)
  837. {
  838. *(target + columns - 1) = 0xff;
  839. }
  840. else
  841. {
  842. wcounts = 0;
  843. if (*(source + columns - 2) != 0) wcounts++;
  844. if (*(source + columns * 2 - 1) != 0) wcounts++;
  845. if (*(source + columns * 2 - 2) != 0) wcounts++;
  846. // if (wcounts >= wDegree*3/8) // this is a bug here - interger division
  847. if (wcounts * 8 >= wDegree * 3)
  848. {
  849. *(target + columns - 1) = 0xff;
  850. }
  851. else
  852. {
  853. *(target + columns - 1) = 0;
  854. }
  855. }
  856. //bottom-left
  857. if (*(source + (DWORD)columns * (rows - 1)) != 0)
  858. {
  859. *(target + (DWORD)columns * (rows - 1)) = 0xff;
  860. }
  861. else
  862. {
  863. wcounts = 0;
  864. if (*(source + (DWORD)columns * (rows - 1) + 1) != 0) wcounts++;
  865. if (*(source + (DWORD)columns * (rows - 2)) != 0) wcounts++;
  866. if (*(source + (DWORD)columns * (rows - 2) + 1) != 0) wcounts++;
  867. // if (wcounts >= wDegree*3/8) // this is a bug here - interger division
  868. if (wcounts * 8 >= wDegree * 3)
  869. {
  870. *(target + (DWORD)columns * (rows - 1)) = 0xff;
  871. }
  872. else
  873. {
  874. *(target + (DWORD)columns * (rows - 1)) = 0;
  875. }
  876. }
  877. //bottom-right
  878. if (*(source + (DWORD)columns * rows - 1) != 0)
  879. {
  880. *(target + (DWORD)columns * rows - 1) = 0xff;
  881. }
  882. else
  883. {
  884. wcounts = 0;
  885. if (*(source + (DWORD)columns * rows - 2) != 0) wcounts++;
  886. if (*(source + (DWORD)columns * (rows - 1) - 2) != 0) wcounts++;
  887. if (*(source + (DWORD)columns * (rows - 1) - 1) != 0) wcounts++;
  888. // if (wcounts >= wDegree*3/8) // this is a bug here - interger division
  889. if (wcounts * 8 >= wDegree * 3)
  890. {
  891. *(target + (DWORD)columns * rows - 1) = 0xff;
  892. }
  893. else
  894. {
  895. *(target + (DWORD)columns * rows - 1) = 0;
  896. }
  897. }
  898. return;
  899. }
  900. // ReZoom the picture with re-magnification
  901. BOOL COTSImageProcess::ReZoom(CString InPutPath, CString OutPutPath)
  902. {
  903. Mat cvSrcImg;
  904. string strInputPath;
  905. strInputPath = CStringA(InPutPath);
  906. // Pictures loop in folder
  907. std::vector<cv::String> ImageFolder;
  908. cv::glob(strInputPath, ImageFolder);
  909. if (ImageFolder.size() == 0)
  910. {
  911. return FALSE;
  912. }
  913. for (unsigned int nImgNum = 0; nImgNum < ImageFolder.size(); ++nImgNum) {
  914. cvSrcImg = cv::imread(ImageFolder[nImgNum], CV_LOAD_IMAGE_GRAYSCALE);
  915. // Image convolution operation
  916. //// convolution kernel
  917. float kernel[] = { -1, -1 , -1, -1 , 0, -1, -1 , -1 , -1 };
  918. cv::Mat ker = cv::Mat(nImage_Size, nImage_Size, CV_32F, &kernel);
  919. cv::Mat cvDstImg = cv::Mat(cvSrcImg.size(), cvSrcImg.type());
  920. // anchor of the kernel
  921. cv::Point anchor(-1, -1);
  922. cv::filter2D(cvSrcImg, cvDstImg, CV_32F, ker, anchor, delta, cv::THRESH_TRUNC);
  923. // Maximum Pixel Value
  924. cvDstImg = abs(cvDstImg);
  925. double minVal, maxVal;
  926. minMaxLoc(cvDstImg, &minVal, &maxVal);
  927. // Grayscale image
  928. int nReduce;
  929. Mat onesImg = Mat::ones(cvDstImg.rows, cvDstImg.cols, CV_32F) * (int)minVal;
  930. absdiff(cvDstImg, onesImg, cvDstImg);
  931. nReduce = (int)maxVal - minVal;
  932. cvDstImg = cvDstImg * nBlackColor / nReduce;
  933. // Output image convert data to int
  934. cvDstImg.convertTo(cvDstImg, CV_8U);
  935. // Process the picture to 128 pixels
  936. resize(cvDstImg, cvDstImg, Size(nPictureSize, nPictureSize));
  937. threshold(cvDstImg, cvDstImg, nProcessParam, nBlackColor, CV_THRESH_BINARY);
  938. string strOutPutPath;
  939. strOutPutPath = CStringA(OutPutPath);
  940. imwrite(strOutPutPath , cvDstImg);
  941. }
  942. return TRUE;
  943. }
  944. BOOL COTSImageProcess::RemoveBSEImageBG(CBSEImgPtr m_pBSEImg, COTSImageProcessParamPtr a_pImgProcessParam,COTSFieldDataPtr m_pFieldData)
  945. {
  946. ASSERT(m_pFieldData);
  947. if (!m_pFieldData)
  948. {
  949. LogErrorTrace(__FILE__, __LINE__, _T("RemoveBSEImageBG: there is no field data"));
  950. return FALSE;
  951. }
  952. ASSERT(m_pBSEImg);
  953. if (!m_pBSEImg)
  954. {
  955. LogErrorTrace(__FILE__, __LINE__, _T("RemoveBSEImageBG: there is no image data"));
  956. return FALSE;
  957. }
  958. ASSERT(a_pImgProcessParam);
  959. if (!a_pImgProcessParam)
  960. {
  961. LogErrorTrace(__FILE__, __LINE__, _T("RemoveBSEImageBG: there is no image process data"));
  962. return FALSE;
  963. }
  964. int nWidthImg = m_pBSEImg->GetWidth();
  965. int nHeightImg = m_pBSEImg->GetHeight();
  966. m_pFieldData->Width = nWidthImg;
  967. m_pFieldData->Height = nHeightImg;
  968. long nImgSize = nWidthImg * nHeightImg;
  969. BYTE* pSrcImg = m_pBSEImg->GetImageDataPointer();
  970. BYTE* pTempImg = new BYTE[nImgSize];
  971. CRect r = CRect(0, 0, nWidthImg, nHeightImg);
  972. CBSEImgPtr imgNoBGBinary = CBSEImgPtr(new CBSEImg(r));
  973. long nNumParticle = 0;
  974. RemoveBackGround(m_pBSEImg, a_pImgProcessParam, imgNoBGBinary,nNumParticle);
  975. BYTE* pPixel = imgNoBGBinary->GetImageDataPointer();
  976. long nPtStart = a_pImgProcessParam->GetParticleGray().GetStart();
  977. long nPtEnd = a_pImgProcessParam->GetParticleGray().GetEnd();
  978. if (nNumParticle == 0)
  979. {
  980. COTSParticleList listParticleEmpty;
  981. listParticleEmpty.clear();
  982. m_pFieldData->SetParticleList(listParticleEmpty);
  983. }
  984. else
  985. {
  986. // get the area image
  987. Mat blurImg;
  988. Mat srcImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);
  989. //medianBlur(srcImg, blurImg, 3);//smooth the edge
  990. COTSParticleList listParticleOut;
  991. if (!GetParticles(0,0,nWidthImg, nHeightImg, srcImg.data, listParticleOut))
  992. {
  993. COTSParticleList listParticleEmpty;
  994. listParticleEmpty.clear();
  995. m_pFieldData->SetParticleList(listParticleEmpty);
  996. }
  997. // form a image only have particles on
  998. COTSSegmentsList listImage;
  999. for (auto pParticle : listParticleOut)
  1000. {
  1001. COTSFeaturePtr pFeature = pParticle->GetFeature();
  1002. COTSSegmentsList listSegment = pFeature->GetSegmentsList();
  1003. long nPixelNum = 0;
  1004. long nPixelAll = 0;
  1005. int nStartS = 0;
  1006. int nHeightS = 0;
  1007. int nLengthS = 0;
  1008. for (auto pSegment : listSegment)
  1009. {
  1010. // update image list
  1011. COTSSegmentPtr pSegNew = COTSSegmentPtr(new COTSSegment(*pSegment.get()));
  1012. listImage.push_back(pSegNew);
  1013. // get particle average gray
  1014. nStartS = pSegment->GetStart();
  1015. nHeightS = pSegment->GetHeight();
  1016. nLengthS = pSegment->GetLength();
  1017. nPixelNum += (long)nLengthS;
  1018. if (nHeightS > nHeightImg)
  1019. {
  1020. LogErrorTrace(__FILE__, __LINE__, _T("seg height is wrong."));
  1021. return FALSE;
  1022. }
  1023. if ((nStartS + nLengthS - 1) > nWidthImg)
  1024. {
  1025. LogErrorTrace(__FILE__, __LINE__, _T("seg starst and length is wrong."));
  1026. return FALSE;
  1027. }
  1028. for (unsigned int i = 0; i < nLengthS; i++)
  1029. {
  1030. if ((nStartS + i) > nWidthImg)
  1031. {
  1032. LogErrorTrace(__FILE__, __LINE__, _T("seg start is wrong."));
  1033. return FALSE;
  1034. }
  1035. else if (nHeightS > nHeightImg)
  1036. {
  1037. LogErrorTrace(__FILE__, __LINE__, _T("seg height is wrong."));
  1038. return FALSE;
  1039. }
  1040. long nValueTemp = (long)*(pSrcImg + nHeightS * nWidthImg + nStartS + i);
  1041. nPixelAll += nValueTemp;
  1042. }
  1043. }
  1044. BYTE nAveGray = (BYTE)(nPixelAll / nPixelNum);
  1045. pParticle->SetAveGray(nAveGray);
  1046. pParticle->SetActualArea(nPixelNum);
  1047. }
  1048. m_pFieldData->SetParticleList(listParticleOut);
  1049. }
  1050. delete[]pTempImg;
  1051. return TRUE;
  1052. }
  1053. BOOL COTSImageProcess::RemoveBGByFindContour(CBSEImgPtr m_pBSEImg, COTSImageProcessParamPtr a_pImageProcessParam, COTSFieldDataPtr m_pFieldData)
  1054. {
  1055. ASSERT(m_pFieldData);
  1056. ASSERT(m_pBSEImg);
  1057. ASSERT(a_pImageProcessParam);
  1058. int nWidthImg = m_pBSEImg->GetWidth();
  1059. int nHeightImg = m_pBSEImg->GetHeight();
  1060. m_pFieldData->Width = nWidthImg;
  1061. m_pFieldData->Height = nHeightImg;
  1062. long nImgSize = nWidthImg * nHeightImg;
  1063. BYTE* pSrcImg = m_pBSEImg->GetImageDataPointer();
  1064. BYTE* pTempImg = new BYTE[nImgSize];
  1065. CRect r = CRect(0, 0, nWidthImg, nHeightImg);
  1066. CBSEImgPtr imgNoBGBinary = CBSEImgPtr(new CBSEImg(r));
  1067. long nNumParticle = 0;
  1068. RemoveBackGround(m_pBSEImg, a_pImageProcessParam, imgNoBGBinary, nNumParticle);
  1069. BYTE* pPixel = imgNoBGBinary->GetImageDataPointer();
  1070. long nPtStart = a_pImageProcessParam->GetParticleGray().GetStart();
  1071. long nPtEnd = a_pImageProcessParam->GetParticleGray().GetEnd();
  1072. if (nNumParticle == 0)
  1073. {
  1074. COTSParticleList listParticleEmpty;
  1075. listParticleEmpty.clear();
  1076. m_pFieldData->SetParticleList(listParticleEmpty);
  1077. }
  1078. else
  1079. {
  1080. // get the area image
  1081. Mat cvcopyImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);
  1082. vector<vector<Point>>contours;
  1083. findContours(cvcopyImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
  1084. COTSParticleList listParticleOut;
  1085. for (size_t i = 0; i < contours.size(); i++)
  1086. {
  1087. Rect rectMax = boundingRect(contours[i]);
  1088. Mat rectROI = cvcopyImg(rectMax).clone();
  1089. //exclude the point which intersect into this bounding box but is not in this contour.
  1090. for (int nX = 0; nX < rectROI.rows; nX++)
  1091. {
  1092. for (int nY = 0; nY < rectROI.cols; nY++)
  1093. {
  1094. double localPos = pointPolygonTest(contours[i], Point2f(nX + rectMax.x, nY + rectMax.y), false);
  1095. if (localPos == -1)
  1096. {
  1097. 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.
  1098. }
  1099. }
  1100. }
  1101. GetParticles(rectMax.x, rectMax.y, rectMax.width, rectMax.height, rectROI.data, listParticleOut);
  1102. }
  1103. // form a image only have particles on
  1104. //COTSSegmentsList listImage;
  1105. for (auto pParticle : listParticleOut)
  1106. {
  1107. COTSFeaturePtr pFeature = pParticle->GetFeature();
  1108. COTSSegmentsList listSegment = pFeature->GetSegmentsList();
  1109. long nPixelNum = 0;
  1110. long nPixelAll = 0;
  1111. int nStartS = 0;
  1112. int nHeightS = 0;
  1113. int nLengthS = 0;
  1114. for (auto pSegment : listSegment)
  1115. {
  1116. // get particle average gray
  1117. nStartS = pSegment->GetStart();
  1118. nHeightS = pSegment->GetHeight();
  1119. nLengthS = pSegment->GetLength();
  1120. nPixelNum += (long)nLengthS;
  1121. for (unsigned int i = 0; i < nLengthS; i++)
  1122. {
  1123. long nValueTemp = (long)*(pSrcImg + nHeightS * nWidthImg + nStartS + i);
  1124. nPixelAll += nValueTemp;
  1125. }
  1126. }
  1127. BYTE nAveGray = (BYTE)(nPixelAll / nPixelNum);
  1128. pParticle->SetAveGray(nAveGray);
  1129. pParticle->SetActualArea(nPixelNum);
  1130. }
  1131. m_pFieldData->SetParticleList(listParticleOut);
  1132. }
  1133. delete[]pTempImg;
  1134. return TRUE;
  1135. }
  1136. BOOL COTSImageProcess::RemoveBGByCVconnectivities(CBSEImgPtr inBSEImg, COTSImageProcessParamPtr a_pImageProcessParam,double a_pixelSize, COTSFieldDataPtr m_pFieldData)
  1137. {
  1138. ASSERT(m_pFieldData);
  1139. ASSERT(inBSEImg);
  1140. ASSERT(a_pImageProcessParam);
  1141. int nWidthImg = inBSEImg->GetWidth();
  1142. int nHeightImg = inBSEImg->GetHeight();
  1143. m_pFieldData->Width = nWidthImg;
  1144. m_pFieldData->Height = nHeightImg;
  1145. long nImgSize = nWidthImg * nHeightImg;
  1146. BYTE* pSrcImg = inBSEImg->GetImageDataPointer();
  1147. BYTE* pTempImg = new BYTE[nImgSize];
  1148. CRect r = CRect(0, 0, nWidthImg, nHeightImg);
  1149. CBSEImgPtr imgNoBGBinary = CBSEImgPtr(new CBSEImg(r));
  1150. long nNumParticle = 0;
  1151. RemoveBackGround(inBSEImg, a_pImageProcessParam, imgNoBGBinary, nNumParticle);
  1152. BYTE* pPixel = imgNoBGBinary->GetImageDataPointer();
  1153. long nPtStart = a_pImageProcessParam->GetParticleGray().GetStart();
  1154. long nPtEnd = a_pImageProcessParam->GetParticleGray().GetEnd();
  1155. if (nNumParticle == 0)
  1156. {
  1157. COTSParticleList listParticleEmpty;
  1158. listParticleEmpty.clear();
  1159. m_pFieldData->SetParticleList(listParticleEmpty);
  1160. }
  1161. else
  1162. {
  1163. // get the area image
  1164. Mat cvcopyImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);
  1165. //Mat blurImg;
  1166. //medianBlur(cvcopyImg, blurImg, 5);//get rid of the noise point.
  1167. Mat labels = Mat::zeros(cvcopyImg.size(), CV_32S);
  1168. Mat stats, centroids;
  1169. int number = connectedComponentsWithStats(cvcopyImg, labels, stats, centroids, 8, CV_32S);
  1170. double rMin = a_pImageProcessParam->GetIncArea().GetStart()/2.0;
  1171. double rMax = a_pImageProcessParam->GetIncArea().GetEnd()/2.0;
  1172. double partAreaMin = rMin * rMin * 3.14159;
  1173. double partAreaMax = rMax * rMax * 3.14159;
  1174. COTSParticleList listParticleOut;
  1175. for (size_t i = 1; i < number; i++)
  1176. {
  1177. int center_x = centroids.at<double>(i, 0);
  1178. int center_y = centroids.at<double>(i, 1);
  1179. //矩形边框
  1180. int x = stats.at<int>(i, CC_STAT_LEFT);
  1181. int y = stats.at<int>(i, CC_STAT_TOP);
  1182. int w = stats.at<int>(i, CC_STAT_WIDTH);
  1183. int h = stats.at<int>(i, CC_STAT_HEIGHT);
  1184. int area = stats.at<int>(i, CC_STAT_AREA);
  1185. double actualArea = area * a_pixelSize * a_pixelSize;
  1186. if (actualArea >= partAreaMin && actualArea < partAreaMax)
  1187. {
  1188. Rect rectMax = Rect(x, y, w, h);
  1189. Mat rectROI = labels(rectMax).clone();
  1190. Mat imageROI = Mat::zeros(rectMax.size(), cvcopyImg.type());
  1191. //exclude the point which intersect into this bounding box but is not in this group.
  1192. int label = i;
  1193. for (int row = 0; row < rectROI.rows; row++)
  1194. {
  1195. for (int col = 0; col < rectROI.cols; col++)
  1196. {
  1197. int v = rectROI.at<int>(row, col);
  1198. if (v == label)
  1199. {
  1200. 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.
  1201. }
  1202. }
  1203. }
  1204. COTSParticleList roiParts;
  1205. if (GetOneParticleFromROI(rectMax.x, rectMax.y, rectMax.width, rectMax.height, imageROI.data, roiParts))
  1206. {
  1207. if (roiParts.size() > 0)
  1208. {
  1209. COTSParticlePtr roiPart = roiParts[0];//we will find only one part in the roi.
  1210. roiPart->SetXRayPos(CPoint(center_x, center_y));
  1211. CRect r = CRect(x, y, x + w, y + h);
  1212. roiPart->SetParticleRect(r);
  1213. roiPart->SetActualArea(actualArea);
  1214. roiPart->SetPixelArea(area);
  1215. listParticleOut.push_back(roiPart);
  1216. }
  1217. }
  1218. }
  1219. }
  1220. int nTagId;
  1221. for (auto pParticle : listParticleOut)
  1222. {
  1223. COTSFeaturePtr pFeature = pParticle->GetFeature();
  1224. COTSSegmentsList listSegment = pFeature->GetSegmentsList();
  1225. long nPixelNum = 0;
  1226. long nPixelAll = 0;
  1227. int nStartS = 0;
  1228. int nHeightS = 0;
  1229. int nLengthS = 0;
  1230. for (auto pSegment : listSegment)
  1231. {
  1232. // get particle average gray
  1233. nStartS = pSegment->GetStart();
  1234. nHeightS = pSegment->GetHeight();
  1235. nLengthS = pSegment->GetLength();
  1236. nPixelNum += (long)nLengthS;
  1237. for (unsigned int i = 0; i < nLengthS; i++)
  1238. {
  1239. long nValueTemp = (long)*(pSrcImg + nHeightS * nWidthImg + nStartS + i);
  1240. nPixelAll += nValueTemp;
  1241. }
  1242. }
  1243. BYTE nAveGray = (BYTE)(nPixelAll / nPixelNum);
  1244. pParticle->SetAveGray(nAveGray);
  1245. }
  1246. m_pFieldData->SetParticleList(listParticleOut);
  1247. }
  1248. delete[]pTempImg;
  1249. return TRUE;
  1250. }
  1251. BOOL COTSImageProcess::GetParticlesBySpecialGrayRange(CBSEImgPtr a_pBSEImg, CIntRangePtr a_grayRange,CDoubleRangePtr a_diameterRange,double a_pixelSize, COTSFieldDataPtr m_pFieldData)
  1252. {
  1253. ASSERT(m_pFieldData);
  1254. ASSERT(a_pBSEImg);
  1255. ASSERT(a_grayRange);
  1256. int nWidthImg = a_pBSEImg->GetWidth();
  1257. int nHeightImg = a_pBSEImg->GetHeight();
  1258. m_pFieldData->Width = nWidthImg;
  1259. m_pFieldData->Height = nHeightImg;
  1260. long nImgSize = nWidthImg * nHeightImg;
  1261. BYTE* pSrcImg = a_pBSEImg->GetImageDataPointer();
  1262. BYTE* pTempImg = new BYTE[nImgSize];
  1263. CRect r = CRect(0, 0, nWidthImg, nHeightImg);
  1264. CBSEImgPtr imgNoBGBinary = CBSEImgPtr(new CBSEImg(r));
  1265. long nNumParticle = 0;
  1266. GetSpecialGrayRangeImage(a_pBSEImg, a_grayRange, imgNoBGBinary, nNumParticle);
  1267. BYTE* pPixel = imgNoBGBinary->GetImageDataPointer();
  1268. if (nNumParticle == 0)
  1269. {
  1270. COTSParticleList listParticleEmpty;
  1271. listParticleEmpty.clear();
  1272. m_pFieldData->SetParticleList(listParticleEmpty);
  1273. }
  1274. else
  1275. {
  1276. // get the area image
  1277. Mat cvcopyImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);
  1278. Mat labels = Mat::zeros(cvcopyImg.size(), CV_32S);
  1279. Mat stats, centroids;
  1280. int number = connectedComponentsWithStats(cvcopyImg, labels, stats, centroids, 8, CV_32S);
  1281. double rStart = a_diameterRange->GetStart() / 2.0;
  1282. double rEnd = a_diameterRange->GetEnd() / 2.0;
  1283. double areaStart = rStart * rStart * 3.14159;
  1284. double areaEnd = rEnd * rEnd * 3.14159;
  1285. COTSParticleList listParticleOut;
  1286. for (size_t i = 1; i < number; i++)
  1287. {
  1288. int center_x = centroids.at<double>(i, 0);
  1289. int center_y = centroids.at<double>(i, 1);
  1290. //矩形边框
  1291. int x = stats.at<int>(i, CC_STAT_LEFT);
  1292. int y = stats.at<int>(i, CC_STAT_TOP);
  1293. int w = stats.at<int>(i, CC_STAT_WIDTH);
  1294. int h = stats.at<int>(i, CC_STAT_HEIGHT);
  1295. int area = stats.at<int>(i, CC_STAT_AREA);
  1296. double actualArea = area * a_pixelSize * a_pixelSize;
  1297. if (actualArea >= areaStart && actualArea < areaEnd)
  1298. {
  1299. Rect rectMax = Rect(x, y, w, h);
  1300. Mat rectROI = labels(rectMax).clone();
  1301. Mat imageROI = Mat::zeros(rectMax.size(), cvcopyImg.type());
  1302. //exclude the point which intersect into this bounding box but is not in this group.
  1303. int label = i;
  1304. for (int row = 0; row < rectROI.rows; row++)
  1305. {
  1306. for (int col = 0; col < rectROI.cols; col++)
  1307. {
  1308. int v = rectROI.at<int>(row, col);
  1309. if (v == label)
  1310. {
  1311. imageROI.at<uchar>(row, col) = 255;
  1312. }
  1313. }
  1314. }
  1315. COTSParticleList roiParts;
  1316. if (!GetOneParticleFromROI(rectMax.x, rectMax.y, rectMax.width, rectMax.height, imageROI.data, roiParts))
  1317. {
  1318. continue;
  1319. }
  1320. if (roiParts.size() > 0)
  1321. {
  1322. COTSParticlePtr roiPart = roiParts[0];
  1323. roiPart->SetXRayPos(CPoint(center_x, center_y));
  1324. CRect r = CRect(x, y, x + w, y + h);
  1325. roiPart->SetParticleRect(r);
  1326. roiPart->SetActualArea(actualArea);
  1327. roiPart->SetPixelArea(area);
  1328. listParticleOut.push_back(roiPart);
  1329. }
  1330. }
  1331. }
  1332. // form a image only have particles on
  1333. //COTSSegmentsList listImage;
  1334. for (auto pParticle : listParticleOut)
  1335. {
  1336. int area = pParticle->GetActualArea();
  1337. double pActualArea = area ;
  1338. COTSFeaturePtr pFeature = pParticle->GetFeature();
  1339. COTSSegmentsList listSegment = pFeature->GetSegmentsList();
  1340. long nPixelNum = 0;
  1341. long nPixelAll = 0;
  1342. int nStartS = 0;
  1343. int nHeightS = 0;
  1344. int nLengthS = 0;
  1345. for (auto pSegment : listSegment)
  1346. {
  1347. // get particle average gray
  1348. nStartS = pSegment->GetStart();
  1349. nHeightS = pSegment->GetHeight();
  1350. nLengthS = pSegment->GetLength();
  1351. nPixelNum += (long)nLengthS;
  1352. for (unsigned int i = 0; i < nLengthS; i++)
  1353. {
  1354. long nValueTemp = (long)*(pSrcImg + nHeightS * nWidthImg + nStartS + i);
  1355. nPixelAll += nValueTemp;
  1356. }
  1357. }
  1358. BYTE nAveGray = (BYTE)(nPixelAll / nPixelNum);
  1359. pParticle->SetAveGray(nAveGray);
  1360. }
  1361. m_pFieldData->SetParticleList(listParticleOut);
  1362. }
  1363. delete[]pTempImg;
  1364. return TRUE;
  1365. }
  1366. CIntRangePtr COTSImageProcess::CalBackground(CBSEImgPtr m_pBSEImg)
  1367. {
  1368. CIntRangePtr pBackground = CIntRangePtr(new CIntRange());
  1369. WORD nBSEChart[MAXBYTE];
  1370. //1. get chart data
  1371. m_pBSEImg->SetChartData();
  1372. linearSmooth5(m_pBSEImg->GetBSEChart(), nBSEChart,MAXBYTE);
  1373. //2. get down edge
  1374. int nLengthEdge = MAXBYTE + 2;
  1375. WORD n_aBSEChart[MAXBYTE + 2];
  1376. memset(n_aBSEChart, 0, sizeof(WORD) * nLengthEdge);
  1377. std::map<long, std::vector <int>> upEdgeSeries;
  1378. std::map<long, std::vector<int>> downEdgeSeries;
  1379. std::vector<int> currentUpSeries;
  1380. std::vector<int> currentDownSeries;
  1381. // make sure the wave begin with up edge and end with down edge
  1382. n_aBSEChart[0] = 0;
  1383. n_aBSEChart[nLengthEdge - 1] = 0;
  1384. memcpy(&n_aBSEChart[1], &nBSEChart, sizeof(WORD) * MAXBYTE);
  1385. int nLengthCom = MAXBYTE + 1;
  1386. // up edge
  1387. for (int i = 0; i < nLengthCom; i++)
  1388. {
  1389. if (n_aBSEChart[i] <= n_aBSEChart[i + 1])
  1390. {
  1391. if (currentDownSeries.size() > 0)
  1392. {
  1393. int seriesSize = currentDownSeries.size();
  1394. long area = 0;
  1395. for (int i = 0; i < seriesSize; i++)
  1396. {
  1397. area = area + n_aBSEChart[currentDownSeries[i]];
  1398. }
  1399. downEdgeSeries[area] = currentDownSeries;
  1400. currentDownSeries.clear();
  1401. }
  1402. currentUpSeries.push_back(i);
  1403. }
  1404. else
  1405. {
  1406. if (currentUpSeries.size() > 0)
  1407. {
  1408. int seriesSize = currentUpSeries.size();
  1409. long area = 0;
  1410. for (int i = 0; i < seriesSize; i++)
  1411. {
  1412. area = area + n_aBSEChart[currentUpSeries[i]];
  1413. }
  1414. upEdgeSeries[area] = currentUpSeries;
  1415. currentUpSeries.clear();
  1416. }
  1417. currentDownSeries.push_back(i);
  1418. }
  1419. }
  1420. CIntRangePtr pRangeFirst = CIntRangePtr(new CIntRange());
  1421. if (upEdgeSeries.size() > 0)
  1422. {
  1423. pRangeFirst->SetStart((upEdgeSeries.rbegin())->second[0]);
  1424. }
  1425. if (downEdgeSeries.size() > 0)
  1426. {
  1427. auto s = downEdgeSeries.rbegin()->second;
  1428. pRangeFirst->SetEnd((long)(s[s.size() - 1]));
  1429. }
  1430. return pRangeFirst;
  1431. }
  1432. void COTSImageProcess::GetSpecialGrayRangeImage(CBSEImgPtr a_pImgIn, CIntRangePtr a_SpecialGrayRange, CBSEImgPtr a_pBinImgOut, long& foundedPixelNum)
  1433. {
  1434. // the background pixel will be 0,and the other part will be 255.
  1435. ASSERT(a_pImgIn);
  1436. int nWidthImg = a_pImgIn->GetWidth();
  1437. int nHeightImg = a_pImgIn->GetHeight();
  1438. long nImgSize = nWidthImg * nHeightImg;
  1439. BYTE* pTempImg = new BYTE[nImgSize];
  1440. BYTE* pSrcImg = a_pImgIn->GetImageDataPointer();
  1441. BYTE* pPixel = new byte[nImgSize];
  1442. long nBGStart;
  1443. long nBGEnd;
  1444. long nNumParticle = 0;
  1445. nBGStart = a_SpecialGrayRange->GetStart();
  1446. nBGEnd = a_SpecialGrayRange->GetEnd();
  1447. // delete background
  1448. for (unsigned int i = 0; i < nImgSize; i++)
  1449. {
  1450. if (pSrcImg[i] >= nBGStart && pSrcImg[i] <= nBGEnd)
  1451. {
  1452. pPixel[i] = 255;
  1453. nNumParticle++;
  1454. }
  1455. else
  1456. {
  1457. pPixel[i] = 0;
  1458. }
  1459. }
  1460. //Mat cvcopyImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);// use the medianblur method to achieve the same effect as open morphology(errod and dialate).
  1461. //pPixel = cvcopyImg.data;
  1462. COTSImageProcess::BErode3(pPixel, pTempImg, 5, nHeightImg, nWidthImg);
  1463. COTSImageProcess::BDilate3(pTempImg, pPixel, 5, nHeightImg, nWidthImg);
  1464. a_pBinImgOut->SetImageData(pPixel, nWidthImg, nHeightImg);
  1465. foundedPixelNum = nNumParticle;
  1466. delete[] pTempImg;
  1467. return;
  1468. }
  1469. void COTSImageProcess::RemoveBackGround(CBSEImgPtr a_pImgIn, COTSImageProcessParamPtr a_pImageProcessParam, CBSEImgPtr a_pBinImgOut,long& foundedPixelNum)
  1470. {
  1471. // the background pixel will be 0,and the other part will be 255.
  1472. ASSERT(a_pImgIn);
  1473. ASSERT(a_pImageProcessParam);
  1474. int nWidthImg = a_pImgIn->GetWidth();
  1475. int nHeightImg = a_pImgIn->GetHeight();
  1476. long nImgSize = nWidthImg * nHeightImg;
  1477. BYTE* pTempImg = new BYTE[nImgSize];
  1478. BYTE* pSrcImg = a_pImgIn->GetImageDataPointer();
  1479. BYTE* pPixel = new byte[nImgSize];
  1480. long nBGStart;
  1481. long nBGEnd;
  1482. long nPartStart;
  1483. long nPartEnd;
  1484. long nNumParticle = 0;
  1485. if (a_pImageProcessParam->GetBGRemoveType() == OTS_BGREMOVE_TYPE::MANUAL)
  1486. {
  1487. nBGStart = a_pImageProcessParam->GetBGGray().GetStart();
  1488. nBGEnd = a_pImageProcessParam->GetBGGray().GetEnd();
  1489. nPartStart = a_pImageProcessParam->GetParticleGray().GetStart();
  1490. nPartEnd = a_pImageProcessParam->GetParticleGray().GetEnd();
  1491. // delete background
  1492. for (unsigned int i = 0; i < nImgSize; i++)
  1493. {
  1494. if (pSrcImg[i] >= nBGStart && pSrcImg[i] <= nBGEnd)
  1495. {
  1496. pPixel[i] = 0;
  1497. }
  1498. else
  1499. {
  1500. pPixel[i] = 255;
  1501. nNumParticle++;
  1502. }
  1503. if (pSrcImg[i]<nPartStart || pSrcImg[i]>nPartEnd)
  1504. {
  1505. pPixel[i] = 0;
  1506. }
  1507. }
  1508. int errodDilateParam = a_pImageProcessParam->GetErrodDilateParam();
  1509. if (errodDilateParam > 0)
  1510. {
  1511. COTSImageProcess::BErode3(pPixel, pTempImg, errodDilateParam, nHeightImg, nWidthImg);
  1512. COTSImageProcess::BDilate3(pTempImg, pPixel, errodDilateParam, nHeightImg, nWidthImg);
  1513. }
  1514. //Mat cvcopyImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);// use the medianblur method to achieve the same effect as open morphology(errod and dialate).
  1515. //medianBlur(cvcopyImg, cvcopyImg, 5);
  1516. //pPixel = cvcopyImg.data;
  1517. }
  1518. else
  1519. {
  1520. auto range = CalBackground(a_pImgIn);
  1521. nBGStart = range->GetStart();
  1522. nBGEnd = range->GetEnd();
  1523. switch (a_pImageProcessParam->GetAutoBGRemoveType())
  1524. {
  1525. case OTS_AUTOBGREMOVE_TYPE::DOWNWARD:
  1526. for (unsigned int i = 0; i < nImgSize; i++)
  1527. {
  1528. if (pSrcImg[i] <= nBGEnd)
  1529. {
  1530. pPixel[i] = 0;
  1531. }
  1532. else
  1533. {
  1534. pPixel[i] = 255;
  1535. nNumParticle++;
  1536. }
  1537. }
  1538. break;
  1539. case OTS_AUTOBGREMOVE_TYPE::UPWARD:
  1540. for (unsigned int i = 0; i < nImgSize; i++)
  1541. {
  1542. if (pSrcImg[i] >= nBGStart)
  1543. {
  1544. pPixel[i] = 0;
  1545. }
  1546. else
  1547. {
  1548. pPixel[i] = 255;
  1549. nNumParticle++;
  1550. }
  1551. }
  1552. break;
  1553. case OTS_AUTOBGREMOVE_TYPE::MIDDLE:
  1554. for (unsigned int i = 0; i < nImgSize; i++)
  1555. {
  1556. if (pSrcImg[i] >= nBGStart && pSrcImg[i] <= nBGEnd)
  1557. {
  1558. pPixel[i] = 0;
  1559. }
  1560. else
  1561. {
  1562. pPixel[i] = 255;
  1563. nNumParticle++;
  1564. }
  1565. }
  1566. break;
  1567. default:
  1568. break;
  1569. }
  1570. int errodDilateParam = a_pImageProcessParam->GetErrodDilateParam();
  1571. if (errodDilateParam > 0)
  1572. {
  1573. COTSImageProcess::BErode3(pPixel, pTempImg, errodDilateParam, nHeightImg, nWidthImg);
  1574. COTSImageProcess::BDilate3(pTempImg, pPixel, errodDilateParam, nHeightImg, nWidthImg);
  1575. }
  1576. /*Mat cvcopyImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);
  1577. medianBlur(cvcopyImg, cvcopyImg, 5);
  1578. pPixel = cvcopyImg.data;*/
  1579. }
  1580. a_pBinImgOut->SetImageData(pPixel,nWidthImg,nHeightImg);
  1581. foundedPixelNum = nNumParticle;
  1582. delete[] pTempImg;
  1583. return ;
  1584. }
  1585. BOOL COTSImageProcess::GetParticles(long left, long top, long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSParticleList& a_listParticles)
  1586. {
  1587. ASSERT(a_pPixel);
  1588. if (!a_pPixel)
  1589. {
  1590. return FALSE;
  1591. }
  1592. //a_listParticles.clear();
  1593. COTSParticleList findedParts;
  1594. COTSSegmentsList listSegment;
  1595. listSegment.clear();
  1596. //1. get segment line by line
  1597. if (!GetSegmentList(left, top, a_nWidth, a_nHeight, a_pPixel, listSegment))
  1598. {
  1599. return FALSE;
  1600. }
  1601. if ((int)listSegment.size() == 0)
  1602. {
  1603. return FALSE;
  1604. }
  1605. //2. save the temp feature
  1606. COTSFeatureList listFeature;
  1607. listFeature.clear();
  1608. if (!GetFeatureList(listSegment, listFeature))//get every feature for all the particle,the complete feature.
  1609. {
  1610. return FALSE;
  1611. }
  1612. if ((int)listFeature.size() == 0)
  1613. {
  1614. return FALSE;
  1615. }
  1616. /*COTSParticleList listParticles;
  1617. listParticles.clear();*/
  1618. if (!ChangeFeaturelist(listFeature, findedParts))
  1619. {
  1620. return FALSE;
  1621. }
  1622. for (auto f : findedParts)
  1623. {
  1624. a_listParticles.push_back(f);
  1625. }
  1626. return TRUE;
  1627. }
  1628. BOOL COTSImageProcess::GetOneParticleFromROI(long left, long top, long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSParticleList& a_listParticles)
  1629. {
  1630. ASSERT(a_pPixel);
  1631. if (!a_pPixel)
  1632. {
  1633. return FALSE;
  1634. }
  1635. //a_listParticles.clear();
  1636. COTSParticleList findedParts;
  1637. COTSSegmentsList listSegment;
  1638. listSegment.clear();
  1639. //1. get segment line by line
  1640. if (!GetSegmentList(left, top, a_nWidth, a_nHeight, a_pPixel, listSegment))
  1641. {
  1642. return FALSE;
  1643. }
  1644. if ((int)listSegment.size() == 0)
  1645. {
  1646. return FALSE;
  1647. }
  1648. //2. save the temp feature
  1649. COTSFeatureList listFeature;
  1650. listFeature.clear();
  1651. COTSFeaturePtr fea = COTSFeaturePtr(new COTSFeature());
  1652. fea->SetSegmentsList(listSegment);
  1653. listFeature.push_back(fea);
  1654. if ((int)listFeature.size() == 0)
  1655. {
  1656. return FALSE;
  1657. }
  1658. if (!ChangeFeaturelist(listFeature, findedParts))
  1659. {
  1660. return FALSE;
  1661. }
  1662. for (auto f : findedParts)
  1663. {
  1664. a_listParticles.push_back(f);
  1665. }
  1666. return TRUE;
  1667. }
  1668. BOOL COTSImageProcess::GetSegmentList(long left, long top, long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSSegmentsList& a_listSegments)
  1669. {
  1670. ASSERT(a_pPixel);
  1671. if (!a_pPixel)
  1672. {
  1673. //LogErrorTrace(__FILE__, __LINE__, _T("GetSegments: there is no image data"));
  1674. return FALSE;
  1675. }
  1676. long nImgSize = a_nWidth * a_nHeight;
  1677. a_listSegments.clear();
  1678. //1. get segment line by line
  1679. long nLine, nm, nn;
  1680. long nStart = 0, nLength = 0;
  1681. for (nLine = 0; nLine < a_nHeight; nLine++)
  1682. {
  1683. for (nm = 0; nm < a_nWidth; nm += (nLength + 1))
  1684. {
  1685. nLength = 0;
  1686. // get start
  1687. if (*(a_pPixel + nLine * a_nWidth + nm) != 0)
  1688. {
  1689. nStart = nm;
  1690. nLength++;
  1691. //get length
  1692. for (nn = nm + 1; nn < a_nWidth; nn++)
  1693. {
  1694. // check if segment is over, break
  1695. if (nLength != 0)
  1696. {
  1697. if (*(a_pPixel + nLine * a_nWidth + nn) == 0)
  1698. break;
  1699. }
  1700. if (*(a_pPixel + nLine * a_nWidth + nn) != 0)
  1701. {
  1702. nLength++;
  1703. }
  1704. }
  1705. // generate segment
  1706. COTSSegmentPtr pSegment = COTSSegmentPtr(new COTSSegment(nLine + top, nStart + left, nLength));
  1707. a_listSegments.push_back(pSegment);
  1708. }
  1709. else
  1710. {
  1711. continue;
  1712. }
  1713. }
  1714. }
  1715. if ((int)a_listSegments.size() == 0)
  1716. {
  1717. //LogErrorTrace(__FILE__, __LINE__, _T("no particle is found."));
  1718. return FALSE;
  1719. }
  1720. return TRUE;
  1721. }
  1722. BOOL COTSImageProcess::GetFeatureList(COTSSegmentsList& a_listSegments, COTSFeatureList& a_listFeatures)
  1723. {
  1724. COTSSegmentsList listSegmentNew;
  1725. std::map<long, COTSSegmentsList > mapOneLineSegments;
  1726. for each (auto s in a_listSegments)
  1727. {
  1728. mapOneLineSegments[s->GetHeight()].push_back(s);//sorting all the segments base on the line number.
  1729. }
  1730. std::map<long, COTSSegmentsList >::iterator lineItr = mapOneLineSegments.begin();//find the highest line
  1731. while (lineItr != mapOneLineSegments.end())
  1732. {
  1733. for (auto s = lineItr->second.begin(); s < lineItr->second.end(); )//find one segment of this line.
  1734. {
  1735. COTSSegmentPtr bottomSeg = *s;
  1736. listSegmentNew.clear();
  1737. listSegmentNew.push_back(*s);
  1738. s = lineItr->second.erase(s);
  1739. std::map<long, COTSSegmentsList >::iterator tempItr = lineItr;
  1740. tempItr++;
  1741. for (; tempItr != mapOneLineSegments.end(); tempItr++)//find all other lines of segments
  1742. {
  1743. if (tempItr->first - bottomSeg->GetHeight() > 1)
  1744. {
  1745. break;
  1746. }
  1747. for (auto nextLineSegment = tempItr->second.begin(); nextLineSegment < tempItr->second.end();)//find next line's all segments
  1748. {
  1749. if (((*nextLineSegment)->GetStart() - (bottomSeg->GetStart() + bottomSeg->GetLength())) > 1)
  1750. {
  1751. break;
  1752. }
  1753. if (bottomSeg->UpDownConection(**nextLineSegment))
  1754. {
  1755. listSegmentNew.push_back(*nextLineSegment);
  1756. bottomSeg = *nextLineSegment;
  1757. nextLineSegment = tempItr->second.erase(nextLineSegment);
  1758. break;
  1759. }
  1760. if (tempItr->second.size() > 0)
  1761. {
  1762. nextLineSegment++;
  1763. }
  1764. else
  1765. {
  1766. break;
  1767. }
  1768. }
  1769. }
  1770. COTSFeaturePtr pFeature = COTSFeaturePtr(new COTSFeature());
  1771. pFeature->SetSegmentsList(listSegmentNew);
  1772. //check if this new feature is connected with other found feature.
  1773. COTSSegmentPtr topSeg = listSegmentNew[0];//find the toppest segment of this new feature.
  1774. COTSSegmentPtr bottomSegment = listSegmentNew[listSegmentNew.size() - 1];//find the lowest segment of this new feature.
  1775. bool haveMerged = false;
  1776. for each (auto f in a_listFeatures)
  1777. {
  1778. for (auto seg : f->GetSegmentsList())
  1779. {
  1780. if (bottomSegment->UpDownConection(*seg) || topSeg->UpDownConection(*seg))
  1781. {
  1782. COTSSegmentsList segs = f->GetSegmentsList();
  1783. for (auto s : listSegmentNew)
  1784. {
  1785. segs.push_back(s);
  1786. }
  1787. f->SetSegmentsList(segs);
  1788. haveMerged = true;
  1789. break;
  1790. }
  1791. }
  1792. if (haveMerged)
  1793. {
  1794. break;
  1795. }
  1796. }
  1797. if (!haveMerged)
  1798. {
  1799. a_listFeatures.push_back(pFeature);
  1800. }
  1801. if (lineItr->second.size() == 0)
  1802. {
  1803. break;
  1804. }
  1805. }
  1806. lineItr++;
  1807. }
  1808. return true;
  1809. }
  1810. BOOL COTSImageProcess::ChangeFeaturelist(COTSFeatureList& a_listFeatures, COTSParticleList& a_listParticle)
  1811. {
  1812. for (auto pFeature : a_listFeatures)
  1813. {
  1814. COTSParticlePtr pParticle = COTSParticlePtr(new COTSParticle());
  1815. pParticle->SetFeature(pFeature);
  1816. a_listParticle.push_back(pParticle);
  1817. }
  1818. if ((int)a_listParticle.size() == 0)
  1819. {
  1820. return FALSE;
  1821. }
  1822. return TRUE;
  1823. }
  1824. BOOL COTSImageProcess::CalcuParticleImagePropertes(COTSParticlePtr a_pOTSPart, double a_PixelSize)
  1825. {
  1826. //--------- convert this particle data to image data,construct an image only with this particle.------
  1827. const int nExpand_Size = 3;
  1828. const int nWhiteColor = 0;
  1829. const int nThickness = 1;
  1830. // lineType Type of the line
  1831. const int nLineType = 8;
  1832. // get rectangle of the particle
  1833. CRect rect = a_pOTSPart->GetParticleRect();
  1834. 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.
  1835. {
  1836. double w = 0, h = 0;
  1837. w = (double)rect.Width()*a_PixelSize;
  1838. h = (double)rect.Height()*a_PixelSize;
  1839. a_pOTSPart->SetDMax(MAX(w, h));
  1840. a_pOTSPart->SetDMin(MIN(w, h));
  1841. a_pOTSPart->SetDMean((w + h) / 2);
  1842. a_pOTSPart->SetFeretDiameter((w + h) / 2);
  1843. a_pOTSPart->SetDElong(MAX(w, h));
  1844. a_pOTSPart->SetPerimeter((w+h)*2);
  1845. a_pOTSPart->SetDPerp(MIN(w, h));
  1846. a_pOTSPart->SetDInscr(MIN(w, h));
  1847. return true;
  1848. }
  1849. // calculate the particle image data size, expand 3 pixel at the edge
  1850. Mat particleImage = Mat::zeros(rect.Height() + nExpand_Size , rect.Width() + nExpand_Size , CV_8U);
  1851. // get the segment list
  1852. COTSSegmentsList listSegment = a_pOTSPart->GetFeature()->GetSegmentsList();
  1853. for (auto pSegment : listSegment)
  1854. {
  1855. int nStart = pSegment->GetStart() - rect.left + nExpand_Size;
  1856. int nEnd = pSegment->GetStart() + pSegment->GetLength() - rect.left - 1 + nExpand_Size;
  1857. int nHeight = pSegment->GetHeight() - rect.top + nExpand_Size;
  1858. line(particleImage, Point(nStart, nHeight), Point(nEnd, nHeight), Scalar(nBlackColor), nThickness, nLineType);
  1859. }
  1860. //--------abstract the contour of the particle.
  1861. Mat cvcopyImg;
  1862. medianBlur(particleImage, cvcopyImg, 5);//smooth the edge
  1863. vector<vector<Point>>contours;
  1864. findContours(cvcopyImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
  1865. 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.
  1866. {
  1867. double w = 0, h = 0;
  1868. w = (double)rect.Width()*a_PixelSize;
  1869. h = (double)rect.Height()*a_PixelSize;
  1870. a_pOTSPart->SetDMax(MAX(w, h));
  1871. a_pOTSPart->SetDMin(MIN(w, h));
  1872. a_pOTSPart->SetDMean((w + h) / 2);
  1873. a_pOTSPart->SetFeretDiameter((w + h) / 2);
  1874. a_pOTSPart->SetDElong(MAX(w, h));
  1875. a_pOTSPart->SetPerimeter((w + h) * 2);
  1876. a_pOTSPart->SetDPerp(MIN(w, h));
  1877. a_pOTSPart->SetDInscr(MIN(w, h));
  1878. return true;
  1879. }
  1880. int imaxcontour = 0, imax = 0;
  1881. for (unsigned int i = 0; i < contours.size(); i++) {
  1882. int itmp = contourArea(contours[i]);
  1883. if (imaxcontour < itmp) {
  1884. imax = i;
  1885. imaxcontour = itmp;
  1886. }
  1887. }
  1888. vector<Point > listEdge = contours[imax];
  1889. vector<vector<Point>>Outcontours;
  1890. Outcontours.push_back(listEdge);
  1891. //---------calculate the minimium rectangle
  1892. auto rRect = cv::minAreaRect(listEdge);
  1893. Point2f p[4];
  1894. rRect.points(p);
  1895. int D_MIN =getDistance(p[0], p[1]);
  1896. int D_MinRecLen = 0;//minareaRect's length(the lenger side).
  1897. for (int j = 0; j <= 2; j++)
  1898. {
  1899. //line(cvContourImg, p[j], p[(j + 1) % 4], Scalar(100, 100, 0), 2);
  1900. int d = getDistance(p[j], p[j + 1]);
  1901. if (d < D_MIN)
  1902. {
  1903. D_MIN = d;
  1904. }
  1905. if (d > D_MinRecLen)
  1906. {
  1907. D_MinRecLen = d;
  1908. }
  1909. }
  1910. a_pOTSPart->SetDMin(D_MIN*a_PixelSize);
  1911. a_pOTSPart->SetOrientation(rRect.angle);
  1912. //----------calculate the perimeter
  1913. double d = arcLength(listEdge, true);
  1914. a_pOTSPart->SetPerimeter(d*a_PixelSize);
  1915. //-----------calculate the Max Diameter. Find the min enclosing circle first ,then find the two farthest circle connected point.
  1916. Point2f center; float radius;
  1917. minEnclosingCircle(listEdge, center, radius);
  1918. //circle(cvContourImg, center, radius, Scalar(100), 2);
  1919. std::vector <Point> outContour = listEdge;
  1920. std::vector <Point> rst;
  1921. for (unsigned int k = 0; k < outContour.size(); k++)
  1922. {
  1923. Point p = outContour[k];
  1924. double d = sqrt(pow((p.x - center.x), 2) + pow((p.y - center.y), 2));
  1925. if (fabs(d - radius) < 0.01)
  1926. {
  1927. rst.push_back(p);
  1928. }
  1929. }
  1930. double D_MAX = 0;
  1931. Point lineDmax[2];
  1932. for (unsigned int m = 0; m < rst.size(); m++)
  1933. {
  1934. Point p = rst[m];
  1935. for (unsigned int n = m + 1; n < rst.size(); n++)
  1936. {
  1937. Point p1 = rst[n];
  1938. double d = sqrt(powf((p.x - p1.x), 2) + powf((p.y - p1.y), 2));
  1939. if (d > D_MAX)
  1940. {
  1941. D_MAX = d;
  1942. lineDmax[0] = p;
  1943. lineDmax[1] = p1;
  1944. }
  1945. }
  1946. }
  1947. a_pOTSPart->SetDMax(D_MAX*a_PixelSize);
  1948. //--------calculate the D_PERP property using the D_MAX's two endpoints.
  1949. std::vector<Point> curve1;
  1950. std::vector<Point> curve2;
  1951. for (unsigned int i = 0; i < outContour.size(); i++)
  1952. {
  1953. Point pt = outContour[i];
  1954. bool start = false;
  1955. int clockwise = Side(lineDmax[0], lineDmax[1], pt);// devide these points into two group ,separate into the two sides.
  1956. if (clockwise > 0)
  1957. {
  1958. curve1.push_back(pt);
  1959. }
  1960. else
  1961. {
  1962. curve2.push_back(pt);
  1963. }
  1964. }
  1965. double d_perp1 = 0, d_perp2 = 0;
  1966. for (unsigned int i = 0; i < curve1.size(); i++)
  1967. {
  1968. double d = getDist_P2L(curve1[i], lineDmax[0], lineDmax[1]);
  1969. if (d > d_perp1)
  1970. {
  1971. d_perp1 = d;
  1972. }
  1973. }
  1974. for (unsigned int i = 0; i < curve2.size(); i++)
  1975. {
  1976. double d = getDist_P2L(curve2[i], lineDmax[0], lineDmax[1]);
  1977. if (d > d_perp2)
  1978. {
  1979. d_perp2 = d;
  1980. }
  1981. }
  1982. a_pOTSPart->SetDPerp((d_perp1 + d_perp2)*a_PixelSize);
  1983. //----------find the diameter of max inscribed circle
  1984. int r;
  1985. Point inscribeCirclecenter;
  1986. FindInnerCircleInContour(outContour, inscribeCirclecenter, r);
  1987. //--------------------------------------------------------calculate the xraypos !
  1988. CRect rec = a_pOTSPart->GetParticleRect();
  1989. a_pOTSPart->SetXRayPos(CPoint(inscribeCirclecenter.x - nExpand_Size + rec.left - 1, inscribeCirclecenter.y - nExpand_Size + rec.top - 1));
  1990. a_pOTSPart->SetDInscr(r * 2 * a_PixelSize);
  1991. //---------------calculate the image other caracater: length/width realArea/minRectangeArea etc. we can use these propertes to do forward process.
  1992. double minRectArea = D_MIN * D_MinRecLen*a_PixelSize*a_PixelSize;//最小外接矩形面积
  1993. double fillRatio = a_pOTSPart->GetActualArea() / minRectArea;//实际面积与最小外接矩形面积比,that's the fill rate.
  1994. double lengthWidthRatio;
  1995. lengthWidthRatio = (double)D_MinRecLen / D_MIN;//长宽比
  1996. //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.
  1997. bool isStripShape = false;
  1998. double curveLength = 0;
  1999. double D_MEAN=0;
  2000. Moments mu;
  2001. mu = moments(listEdge, false);
  2002. int nx = mu.m10 / mu.m00;
  2003. int ny = mu.m01 / mu.m00;
  2004. //circle(cvcopyImg, Point(nx, ny), 1, (255), 1);
  2005. Point ptCenter = Point((int)nx, (int)ny);
  2006. if (pointPolygonTest(listEdge, ptCenter, false) != 1)// the center point doesn't contain in the contour, we think it as curve shape.
  2007. {
  2008. isStripShape = true;
  2009. }
  2010. /*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.
  2011. {
  2012. isStripShape = true;
  2013. }*/
  2014. 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.
  2015. {
  2016. isStripShape = true;
  2017. }
  2018. if (isStripShape)
  2019. {
  2020. 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.
  2021. if (curveLength < D_MAX)
  2022. {
  2023. curveLength = D_MAX;
  2024. }
  2025. if (curveLength < MIN_DOUBLE_VALUE || a_pOTSPart->GetActualArea()<MIN_DOUBLE_VALUE)
  2026. {
  2027. D_MEAN = 0;
  2028. }
  2029. else
  2030. {
  2031. D_MEAN = a_pOTSPart->GetActualArea() / curveLength;
  2032. }
  2033. a_pOTSPart->SetDMean(D_MEAN*a_PixelSize);
  2034. a_pOTSPart->SetFeretDiameter(D_MEAN*a_PixelSize);
  2035. a_pOTSPart->SetDElong (curveLength*a_PixelSize);
  2036. }
  2037. else//it's a ball shape particle
  2038. {
  2039. curveLength = D_MAX;
  2040. double ftd = 0, maxD = 0, minD = 0, dratio = 0;
  2041. GetParticleAverageChord(outContour, a_PixelSize, ftd);
  2042. a_pOTSPart->SetDMean(ftd);
  2043. a_pOTSPart->SetFeretDiameter(ftd);
  2044. a_pOTSPart->SetDElong(curveLength*a_PixelSize);
  2045. }
  2046. return true;
  2047. }
  2048. BOOL COTSImageProcess::SplitRawParticleIntoMatricsParticle(COTSParticlePtr a_pOTSPart,int imageWidth,int imageHeight, double a_PixelSize, double a_XrayStep)
  2049. {
  2050. //--------- convert this particle data to image data,construct an image only with this particle.------
  2051. const int nExpand_Size = 3;
  2052. const int nWhiteColor = 0;
  2053. const int nThickness = 1;
  2054. // lineType Type of the line
  2055. const int nLineType = 8;
  2056. // get rectangle of the particle
  2057. CRect rect = a_pOTSPart->GetParticleRect();
  2058. 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.
  2059. {
  2060. double w = 0, h = 0;
  2061. w = (double)rect.Width() * a_PixelSize;
  2062. h = (double)rect.Height() * a_PixelSize;
  2063. a_pOTSPart->SetDMax(MAX(w, h));
  2064. a_pOTSPart->SetDMin(MIN(w, h));
  2065. a_pOTSPart->SetDMean((w + h) / 2);
  2066. a_pOTSPart->SetFeretDiameter((w + h) / 2);
  2067. a_pOTSPart->SetDElong(MAX(w, h));
  2068. a_pOTSPart->SetPerimeter((w + h) * 2);
  2069. a_pOTSPart->SetDPerp(MIN(w, h));
  2070. a_pOTSPart->SetDInscr(MIN(w, h));
  2071. return true;
  2072. }
  2073. // calculate the particle image data size, expand 3 pixel at the edge
  2074. Mat particleImage = Mat::zeros(rect.Height() + nExpand_Size, rect.Width() + nExpand_Size, CV_8U);
  2075. // get the segment list
  2076. COTSSegmentsList listSegment = a_pOTSPart->GetFeature()->GetSegmentsList();
  2077. for (auto pSegment : listSegment)
  2078. {
  2079. int nStart = pSegment->GetStart() - rect.left + nExpand_Size;
  2080. int nEnd = pSegment->GetStart() + pSegment->GetLength() - rect.left - 1 + nExpand_Size;
  2081. int nHeight = pSegment->GetHeight() - rect.top + nExpand_Size;
  2082. line(particleImage, Point(nStart, nHeight), Point(nEnd, nHeight), Scalar(nBlackColor), nThickness, nLineType);
  2083. }
  2084. //--------abstract the contour of the particle.
  2085. Mat cvcopyImg;
  2086. medianBlur(particleImage, cvcopyImg, 5);//smooth the edge
  2087. vector<vector<Point>>contours;
  2088. findContours(cvcopyImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
  2089. 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.
  2090. {
  2091. double w = 0, h = 0;
  2092. w = (double)rect.Width() * a_PixelSize;
  2093. h = (double)rect.Height() * a_PixelSize;
  2094. a_pOTSPart->SetDMax(MAX(w, h));
  2095. a_pOTSPart->SetDMin(MIN(w, h));
  2096. a_pOTSPart->SetDMean((w + h) / 2);
  2097. a_pOTSPart->SetFeretDiameter((w + h) / 2);
  2098. a_pOTSPart->SetDElong(MAX(w, h));
  2099. a_pOTSPart->SetPerimeter((w + h) * 2);
  2100. a_pOTSPart->SetDPerp(MIN(w, h));
  2101. a_pOTSPart->SetDInscr(MIN(w, h));
  2102. return true;
  2103. }
  2104. int imaxcontour = 0, imax = 0;
  2105. for (unsigned int i = 0; i < contours.size(); i++) {
  2106. int itmp = contourArea(contours[i]);
  2107. if (imaxcontour < itmp) {
  2108. imax = i;
  2109. imaxcontour = itmp;
  2110. }
  2111. }
  2112. vector<Point > listEdge = contours[imax];
  2113. if (a_XrayStep > 0)
  2114. {
  2115. COTSParticleList matricsParts;
  2116. int xrayStep = a_XrayStep;// *a_PixelSize;
  2117. GetMatricsParticlesFromRawParticle(a_pOTSPart, imageWidth,imageHeight,a_PixelSize, xrayStep, matricsParts);
  2118. a_pOTSPart->SetSubParticles(matricsParts);
  2119. }
  2120. //-----------
  2121. }
  2122. BOOL COTSImageProcess::MergeBigBoundaryParticles(COTSFieldDataList allFields,double pixelSize,int scanFieldSize, CSize ResolutionSize, COTSParticleList& mergedParts)
  2123. {
  2124. COTSSegmentsList boarderSegs;
  2125. auto FldMgr = new CFieldMgr(scanFieldSize, ResolutionSize);
  2126. std::map<COTSParticle*, COTSParticleList> mapMergeParticles;//hold up all the boundary connected particles. the pair's first is also the member of these particles.
  2127. std::map<COTSParticle*, COTSSegmentsList> mapMergedSegments;//hold up all the segment's corresponding clone in the connected particles.
  2128. for (auto centerfld : allFields)
  2129. {
  2130. // find neighbor field on the left.
  2131. auto leftFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::LEFT);
  2132. if (leftFld != nullptr)
  2133. {
  2134. auto leftParts = centerfld->GetLeftBorderedBigParticles();
  2135. auto rightParts = leftFld->GetRightBorderedBigParticles();
  2136. for (auto leftp : leftParts)
  2137. {
  2138. for (auto rightp : rightParts)
  2139. {
  2140. if (leftp->IsConnected(rightp.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::LEFT))
  2141. {
  2142. if (leftp->headerParticle != NULL)
  2143. {
  2144. if (rightp->headerParticle == NULL)
  2145. {
  2146. rightp->headerParticle = leftp->headerParticle;
  2147. mapMergeParticles[leftp->headerParticle].push_back(rightp);
  2148. }
  2149. }
  2150. else
  2151. {
  2152. if (rightp->headerParticle != NULL)
  2153. {
  2154. leftp->headerParticle = rightp.get();
  2155. mapMergeParticles[rightp.get()].push_back(leftp);
  2156. }
  2157. else
  2158. {
  2159. leftp->headerParticle = leftp.get();
  2160. rightp->headerParticle = leftp.get();
  2161. mapMergeParticles[leftp.get()].push_back(rightp);
  2162. }
  2163. }
  2164. }
  2165. }
  2166. }
  2167. }
  2168. //find neighbor field on the upward
  2169. auto upFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::UP);
  2170. if (upFld != nullptr)
  2171. {
  2172. auto upParts = centerfld->GetTopBorderedBigParticles();
  2173. auto downParts = upFld->GetBottomBorderedBigParticles();
  2174. for (auto upprt : upParts)
  2175. {
  2176. for (auto downprt : downParts)
  2177. {
  2178. if (upprt->IsConnected(downprt.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::UP))
  2179. {
  2180. if (upprt->headerParticle != NULL)
  2181. {
  2182. if (downprt->headerParticle == NULL)
  2183. {
  2184. downprt->headerParticle = upprt->headerParticle;
  2185. mapMergeParticles[upprt->headerParticle].push_back(downprt);
  2186. }
  2187. }
  2188. else
  2189. {
  2190. if (downprt->headerParticle != NULL)
  2191. {
  2192. upprt->headerParticle = downprt->headerParticle;
  2193. mapMergeParticles[downprt.get()].push_back(upprt);
  2194. }
  2195. else
  2196. {
  2197. upprt->headerParticle = upprt.get();
  2198. downprt->headerParticle = upprt.get();
  2199. mapMergeParticles[upprt.get()].push_back(downprt);
  2200. }
  2201. }
  2202. }
  2203. }
  2204. }
  2205. }
  2206. //find neighbor field on the downward.
  2207. auto downFld = FldMgr->FindNeighborField(allFields, centerfld,SORTING_DIRECTION::DOWN);
  2208. if (downFld != nullptr)
  2209. {
  2210. auto downParts = centerfld->GetBottomBorderedBigParticles();
  2211. auto upParts = downFld->GetTopBorderedBigParticles();
  2212. for (auto downprt : downParts)
  2213. {
  2214. for (auto upprt : upParts)
  2215. {
  2216. if (downprt->IsConnected(upprt.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::DOWN))
  2217. {
  2218. if (downprt->headerParticle != NULL)
  2219. {
  2220. if (upprt->headerParticle == NULL)
  2221. {
  2222. upprt->headerParticle = downprt->headerParticle;
  2223. mapMergeParticles[downprt->headerParticle].push_back(upprt);
  2224. }
  2225. }
  2226. else
  2227. {
  2228. if (upprt->headerParticle != NULL)
  2229. {
  2230. downprt->headerParticle = upprt->headerParticle;
  2231. mapMergeParticles[upprt->headerParticle].push_back(downprt);
  2232. }
  2233. else
  2234. {
  2235. downprt->headerParticle = downprt.get();
  2236. upprt->headerParticle = downprt.get();
  2237. mapMergeParticles[downprt.get()].push_back(upprt);
  2238. }
  2239. }
  2240. }
  2241. }
  2242. }
  2243. }
  2244. //find neighbor field on the right.
  2245. auto rightFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::RIGHT);
  2246. if (rightFld != nullptr)
  2247. {
  2248. auto rightParts = centerfld->GetRightBorderedBigParticles();
  2249. auto leftParts = rightFld->GetLeftBorderedBigParticles();
  2250. for (auto rightprt : rightParts)
  2251. {
  2252. for (auto leftprt : leftParts)
  2253. {
  2254. if (rightprt->IsConnected(leftprt.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::RIGHT))
  2255. {
  2256. if (rightprt->headerParticle != NULL)
  2257. {
  2258. if (leftprt->headerParticle == NULL)
  2259. {
  2260. leftprt->headerParticle = rightprt->headerParticle;
  2261. mapMergeParticles[rightprt->headerParticle].push_back(leftprt);
  2262. }
  2263. }
  2264. else
  2265. {
  2266. if (leftprt->headerParticle != NULL)
  2267. {
  2268. rightprt->headerParticle = leftprt->headerParticle;
  2269. mapMergeParticles[leftprt->headerParticle].push_back(rightprt);
  2270. }
  2271. else
  2272. {
  2273. rightprt->headerParticle = rightprt.get();
  2274. leftprt->headerParticle = rightprt.get();
  2275. mapMergeParticles[rightprt.get()].push_back(leftprt);
  2276. }
  2277. }
  2278. }
  2279. }
  2280. }
  2281. }
  2282. }
  2283. static int partTagId;
  2284. for (auto pair : mapMergeParticles)
  2285. {
  2286. struct EleAreaPercentage
  2287. {
  2288. EleAreaPercentage(double p, CElementChemistryPtr e)
  2289. {
  2290. areaPercentage = p;
  2291. eleData = e;
  2292. }
  2293. double areaPercentage;
  2294. CElementChemistryPtr eleData;
  2295. };
  2296. auto newPart = COTSParticlePtr(new COTSParticle());
  2297. COTSSegmentsList newSegs;
  2298. auto p = pair.first;
  2299. newPart->SetAbsolutePos(p->GetAbsolutPos());
  2300. //firstly,we sum up all the merged particles's area and get the represent string.
  2301. std::string partsStr = std::to_string(p->GetFieldId()) + ":" + std::to_string(p->GetAnalysisId());
  2302. double allPartArea = p->GetActualArea();//Get the first particle's area.
  2303. for (auto other : pair.second)// Get the total area of all these particles for the use of ele calcu.
  2304. {
  2305. partsStr += "," + std::to_string(other->GetFieldId()) + ":" + std::to_string(other->GetAnalysisId());//Get the subparticles string such as "1:1,2:1" etc.
  2306. allPartArea += other->GetActualArea();//Get other particle's area
  2307. }
  2308. // calculate all the new segment's position.
  2309. std::vector <COTSParticle*> allSubParts;
  2310. allSubParts.push_back(p);
  2311. for (auto other : pair.second)// Get the total area of all these particles for the use of ele calcu.
  2312. {
  2313. allSubParts.push_back(other.get());
  2314. }
  2315. for (auto subp : allSubParts)
  2316. {
  2317. int fid = subp->GetFieldId();
  2318. CPoint myFldPos;
  2319. for (auto f : allFields)//find this particle's filed.
  2320. {
  2321. if (f->GetId() == fid)
  2322. {
  2323. myFldPos = f->GetPosition();
  2324. }
  2325. }
  2326. int fldWidth = allFields[0]->Width;
  2327. int fldHeight = allFields[0]->Height;
  2328. CPoint fldLeftUpPos = CPoint(myFldPos.x + fldWidth / 2 , myFldPos.y + fldHeight / 2 );
  2329. for (auto s : subp->GetFeature()->GetSegmentsList())
  2330. {
  2331. COTSSegmentPtr newseg = COTSSegmentPtr(new COTSSegment());
  2332. newseg->SetStart(s->GetStart() + fldLeftUpPos.x);
  2333. 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.
  2334. newseg->SetLength(s->GetLength());
  2335. newSegs.push_back(newseg);
  2336. }
  2337. }
  2338. COTSFeaturePtr newFeature = COTSFeaturePtr(new COTSFeature());
  2339. newFeature->SetSegmentsList(newSegs);
  2340. newPart->SetFeature(newFeature);
  2341. newPart->CalCoverRect();
  2342. //second, we get all the element data and their area percentage .
  2343. std::map<std::string, std::vector<EleAreaPercentage>> mapEleData;
  2344. CPosXrayPtr pXray1 = p->GetXrayInfo();
  2345. if (pXray1 != nullptr)
  2346. {
  2347. for (auto ele : pXray1->GetElementQuantifyData())
  2348. {
  2349. mapEleData[ele->GetName().GetBuffer()].push_back(EleAreaPercentage(p->GetActualArea() / allPartArea, ele));
  2350. }
  2351. }
  2352. for (auto other : pair.second)
  2353. {
  2354. auto otherXray = other->GetXrayInfo();
  2355. if (otherXray != nullptr)
  2356. {
  2357. for (auto eledata : otherXray->GetElementQuantifyData())
  2358. {
  2359. mapEleData[eledata->GetName().GetBuffer()].push_back(EleAreaPercentage(other->GetActualArea() / allPartArea, eledata));
  2360. }
  2361. }
  2362. }
  2363. // third,we calculate all the element's new percentage data and get a new element chemistry list.
  2364. CElementChemistriesList newCheList;
  2365. for (auto eledata : mapEleData)
  2366. {
  2367. CElementChemistryPtr newEleche = CElementChemistryPtr(new CElementChemistry());
  2368. newEleche->SetName(CString(eledata.first.c_str()));
  2369. double newPercentage = 0;
  2370. for (auto d : eledata.second)
  2371. {
  2372. newPercentage += d.areaPercentage * d.eleData->GetPercentage();
  2373. }
  2374. newEleche->SetPercentage(newPercentage);
  2375. newCheList.push_back(newEleche);
  2376. }
  2377. CPosXrayPtr xray(new CPosXray());
  2378. xray->SetElementQuantifyData(newCheList);
  2379. newPart->SetXrayInfo(xray);
  2380. newPart->SetSubParticlesName(partsStr);
  2381. newPart->SetActualArea(allPartArea);
  2382. partTagId++;
  2383. newPart->SetParticleId(partTagId);
  2384. newPart->SetAnalysisId(partTagId);
  2385. std::string name = p->TypeName();
  2386. newPart->TypeName(name);
  2387. newPart->TypeColor(p->TypeColor());
  2388. mergedParts.push_back(newPart);
  2389. }
  2390. return true;
  2391. }
  2392. }