OTSImageProcess.cpp 60 KB

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