OTSImageProcess.cpp 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399
  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 "OTSMorphology.h"
  10. #include "../OTSLog/COTSUtilityDllFunExport.h"
  11. #include "FieldMgr.h"
  12. #include "BaseFunction.h"
  13. #include "GreyPeak.h"
  14. namespace OTSIMGPROC
  15. {
  16. using namespace cv;
  17. using namespace std;
  18. const int nBlackColor = 255;
  19. namespace
  20. {
  21. cv::Mat GetMatDataFromParticles(COTSParticleList parts, int width, int height)
  22. {
  23. int rows, cols;
  24. cols = width;
  25. rows = height;
  26. BYTE* pPixel = new BYTE[width * height];
  27. Mat cvcopyImg = Mat(rows, cols, CV_8UC1, pPixel);
  28. cvcopyImg = Scalar::all(0);
  29. for (auto p : parts)
  30. {
  31. auto area = p->GetPixelArea();
  32. if (area > 100)
  33. {
  34. auto fea = p->GetFeature();
  35. auto segs = fea->GetSegmentsList();
  36. for (auto seg : segs)
  37. {
  38. auto row = seg->GetHeight();
  39. for (int i = 0; i < seg->GetLength(); i++)
  40. {
  41. auto col = seg->GetStart() + i;
  42. cvcopyImg.at<char>(row, col) = 255;
  43. }
  44. }
  45. }
  46. }
  47. return cvcopyImg;
  48. }
  49. }
  50. COTSImageProcess::COTSImageProcess(COTSImageProcessParamPtr a_pImageProcessParam)
  51. {
  52. m_imageProcessParam = a_pImageProcessParam;
  53. }
  54. COTSImageProcess::~COTSImageProcess()
  55. {
  56. }
  57. BOOL COTSImageProcess::RemoveBGByCVconnectivities(CBSEImgPtr inBSEImg, double a_pixelSize, COTSFieldDataPtr m_pFieldData)
  58. {
  59. ASSERT(m_pFieldData);
  60. ASSERT(inBSEImg);
  61. ASSERT(m_imageProcessParam);
  62. int nWidthImg = inBSEImg->GetWidth();
  63. int nHeightImg = inBSEImg->GetHeight();
  64. m_pFieldData->Width = nWidthImg;
  65. m_pFieldData->Height = nHeightImg;
  66. long nImgSize = nWidthImg * nHeightImg;
  67. BYTE* pSrcImg = inBSEImg->GetImageDataPointer();
  68. BYTE* pTempImg = new BYTE[nImgSize];
  69. CRect r = CRect(0, 0, nWidthImg, nHeightImg);
  70. CBSEImgPtr imgNoBGBinary = CBSEImgPtr(new CBSEImg(r));
  71. BinaryProcess(inBSEImg, m_imageProcessParam, imgNoBGBinary/*, nNumParticle*/);
  72. BYTE* pPixel = imgNoBGBinary->GetImageDataPointer();
  73. long nEigenGrayStart = m_imageProcessParam->GetParticleGray().GetStart();
  74. long nEigenGrayEnd = m_imageProcessParam->GetParticleGray().GetEnd();
  75. // get the area image
  76. Mat cvcopyImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);
  77. Mat labels = Mat::zeros(cvcopyImg.size(), CV_32S);
  78. Mat stats, centroids;
  79. int number = connectedComponentsWithStats(cvcopyImg, labels, stats, centroids, 8, CV_32S);
  80. double rMin = m_imageProcessParam->GetIncArea().GetStart()/2.0;
  81. double rMax = m_imageProcessParam->GetIncArea().GetEnd()/2.0;
  82. double partAreaMin = rMin * rMin * 3.14159;
  83. double partAreaMax = rMax * rMax * 3.14159;
  84. COTSParticleList listParticleOut;
  85. for (size_t i = 1; i < number; i++)
  86. {
  87. int center_x = centroids.at<double>(i, 0);
  88. int center_y = centroids.at<double>(i, 1);
  89. int x = stats.at<int>(i, CC_STAT_LEFT);
  90. int y = stats.at<int>(i, CC_STAT_TOP);
  91. int w = stats.at<int>(i, CC_STAT_WIDTH);
  92. int h = stats.at<int>(i, CC_STAT_HEIGHT);
  93. int area = stats.at<int>(i, CC_STAT_AREA);
  94. double actualArea = area * a_pixelSize * a_pixelSize;
  95. if (actualArea >= partAreaMin && actualArea < partAreaMax)
  96. {
  97. Rect rectMax = Rect(x, y, w, h);
  98. Mat rectROI = labels(rectMax).clone();
  99. Mat imageROI = Mat::zeros(rectMax.size(), cvcopyImg.type());
  100. //exclude the point which intersect into this bounding box but is not in this group.
  101. int label = i;
  102. for (int row = 0; row < rectROI.rows; row++)
  103. {
  104. for (int col = 0; col < rectROI.cols; col++)
  105. {
  106. int v = rectROI.at<int>(row, col);
  107. if (v == label)
  108. {
  109. 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.
  110. }
  111. }
  112. }
  113. COTSParticleList roiParts;
  114. if (GetOneParticleFromROI(rectMax.x, rectMax.y, rectMax.width, rectMax.height, imageROI.data, roiParts))
  115. {
  116. if (roiParts.size() > 0)
  117. {
  118. COTSParticlePtr roiPart = roiParts[0];//we will find only one part in the roi.
  119. roiPart->SetXRayPos(CPoint(center_x, center_y));
  120. CRect r = CRect(x, y, x + w, y + h);
  121. roiPart->SetParticleRect(r);
  122. roiPart->SetActualArea(actualArea);
  123. roiPart->SetPixelArea(area);
  124. listParticleOut.push_back(roiPart);
  125. }
  126. }
  127. }
  128. }
  129. int nTagId;
  130. COTSParticleList listParticleSearched;
  131. for (auto pParticle : listParticleOut)
  132. {
  133. COTSFeaturePtr pFeature = pParticle->GetFeature();
  134. COTSSegmentsList listSegment = pFeature->GetSegmentsList();
  135. long nPixelNum = 0;
  136. long eigenPixelNum = 0;
  137. long nPixelAll = 0;
  138. int nStartS = 0;
  139. int nHeightS = 0;
  140. int nLengthS = 0;
  141. for (auto pSegment : listSegment)
  142. {
  143. // get particle average gray
  144. nStartS = pSegment->GetStart();
  145. nHeightS = pSegment->GetHeight();
  146. nLengthS = pSegment->GetLength();
  147. nPixelNum += (long)nLengthS;
  148. for (unsigned int i = 0; i < nLengthS; i++)
  149. {
  150. long nValueTemp = (long)*(pSrcImg + nHeightS * nWidthImg + nStartS + i);
  151. nPixelAll += nValueTemp;
  152. if (nValueTemp >= nEigenGrayStart && nValueTemp <= nEigenGrayEnd)
  153. {
  154. eigenPixelNum += 1;
  155. }
  156. }
  157. }
  158. double r = m_imageProcessParam->GetIncArea().GetStart() / 2;
  159. double minPartArea = 3.14159 * r * r;
  160. if (eigenPixelNum* a_pixelSize*a_pixelSize > minPartArea)
  161. {
  162. BYTE nAveGray = (BYTE)(nPixelAll / nPixelNum);
  163. pParticle->SetAveGray(nAveGray);
  164. auto fieldOTSRect = m_pFieldData->GetOTSRect();
  165. CPoint leftTop = fieldOTSRect.GetTopLeft();
  166. CRect rectInSinglefld = pParticle->GetParticleRect();
  167. CPoint OTSLeftTop = CPoint(leftTop.x + rectInSinglefld.left * a_pixelSize, leftTop.y - rectInSinglefld.top * a_pixelSize);
  168. CPoint OTSRightBottom = CPoint(leftTop.x + rectInSinglefld.right * a_pixelSize, leftTop.y - rectInSinglefld.bottom * a_pixelSize);
  169. COTSRect recInOTSCord = COTSRect(OTSLeftTop, OTSRightBottom);
  170. pParticle->SetOTSRect(recInOTSCord);
  171. listParticleSearched.push_back(pParticle);
  172. }
  173. }
  174. m_pFieldData->SetParticleList(listParticleSearched);
  175. delete[]pTempImg;
  176. return TRUE;
  177. }
  178. BOOL COTSImageProcess::GetParticlesBySpecialGrayRange(CBSEImgPtr a_pBSEImg, CIntRangePtr a_grayRange,CDoubleRangePtr a_diameterRange,double a_pixelSize, COTSFieldDataPtr m_pFieldData)
  179. {
  180. ASSERT(m_pFieldData);
  181. ASSERT(a_pBSEImg);
  182. ASSERT(a_grayRange);
  183. int nWidthImg = a_pBSEImg->GetWidth();
  184. int nHeightImg = a_pBSEImg->GetHeight();
  185. m_pFieldData->Width = nWidthImg;
  186. m_pFieldData->Height = nHeightImg;
  187. long nImgSize = nWidthImg * nHeightImg;
  188. BYTE* pSrcImg = a_pBSEImg->GetImageDataPointer();
  189. BYTE* pTempImg = new BYTE[nImgSize];
  190. CRect r = CRect(0, 0, nWidthImg, nHeightImg);
  191. CBSEImgPtr imgNoBGBinary = CBSEImgPtr(new CBSEImg(r));
  192. long nNumParticle = 0;
  193. GetSpecialGrayRangeImage(a_pBSEImg, *a_grayRange, imgNoBGBinary, nNumParticle);
  194. BYTE* pPixel = imgNoBGBinary->GetImageDataPointer();
  195. if (nNumParticle == 0)
  196. {
  197. COTSParticleList listParticleEmpty;
  198. listParticleEmpty.clear();
  199. m_pFieldData->SetParticleList(listParticleEmpty);
  200. }
  201. else
  202. {
  203. // get the area image
  204. Mat cvcopyImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);
  205. Mat labels = Mat::zeros(cvcopyImg.size(), CV_32S);
  206. Mat stats, centroids;
  207. int number = connectedComponentsWithStats(cvcopyImg, labels, stats, centroids, 8, CV_32S);
  208. double rStart = a_diameterRange->GetStart() / 2.0;
  209. double rEnd = a_diameterRange->GetEnd() / 2.0;
  210. double areaStart = rStart * rStart * 3.14159;
  211. double areaEnd = rEnd * rEnd * 3.14159;
  212. COTSParticleList listParticleOut;
  213. for (size_t i = 1; i < number; i++)
  214. {
  215. int center_x = centroids.at<double>(i, 0);
  216. int center_y = centroids.at<double>(i, 1);
  217. int x = stats.at<int>(i, CC_STAT_LEFT);
  218. int y = stats.at<int>(i, CC_STAT_TOP);
  219. int w = stats.at<int>(i, CC_STAT_WIDTH);
  220. int h = stats.at<int>(i, CC_STAT_HEIGHT);
  221. int area = stats.at<int>(i, CC_STAT_AREA);
  222. double actualArea = area * a_pixelSize * a_pixelSize;
  223. if (actualArea >= areaStart && actualArea < areaEnd)
  224. {
  225. Rect rectMax = Rect(x, y, w, h);
  226. Mat rectROI = labels(rectMax).clone();
  227. Mat imageROI = Mat::zeros(rectMax.size(), cvcopyImg.type());
  228. //exclude the point which intersect into this bounding box but is not in this group.
  229. int label = i;
  230. for (int row = 0; row < rectROI.rows; row++)
  231. {
  232. for (int col = 0; col < rectROI.cols; col++)
  233. {
  234. int v = rectROI.at<int>(row, col);
  235. if (v == label)
  236. {
  237. imageROI.at<uchar>(row, col) = 255;
  238. }
  239. }
  240. }
  241. COTSParticleList roiParts;
  242. if (!GetOneParticleFromROI(rectMax.x, rectMax.y, rectMax.width, rectMax.height, imageROI.data, roiParts))
  243. {
  244. continue;
  245. }
  246. if (roiParts.size() > 0)
  247. {
  248. COTSParticlePtr roiPart = roiParts[0];
  249. roiPart->SetXRayPos(CPoint(center_x, center_y));
  250. CRect r = CRect(x, y, x + w, y + h);
  251. roiPart->SetParticleRect(r);
  252. roiPart->SetActualArea(actualArea);
  253. roiPart->SetPixelArea(area);
  254. listParticleOut.push_back(roiPart);
  255. }
  256. }
  257. }
  258. // form a image only have particles on
  259. //COTSSegmentsList listImage;
  260. for (auto pParticle : listParticleOut)
  261. {
  262. int area = pParticle->GetActualArea();
  263. double pActualArea = area ;
  264. COTSFeaturePtr pFeature = pParticle->GetFeature();
  265. COTSSegmentsList listSegment = pFeature->GetSegmentsList();
  266. long nPixelNum = 0;
  267. long nPixelAll = 0;
  268. int nStartS = 0;
  269. int nHeightS = 0;
  270. int nLengthS = 0;
  271. for (auto pSegment : listSegment)
  272. {
  273. // get particle average gray
  274. nStartS = pSegment->GetStart();
  275. nHeightS = pSegment->GetHeight();
  276. nLengthS = pSegment->GetLength();
  277. nPixelNum += (long)nLengthS;
  278. for (unsigned int i = 0; i < nLengthS; i++)
  279. {
  280. long nValueTemp = (long)*(pSrcImg + nHeightS * nWidthImg + nStartS + i);
  281. nPixelAll += nValueTemp;
  282. }
  283. }
  284. BYTE nAveGray = (BYTE)(nPixelAll / nPixelNum);
  285. pParticle->SetAveGray(nAveGray);
  286. auto fieldOTSRect = m_pFieldData->GetOTSRect();
  287. CPoint leftTop = fieldOTSRect.GetTopLeft();
  288. CRect rectInSinglefld = pParticle->GetParticleRect();
  289. CPoint OTSLeftTop = CPoint(leftTop.x + rectInSinglefld.left * a_pixelSize, leftTop.y - rectInSinglefld.top * a_pixelSize);
  290. CPoint OTSRightBottom = CPoint(leftTop.x + rectInSinglefld.right * a_pixelSize, leftTop.y - rectInSinglefld.bottom * a_pixelSize);
  291. COTSRect recInOTSCord = COTSRect(OTSLeftTop, OTSRightBottom);
  292. pParticle->SetOTSRect(recInOTSCord);
  293. }
  294. m_pFieldData->SetParticleList(listParticleOut);
  295. }
  296. delete[]pTempImg;
  297. return TRUE;
  298. }
  299. BOOL COTSImageProcess::SplitFieldImageIntoMatricsParticle(CBSEImgPtr fieldImg, double a_PixelSize, COTSFieldDataPtr outFldData)
  300. {
  301. int nWidthImg = fieldImg->GetWidth();
  302. int nHeightImg = fieldImg->GetHeight();
  303. outFldData->Width = nWidthImg;
  304. outFldData->Height = nHeightImg;
  305. CRect r = CRect(0, 0, nWidthImg, nHeightImg);
  306. CBSEImgPtr imgNoBGBinary = CBSEImgPtr(new CBSEImg(r));
  307. double xrayStep = m_imageProcessParam->GetXrayStep();
  308. xrayStep = xrayStep / a_PixelSize;
  309. auto rect = fieldImg->GetImageRect();
  310. std::vector<CPoint> matrixPs;
  311. GetMatrixPointsFromRect(rect, xrayStep, matrixPs);
  312. int i = 0;
  313. COTSParticleList matrixParts;
  314. for (auto point : matrixPs)
  315. {
  316. COTSParticlePtr part = COTSParticlePtr(new COTSParticle());
  317. COTSSegmentsList segs;
  318. for (int i = 0; i < xrayStep; i++)
  319. {
  320. COTSSegmentPtr seg = COTSSegmentPtr(new COTSSegment());
  321. seg->SetStart(point.x - xrayStep / 2);
  322. seg->SetLength(xrayStep);
  323. seg->SetHeight(point.y - xrayStep / 2 + i);
  324. //auto originalSegs = segsOnTheSameHeight[seg->GetHeight()];
  325. int currentH = seg->GetHeight();
  326. int segStart = seg->GetStart();
  327. int segEnd = seg->GetEnd();
  328. if (segStart < 0) segStart = 0;
  329. if (segEnd >= rect.Width()) segEnd = rect.Width()-1;
  330. if (currentH >= rect.Height()) continue;
  331. if (currentH < 0) continue;
  332. seg->SetStart(segStart);
  333. seg->SetEnd(segEnd);
  334. segs.push_back(seg);
  335. }
  336. if (segs.size() > 0)
  337. {
  338. part->GetFeature()->SetSegmentsList(segs);
  339. part->SetActualArea(xrayStep * xrayStep);
  340. part->CalXRayPos();
  341. part->SetFieldId(outFldData->GetId());
  342. part->SetAnalysisId(i);
  343. matrixParts.push_back(part);
  344. i++;
  345. }
  346. }
  347. outFldData->SetParticleList(matrixParts);
  348. return true;
  349. //-----------
  350. }
  351. BOOL COTSImageProcess::SplitRawImageIntoParticlesByWaterShed(CBSEImgPtr fieldImg, double a_PixelSize, COTSFieldDataPtr outFldData)
  352. {
  353. int nWidthImg = fieldImg->GetWidth();
  354. int nHeightImg = fieldImg->GetHeight();
  355. outFldData->Width = nWidthImg;
  356. outFldData->Height = nHeightImg;
  357. CRect r = CRect(0, 0, nWidthImg, nHeightImg);
  358. CBSEImgPtr imgNoBGBinary = CBSEImgPtr(new CBSEImg(r));
  359. COTSParticleList listMainParticle;
  360. BinaryProcess(fieldImg, m_imageProcessParam, imgNoBGBinary);
  361. CDoubleRange areaRng = m_imageProcessParam->GetIncArea();
  362. GetParticlesFromBinaryImage(imgNoBGBinary, areaRng, a_PixelSize, listMainParticle);
  363. outFldData->SetParticleList(listMainParticle);
  364. CBSEImgPtr noBgImg = CBSEImgPtr(new CBSEImg(fieldImg->GetImageRect()));
  365. RemoveBackGround(fieldImg, noBgImg);
  366. BlurImage(noBgImg);
  367. std::vector<CPoint> seeds;
  368. seeds.clear();
  369. FindSeedsByGrayScale(noBgImg, a_PixelSize, seeds);
  370. std::vector<CPoint> matrixseeds;
  371. matrixseeds.clear();
  372. if (seeds.size() <50)
  373. {
  374. auto step = areaRng.GetStart() * 4;
  375. GetMatrixPointsFromRect(fieldImg->GetImageRect(), step / a_PixelSize, matrixseeds);
  376. seeds.insert(seeds.end(), matrixseeds.begin(), matrixseeds.end());
  377. }
  378. Mat matImg = GetMatDataFromBseImg(noBgImg);
  379. Mat marks(matImg.size(), CV_32S);
  380. marks = Scalar::all(0);
  381. for (int index = 0; index < seeds.size(); index++)
  382. {
  383. auto p = seeds[index];
  384. marks.at<INT32>(p.y, p.x) = index + 10;
  385. }
  386. Mat imageGray3;
  387. cv::cvtColor(matImg, imageGray3, cv::COLOR_GRAY2RGB);// opencv4
  388. cv::watershed(imageGray3, marks);
  389. auto matBinNobg = GetMatDataFromParticles(listMainParticle, nWidthImg, nHeightImg);
  390. //the first "long" is the index of particle,the second "long" is the row number of the segment, the third "long" is the sequence of the pixel.
  391. std::map<long, std::map<long, std::set<long>>> pixelMap;
  392. for (int i = 0; i < marks.rows; i++)
  393. {
  394. for (int j = 0; j < marks.cols; j++)
  395. {
  396. if (matBinNobg.at<char>(i, j) == 0)
  397. {
  398. marks.at<int>(i, j) = 0;
  399. continue;
  400. }
  401. long index = marks.at<int>(i, j);
  402. if (index == -1)//if this pixel is on the border,then it will be absorbed by it's neighbor.
  403. {
  404. if (i + 1 < marks.rows && marks.at<int>(i + 1, j) != -1 && matBinNobg.at<char>(i + 1, j) != 0)
  405. {
  406. index = marks.at<int>(i + 1, j);
  407. }
  408. else if (j + 1 < marks.cols && marks.at<int>(i, j + 1) != -1 && matBinNobg.at<char>(i, j + 1) != 0)
  409. {
  410. index = marks.at<int>(i, j + 1);
  411. }
  412. else if (i > 0 && marks.at<int>(i - 1, j) != -1 && matBinNobg.at<char>(i - 1, j) != 0)
  413. {
  414. index = marks.at<int>(i - 1, j);
  415. }
  416. else if (j > 0 && marks.at<int>(i, j - 1) != -1 && matBinNobg.at<char>(i, j - 1) != 0)
  417. {
  418. index = marks.at<int>(i, j - 1);
  419. }
  420. marks.at<int>(i, j) = index;
  421. }
  422. pixelMap[index][i].insert(j);
  423. }
  424. }
  425. COTSParticleList listParticleOut;
  426. for (auto it = pixelMap.begin(); it != pixelMap.end(); it++)
  427. {
  428. if (it->first == -1) continue;
  429. if (it->first == 0) continue;
  430. auto pixelsegs = it->second;
  431. COTSParticlePtr newsubpart = COTSParticlePtr(new COTSParticle());
  432. COTSFeaturePtr newFea = COTSFeaturePtr(new COTSFeature());
  433. for (auto pixelseg : pixelsegs)
  434. {
  435. auto start = (*pixelseg.second.begin());
  436. for (auto pix = pixelseg.second.begin(); pix != pixelseg.second.end(); pix++)
  437. {
  438. auto curNode = pix;
  439. ++curNode;
  440. if ((curNode) != pixelseg.second.end())
  441. {
  442. if (*curNode - *pix > 1)
  443. {
  444. COTSSegmentPtr seg = COTSSegmentPtr(new COTSSegment());
  445. seg->SetStart(start);
  446. seg->SetEnd(*pix);
  447. seg->SetHeight(pixelseg.first);
  448. newFea->AddSegment(seg);
  449. start = *curNode;
  450. }
  451. }
  452. else
  453. {
  454. COTSSegmentPtr seg = COTSSegmentPtr(new COTSSegment());
  455. seg->SetStart(start);
  456. seg->SetEnd(*pix);
  457. seg->SetHeight(pixelseg.first);
  458. newFea->AddSegment(seg);
  459. }
  460. }
  461. }
  462. newsubpart->SetFeature(newFea);
  463. newsubpart->CalXRayPos();
  464. newsubpart->CalActualArea(a_PixelSize);
  465. CPoint pos = newsubpart->GetXRayPos();
  466. for (auto mp : listMainParticle)
  467. {
  468. auto fieldOTSRect = outFldData->GetOTSRect();
  469. CPoint leftTop = fieldOTSRect.GetTopLeft();
  470. CRect rectInSinglefld = mp->GetParticleRect();
  471. CPoint OTSLeftTop = CPoint(leftTop.x + rectInSinglefld.left * a_PixelSize, leftTop.y - rectInSinglefld.top * a_PixelSize);
  472. CPoint OTSRightBottom = CPoint(leftTop.x + rectInSinglefld.right * a_PixelSize, leftTop.y - rectInSinglefld.bottom * a_PixelSize);
  473. COTSRect recInOTSCord = COTSRect(OTSLeftTop, OTSRightBottom);
  474. mp->SetOTSRect(recInOTSCord);
  475. mp->SetFieldId(outFldData->GetId());
  476. if (mp->IfContain(pos))
  477. {
  478. CRect rectInSinglefld = newsubpart->GetParticleRect();
  479. CPoint OTSLeftTop = CPoint(leftTop.x + rectInSinglefld.left * a_PixelSize, leftTop.y - rectInSinglefld.top * a_PixelSize);
  480. CPoint OTSRightBottom = CPoint(leftTop.x + rectInSinglefld.right * a_PixelSize, leftTop.y - rectInSinglefld.bottom * a_PixelSize);
  481. COTSRect recInOTSCord = COTSRect(OTSLeftTop, OTSRightBottom);
  482. newsubpart->SetFieldId(mp->GetFieldId());
  483. newsubpart->SetOTSRect(recInOTSCord);
  484. mp->AddSubParticle(newsubpart);
  485. break;
  486. }
  487. }
  488. }
  489. return true;
  490. }
  491. CIntRangePtr COTSImageProcess::CalBackground(CBSEImgPtr m_pBSEImg)
  492. {
  493. auto ranges = CalcuGrayLevelRange(m_pBSEImg);
  494. return ranges[0];
  495. }
  496. std::vector<CIntRangePtr> COTSImageProcess::CalcuGrayLevelRange(CBSEImgPtr m_pBSEImg)
  497. {
  498. CIntRangePtr pBackground = CIntRangePtr(new CIntRange());
  499. WORD originChartData[MAXBYTE];
  500. WORD firstSmoothChart[MAXBYTE];
  501. WORD secondSmooth[MAXBYTE];
  502. //1. get chart data
  503. m_pBSEImg->InitChartData();
  504. memcpy(originChartData, m_pBSEImg->GetBSEChart(), sizeof(WORD) * MAXBYTE);
  505. originChartData[0] = 0;
  506. originChartData[254] = 0;
  507. linearSmooth5(originChartData, firstSmoothChart, MAXBYTE);
  508. linearSmooth5(firstSmoothChart, secondSmooth, MAXBYTE);
  509. //2. get down edge
  510. int nLengthEdge = MAXBYTE + 2;
  511. WORD n_aBSEChart[MAXBYTE + 2];
  512. memset(n_aBSEChart, 0, sizeof(WORD) * nLengthEdge);
  513. std::map<long, std::vector<int>> peakMap;// hold all the peaks in this spectrum which are sorted by there area.
  514. std::vector<int> currentUpSeries;
  515. std::vector<int> currentPeakSeries;
  516. // make sure the wave begin with up edge and end with down edge
  517. n_aBSEChart[0] = 0;
  518. n_aBSEChart[nLengthEdge - 1] = 0;
  519. memcpy(&n_aBSEChart[1], &secondSmooth, sizeof(WORD) * MAXBYTE);
  520. int nLengthCom = MAXBYTE + 1;
  521. // up edge
  522. for (int i = 0; i < nLengthCom; i++)
  523. {
  524. if (n_aBSEChart[i] <= n_aBSEChart[i + 1])//this is a upward edge
  525. {
  526. if (currentPeakSeries.size() > 0)
  527. {
  528. int seriesSize = currentPeakSeries.size();
  529. long area = 0;
  530. for (int i = 0; i < seriesSize; i++)
  531. {
  532. area = area + n_aBSEChart[currentPeakSeries[i]];
  533. }
  534. peakMap[area] = currentPeakSeries;
  535. currentPeakSeries.clear();
  536. }
  537. currentUpSeries.push_back(i + 1);// save all the continuous up edge
  538. }
  539. else//this is a downward edge
  540. {
  541. // encounter a downward edge means upward edge series end,
  542. if (currentUpSeries.size() > 0)
  543. {
  544. currentPeakSeries = currentUpSeries;
  545. currentUpSeries.clear();
  546. }
  547. currentPeakSeries.push_back(i + 1);
  548. }
  549. }
  550. if (currentPeakSeries.size() > 0)
  551. {
  552. int seriesSize = currentPeakSeries.size();
  553. long area = 0;
  554. for (int i = 0; i < seriesSize; i++)
  555. {
  556. area = area + n_aBSEChart[currentPeakSeries[i]];
  557. }
  558. peakMap[area] = currentPeakSeries;
  559. currentPeakSeries.clear();
  560. }
  561. std::vector<CIntRangePtr> ranges;
  562. std::map<long, std::vector<int>>::reverse_iterator it;
  563. for (it=peakMap.rbegin();it!=peakMap.rend();it++)
  564. {
  565. CIntRangePtr pRange = CIntRangePtr(new CIntRange());
  566. pRange->SetStart(it->second[0]);
  567. pRange->SetEnd(it->second[it->second.size()-1]);
  568. ranges.push_back(pRange);
  569. }
  570. return ranges;
  571. }
  572. void COTSImageProcess::GetSpecialGrayRangeImage(CBSEImgPtr a_pImgIn, CIntRange a_SpecialGrayRange, CBSEImgPtr a_pBinImgOut, long& foundedPixelNum)
  573. {
  574. // the background pixel will be 0,and the other part will be 255.
  575. ASSERT(a_pImgIn);
  576. int nWidthImg = a_pImgIn->GetWidth();
  577. int nHeightImg = a_pImgIn->GetHeight();
  578. long nImgSize = nWidthImg * nHeightImg;
  579. BYTE* pTempImg = new BYTE[nImgSize];
  580. BYTE* pSrcImg = a_pImgIn->GetImageDataPointer();
  581. BYTE* pPixel = new byte[nImgSize];
  582. long nBGStart;
  583. long nBGEnd;
  584. long nNumParticle = 0;
  585. nBGStart = a_SpecialGrayRange.GetStart();
  586. nBGEnd = a_SpecialGrayRange.GetEnd();
  587. // delete background
  588. for (unsigned int i = 0; i < nImgSize; i++)
  589. {
  590. if (pSrcImg[i] >= nBGStart && pSrcImg[i] <= nBGEnd)
  591. {
  592. pPixel[i] = 255;
  593. nNumParticle++;
  594. }
  595. else
  596. {
  597. pPixel[i] = 0;
  598. }
  599. }
  600. BErode3(pPixel, pTempImg, 5, nHeightImg, nWidthImg);
  601. BDilate3(pTempImg, pPixel, 5, nHeightImg, nWidthImg);
  602. a_pBinImgOut->SetImageData(pPixel, nWidthImg, nHeightImg);
  603. foundedPixelNum = nNumParticle;
  604. delete[] pTempImg;
  605. return;
  606. }
  607. void COTSImageProcess::BinaryProcess(CBSEImgPtr a_pImgIn, COTSImageProcessParamPtr a_pImageProcessParam, CBSEImgPtr a_pBinImgOut/*,long& foundedPixelNum*/)
  608. {
  609. // the background pixel will be 0,and the other part will be 255.
  610. ASSERT(a_pImgIn);
  611. ASSERT(a_pImageProcessParam);
  612. int nWidthImg = a_pImgIn->GetWidth();
  613. int nHeightImg = a_pImgIn->GetHeight();
  614. long nImgSize = nWidthImg * nHeightImg;
  615. BYTE* pSrcImg = a_pImgIn->GetImageDataPointer();
  616. BYTE* pPixel= new BYTE[nImgSize];
  617. Mat srcImgMat = GetMatDataFromBseImg(a_pImgIn);
  618. Mat rstMat;
  619. long nBGStart;
  620. long nBGEnd;
  621. long nPartStart;
  622. long nPartEnd;
  623. long nNumParticle = 0;
  624. if (a_pImageProcessParam->GetBGRemoveType() == OTS_BGREMOVE_TYPE::MANUAL || a_pImageProcessParam->GetBGRemoveType() == OTS_BGREMOVE_TYPE::WaterShed)
  625. {
  626. nBGStart = a_pImageProcessParam->GetBGGray().GetStart();
  627. nBGEnd = a_pImageProcessParam->GetBGGray().GetEnd();
  628. nPartStart = a_pImageProcessParam->GetParticleGray().GetStart();
  629. nPartEnd = a_pImageProcessParam->GetParticleGray().GetEnd();
  630. //CVRemoveBG(srcImgMat, rstMat, nBGStart, nBGEnd);
  631. RemoveBG_old(srcImgMat, rstMat, nBGStart, nBGEnd, nNumParticle);
  632. pPixel = rstMat.data;
  633. }
  634. else
  635. {
  636. switch (a_pImageProcessParam->GetAutoBGRemoveType())
  637. {
  638. case OTS_AUTOBGREMOVE_TYPE::DOWNWARD:
  639. //RemoveBG_old(srcImgMat, rstMat, 0, nBGEnd, nNumParticle);
  640. //CVRemoveBG(srcImgMat, rstMat, 0, nBGEnd, nNumParticle);
  641. AutoRemove_background_OTS(srcImgMat, rstMat, 1);
  642. break;
  643. case OTS_AUTOBGREMOVE_TYPE::UPWARD:
  644. //RemoveBG_old(srcImgMat, rstMat, nBGStart, 255, nNumParticle);
  645. //CVRemoveBG(srcImgMat, rstMat, nBGStart, 255, nNumParticle);
  646. AutoRemove_background_OTS(srcImgMat, rstMat, 0);
  647. break;
  648. case OTS_AUTOBGREMOVE_TYPE::MIDDLE:
  649. //RemoveBG_old(srcImgMat, rstMat, nBGStart, nBGEnd, nNumParticle);
  650. //CVRemoveBG(srcImgMat, rstMat, nBGStart, nBGEnd, nNumParticle);
  651. AutoRemove_background_OTS(srcImgMat, rstMat, 2);
  652. break;
  653. default:
  654. break;
  655. }
  656. pPixel = rstMat.data;
  657. }
  658. a_pBinImgOut->SetImageData(pPixel,nWidthImg,nHeightImg);
  659. return ;
  660. }
  661. void COTSImageProcess::RemoveBackGround(CBSEImgPtr a_pImgIn, CBSEImgPtr a_pBinImgOut)
  662. {
  663. // the background pixel will be 0,and the other part will be 255.
  664. ASSERT(a_pImgIn);
  665. ASSERT(m_imageProcessParam);
  666. int nWidthImg = a_pImgIn->GetWidth();
  667. int nHeightImg = a_pImgIn->GetHeight();
  668. long nImgSize = nWidthImg * nHeightImg;
  669. BYTE* pTempImg = new BYTE[nImgSize];
  670. BYTE* pSrcImg = a_pImgIn->GetImageDataPointer();
  671. BYTE* pPixel = new byte[nImgSize];
  672. long nBGStart;
  673. long nBGEnd;
  674. long nPartStart;
  675. long nPartEnd;
  676. long nNumParticle = 0;
  677. if (m_imageProcessParam->GetBGRemoveType() == OTS_BGREMOVE_TYPE::MANUAL|| m_imageProcessParam->GetBGRemoveType() == OTS_BGREMOVE_TYPE::WaterShed)
  678. {
  679. nBGStart = m_imageProcessParam->GetBGGray().GetStart();
  680. nBGEnd = m_imageProcessParam->GetBGGray().GetEnd();
  681. nPartStart = m_imageProcessParam->GetParticleGray().GetStart();
  682. nPartEnd = m_imageProcessParam->GetParticleGray().GetEnd();
  683. // delete background
  684. for (unsigned int i = 0; i < nImgSize; i++)
  685. {
  686. if (pSrcImg[i] >= nBGStart && pSrcImg[i] <= nBGEnd)
  687. {
  688. pPixel[i] = 0;
  689. }
  690. else
  691. {
  692. pPixel[i] = pSrcImg[i];
  693. nNumParticle++;
  694. }
  695. if (pSrcImg[i]<nPartStart || pSrcImg[i]>nPartEnd)
  696. {
  697. pPixel[i] = 0;
  698. }
  699. }
  700. }
  701. a_pBinImgOut->SetImageData(pPixel, nWidthImg, nHeightImg);
  702. delete[] pTempImg;
  703. return;
  704. }
  705. BOOL COTSImageProcess::GetParticles(long left, long top, long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSParticleList& a_listParticles)
  706. {
  707. ASSERT(a_pPixel);
  708. if (!a_pPixel)
  709. {
  710. return FALSE;
  711. }
  712. //a_listParticles.clear();
  713. COTSParticleList findedParts;
  714. COTSSegmentsList listSegment;
  715. listSegment.clear();
  716. //1. get segment line by line
  717. if (!GetSegmentList(left, top, a_nWidth, a_nHeight, a_pPixel, listSegment))
  718. {
  719. return FALSE;
  720. }
  721. if ((int)listSegment.size() == 0)
  722. {
  723. return FALSE;
  724. }
  725. //2. save the temp feature
  726. COTSFeatureList listFeature;
  727. listFeature.clear();
  728. if (!GetFeatureList(listSegment, listFeature))//get every feature for all the particle,the complete feature.
  729. {
  730. return FALSE;
  731. }
  732. if ((int)listFeature.size() == 0)
  733. {
  734. return FALSE;
  735. }
  736. /*COTSParticleList listParticles;
  737. listParticles.clear();*/
  738. if (!ChangeFeaturelist(listFeature, findedParts))
  739. {
  740. return FALSE;
  741. }
  742. for (auto f : findedParts)
  743. {
  744. a_listParticles.push_back(f);
  745. }
  746. return TRUE;
  747. }
  748. BOOL COTSImageProcess::GetOneParticleFromROI(long left, long top, long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSParticleList& a_listParticles)
  749. {
  750. ASSERT(a_pPixel);
  751. if (!a_pPixel)
  752. {
  753. return FALSE;
  754. }
  755. //a_listParticles.clear();
  756. COTSParticleList findedParts;
  757. COTSSegmentsList listSegment;
  758. listSegment.clear();
  759. //1. get segment line by line
  760. if (!GetSegmentList(left, top, a_nWidth, a_nHeight, a_pPixel, listSegment))
  761. {
  762. return FALSE;
  763. }
  764. if ((int)listSegment.size() == 0)
  765. {
  766. return FALSE;
  767. }
  768. //2. save the temp feature
  769. COTSFeatureList listFeature;
  770. listFeature.clear();
  771. COTSFeaturePtr fea = COTSFeaturePtr(new COTSFeature());
  772. fea->SetSegmentsList(listSegment);
  773. listFeature.push_back(fea);
  774. if ((int)listFeature.size() == 0)
  775. {
  776. return FALSE;
  777. }
  778. if (!ChangeFeaturelist(listFeature, findedParts))
  779. {
  780. return FALSE;
  781. }
  782. for (auto f : findedParts)
  783. {
  784. a_listParticles.push_back(f);
  785. }
  786. return TRUE;
  787. }
  788. BOOL COTSImageProcess::GetSegmentList(long left, long top, long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSSegmentsList& a_listSegments)
  789. {
  790. ASSERT(a_pPixel);
  791. long nImgSize = a_nWidth * a_nHeight;
  792. a_listSegments.clear();
  793. //1. get segment line by line
  794. long nLine, nm, nn;
  795. long nStart = 0, nLength = 0;
  796. for (nLine = 0; nLine < a_nHeight; nLine++)
  797. {
  798. for (nm = 0; nm < a_nWidth; nm += (nLength + 1))
  799. {
  800. nLength = 0;
  801. // get start
  802. if (*(a_pPixel + nLine * a_nWidth + nm) != 0)
  803. {
  804. nStart = nm;
  805. nLength++;
  806. //get length
  807. for (nn = nm + 1; nn < a_nWidth; nn++)
  808. {
  809. // check if segment is over, break
  810. if (nLength != 0)
  811. {
  812. if (*(a_pPixel + nLine * a_nWidth + nn) == 0)
  813. break;
  814. }
  815. if (*(a_pPixel + nLine * a_nWidth + nn) != 0)
  816. {
  817. nLength++;
  818. }
  819. }
  820. // generate segment
  821. COTSSegmentPtr pSegment = COTSSegmentPtr(new COTSSegment(nLine + top, nStart + left, nLength));
  822. a_listSegments.push_back(pSegment);
  823. }
  824. else
  825. {
  826. continue;
  827. }
  828. }
  829. }
  830. if ((int)a_listSegments.size() == 0)
  831. {
  832. //LogErrorTrace(__FILE__, __LINE__, _T("no particle is found."));
  833. return FALSE;
  834. }
  835. return TRUE;
  836. }
  837. BOOL COTSImageProcess::GetFeatureList(COTSSegmentsList& a_listSegments, COTSFeatureList& a_listFeatures)
  838. {
  839. COTSSegmentsList listSegmentNew;
  840. std::map<long, COTSSegmentsList > mapOneLineSegments;
  841. for each (auto s in a_listSegments)
  842. {
  843. mapOneLineSegments[s->GetHeight()].push_back(s);//sorting all the segments base on the line number.
  844. }
  845. std::map<long, COTSSegmentsList >::iterator lineItr = mapOneLineSegments.begin();//find the highest line
  846. while (lineItr != mapOneLineSegments.end())
  847. {
  848. for (auto s = lineItr->second.begin(); s < lineItr->second.end(); )//find one segment of this line.
  849. {
  850. COTSSegmentPtr bottomSeg = *s;
  851. listSegmentNew.clear();
  852. listSegmentNew.push_back(*s);
  853. s = lineItr->second.erase(s);
  854. std::map<long, COTSSegmentsList >::iterator tempItr = lineItr;
  855. tempItr++;
  856. for (; tempItr != mapOneLineSegments.end(); tempItr++)//find all other lines of segments
  857. {
  858. if (tempItr->first - bottomSeg->GetHeight() > 1)
  859. {
  860. break;
  861. }
  862. for (auto nextLineSegment = tempItr->second.begin(); nextLineSegment < tempItr->second.end();)//find next line's all segments
  863. {
  864. if (((*nextLineSegment)->GetStart() - (bottomSeg->GetStart() + bottomSeg->GetLength())) > 1)
  865. {
  866. break;
  867. }
  868. if (bottomSeg->UpDownConection(**nextLineSegment))
  869. {
  870. listSegmentNew.push_back(*nextLineSegment);
  871. bottomSeg = *nextLineSegment;
  872. nextLineSegment = tempItr->second.erase(nextLineSegment);
  873. break;
  874. }
  875. if (tempItr->second.size() > 0)
  876. {
  877. nextLineSegment++;
  878. }
  879. else
  880. {
  881. break;
  882. }
  883. }
  884. }
  885. COTSFeaturePtr pFeature = COTSFeaturePtr(new COTSFeature());
  886. pFeature->SetSegmentsList(listSegmentNew);
  887. //check if this new feature is connected with other found feature.
  888. COTSSegmentPtr topSeg = listSegmentNew[0];//find the toppest segment of this new feature.
  889. COTSSegmentPtr bottomSegment = listSegmentNew[listSegmentNew.size() - 1];//find the lowest segment of this new feature.
  890. bool haveMerged = false;
  891. for each (auto f in a_listFeatures)
  892. {
  893. for (auto seg : f->GetSegmentsList())
  894. {
  895. if (bottomSegment->UpDownConection(*seg) || topSeg->UpDownConection(*seg))
  896. {
  897. COTSSegmentsList segs = f->GetSegmentsList();
  898. for (auto s : listSegmentNew)
  899. {
  900. segs.push_back(s);
  901. }
  902. f->SetSegmentsList(segs);
  903. haveMerged = true;
  904. break;
  905. }
  906. }
  907. if (haveMerged)
  908. {
  909. break;
  910. }
  911. }
  912. if (!haveMerged)
  913. {
  914. a_listFeatures.push_back(pFeature);
  915. }
  916. if (lineItr->second.size() == 0)
  917. {
  918. break;
  919. }
  920. }
  921. lineItr++;
  922. }
  923. return true;
  924. }
  925. BOOL COTSImageProcess::ChangeFeaturelist(COTSFeatureList& a_listFeatures, COTSParticleList& a_listParticle)
  926. {
  927. for (auto pFeature : a_listFeatures)
  928. {
  929. COTSParticlePtr pParticle = COTSParticlePtr(new COTSParticle());
  930. pParticle->SetFeature(pFeature);
  931. a_listParticle.push_back(pParticle);
  932. }
  933. if ((int)a_listParticle.size() == 0)
  934. {
  935. return FALSE;
  936. }
  937. return TRUE;
  938. }
  939. BOOL COTSImageProcess::GetParticlesFromBinaryImage(CBSEImgPtr m_pBSEImg, CDoubleRange a_diameterRange, double a_pixelSize, COTSParticleList& listParticleOut)
  940. {
  941. int height, width;
  942. height = m_pBSEImg->GetHeight();
  943. width = m_pBSEImg->GetWidth();
  944. BYTE* pPixel = m_pBSEImg->GetImageDataPointer();
  945. Mat nobgMat = Mat(height, width, CV_8UC1, pPixel);
  946. Mat labels = Mat::zeros(nobgMat.size(), CV_32S);
  947. Mat stats, centroids;
  948. int number = connectedComponentsWithStats(nobgMat, labels, stats, centroids, 8, CV_32S);
  949. double rStart = a_diameterRange.GetStart()/2.0;
  950. double rEnd = a_diameterRange.GetEnd()/2.0;
  951. double areaStart = rStart* rStart * 3.14159;
  952. double areaEnd = rEnd* rEnd * 3.14159;
  953. //COTSParticleList listParticleOut;
  954. for (size_t i = 1; i < number; i++)
  955. {
  956. int center_x = centroids.at<double>(i, 0);
  957. int center_y = centroids.at<double>(i, 1);
  958. //���α߿�
  959. int x = stats.at<int>(i, CC_STAT_LEFT);
  960. int y = stats.at<int>(i, CC_STAT_TOP);
  961. int w = stats.at<int>(i, CC_STAT_WIDTH);
  962. int h = stats.at<int>(i, CC_STAT_HEIGHT);
  963. int area = stats.at<int>(i, CC_STAT_AREA);
  964. double actualArea = area * a_pixelSize * a_pixelSize;
  965. if (actualArea >= areaStart && actualArea < areaEnd)
  966. {
  967. Rect rectMax = Rect(x, y, w, h);
  968. Mat rectROI = labels(rectMax).clone();
  969. Mat imageROI = Mat::zeros(rectMax.size(), nobgMat.type());
  970. //exclude the point which intersect into this bounding box but is not in this group.
  971. int label = i;
  972. for (int row = 0; row < rectROI.rows; row++)
  973. {
  974. for (int col = 0; col < rectROI.cols; col++)
  975. {
  976. int v = rectROI.at<int>(row, col);
  977. if (v == label)
  978. {
  979. imageROI.at<uchar>(row, col) = 255;
  980. }
  981. }
  982. }
  983. COTSParticleList roiParts;
  984. if (!GetOneParticleFromROI(rectMax.x, rectMax.y, rectMax.width, rectMax.height, imageROI.data, roiParts))
  985. {
  986. continue;
  987. }
  988. if (roiParts.size() > 0)
  989. {
  990. COTSParticlePtr roiPart = roiParts[0];
  991. roiPart->SetXRayPos(CPoint(center_x, center_y));
  992. CRect r = CRect(x, y, x + w, y + h);
  993. roiPart->SetParticleRect(r);
  994. roiPart->SetActualArea(actualArea);
  995. roiPart->SetPixelArea(area);
  996. listParticleOut.push_back(roiPart);
  997. }
  998. }
  999. }
  1000. return TRUE;
  1001. }
  1002. void COTSImageProcess::findPeakAndValley(const vector<int>& v, vector<int>& peakPositions, vector<int>& valleyPositions)
  1003. {
  1004. vector<int> diff_v(v.size() - 1, 0);
  1005. for (vector<int>::size_type i = 0; i != diff_v.size(); i++)
  1006. {
  1007. if (v[i + 1] - v[i] > 0)
  1008. diff_v[i] = 1;
  1009. else if (v[i + 1] - v[i] < 0)
  1010. diff_v[i] = -1;
  1011. else
  1012. diff_v[i] = 0;
  1013. }
  1014. for (int i = diff_v.size() - 1; i >= 0; i--)
  1015. {
  1016. if (diff_v[i] == 0 && i == diff_v.size() - 1)
  1017. {
  1018. diff_v[i] = 1;
  1019. }
  1020. else if (diff_v[i] == 0)
  1021. {
  1022. if (diff_v[i + 1] >= 0)
  1023. diff_v[i] = 1;
  1024. else
  1025. diff_v[i] = -1;
  1026. }
  1027. }
  1028. for (vector<int>::size_type i = 0; i != diff_v.size() - 1; i++)
  1029. {
  1030. if (diff_v[i + 1] - diff_v[i] == -2)
  1031. peakPositions.push_back(i + 1);
  1032. if (diff_v[i + 1] - diff_v[i] == 2)
  1033. valleyPositions.push_back(i + 1);
  1034. }
  1035. }
  1036. std::vector<CIntRange> COTSImageProcess::GetValidGreyLevelRanges(CBSEImgPtr pBSEImg)
  1037. {
  1038. WORD originChartData[MAXBYTE];
  1039. //1. get chart data
  1040. pBSEImg->InitChartData();
  1041. memcpy(originChartData, pBSEImg->GetBSEChart(), sizeof(WORD) * MAXBYTE);
  1042. originChartData[0] = 0;
  1043. std::vector<int> greyChart;
  1044. for (int i = 0; i < MAXBYTE; i++)
  1045. {
  1046. greyChart.push_back((int)originChartData[i]);
  1047. }
  1048. std::vector<int> peaks;
  1049. std::vector<int> valleys;
  1050. findPeakAndValley(greyChart, peaks, valleys);
  1051. if ((peaks.size() - valleys.size()) == 1)
  1052. {
  1053. valleys.push_back(254);
  1054. }
  1055. std::map<int, MyGreyPeak*> peakMap;// hold all the peaks in this chart which are sorted by there area.
  1056. MyGreyPeak* lastRng = NULL;
  1057. MyGreyPeak* firstRng = NULL;
  1058. for (int i = 0; i < peaks.size(); i++)
  1059. {
  1060. MyGreyPeak* gr = new MyGreyPeak();
  1061. if (i == 0)
  1062. {
  1063. gr->start = 0;
  1064. }
  1065. else
  1066. {
  1067. gr->start = valleys[i - 1];
  1068. }
  1069. gr->end = valleys[i];
  1070. for (int j = gr->start; j < gr->end; j++)
  1071. {
  1072. gr->area += originChartData[j];
  1073. }
  1074. gr->peakPos = peaks[i];
  1075. gr->peakValue = originChartData[peaks[i]];
  1076. gr->valleyValue = valleys[i];
  1077. if (firstRng == NULL)
  1078. {
  1079. firstRng = gr;
  1080. lastRng = gr;
  1081. }
  1082. else
  1083. {
  1084. gr->preRng = lastRng;
  1085. lastRng->nextRng = gr;
  1086. }
  1087. lastRng = gr;
  1088. peakMap[gr->start] = gr;
  1089. }
  1090. int validGreyLevelNum = 0;//statistic the none zero grey level number
  1091. long partArea = 0;
  1092. for (int i = 0; i < 255; i++)
  1093. {
  1094. if (originChartData[i] != 0)
  1095. {
  1096. validGreyLevelNum += 1;
  1097. partArea += originChartData[i];
  1098. }
  1099. }
  1100. auto curRange = firstRng;
  1101. int maxPeakValue = curRange->peakValue;
  1102. while (curRange != NULL)
  1103. {
  1104. if (curRange->peakValue > maxPeakValue)
  1105. {
  1106. maxPeakValue = curRange->peakValue;
  1107. }
  1108. curRange = curRange->nextRng;
  1109. }
  1110. curRange = firstRng;
  1111. while (curRange != NULL)
  1112. {
  1113. curRange->maxPeakvalue = maxPeakValue;
  1114. curRange->allNoneZeroGreyLevelNum = validGreyLevelNum;
  1115. curRange = curRange->nextRng;
  1116. }
  1117. MyGreyPeak* firstRng1=NULL;
  1118. MyGreyPeak::MergeGreyPeaks(firstRng,firstRng1);
  1119. std::vector<CIntRange> ranges;
  1120. auto curRange1 = firstRng1;
  1121. while (curRange1 != NULL)
  1122. {
  1123. CIntRange pRange;
  1124. pRange.SetStart(curRange1->start);
  1125. pRange.SetEnd(curRange1->end);
  1126. ranges.push_back(pRange);
  1127. curRange1 = curRange1->nextRng;
  1128. }
  1129. return ranges;
  1130. }
  1131. BOOL COTSImageProcess::FindSeedsByGrayScale(CBSEImgPtr fieldImg, double a_PixelSize, std::vector<CPoint>& OutSeeds)
  1132. {
  1133. ASSERT(fieldImg);
  1134. ASSERT(m_imageProcessParam);
  1135. int nWidthImg = fieldImg->GetWidth();
  1136. int nHeightImg = fieldImg->GetHeight();
  1137. long nImgSize = nWidthImg * nHeightImg;
  1138. std::vector<CIntRange> validPeaks = GetValidGreyLevelRanges(fieldImg);
  1139. CRect r = CRect(0, 0, nWidthImg, nHeightImg);
  1140. CBSEImgPtr imgNoBGBinary = CBSEImgPtr(new CBSEImg(r));
  1141. for (int i = 0; i < validPeaks.size(); i++)
  1142. {
  1143. CIntRange pRange = validPeaks[i];
  1144. if (pRange.GetStart() == 0)//prevent the 0 grey pixels being recognized as particle.
  1145. {
  1146. pRange.SetStart(3);
  1147. }
  1148. long nNumParticle = 0;
  1149. GetSpecialGrayRangeImage(fieldImg, pRange, imgNoBGBinary, nNumParticle);
  1150. if (nNumParticle == 0)
  1151. {
  1152. continue;
  1153. }
  1154. else
  1155. {
  1156. // get the area image
  1157. Mat matImg = GetMatDataFromBseImg(imgNoBGBinary);
  1158. Mat labels = Mat::zeros(matImg.size(), CV_32S);
  1159. Mat stats, centroids;
  1160. int number = connectedComponentsWithStats(matImg, labels, stats, centroids, 8, CV_32S);
  1161. auto incsize = m_imageProcessParam->GetIncArea();
  1162. double rStart = incsize.GetStart() / 2.0;
  1163. double rEnd = incsize.GetEnd() / 2.0;
  1164. double pixelarea = a_PixelSize * a_PixelSize;
  1165. double areaStart = rStart* rStart * 3.14159*pixelarea;
  1166. double areaEnd = rEnd* rEnd * 3.14159*pixelarea;
  1167. /*double areaStart = 0;
  1168. double areaEnd = 1000;*/
  1169. for (size_t i = 1; i < number; i++)
  1170. {
  1171. int center_x = centroids.at<double>(i, 0);
  1172. int center_y = centroids.at<double>(i, 1);
  1173. int area = stats.at<int>(i, CC_STAT_AREA);
  1174. double actualArea = area * a_PixelSize * a_PixelSize;
  1175. if (actualArea >= areaStart && actualArea < areaEnd)
  1176. {
  1177. CPoint seed = CPoint(center_x, center_y);
  1178. OutSeeds.push_back(seed);
  1179. }
  1180. }
  1181. }
  1182. }
  1183. return true;
  1184. }
  1185. BOOL COTSImageProcess::GetMatrixPointsFromRect(CRect rect, double xrayStep, std::vector<CPoint>& matrixPs)
  1186. {
  1187. int colnum = ceil((double)rect.Width() / xrayStep + 0.5);
  1188. if (colnum % 2 == 0) colnum += 1;//let the number to be an odd number.Then we can make the middle point in the center of the particle exactly.
  1189. int rownum = ceil((double)rect.Height() / xrayStep + 0.5);
  1190. if (rownum % 2 == 0) rownum += 1;
  1191. CPoint theFirst = CPoint(rect.left - (colnum * xrayStep - rect.Width()) / 2 + xrayStep / 2, rect.top - (rownum * xrayStep - rect.Height()) / 2 + xrayStep / 2);
  1192. for (int i = 0; i < rownum; i++)
  1193. {
  1194. for (int j = 0; j < colnum; j++)
  1195. {
  1196. double x = (double)theFirst.x + (double)j * xrayStep;
  1197. double y = (double)theFirst.y + (double)i * xrayStep;
  1198. CPoint thePoint = CPoint(x, y);
  1199. if (rect.PtInRect(thePoint))
  1200. {
  1201. matrixPs.push_back(thePoint);
  1202. }
  1203. }
  1204. }
  1205. return true;
  1206. }
  1207. BOOL COTSImageProcess::CalcuParticleImagePropertes(COTSParticlePtr a_pOTSPart, double a_PixelSize)
  1208. {
  1209. //--------- convert this particle data to image data,construct an image only with this particle.------
  1210. const int nExpand_Size = 3;
  1211. const int nWhiteColor = 0;
  1212. const int nThickness = 1;
  1213. // lineType Type of the line
  1214. const int nLineType = 8;
  1215. // get rectangle of the particle
  1216. CRect rect = a_pOTSPart->GetParticleRect();
  1217. if (a_pOTSPart->GetActualArea() < 20 * 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.
  1218. {
  1219. double w = 0, h = 0;
  1220. w = (double)rect.Width()*a_PixelSize;
  1221. h = (double)rect.Height()*a_PixelSize;
  1222. a_pOTSPart->SetDMax(MAX(w, h));
  1223. a_pOTSPart->SetDMin(MIN(w, h));
  1224. a_pOTSPart->SetDMean((w + h) / 2);
  1225. a_pOTSPart->SetFeretDiameter((w + h) / 2);
  1226. a_pOTSPart->SetDElong(MAX(w, h));
  1227. a_pOTSPart->SetPerimeter((w+h)*2);
  1228. a_pOTSPart->SetDPerp(MIN(w, h));
  1229. a_pOTSPart->SetDInscr(MIN(w, h));
  1230. a_pOTSPart->SetMinWidth(w);
  1231. a_pOTSPart->SetMinHeight(h);
  1232. return true;
  1233. }
  1234. // calculate the particle image data size, expand 3 pixel at the edge
  1235. Mat particleImage = Mat::zeros(rect.Height() + nExpand_Size , rect.Width() + nExpand_Size , CV_8U);
  1236. // get the segment list
  1237. COTSSegmentsList listSegment = a_pOTSPart->GetFeature()->GetSegmentsList();
  1238. for (auto pSegment : listSegment)
  1239. {
  1240. int nStart = pSegment->GetStart() - rect.left + nExpand_Size;
  1241. int nEnd = pSegment->GetStart() + pSegment->GetLength() - rect.left - 1 + nExpand_Size;
  1242. int nHeight = pSegment->GetHeight() - rect.top + nExpand_Size;
  1243. line(particleImage, Point(nStart, nHeight), Point(nEnd, nHeight), Scalar(nBlackColor), nThickness, nLineType);
  1244. }
  1245. //--------abstract the contour of the particle.
  1246. vector<vector<Point>>contours;
  1247. findContours(particleImage, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
  1248. 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.
  1249. {
  1250. double w = 0, h = 0;
  1251. w = (double)rect.Width()*a_PixelSize;
  1252. h = (double)rect.Height()*a_PixelSize;
  1253. a_pOTSPart->SetDMax(MAX(w, h));
  1254. a_pOTSPart->SetDMin(MIN(w, h));
  1255. a_pOTSPart->SetDMean((w + h) / 2);
  1256. a_pOTSPart->SetFeretDiameter((w + h) / 2);
  1257. a_pOTSPart->SetDElong(MAX(w, h));
  1258. a_pOTSPart->SetPerimeter((w + h) * 2);
  1259. a_pOTSPart->SetDPerp(MIN(w, h));
  1260. a_pOTSPart->SetDInscr(MIN(w, h));
  1261. a_pOTSPart->SetMinWidth(w);
  1262. a_pOTSPart->SetMinHeight(h);
  1263. return true;
  1264. }
  1265. int imaxcontour = 0, imax = 0;
  1266. for (unsigned int i = 0; i < contours.size(); i++) {
  1267. int itmp = contourArea(contours[i]);
  1268. if (imaxcontour < itmp) {
  1269. imax = i;
  1270. imaxcontour = itmp;
  1271. }
  1272. }
  1273. vector<Point > listEdge = contours[imax];
  1274. vector<vector<Point>>Outcontours;
  1275. Outcontours.push_back(listEdge);
  1276. //---------calculate the minimium rectangle
  1277. auto rRect = cv::minAreaRect(listEdge);
  1278. Point2f p[4];
  1279. rRect.points(p);
  1280. int D_MIN =getDistance(p[0], p[1]);
  1281. int D_MinRecLen = 0;//minareaRect's length(the longger side).
  1282. for (int j = 0; j <= 2; j++)
  1283. {
  1284. //line(cvContourImg, p[j], p[(j + 1) % 4], Scalar(100, 100, 0), 2);
  1285. int d = getDistance(p[j], p[j + 1]);
  1286. if (d < D_MIN)
  1287. {
  1288. D_MIN = d;
  1289. }
  1290. if (d > D_MinRecLen)
  1291. {
  1292. D_MinRecLen = d;
  1293. }
  1294. }
  1295. a_pOTSPart->SetDMin(D_MIN*a_PixelSize);
  1296. a_pOTSPart->SetMinWidth(rRect.size.width);
  1297. a_pOTSPart->SetMinHeight(rRect.size.height);
  1298. //----------calculate the perimeter
  1299. double d = arcLength(listEdge, true);
  1300. a_pOTSPart->SetPerimeter(d*a_PixelSize);
  1301. //-----------calculate the Max Diameter. Find the min enclosing circle first ,then find the two farthest circle connected point.
  1302. Point2f center; float radius;
  1303. minEnclosingCircle(listEdge, center, radius);
  1304. //circle(cvContourImg, center, radius, Scalar(100), 2);
  1305. std::vector <Point> outContour = listEdge;
  1306. std::vector <Point> rst;
  1307. for (unsigned int k = 0; k < outContour.size(); k++)
  1308. {
  1309. Point p = outContour[k];
  1310. double d = sqrt(pow((p.x - center.x), 2) + pow((p.y - center.y), 2));
  1311. if (fabs(d - radius) < 0.01)
  1312. {
  1313. rst.push_back(p);
  1314. }
  1315. }
  1316. double D_MAX = 0;
  1317. Point lineDmax[2];
  1318. for (unsigned int m = 0; m < rst.size(); m++)
  1319. {
  1320. Point p = rst[m];
  1321. for (unsigned int n = m + 1; n < rst.size(); n++)
  1322. {
  1323. Point p1 = rst[n];
  1324. double d = sqrt(powf((p.x - p1.x), 2) + powf((p.y - p1.y), 2));
  1325. if (d > D_MAX)
  1326. {
  1327. D_MAX = d;
  1328. lineDmax[0] = p;
  1329. lineDmax[1] = p1;
  1330. }
  1331. }
  1332. }
  1333. a_pOTSPart->SetDMax(D_MAX*a_PixelSize);
  1334. //--------calculate the D_PERP property using the D_MAX's two endpoints.
  1335. std::vector<Point> curve1;
  1336. std::vector<Point> curve2;
  1337. for (unsigned int i = 0; i < outContour.size(); i++)
  1338. {
  1339. Point pt = outContour[i];
  1340. bool start = false;
  1341. int clockwise = Side(lineDmax[0], lineDmax[1], pt);// devide these points into two group ,separate into the two sides.
  1342. if (clockwise > 0)
  1343. {
  1344. curve1.push_back(pt);
  1345. }
  1346. else
  1347. {
  1348. curve2.push_back(pt);
  1349. }
  1350. }
  1351. double d_perp1 = 0, d_perp2 = 0;
  1352. for (unsigned int i = 0; i < curve1.size(); i++)
  1353. {
  1354. double d = getDist_P2L(curve1[i], lineDmax[0], lineDmax[1]);
  1355. if (d > d_perp1)
  1356. {
  1357. d_perp1 = d;
  1358. }
  1359. }
  1360. for (unsigned int i = 0; i < curve2.size(); i++)
  1361. {
  1362. double d = getDist_P2L(curve2[i], lineDmax[0], lineDmax[1]);
  1363. if (d > d_perp2)
  1364. {
  1365. d_perp2 = d;
  1366. }
  1367. }
  1368. a_pOTSPart->SetDPerp((d_perp1 + d_perp2)*a_PixelSize);
  1369. //----------find the diameter of max inscribed circle
  1370. int r;
  1371. Point inscribeCirclecenter;
  1372. FindInnerCircleInContour(outContour, inscribeCirclecenter, r);
  1373. //--------------------------------------------------------calculate the xraypos !
  1374. CRect rec = a_pOTSPart->GetParticleRect();
  1375. a_pOTSPart->SetXRayPos(CPoint(inscribeCirclecenter.x - nExpand_Size + rec.left - 1, inscribeCirclecenter.y - nExpand_Size + rec.top - 1));
  1376. a_pOTSPart->SetDInscr(r * 2 * a_PixelSize);
  1377. //---------------calculate the image other caracater: length/width realArea/minRectangeArea etc. we can use these propertes to do forward process.
  1378. double minRectArea = D_MIN * D_MinRecLen*a_PixelSize*a_PixelSize;//��С��Ӿ������
  1379. double fillRatio = a_pOTSPart->GetActualArea() / minRectArea;//ʵ���������С��Ӿ��������,that's the fill rate.
  1380. double lengthWidthRatio;
  1381. lengthWidthRatio = (double)D_MinRecLen / D_MIN;//�����
  1382. //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.
  1383. bool isStripShape = false;
  1384. double curveLength = 0;
  1385. double D_MEAN=0;
  1386. Moments mu;
  1387. mu = moments(listEdge, false);
  1388. int nx = mu.m10 / mu.m00;
  1389. int ny = mu.m01 / mu.m00;
  1390. //circle(cvcopyImg, Point(nx, ny), 1, (255), 1);
  1391. Point ptCenter = Point((int)nx, (int)ny);
  1392. if (pointPolygonTest(listEdge, ptCenter, false) != 1)// the center point doesn't contain in the contour, we think it as curve shape.
  1393. {
  1394. isStripShape = true;
  1395. }
  1396. /*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.
  1397. {
  1398. isStripShape = true;
  1399. }*/
  1400. 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.
  1401. {
  1402. isStripShape = true;
  1403. }
  1404. if (isStripShape)
  1405. {
  1406. 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.
  1407. if (curveLength < D_MAX)
  1408. {
  1409. curveLength = D_MAX;
  1410. }
  1411. if (curveLength < MIN_DOUBLE_VALUE || a_pOTSPart->GetActualArea()<MIN_DOUBLE_VALUE)
  1412. {
  1413. D_MEAN = 0;
  1414. }
  1415. else
  1416. {
  1417. D_MEAN = a_pOTSPart->GetActualArea() / curveLength;
  1418. }
  1419. a_pOTSPart->SetDMean(D_MEAN*a_PixelSize);
  1420. a_pOTSPart->SetFeretDiameter(D_MEAN*a_PixelSize);
  1421. a_pOTSPart->SetDElong (curveLength*a_PixelSize);
  1422. }
  1423. else//it's a ball shape particle
  1424. {
  1425. curveLength = D_MAX;
  1426. double ftd = 0, maxD = 0, minD = 0, dratio = 0;
  1427. GetParticleAverageChord(outContour, a_PixelSize, ftd);
  1428. a_pOTSPart->SetDMean(ftd);
  1429. a_pOTSPart->SetFeretDiameter(ftd);
  1430. a_pOTSPart->SetDElong(curveLength*a_PixelSize);
  1431. }
  1432. if (a_pOTSPart->GetAspectRatio() > 2)
  1433. {
  1434. double angle;
  1435. if (rRect.size.width > rRect.size.height) // w > h
  1436. {
  1437. angle = abs(rRect.angle);
  1438. }
  1439. else
  1440. {
  1441. angle = 90.0 + abs(rRect.angle);
  1442. }
  1443. a_pOTSPart->SetOrientation(angle);
  1444. }
  1445. else
  1446. {
  1447. a_pOTSPart->SetOrientation(0);
  1448. }
  1449. return true;
  1450. }
  1451. void COTSImageProcess::ImshowImage(CBSEImgPtr img)
  1452. {
  1453. BYTE* data = img->GetImageDataPointer();
  1454. //Mat cvImg;
  1455. cv::Size s;
  1456. s.width = img->GetImageSize().cx;
  1457. s.height = img->GetImageSize().cy;
  1458. Mat cvImg=Mat::zeros(s, CV_8U);
  1459. cvImg.data = data;
  1460. cv::imshow("dd", cvImg);
  1461. cv::waitKey();
  1462. }
  1463. void COTSImageProcess::ImshowChartData(CBSEImgPtr img)
  1464. {
  1465. img->InitChartData();
  1466. WORD* data = img->GetBSEChart();
  1467. //Mat cvImg;
  1468. cv::Size s;
  1469. s.width = 255;
  1470. s.height = 100;
  1471. Mat cvImg = Mat::zeros(s, CV_8U);
  1472. //cvImg.data = data;
  1473. WORD nBSEChart[MAXBYTE];
  1474. //1. get chart data
  1475. linearSmooth5(data, nBSEChart, MAXBYTE);
  1476. for (int i=1;i<255;i++)
  1477. {
  1478. line(cvImg, Point(i, 100-nBSEChart[i]), Point(i+1, 100-nBSEChart[i+1]), Scalar(nBlackColor), 1, 8);
  1479. }
  1480. cv::imshow("chart", cvImg);
  1481. cv::waitKey();
  1482. }
  1483. BOOL COTSImageProcess::MergeBigBoundaryParticles(COTSFieldDataList allFields,double pixelSize,int scanFieldSize, CSize ResolutionSize, COTSParticleList& mergedParts)
  1484. {
  1485. class BorderPart
  1486. {
  1487. typedef std::shared_ptr<BorderPart> CBorderPartPtr;
  1488. BorderPart(COTSParticlePtr p)
  1489. {
  1490. myPart = p;
  1491. headerParticle = NULL;
  1492. }
  1493. public:
  1494. COTSParticlePtr myPart;
  1495. COTSParticle* headerParticle;//used to merge particles ,if this particle has been merged then this pointer will point to the first particle of these merged particles or else it's NULL.
  1496. static std::vector<CBorderPartPtr> ConvertPartToBorderPart(COTSParticleList parts)
  1497. {
  1498. static std::map<COTSParticle*, CBorderPartPtr> allborderPart;
  1499. std::vector<CBorderPartPtr> borderParts;
  1500. for (auto p : parts)
  1501. {
  1502. if (allborderPart.find(p.get()) == allborderPart.end())
  1503. {
  1504. auto borderp = CBorderPartPtr(new BorderPart(p));
  1505. borderParts.push_back(borderp);
  1506. allborderPart[p.get()] = borderp;
  1507. }
  1508. else
  1509. {
  1510. borderParts.push_back(allborderPart[p.get()]);
  1511. }
  1512. }
  1513. return borderParts;
  1514. }
  1515. };
  1516. auto FldMgr = new CFieldMgr(scanFieldSize, ResolutionSize);
  1517. std::map<COTSParticle*, COTSParticleList> mapMergeParticles;//hold up all the boundary connected particles. the pair's first is also the member of these particles.
  1518. std::map<COTSParticle*, COTSSegmentsList> mapMergedSegments;//hold up all the segment's corresponding clone in the connected particles.
  1519. for (auto centerfld : allFields)
  1520. {
  1521. // find neighbor field on the left.
  1522. auto leftFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::LEFT);
  1523. if (leftFld != nullptr)
  1524. {
  1525. auto lParts = centerfld->GetLeftBorderedBigParticles();
  1526. auto rParts = leftFld->GetRightBorderedBigParticles();
  1527. auto leftParts = BorderPart::ConvertPartToBorderPart(lParts);
  1528. auto rightParts = BorderPart::ConvertPartToBorderPart(rParts);
  1529. for (auto leftp : leftParts)
  1530. {
  1531. for (auto rightp : rightParts)
  1532. {
  1533. if (leftp->myPart->IsConnected(rightp->myPart.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::LEFT))
  1534. {
  1535. if (leftp->headerParticle != NULL)
  1536. {
  1537. if (rightp->headerParticle == NULL)
  1538. {
  1539. rightp->headerParticle = leftp->headerParticle;
  1540. mapMergeParticles[leftp->headerParticle].push_back(rightp->myPart);
  1541. }
  1542. }
  1543. else
  1544. {
  1545. if (rightp->headerParticle != NULL)
  1546. {
  1547. leftp->headerParticle = rightp->myPart.get();
  1548. mapMergeParticles[rightp->myPart.get()].push_back(leftp->myPart);
  1549. }
  1550. else
  1551. {
  1552. leftp->headerParticle = leftp->myPart.get();
  1553. rightp->headerParticle = leftp->myPart.get();
  1554. mapMergeParticles[leftp->myPart.get()].push_back(rightp->myPart);
  1555. }
  1556. }
  1557. }
  1558. }
  1559. }
  1560. }
  1561. //find neighbor field on the upward
  1562. auto upFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::UP);
  1563. if (upFld != nullptr)
  1564. {
  1565. auto topBorderParts = centerfld->GetTopBorderedBigParticles();
  1566. auto bottomBorderParts = upFld->GetBottomBorderedBigParticles();
  1567. auto upParts = BorderPart::ConvertPartToBorderPart(topBorderParts);
  1568. auto downParts = BorderPart::ConvertPartToBorderPart(bottomBorderParts);
  1569. for (auto upprt : upParts)
  1570. {
  1571. for (auto downprt : downParts)
  1572. {
  1573. if (upprt->myPart->IsConnected(downprt->myPart.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::UP))
  1574. {
  1575. if (upprt->headerParticle != NULL)
  1576. {
  1577. if (downprt->headerParticle == NULL)
  1578. {
  1579. downprt->headerParticle = upprt->headerParticle;
  1580. mapMergeParticles[upprt->headerParticle].push_back(downprt->myPart);
  1581. }
  1582. }
  1583. else
  1584. {
  1585. if (downprt->headerParticle != NULL)
  1586. {
  1587. upprt->headerParticle = downprt->headerParticle;
  1588. mapMergeParticles[downprt->myPart.get()].push_back(upprt->myPart);
  1589. }
  1590. else
  1591. {
  1592. upprt->headerParticle = upprt->myPart.get();
  1593. downprt->headerParticle = upprt->myPart.get();
  1594. mapMergeParticles[upprt->myPart.get()].push_back(downprt->myPart);
  1595. }
  1596. }
  1597. }
  1598. }
  1599. }
  1600. }
  1601. //find neighbor field on the downward.
  1602. auto downFld = FldMgr->FindNeighborField(allFields, centerfld,SORTING_DIRECTION::DOWN);
  1603. if (downFld != nullptr)
  1604. {
  1605. auto bottomParts = centerfld->GetBottomBorderedBigParticles();
  1606. auto topParts = downFld->GetTopBorderedBigParticles();
  1607. auto downParts = BorderPart::ConvertPartToBorderPart(bottomParts);
  1608. auto upParts= BorderPart::ConvertPartToBorderPart(topParts);
  1609. for (auto downprt : downParts)
  1610. {
  1611. for (auto upprt : upParts)
  1612. {
  1613. if (downprt->myPart->IsConnected(upprt->myPart.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::DOWN))
  1614. {
  1615. if (downprt->headerParticle != NULL)
  1616. {
  1617. if (upprt->headerParticle == NULL)
  1618. {
  1619. upprt->headerParticle = downprt->headerParticle;
  1620. mapMergeParticles[downprt->headerParticle].push_back(upprt->myPart);
  1621. }
  1622. }
  1623. else
  1624. {
  1625. if (upprt->headerParticle != NULL)
  1626. {
  1627. downprt->headerParticle = upprt->headerParticle;
  1628. mapMergeParticles[upprt->headerParticle].push_back(downprt->myPart);
  1629. }
  1630. else
  1631. {
  1632. downprt->headerParticle = downprt->myPart.get();
  1633. upprt->headerParticle = downprt->myPart.get();
  1634. mapMergeParticles[downprt->myPart.get()].push_back(upprt->myPart);
  1635. }
  1636. }
  1637. }
  1638. }
  1639. }
  1640. }
  1641. //find neighbor field on the right.
  1642. auto rightFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::RIGHT);
  1643. if (rightFld != nullptr)
  1644. {
  1645. auto rParts = centerfld->GetRightBorderedBigParticles();
  1646. auto lParts = rightFld->GetLeftBorderedBigParticles();
  1647. auto rightParts = BorderPart::ConvertPartToBorderPart(rParts);
  1648. auto leftParts = BorderPart::ConvertPartToBorderPart(lParts);
  1649. for (auto rightprt : rightParts)
  1650. {
  1651. for (auto leftprt : leftParts)
  1652. {
  1653. if (rightprt->myPart->IsConnected(leftprt->myPart.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::RIGHT))
  1654. {
  1655. if (rightprt->headerParticle != NULL)
  1656. {
  1657. if (leftprt->headerParticle == NULL)
  1658. {
  1659. leftprt->headerParticle = rightprt->headerParticle;
  1660. mapMergeParticles[rightprt->headerParticle].push_back(leftprt->myPart);
  1661. }
  1662. }
  1663. else
  1664. {
  1665. if (leftprt->headerParticle != NULL)
  1666. {
  1667. rightprt->headerParticle = leftprt->headerParticle;
  1668. mapMergeParticles[leftprt->headerParticle].push_back(rightprt->myPart);
  1669. }
  1670. else
  1671. {
  1672. rightprt->headerParticle = rightprt->myPart.get();
  1673. leftprt->headerParticle = rightprt->myPart.get();
  1674. mapMergeParticles[rightprt->myPart.get()].push_back(leftprt->myPart);
  1675. }
  1676. }
  1677. }
  1678. }
  1679. }
  1680. }
  1681. }
  1682. /*for (auto particle : mapMergeParticles)
  1683. {
  1684. }*/
  1685. static int partTagId;
  1686. for (auto pair : mapMergeParticles)
  1687. {
  1688. struct EleAreaPercentage
  1689. {
  1690. EleAreaPercentage(double p, CElementChemistryPtr e)
  1691. {
  1692. areaPercentage = p;
  1693. eleData = e;
  1694. }
  1695. double areaPercentage;
  1696. CElementChemistryPtr eleData;
  1697. };
  1698. auto newPart = COTSParticlePtr(new COTSParticle());
  1699. COTSSegmentsList newSegs;
  1700. auto p = pair.first;
  1701. newPart->SetSEMPos(p->GetSEMPos());
  1702. //firstly,we sum up all the merged particles's area and get the represent string.
  1703. std::string partsStr = std::to_string(p->GetFieldId()) + ":" + std::to_string(p->GetAnalysisId());
  1704. double allPartArea = p->GetActualArea();//Get the first particle's area.
  1705. for (auto other : pair.second)// Get the total area of all these particles for the use of ele calcu.
  1706. {
  1707. partsStr += "," + std::to_string(other->GetFieldId()) + ":" + std::to_string(other->GetAnalysisId());//Get the subparticles string such as "1:1,2:1" etc.
  1708. allPartArea += other->GetActualArea();//Get other particle's area
  1709. }
  1710. // calculate all the new segment's position.
  1711. std::vector <COTSParticle*> allSubParts;
  1712. allSubParts.push_back(p);
  1713. for (auto other : pair.second)// Get the total area of all these particles for the use of ele calcu.
  1714. {
  1715. allSubParts.push_back(other.get());
  1716. }
  1717. for (auto subp : allSubParts)
  1718. {
  1719. int fid = subp->GetFieldId();
  1720. CPoint myFldPos;
  1721. for (auto f : allFields)//find this particle's filed.
  1722. {
  1723. if (f->GetId() == fid)
  1724. {
  1725. myFldPos = f->GetPosition();
  1726. }
  1727. }
  1728. int fldWidth = allFields[0]->Width;
  1729. int fldHeight = allFields[0]->Height;
  1730. CPoint fldLeftUpPos = CPoint(myFldPos.x + fldWidth / 2 , myFldPos.y + fldHeight / 2 );
  1731. for (auto s : subp->GetFeature()->GetSegmentsList())
  1732. {
  1733. COTSSegmentPtr newseg = COTSSegmentPtr(new COTSSegment());
  1734. newseg->SetStart(s->GetStart() + fldLeftUpPos.x);
  1735. 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.
  1736. newseg->SetLength(s->GetLength());
  1737. newSegs.push_back(newseg);
  1738. }
  1739. }
  1740. COTSFeaturePtr newFeature = COTSFeaturePtr(new COTSFeature());
  1741. newFeature->SetSegmentsList(newSegs);
  1742. newPart->SetFeature(newFeature);
  1743. newPart->CalCoverRectFromSegment();
  1744. //second, we get all the element data and their area percentage .
  1745. std::map<std::string, std::vector<EleAreaPercentage>> mapEleData;
  1746. CPosXrayPtr pXray1 = p->GetXrayInfo();
  1747. if (pXray1 != nullptr)
  1748. {
  1749. for (auto ele : pXray1->GetElementQuantifyData())
  1750. {
  1751. mapEleData[ele->GetName().GetBuffer()].push_back(EleAreaPercentage(p->GetActualArea() / allPartArea, ele));
  1752. }
  1753. }
  1754. for (auto other : pair.second)
  1755. {
  1756. auto otherXray = other->GetXrayInfo();
  1757. if (otherXray != nullptr)
  1758. {
  1759. for (auto eledata : otherXray->GetElementQuantifyData())
  1760. {
  1761. mapEleData[eledata->GetName().GetBuffer()].push_back(EleAreaPercentage(other->GetActualArea() / allPartArea, eledata));
  1762. }
  1763. }
  1764. }
  1765. // third,we calculate all the element's new percentage data and get a new element chemistry list.
  1766. CElementChemistriesList newCheList;
  1767. for (auto eledata : mapEleData)
  1768. {
  1769. CElementChemistryPtr newEleche = CElementChemistryPtr(new CElementChemistry());
  1770. newEleche->SetName(CString(eledata.first.c_str()));
  1771. double newPercentage = 0;
  1772. for (auto d : eledata.second)
  1773. {
  1774. newPercentage += d.areaPercentage * d.eleData->GetPercentage();
  1775. }
  1776. newEleche->SetPercentage(newPercentage);
  1777. newCheList.push_back(newEleche);
  1778. }
  1779. CPosXrayPtr xray(new CPosXray());
  1780. xray->SetElementQuantifyData(newCheList);
  1781. newPart->SetXrayInfo(xray);
  1782. newPart->SetConnectedParticlesSequentialString(partsStr);
  1783. newPart->SetActualArea(allPartArea);
  1784. partTagId++;
  1785. newPart->SetParticleId(partTagId);
  1786. newPart->SetAnalysisId(partTagId);
  1787. std::string name = p->GetClassifyName();
  1788. newPart->SetClassifyName(name);
  1789. newPart->SetColor(p->GetColor());
  1790. mergedParts.push_back(newPart);
  1791. }
  1792. return true;
  1793. }
  1794. BOOL COTSImageProcess::CalcuBrightPhaseDarkPhaseGrayByOtsu(CBSEImgPtr a_pImgIn, int& bright, int& dark)
  1795. {
  1796. Mat srcImgMat = GetMatDataFromBseImg(a_pImgIn);
  1797. Mat brightPhaseMat, darkPhaseMat;
  1798. double thresholdValue = 0;
  1799. thresholdValue =cv::threshold(srcImgMat, brightPhaseMat, thresholdValue, 255, THRESH_TOZERO|cv::THRESH_OTSU);
  1800. cv::threshold(srcImgMat, darkPhaseMat, thresholdValue, 255, THRESH_TOZERO_INV);
  1801. /*cv::Scalar mean1, std1;
  1802. cv::Scalar mean2, std2;
  1803. cv::meanStdDev(brightPhaseMat, mean1, std1);
  1804. cv::meanStdDev(darkPhaseMat, mean2, std2);
  1805. bright = (int)mean1[0];
  1806. dark = (int)mean2[0];*/
  1807. int imagesize = a_pImgIn->GetWidth() * a_pImgIn->GetHeight();
  1808. BYTE* briData = brightPhaseMat.data;
  1809. BYTE* darkData = srcImgMat.data;
  1810. double sum = 0;
  1811. int num = 0;
  1812. for (int i = 0; i < imagesize;i++)
  1813. {
  1814. if (briData[i] > 0)
  1815. {
  1816. num += 1;
  1817. sum += briData[i];
  1818. }
  1819. }
  1820. bright =(int)( sum / num);
  1821. sum = 0;
  1822. num = 0;
  1823. for (int i = 0; i < imagesize;i++)
  1824. {
  1825. if (darkData[i] < thresholdValue)
  1826. {
  1827. num += 1;
  1828. sum += darkData[i];
  1829. }
  1830. }
  1831. dark =(int)( sum / num);
  1832. return TRUE;
  1833. }
  1834. }