OTSImageProcess.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  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. namespace OTSIMGPROC
  14. {
  15. using namespace cv;
  16. using namespace std;
  17. const int nBlackColor = 255;
  18. COTSImageProcess::COTSImageProcess(COTSImageProcessParamPtr a_pImageProcessParam)
  19. {
  20. m_imageProcessParam = a_pImageProcessParam;
  21. }
  22. COTSImageProcess::~COTSImageProcess()
  23. {
  24. }
  25. BOOL COTSImageProcess::RemoveBGByCVconnectivities(CBSEImgPtr inBSEImg, double a_pixelSize, COTSFieldDataPtr m_pFieldData)
  26. {
  27. ASSERT(m_pFieldData);
  28. ASSERT(inBSEImg);
  29. ASSERT(m_imageProcessParam);
  30. int nWidthImg = inBSEImg->GetWidth();
  31. int nHeightImg = inBSEImg->GetHeight();
  32. m_pFieldData->Width = nWidthImg;
  33. m_pFieldData->Height = nHeightImg;
  34. long nImgSize = nWidthImg * nHeightImg;
  35. BYTE* pSrcImg = inBSEImg->GetImageDataPointer();
  36. BYTE* pTempImg = new BYTE[nImgSize];
  37. CRect r = CRect(0, 0, nWidthImg, nHeightImg);
  38. CBSEImgPtr imgNoBGBinary = CBSEImgPtr(new CBSEImg(r));
  39. long nNumParticle = 0;
  40. RemoveBackGround(inBSEImg, m_imageProcessParam, imgNoBGBinary, nNumParticle);
  41. BYTE* pPixel = imgNoBGBinary->GetImageDataPointer();
  42. long nPtStart = m_imageProcessParam->GetParticleGray().GetStart();
  43. long nPtEnd = m_imageProcessParam->GetParticleGray().GetEnd();
  44. if (nNumParticle == 0)
  45. {
  46. COTSParticleList listParticleEmpty;
  47. listParticleEmpty.clear();
  48. m_pFieldData->SetParticleList(listParticleEmpty);
  49. }
  50. else
  51. {
  52. // get the area image
  53. Mat cvcopyImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);
  54. Mat labels = Mat::zeros(cvcopyImg.size(), CV_32S);
  55. Mat stats, centroids;
  56. int number = connectedComponentsWithStats(cvcopyImg, labels, stats, centroids, 8, CV_32S);
  57. double rMin = m_imageProcessParam->GetIncArea().GetStart()/2.0;
  58. double rMax = m_imageProcessParam->GetIncArea().GetEnd()/2.0;
  59. double partAreaMin = rMin * rMin * 3.14159;
  60. double partAreaMax = rMax * rMax * 3.14159;
  61. COTSParticleList listParticleOut;
  62. for (size_t i = 1; i < number; i++)
  63. {
  64. int center_x = centroids.at<double>(i, 0);
  65. int center_y = centroids.at<double>(i, 1);
  66. //앤近긋움
  67. int x = stats.at<int>(i, CC_STAT_LEFT);
  68. int y = stats.at<int>(i, CC_STAT_TOP);
  69. int w = stats.at<int>(i, CC_STAT_WIDTH);
  70. int h = stats.at<int>(i, CC_STAT_HEIGHT);
  71. int area = stats.at<int>(i, CC_STAT_AREA);
  72. double actualArea = area * a_pixelSize * a_pixelSize;
  73. if (actualArea >= partAreaMin && actualArea < partAreaMax)
  74. {
  75. Rect rectMax = Rect(x, y, w, h);
  76. Mat rectROI = labels(rectMax).clone();
  77. Mat imageROI = Mat::zeros(rectMax.size(), cvcopyImg.type());
  78. //exclude the point which intersect into this bounding box but is not in this group.
  79. int label = i;
  80. for (int row = 0; row < rectROI.rows; row++)
  81. {
  82. for (int col = 0; col < rectROI.cols; col++)
  83. {
  84. int v = rectROI.at<int>(row, col);
  85. if (v == label)
  86. {
  87. 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.
  88. }
  89. }
  90. }
  91. COTSParticleList roiParts;
  92. if (GetOneParticleFromROI(rectMax.x, rectMax.y, rectMax.width, rectMax.height, imageROI.data, roiParts))
  93. {
  94. if (roiParts.size() > 0)
  95. {
  96. COTSParticlePtr roiPart = roiParts[0];//we will find only one part in the roi.
  97. roiPart->SetXRayPos(CPoint(center_x, center_y));
  98. CRect r = CRect(x, y, x + w, y + h);
  99. roiPart->SetParticleRect(r);
  100. roiPart->SetActualArea(actualArea);
  101. roiPart->SetPixelArea(area);
  102. listParticleOut.push_back(roiPart);
  103. }
  104. }
  105. }
  106. }
  107. int nTagId;
  108. for (auto pParticle : listParticleOut)
  109. {
  110. COTSFeaturePtr pFeature = pParticle->GetFeature();
  111. COTSSegmentsList listSegment = pFeature->GetSegmentsList();
  112. long nPixelNum = 0;
  113. long nPixelAll = 0;
  114. int nStartS = 0;
  115. int nHeightS = 0;
  116. int nLengthS = 0;
  117. for (auto pSegment : listSegment)
  118. {
  119. // get particle average gray
  120. nStartS = pSegment->GetStart();
  121. nHeightS = pSegment->GetHeight();
  122. nLengthS = pSegment->GetLength();
  123. nPixelNum += (long)nLengthS;
  124. for (unsigned int i = 0; i < nLengthS; i++)
  125. {
  126. long nValueTemp = (long)*(pSrcImg + nHeightS * nWidthImg + nStartS + i);
  127. nPixelAll += nValueTemp;
  128. }
  129. }
  130. BYTE nAveGray = (BYTE)(nPixelAll / nPixelNum);
  131. pParticle->SetAveGray(nAveGray);
  132. auto fieldOTSRect = m_pFieldData->GetOTSRect();
  133. CPoint leftTop = fieldOTSRect.GetTopLeft();
  134. CRect rectInSinglefld = pParticle->GetParticleRect();
  135. CPoint OTSLeftTop = CPoint(leftTop.x + rectInSinglefld.left * a_pixelSize, leftTop.y - rectInSinglefld.top * a_pixelSize);
  136. CPoint OTSRightBottom = CPoint(leftTop.x + rectInSinglefld.right * a_pixelSize, leftTop.y - rectInSinglefld.bottom * a_pixelSize);
  137. COTSRect recInOTSCord = COTSRect(OTSLeftTop, OTSRightBottom);
  138. pParticle->SetOTSRect(recInOTSCord);
  139. }
  140. m_pFieldData->SetParticleList(listParticleOut);
  141. }
  142. delete[]pTempImg;
  143. return TRUE;
  144. }
  145. BOOL COTSImageProcess::GetParticlesBySpecialGrayRange(CBSEImgPtr a_pBSEImg, CIntRangePtr a_grayRange,CDoubleRangePtr a_diameterRange,double a_pixelSize, COTSFieldDataPtr m_pFieldData)
  146. {
  147. ASSERT(m_pFieldData);
  148. ASSERT(a_pBSEImg);
  149. ASSERT(a_grayRange);
  150. int nWidthImg = a_pBSEImg->GetWidth();
  151. int nHeightImg = a_pBSEImg->GetHeight();
  152. m_pFieldData->Width = nWidthImg;
  153. m_pFieldData->Height = nHeightImg;
  154. long nImgSize = nWidthImg * nHeightImg;
  155. BYTE* pSrcImg = a_pBSEImg->GetImageDataPointer();
  156. BYTE* pTempImg = new BYTE[nImgSize];
  157. CRect r = CRect(0, 0, nWidthImg, nHeightImg);
  158. CBSEImgPtr imgNoBGBinary = CBSEImgPtr(new CBSEImg(r));
  159. long nNumParticle = 0;
  160. GetSpecialGrayRangeImage(a_pBSEImg, a_grayRange, imgNoBGBinary, nNumParticle);
  161. BYTE* pPixel = imgNoBGBinary->GetImageDataPointer();
  162. if (nNumParticle == 0)
  163. {
  164. COTSParticleList listParticleEmpty;
  165. listParticleEmpty.clear();
  166. m_pFieldData->SetParticleList(listParticleEmpty);
  167. }
  168. else
  169. {
  170. // get the area image
  171. Mat cvcopyImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);
  172. Mat labels = Mat::zeros(cvcopyImg.size(), CV_32S);
  173. Mat stats, centroids;
  174. int number = connectedComponentsWithStats(cvcopyImg, labels, stats, centroids, 8, CV_32S);
  175. double rStart = a_diameterRange->GetStart() / 2.0;
  176. double rEnd = a_diameterRange->GetEnd() / 2.0;
  177. double areaStart = rStart * rStart * 3.14159;
  178. double areaEnd = rEnd * rEnd * 3.14159;
  179. COTSParticleList listParticleOut;
  180. for (size_t i = 1; i < number; i++)
  181. {
  182. int center_x = centroids.at<double>(i, 0);
  183. int center_y = centroids.at<double>(i, 1);
  184. //앤近긋움
  185. int x = stats.at<int>(i, CC_STAT_LEFT);
  186. int y = stats.at<int>(i, CC_STAT_TOP);
  187. int w = stats.at<int>(i, CC_STAT_WIDTH);
  188. int h = stats.at<int>(i, CC_STAT_HEIGHT);
  189. int area = stats.at<int>(i, CC_STAT_AREA);
  190. double actualArea = area * a_pixelSize * a_pixelSize;
  191. if (actualArea >= areaStart && actualArea < areaEnd)
  192. {
  193. Rect rectMax = Rect(x, y, w, h);
  194. Mat rectROI = labels(rectMax).clone();
  195. Mat imageROI = Mat::zeros(rectMax.size(), cvcopyImg.type());
  196. //exclude the point which intersect into this bounding box but is not in this group.
  197. int label = i;
  198. for (int row = 0; row < rectROI.rows; row++)
  199. {
  200. for (int col = 0; col < rectROI.cols; col++)
  201. {
  202. int v = rectROI.at<int>(row, col);
  203. if (v == label)
  204. {
  205. imageROI.at<uchar>(row, col) = 255;
  206. }
  207. }
  208. }
  209. COTSParticleList roiParts;
  210. if (!GetOneParticleFromROI(rectMax.x, rectMax.y, rectMax.width, rectMax.height, imageROI.data, roiParts))
  211. {
  212. continue;
  213. }
  214. if (roiParts.size() > 0)
  215. {
  216. COTSParticlePtr roiPart = roiParts[0];
  217. roiPart->SetXRayPos(CPoint(center_x, center_y));
  218. CRect r = CRect(x, y, x + w, y + h);
  219. roiPart->SetParticleRect(r);
  220. roiPart->SetActualArea(actualArea);
  221. roiPart->SetPixelArea(area);
  222. listParticleOut.push_back(roiPart);
  223. }
  224. }
  225. }
  226. // form a image only have particles on
  227. //COTSSegmentsList listImage;
  228. for (auto pParticle : listParticleOut)
  229. {
  230. int area = pParticle->GetActualArea();
  231. double pActualArea = area ;
  232. COTSFeaturePtr pFeature = pParticle->GetFeature();
  233. COTSSegmentsList listSegment = pFeature->GetSegmentsList();
  234. long nPixelNum = 0;
  235. long nPixelAll = 0;
  236. int nStartS = 0;
  237. int nHeightS = 0;
  238. int nLengthS = 0;
  239. for (auto pSegment : listSegment)
  240. {
  241. // get particle average gray
  242. nStartS = pSegment->GetStart();
  243. nHeightS = pSegment->GetHeight();
  244. nLengthS = pSegment->GetLength();
  245. nPixelNum += (long)nLengthS;
  246. for (unsigned int i = 0; i < nLengthS; i++)
  247. {
  248. long nValueTemp = (long)*(pSrcImg + nHeightS * nWidthImg + nStartS + i);
  249. nPixelAll += nValueTemp;
  250. }
  251. }
  252. BYTE nAveGray = (BYTE)(nPixelAll / nPixelNum);
  253. pParticle->SetAveGray(nAveGray);
  254. auto fieldOTSRect = m_pFieldData->GetOTSRect();
  255. CPoint leftTop = fieldOTSRect.GetTopLeft();
  256. CRect rectInSinglefld = pParticle->GetParticleRect();
  257. CPoint OTSLeftTop = CPoint(leftTop.x + rectInSinglefld.left * a_pixelSize, leftTop.y - rectInSinglefld.top * a_pixelSize);
  258. CPoint OTSRightBottom = CPoint(leftTop.x + rectInSinglefld.right * a_pixelSize, leftTop.y - rectInSinglefld.bottom * a_pixelSize);
  259. COTSRect recInOTSCord = COTSRect(OTSLeftTop, OTSRightBottom);
  260. pParticle->SetOTSRect(recInOTSCord);
  261. }
  262. m_pFieldData->SetParticleList(listParticleOut);
  263. }
  264. delete[]pTempImg;
  265. return TRUE;
  266. }
  267. CIntRangePtr COTSImageProcess::CalBackground(CBSEImgPtr m_pBSEImg)
  268. {
  269. auto ranges = CalcuGrayLevelRange(m_pBSEImg);
  270. return ranges[0];
  271. }
  272. std::vector<CIntRangePtr> COTSImageProcess::CalcuGrayLevelRange(CBSEImgPtr m_pBSEImg)
  273. {
  274. CIntRangePtr pBackground = CIntRangePtr(new CIntRange());
  275. WORD originChartData[MAXBYTE];
  276. WORD firstSmoothChart[MAXBYTE];
  277. WORD secondSmooth[MAXBYTE];
  278. //1. get chart data
  279. m_pBSEImg->SetChartData();
  280. memcpy(originChartData, m_pBSEImg->GetBSEChart(), sizeof(WORD) * MAXBYTE);
  281. originChartData[0] = 0;
  282. originChartData[254] = 0;
  283. linearSmooth5(originChartData, firstSmoothChart, MAXBYTE);
  284. linearSmooth5(firstSmoothChart, secondSmooth, MAXBYTE);
  285. //2. get down edge
  286. int nLengthEdge = MAXBYTE + 2;
  287. WORD n_aBSEChart[MAXBYTE + 2];
  288. memset(n_aBSEChart, 0, sizeof(WORD) * nLengthEdge);
  289. std::map<long, std::vector<int>> peakMap;// hold all the peaks in this spectrum which are sorted by there area.
  290. std::vector<int> currentUpSeries;
  291. std::vector<int> currentPeakSeries;
  292. // make sure the wave begin with up edge and end with down edge
  293. n_aBSEChart[0] = 0;
  294. n_aBSEChart[nLengthEdge - 1] = 0;
  295. memcpy(&n_aBSEChart[1], &secondSmooth, sizeof(WORD) * MAXBYTE);
  296. int nLengthCom = MAXBYTE + 1;
  297. // up edge
  298. for (int i = 0; i < nLengthCom; i++)
  299. {
  300. if (n_aBSEChart[i] <= n_aBSEChart[i + 1])//this is a upward edge
  301. {
  302. if (currentPeakSeries.size() > 0)
  303. {
  304. int seriesSize = currentPeakSeries.size();
  305. long area = 0;
  306. for (int i = 0; i < seriesSize; i++)
  307. {
  308. area = area + n_aBSEChart[currentPeakSeries[i]];
  309. }
  310. peakMap[area] = currentPeakSeries;
  311. currentPeakSeries.clear();
  312. }
  313. currentUpSeries.push_back(i + 1);// save all the continuous up edge
  314. }
  315. else//this is a downward edge
  316. {
  317. // encounter a downward edge means upward edge series end,
  318. if (currentUpSeries.size() > 0)
  319. {
  320. currentPeakSeries = currentUpSeries;
  321. currentUpSeries.clear();
  322. }
  323. currentPeakSeries.push_back(i + 1);
  324. }
  325. }
  326. if (currentPeakSeries.size() > 0)
  327. {
  328. int seriesSize = currentPeakSeries.size();
  329. long area = 0;
  330. for (int i = 0; i < seriesSize; i++)
  331. {
  332. area = area + n_aBSEChart[currentPeakSeries[i]];
  333. }
  334. peakMap[area] = currentPeakSeries;
  335. currentPeakSeries.clear();
  336. }
  337. std::vector<CIntRangePtr> ranges;
  338. std::map<long, std::vector<int>>::reverse_iterator it;
  339. for (it=peakMap.rbegin();it!=peakMap.rend();it++)
  340. {
  341. CIntRangePtr pRange = CIntRangePtr(new CIntRange());
  342. pRange->SetStart(it->second[0]);
  343. pRange->SetEnd(it->second[it->second.size()-1]);
  344. ranges.push_back(pRange);
  345. }
  346. return ranges;
  347. }
  348. void COTSImageProcess::GetSpecialGrayRangeImage(CBSEImgPtr a_pImgIn, CIntRangePtr a_SpecialGrayRange, CBSEImgPtr a_pBinImgOut, long& foundedPixelNum)
  349. {
  350. // the background pixel will be 0,and the other part will be 255.
  351. ASSERT(a_pImgIn);
  352. int nWidthImg = a_pImgIn->GetWidth();
  353. int nHeightImg = a_pImgIn->GetHeight();
  354. long nImgSize = nWidthImg * nHeightImg;
  355. BYTE* pTempImg = new BYTE[nImgSize];
  356. BYTE* pSrcImg = a_pImgIn->GetImageDataPointer();
  357. BYTE* pPixel = new byte[nImgSize];
  358. long nBGStart;
  359. long nBGEnd;
  360. long nNumParticle = 0;
  361. nBGStart = a_SpecialGrayRange->GetStart();
  362. nBGEnd = a_SpecialGrayRange->GetEnd();
  363. // delete background
  364. for (unsigned int i = 0; i < nImgSize; i++)
  365. {
  366. if (pSrcImg[i] >= nBGStart && pSrcImg[i] <= nBGEnd)
  367. {
  368. pPixel[i] = 255;
  369. nNumParticle++;
  370. }
  371. else
  372. {
  373. pPixel[i] = 0;
  374. }
  375. }
  376. BErode3(pPixel, pTempImg, 5, nHeightImg, nWidthImg);
  377. BDilate3(pTempImg, pPixel, 5, nHeightImg, nWidthImg);
  378. a_pBinImgOut->SetImageData(pPixel, nWidthImg, nHeightImg);
  379. foundedPixelNum = nNumParticle;
  380. delete[] pTempImg;
  381. return;
  382. }
  383. void COTSImageProcess::RemoveBackGround(CBSEImgPtr a_pImgIn, COTSImageProcessParamPtr a_pImageProcessParam, CBSEImgPtr a_pBinImgOut,long& foundedPixelNum)
  384. {
  385. // the background pixel will be 0,and the other part will be 255.
  386. ASSERT(a_pImgIn);
  387. ASSERT(a_pImageProcessParam);
  388. int nWidthImg = a_pImgIn->GetWidth();
  389. int nHeightImg = a_pImgIn->GetHeight();
  390. long nImgSize = nWidthImg * nHeightImg;
  391. BYTE* pTempImg = new BYTE[nImgSize];
  392. BYTE* pSrcImg = a_pImgIn->GetImageDataPointer();
  393. BYTE* pPixel = new byte[nImgSize];
  394. long nBGStart;
  395. long nBGEnd;
  396. long nPartStart;
  397. long nPartEnd;
  398. long nNumParticle = 0;
  399. if (a_pImageProcessParam->GetBGRemoveType() == OTS_BGREMOVE_TYPE::MANUAL)
  400. {
  401. nBGStart = a_pImageProcessParam->GetBGGray().GetStart();
  402. nBGEnd = a_pImageProcessParam->GetBGGray().GetEnd();
  403. nPartStart = a_pImageProcessParam->GetParticleGray().GetStart();
  404. nPartEnd = a_pImageProcessParam->GetParticleGray().GetEnd();
  405. // delete background
  406. for (unsigned int i = 0; i < nImgSize; i++)
  407. {
  408. if (pSrcImg[i] >= nBGStart && pSrcImg[i] <= nBGEnd)
  409. {
  410. pPixel[i] = 0;
  411. }
  412. else
  413. {
  414. pPixel[i] = 255;
  415. nNumParticle++;
  416. }
  417. if (pSrcImg[i]<nPartStart || pSrcImg[i]>nPartEnd)
  418. {
  419. pPixel[i] = 0;
  420. }
  421. }
  422. int errodDilateParam =5;
  423. if (errodDilateParam > 0)
  424. {
  425. BErode3(pPixel, pTempImg, errodDilateParam, nHeightImg, nWidthImg);
  426. BDilate3(pTempImg, pPixel, errodDilateParam, nHeightImg, nWidthImg);
  427. }
  428. }
  429. else
  430. {
  431. auto range = CalBackground(a_pImgIn);
  432. nBGStart = range->GetStart();
  433. nBGEnd = range->GetEnd();
  434. switch (a_pImageProcessParam->GetAutoBGRemoveType())
  435. {
  436. case OTS_AUTOBGREMOVE_TYPE::DOWNWARD:
  437. for (unsigned int i = 0; i < nImgSize; i++)
  438. {
  439. if (pSrcImg[i] <= nBGEnd)
  440. {
  441. pPixel[i] = 0;
  442. }
  443. else
  444. {
  445. pPixel[i] = 255;
  446. nNumParticle++;
  447. }
  448. }
  449. break;
  450. case OTS_AUTOBGREMOVE_TYPE::UPWARD:
  451. for (unsigned int i = 0; i < nImgSize; i++)
  452. {
  453. if (pSrcImg[i] >= nBGStart)
  454. {
  455. pPixel[i] = 0;
  456. }
  457. else
  458. {
  459. pPixel[i] = 255;
  460. nNumParticle++;
  461. }
  462. }
  463. break;
  464. case OTS_AUTOBGREMOVE_TYPE::MIDDLE:
  465. for (unsigned int i = 0; i < nImgSize; i++)
  466. {
  467. if (pSrcImg[i] >= nBGStart && pSrcImg[i] <= nBGEnd)
  468. {
  469. pPixel[i] = 0;
  470. }
  471. else
  472. {
  473. pPixel[i] = 255;
  474. nNumParticle++;
  475. }
  476. }
  477. break;
  478. default:
  479. break;
  480. }
  481. int errodDilateParam = 5;
  482. if (errodDilateParam > 0)
  483. {
  484. BErode3(pPixel, pTempImg, errodDilateParam, nHeightImg, nWidthImg);
  485. BDilate3(pTempImg, pPixel, errodDilateParam, nHeightImg, nWidthImg);
  486. }
  487. }
  488. a_pBinImgOut->SetImageData(pPixel,nWidthImg,nHeightImg);
  489. foundedPixelNum = nNumParticle;
  490. delete[] pTempImg;
  491. return ;
  492. }
  493. BOOL COTSImageProcess::GetParticles(long left, long top, long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSParticleList& a_listParticles)
  494. {
  495. ASSERT(a_pPixel);
  496. if (!a_pPixel)
  497. {
  498. return FALSE;
  499. }
  500. //a_listParticles.clear();
  501. COTSParticleList findedParts;
  502. COTSSegmentsList listSegment;
  503. listSegment.clear();
  504. //1. get segment line by line
  505. if (!GetSegmentList(left, top, a_nWidth, a_nHeight, a_pPixel, listSegment))
  506. {
  507. return FALSE;
  508. }
  509. if ((int)listSegment.size() == 0)
  510. {
  511. return FALSE;
  512. }
  513. //2. save the temp feature
  514. COTSFeatureList listFeature;
  515. listFeature.clear();
  516. if (!GetFeatureList(listSegment, listFeature))//get every feature for all the particle,the complete feature.
  517. {
  518. return FALSE;
  519. }
  520. if ((int)listFeature.size() == 0)
  521. {
  522. return FALSE;
  523. }
  524. /*COTSParticleList listParticles;
  525. listParticles.clear();*/
  526. if (!ChangeFeaturelist(listFeature, findedParts))
  527. {
  528. return FALSE;
  529. }
  530. for (auto f : findedParts)
  531. {
  532. a_listParticles.push_back(f);
  533. }
  534. return TRUE;
  535. }
  536. BOOL COTSImageProcess::GetOneParticleFromROI(long left, long top, long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSParticleList& a_listParticles)
  537. {
  538. ASSERT(a_pPixel);
  539. if (!a_pPixel)
  540. {
  541. return FALSE;
  542. }
  543. //a_listParticles.clear();
  544. COTSParticleList findedParts;
  545. COTSSegmentsList listSegment;
  546. listSegment.clear();
  547. //1. get segment line by line
  548. if (!GetSegmentList(left, top, a_nWidth, a_nHeight, a_pPixel, listSegment))
  549. {
  550. return FALSE;
  551. }
  552. if ((int)listSegment.size() == 0)
  553. {
  554. return FALSE;
  555. }
  556. //2. save the temp feature
  557. COTSFeatureList listFeature;
  558. listFeature.clear();
  559. COTSFeaturePtr fea = COTSFeaturePtr(new COTSFeature());
  560. fea->SetSegmentsList(listSegment);
  561. listFeature.push_back(fea);
  562. if ((int)listFeature.size() == 0)
  563. {
  564. return FALSE;
  565. }
  566. if (!ChangeFeaturelist(listFeature, findedParts))
  567. {
  568. return FALSE;
  569. }
  570. for (auto f : findedParts)
  571. {
  572. a_listParticles.push_back(f);
  573. }
  574. return TRUE;
  575. }
  576. BOOL COTSImageProcess::GetSegmentList(long left, long top, long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSSegmentsList& a_listSegments)
  577. {
  578. ASSERT(a_pPixel);
  579. long nImgSize = a_nWidth * a_nHeight;
  580. a_listSegments.clear();
  581. //1. get segment line by line
  582. long nLine, nm, nn;
  583. long nStart = 0, nLength = 0;
  584. for (nLine = 0; nLine < a_nHeight; nLine++)
  585. {
  586. for (nm = 0; nm < a_nWidth; nm += (nLength + 1))
  587. {
  588. nLength = 0;
  589. // get start
  590. if (*(a_pPixel + nLine * a_nWidth + nm) != 0)
  591. {
  592. nStart = nm;
  593. nLength++;
  594. //get length
  595. for (nn = nm + 1; nn < a_nWidth; nn++)
  596. {
  597. // check if segment is over, break
  598. if (nLength != 0)
  599. {
  600. if (*(a_pPixel + nLine * a_nWidth + nn) == 0)
  601. break;
  602. }
  603. if (*(a_pPixel + nLine * a_nWidth + nn) != 0)
  604. {
  605. nLength++;
  606. }
  607. }
  608. // generate segment
  609. COTSSegmentPtr pSegment = COTSSegmentPtr(new COTSSegment(nLine + top, nStart + left, nLength));
  610. a_listSegments.push_back(pSegment);
  611. }
  612. else
  613. {
  614. continue;
  615. }
  616. }
  617. }
  618. if ((int)a_listSegments.size() == 0)
  619. {
  620. //LogErrorTrace(__FILE__, __LINE__, _T("no particle is found."));
  621. return FALSE;
  622. }
  623. return TRUE;
  624. }
  625. BOOL COTSImageProcess::GetFeatureList(COTSSegmentsList& a_listSegments, COTSFeatureList& a_listFeatures)
  626. {
  627. COTSSegmentsList listSegmentNew;
  628. std::map<long, COTSSegmentsList > mapOneLineSegments;
  629. for each (auto s in a_listSegments)
  630. {
  631. mapOneLineSegments[s->GetHeight()].push_back(s);//sorting all the segments base on the line number.
  632. }
  633. std::map<long, COTSSegmentsList >::iterator lineItr = mapOneLineSegments.begin();//find the highest line
  634. while (lineItr != mapOneLineSegments.end())
  635. {
  636. for (auto s = lineItr->second.begin(); s < lineItr->second.end(); )//find one segment of this line.
  637. {
  638. COTSSegmentPtr bottomSeg = *s;
  639. listSegmentNew.clear();
  640. listSegmentNew.push_back(*s);
  641. s = lineItr->second.erase(s);
  642. std::map<long, COTSSegmentsList >::iterator tempItr = lineItr;
  643. tempItr++;
  644. for (; tempItr != mapOneLineSegments.end(); tempItr++)//find all other lines of segments
  645. {
  646. if (tempItr->first - bottomSeg->GetHeight() > 1)
  647. {
  648. break;
  649. }
  650. for (auto nextLineSegment = tempItr->second.begin(); nextLineSegment < tempItr->second.end();)//find next line's all segments
  651. {
  652. if (((*nextLineSegment)->GetStart() - (bottomSeg->GetStart() + bottomSeg->GetLength())) > 1)
  653. {
  654. break;
  655. }
  656. if (bottomSeg->UpDownConection(**nextLineSegment))
  657. {
  658. listSegmentNew.push_back(*nextLineSegment);
  659. bottomSeg = *nextLineSegment;
  660. nextLineSegment = tempItr->second.erase(nextLineSegment);
  661. break;
  662. }
  663. if (tempItr->second.size() > 0)
  664. {
  665. nextLineSegment++;
  666. }
  667. else
  668. {
  669. break;
  670. }
  671. }
  672. }
  673. COTSFeaturePtr pFeature = COTSFeaturePtr(new COTSFeature());
  674. pFeature->SetSegmentsList(listSegmentNew);
  675. //check if this new feature is connected with other found feature.
  676. COTSSegmentPtr topSeg = listSegmentNew[0];//find the toppest segment of this new feature.
  677. COTSSegmentPtr bottomSegment = listSegmentNew[listSegmentNew.size() - 1];//find the lowest segment of this new feature.
  678. bool haveMerged = false;
  679. for each (auto f in a_listFeatures)
  680. {
  681. for (auto seg : f->GetSegmentsList())
  682. {
  683. if (bottomSegment->UpDownConection(*seg) || topSeg->UpDownConection(*seg))
  684. {
  685. COTSSegmentsList segs = f->GetSegmentsList();
  686. for (auto s : listSegmentNew)
  687. {
  688. segs.push_back(s);
  689. }
  690. f->SetSegmentsList(segs);
  691. haveMerged = true;
  692. break;
  693. }
  694. }
  695. if (haveMerged)
  696. {
  697. break;
  698. }
  699. }
  700. if (!haveMerged)
  701. {
  702. a_listFeatures.push_back(pFeature);
  703. }
  704. if (lineItr->second.size() == 0)
  705. {
  706. break;
  707. }
  708. }
  709. lineItr++;
  710. }
  711. return true;
  712. }
  713. BOOL COTSImageProcess::ChangeFeaturelist(COTSFeatureList& a_listFeatures, COTSParticleList& a_listParticle)
  714. {
  715. for (auto pFeature : a_listFeatures)
  716. {
  717. COTSParticlePtr pParticle = COTSParticlePtr(new COTSParticle());
  718. pParticle->SetFeature(pFeature);
  719. a_listParticle.push_back(pParticle);
  720. }
  721. if ((int)a_listParticle.size() == 0)
  722. {
  723. return FALSE;
  724. }
  725. return TRUE;
  726. }
  727. BOOL COTSImageProcess::CalcuParticleImagePropertes(COTSParticlePtr a_pOTSPart, double a_PixelSize)
  728. {
  729. //--------- convert this particle data to image data,construct an image only with this particle.------
  730. const int nExpand_Size = 3;
  731. const int nWhiteColor = 0;
  732. const int nThickness = 1;
  733. // lineType Type of the line
  734. const int nLineType = 8;
  735. // get rectangle of the particle
  736. CRect rect = a_pOTSPart->GetParticleRect();
  737. 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.
  738. {
  739. double w = 0, h = 0;
  740. w = (double)rect.Width()*a_PixelSize;
  741. h = (double)rect.Height()*a_PixelSize;
  742. a_pOTSPart->SetDMax(MAX(w, h));
  743. a_pOTSPart->SetDMin(MIN(w, h));
  744. a_pOTSPart->SetDMean((w + h) / 2);
  745. a_pOTSPart->SetFeretDiameter((w + h) / 2);
  746. a_pOTSPart->SetDElong(MAX(w, h));
  747. a_pOTSPart->SetPerimeter((w+h)*2);
  748. a_pOTSPart->SetDPerp(MIN(w, h));
  749. a_pOTSPart->SetDInscr(MIN(w, h));
  750. a_pOTSPart->SetMinWidth(w);
  751. a_pOTSPart->SetMinHeight(h);
  752. return true;
  753. }
  754. // calculate the particle image data size, expand 3 pixel at the edge
  755. Mat particleImage = Mat::zeros(rect.Height() + nExpand_Size , rect.Width() + nExpand_Size , CV_8U);
  756. // get the segment list
  757. COTSSegmentsList listSegment = a_pOTSPart->GetFeature()->GetSegmentsList();
  758. for (auto pSegment : listSegment)
  759. {
  760. int nStart = pSegment->GetStart() - rect.left + nExpand_Size;
  761. int nEnd = pSegment->GetStart() + pSegment->GetLength() - rect.left - 1 + nExpand_Size;
  762. int nHeight = pSegment->GetHeight() - rect.top + nExpand_Size;
  763. line(particleImage, Point(nStart, nHeight), Point(nEnd, nHeight), Scalar(nBlackColor), nThickness, nLineType);
  764. }
  765. //--------abstract the contour of the particle.
  766. vector<vector<Point>>contours;
  767. findContours(particleImage, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
  768. 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.
  769. {
  770. double w = 0, h = 0;
  771. w = (double)rect.Width()*a_PixelSize;
  772. h = (double)rect.Height()*a_PixelSize;
  773. a_pOTSPart->SetDMax(MAX(w, h));
  774. a_pOTSPart->SetDMin(MIN(w, h));
  775. a_pOTSPart->SetDMean((w + h) / 2);
  776. a_pOTSPart->SetFeretDiameter((w + h) / 2);
  777. a_pOTSPart->SetDElong(MAX(w, h));
  778. a_pOTSPart->SetPerimeter((w + h) * 2);
  779. a_pOTSPart->SetDPerp(MIN(w, h));
  780. a_pOTSPart->SetDInscr(MIN(w, h));
  781. a_pOTSPart->SetMinWidth(w);
  782. a_pOTSPart->SetMinHeight(h);
  783. return true;
  784. }
  785. int imaxcontour = 0, imax = 0;
  786. for (unsigned int i = 0; i < contours.size(); i++) {
  787. int itmp = contourArea(contours[i]);
  788. if (imaxcontour < itmp) {
  789. imax = i;
  790. imaxcontour = itmp;
  791. }
  792. }
  793. vector<Point > listEdge = contours[imax];
  794. vector<vector<Point>>Outcontours;
  795. Outcontours.push_back(listEdge);
  796. //---------calculate the minimium rectangle
  797. auto rRect = cv::minAreaRect(listEdge);
  798. Point2f p[4];
  799. rRect.points(p);
  800. int D_MIN =getDistance(p[0], p[1]);
  801. int D_MinRecLen = 0;//minareaRect's length(the longger side).
  802. for (int j = 0; j <= 2; j++)
  803. {
  804. //line(cvContourImg, p[j], p[(j + 1) % 4], Scalar(100, 100, 0), 2);
  805. int d = getDistance(p[j], p[j + 1]);
  806. if (d < D_MIN)
  807. {
  808. D_MIN = d;
  809. }
  810. if (d > D_MinRecLen)
  811. {
  812. D_MinRecLen = d;
  813. }
  814. }
  815. a_pOTSPart->SetDMin(D_MIN*a_PixelSize);
  816. a_pOTSPart->SetMinWidth(rRect.size.width);
  817. a_pOTSPart->SetMinHeight(rRect.size.height);
  818. //----------calculate the perimeter
  819. double d = arcLength(listEdge, true);
  820. a_pOTSPart->SetPerimeter(d*a_PixelSize);
  821. //-----------calculate the Max Diameter. Find the min enclosing circle first ,then find the two farthest circle connected point.
  822. Point2f center; float radius;
  823. minEnclosingCircle(listEdge, center, radius);
  824. //circle(cvContourImg, center, radius, Scalar(100), 2);
  825. std::vector <Point> outContour = listEdge;
  826. std::vector <Point> rst;
  827. for (unsigned int k = 0; k < outContour.size(); k++)
  828. {
  829. Point p = outContour[k];
  830. double d = sqrt(pow((p.x - center.x), 2) + pow((p.y - center.y), 2));
  831. if (fabs(d - radius) < 0.01)
  832. {
  833. rst.push_back(p);
  834. }
  835. }
  836. double D_MAX = 0;
  837. Point lineDmax[2];
  838. for (unsigned int m = 0; m < rst.size(); m++)
  839. {
  840. Point p = rst[m];
  841. for (unsigned int n = m + 1; n < rst.size(); n++)
  842. {
  843. Point p1 = rst[n];
  844. double d = sqrt(powf((p.x - p1.x), 2) + powf((p.y - p1.y), 2));
  845. if (d > D_MAX)
  846. {
  847. D_MAX = d;
  848. lineDmax[0] = p;
  849. lineDmax[1] = p1;
  850. }
  851. }
  852. }
  853. a_pOTSPart->SetDMax(D_MAX*a_PixelSize);
  854. //--------calculate the D_PERP property using the D_MAX's two endpoints.
  855. std::vector<Point> curve1;
  856. std::vector<Point> curve2;
  857. for (unsigned int i = 0; i < outContour.size(); i++)
  858. {
  859. Point pt = outContour[i];
  860. bool start = false;
  861. int clockwise = Side(lineDmax[0], lineDmax[1], pt);// devide these points into two group ,separate into the two sides.
  862. if (clockwise > 0)
  863. {
  864. curve1.push_back(pt);
  865. }
  866. else
  867. {
  868. curve2.push_back(pt);
  869. }
  870. }
  871. double d_perp1 = 0, d_perp2 = 0;
  872. for (unsigned int i = 0; i < curve1.size(); i++)
  873. {
  874. double d = getDist_P2L(curve1[i], lineDmax[0], lineDmax[1]);
  875. if (d > d_perp1)
  876. {
  877. d_perp1 = d;
  878. }
  879. }
  880. for (unsigned int i = 0; i < curve2.size(); i++)
  881. {
  882. double d = getDist_P2L(curve2[i], lineDmax[0], lineDmax[1]);
  883. if (d > d_perp2)
  884. {
  885. d_perp2 = d;
  886. }
  887. }
  888. a_pOTSPart->SetDPerp((d_perp1 + d_perp2)*a_PixelSize);
  889. //----------find the diameter of max inscribed circle
  890. int r;
  891. Point inscribeCirclecenter;
  892. FindInnerCircleInContour(outContour, inscribeCirclecenter, r);
  893. //--------------------------------------------------------calculate the xraypos !
  894. CRect rec = a_pOTSPart->GetParticleRect();
  895. a_pOTSPart->SetXRayPos(CPoint(inscribeCirclecenter.x - nExpand_Size + rec.left - 1, inscribeCirclecenter.y - nExpand_Size + rec.top - 1));
  896. a_pOTSPart->SetDInscr(r * 2 * a_PixelSize);
  897. //---------------calculate the image other caracater: length/width realArea/minRectangeArea etc. we can use these propertes to do forward process.
  898. double minRectArea = D_MIN * D_MinRecLen*a_PixelSize*a_PixelSize;//離鬼棍쌈앤近충생
  899. double fillRatio = a_pOTSPart->GetActualArea() / minRectArea;//茄셥충생宅離鬼棍쌈앤近충생궐,that's the fill rate.
  900. double lengthWidthRatio;
  901. lengthWidthRatio = (double)D_MinRecLen / D_MIN;//낀욱궐
  902. //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.
  903. bool isStripShape = false;
  904. double curveLength = 0;
  905. double D_MEAN=0;
  906. Moments mu;
  907. mu = moments(listEdge, false);
  908. int nx = mu.m10 / mu.m00;
  909. int ny = mu.m01 / mu.m00;
  910. //circle(cvcopyImg, Point(nx, ny), 1, (255), 1);
  911. Point ptCenter = Point((int)nx, (int)ny);
  912. if (pointPolygonTest(listEdge, ptCenter, false) != 1)// the center point doesn't contain in the contour, we think it as curve shape.
  913. {
  914. isStripShape = true;
  915. }
  916. /*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.
  917. {
  918. isStripShape = true;
  919. }*/
  920. 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.
  921. {
  922. isStripShape = true;
  923. }
  924. if (isStripShape)
  925. {
  926. 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.
  927. if (curveLength < D_MAX)
  928. {
  929. curveLength = D_MAX;
  930. }
  931. if (curveLength < MIN_DOUBLE_VALUE || a_pOTSPart->GetActualArea()<MIN_DOUBLE_VALUE)
  932. {
  933. D_MEAN = 0;
  934. }
  935. else
  936. {
  937. D_MEAN = a_pOTSPart->GetActualArea() / curveLength;
  938. }
  939. a_pOTSPart->SetDMean(D_MEAN*a_PixelSize);
  940. a_pOTSPart->SetFeretDiameter(D_MEAN*a_PixelSize);
  941. a_pOTSPart->SetDElong (curveLength*a_PixelSize);
  942. }
  943. else//it's a ball shape particle
  944. {
  945. curveLength = D_MAX;
  946. double ftd = 0, maxD = 0, minD = 0, dratio = 0;
  947. GetParticleAverageChord(outContour, a_PixelSize, ftd);
  948. a_pOTSPart->SetDMean(ftd);
  949. a_pOTSPart->SetFeretDiameter(ftd);
  950. a_pOTSPart->SetDElong(curveLength*a_PixelSize);
  951. }
  952. if (a_pOTSPart->GetAspectRatio() > 2)
  953. {
  954. double angle;
  955. if (rRect.size.width > rRect.size.height) // w > h
  956. {
  957. angle = abs(rRect.angle);
  958. }
  959. else
  960. {
  961. angle = 90.0 + abs(rRect.angle);
  962. }
  963. a_pOTSPart->SetOrientation(angle);
  964. }
  965. else
  966. {
  967. a_pOTSPart->SetOrientation(0);
  968. }
  969. return true;
  970. }
  971. void COTSImageProcess::ImshowImage(CBSEImgPtr img)
  972. {
  973. BYTE* data = img->GetImageDataPointer();
  974. //Mat cvImg;
  975. cv::Size s;
  976. s.width = img->GetImageSize().cx;
  977. s.height = img->GetImageSize().cy;
  978. Mat cvImg=Mat::zeros(s, CV_8U);
  979. cvImg.data = data;
  980. cv::imshow("dd", cvImg);
  981. cv::waitKey();
  982. }
  983. void COTSImageProcess::ImshowChartData(CBSEImgPtr img)
  984. {
  985. img->SetChartData();
  986. WORD* data = img->GetBSEChart();
  987. //Mat cvImg;
  988. cv::Size s;
  989. s.width = 255;
  990. s.height = 100;
  991. Mat cvImg = Mat::zeros(s, CV_8U);
  992. //cvImg.data = data;
  993. WORD nBSEChart[MAXBYTE];
  994. //1. get chart data
  995. linearSmooth5(data, nBSEChart, MAXBYTE);
  996. for (int i=1;i<255;i++)
  997. {
  998. line(cvImg, Point(i, 100-nBSEChart[i]), Point(i+1, 100-nBSEChart[i+1]), Scalar(nBlackColor), 1, 8);
  999. }
  1000. cv::imshow("chart", cvImg);
  1001. cv::waitKey();
  1002. }
  1003. BOOL COTSImageProcess::MergeBigBoundaryParticles(COTSFieldDataList allFields,double pixelSize,int scanFieldSize, CSize ResolutionSize, COTSParticleList& mergedParts)
  1004. {
  1005. class BorderPart
  1006. {
  1007. typedef std::shared_ptr<BorderPart> CBorderPartPtr;
  1008. BorderPart(COTSParticlePtr p)
  1009. {
  1010. myPart = p;
  1011. headerParticle = NULL;
  1012. }
  1013. public:
  1014. COTSParticlePtr myPart;
  1015. 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.
  1016. static std::vector<CBorderPartPtr> ConvertPartToBorderPart(COTSParticleList parts)
  1017. {
  1018. static std::map<COTSParticle*, CBorderPartPtr> allborderPart;
  1019. std::vector<CBorderPartPtr> borderParts;
  1020. for (auto p : parts)
  1021. {
  1022. if (allborderPart.find(p.get()) == allborderPart.end())
  1023. {
  1024. auto borderp = CBorderPartPtr(new BorderPart(p));
  1025. borderParts.push_back(borderp);
  1026. allborderPart[p.get()] = borderp;
  1027. }
  1028. else
  1029. {
  1030. borderParts.push_back(allborderPart[p.get()]);
  1031. }
  1032. }
  1033. return borderParts;
  1034. }
  1035. };
  1036. auto FldMgr = new CFieldMgr(scanFieldSize, ResolutionSize);
  1037. std::map<COTSParticle*, COTSParticleList> mapMergeParticles;//hold up all the boundary connected particles. the pair's first is also the member of these particles.
  1038. std::map<COTSParticle*, COTSSegmentsList> mapMergedSegments;//hold up all the segment's corresponding clone in the connected particles.
  1039. for (auto centerfld : allFields)
  1040. {
  1041. // find neighbor field on the left.
  1042. auto leftFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::LEFT);
  1043. if (leftFld != nullptr)
  1044. {
  1045. auto lParts = centerfld->GetLeftBorderedBigParticles();
  1046. auto rParts = leftFld->GetRightBorderedBigParticles();
  1047. auto leftParts = BorderPart::ConvertPartToBorderPart(lParts);
  1048. auto rightParts = BorderPart::ConvertPartToBorderPart(rParts);
  1049. for (auto leftp : leftParts)
  1050. {
  1051. for (auto rightp : rightParts)
  1052. {
  1053. if (leftp->myPart->IsConnected(rightp->myPart.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::LEFT))
  1054. {
  1055. if (leftp->headerParticle != NULL)
  1056. {
  1057. if (rightp->headerParticle == NULL)
  1058. {
  1059. rightp->headerParticle = leftp->headerParticle;
  1060. mapMergeParticles[leftp->headerParticle].push_back(rightp->myPart);
  1061. }
  1062. }
  1063. else
  1064. {
  1065. if (rightp->headerParticle != NULL)
  1066. {
  1067. leftp->headerParticle = rightp->myPart.get();
  1068. mapMergeParticles[rightp->myPart.get()].push_back(leftp->myPart);
  1069. }
  1070. else
  1071. {
  1072. leftp->headerParticle = leftp->myPart.get();
  1073. rightp->headerParticle = leftp->myPart.get();
  1074. mapMergeParticles[leftp->myPart.get()].push_back(rightp->myPart);
  1075. }
  1076. }
  1077. }
  1078. }
  1079. }
  1080. }
  1081. //find neighbor field on the upward
  1082. auto upFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::UP);
  1083. if (upFld != nullptr)
  1084. {
  1085. auto topBorderParts = centerfld->GetTopBorderedBigParticles();
  1086. auto bottomBorderParts = upFld->GetBottomBorderedBigParticles();
  1087. auto upParts = BorderPart::ConvertPartToBorderPart(topBorderParts);
  1088. auto downParts = BorderPart::ConvertPartToBorderPart(bottomBorderParts);
  1089. for (auto upprt : upParts)
  1090. {
  1091. for (auto downprt : downParts)
  1092. {
  1093. if (upprt->myPart->IsConnected(downprt->myPart.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::UP))
  1094. {
  1095. if (upprt->headerParticle != NULL)
  1096. {
  1097. if (downprt->headerParticle == NULL)
  1098. {
  1099. downprt->headerParticle = upprt->headerParticle;
  1100. mapMergeParticles[upprt->headerParticle].push_back(downprt->myPart);
  1101. }
  1102. }
  1103. else
  1104. {
  1105. if (downprt->headerParticle != NULL)
  1106. {
  1107. upprt->headerParticle = downprt->headerParticle;
  1108. mapMergeParticles[downprt->myPart.get()].push_back(upprt->myPart);
  1109. }
  1110. else
  1111. {
  1112. upprt->headerParticle = upprt->myPart.get();
  1113. downprt->headerParticle = upprt->myPart.get();
  1114. mapMergeParticles[upprt->myPart.get()].push_back(downprt->myPart);
  1115. }
  1116. }
  1117. }
  1118. }
  1119. }
  1120. }
  1121. //find neighbor field on the downward.
  1122. auto downFld = FldMgr->FindNeighborField(allFields, centerfld,SORTING_DIRECTION::DOWN);
  1123. if (downFld != nullptr)
  1124. {
  1125. auto bottomParts = centerfld->GetBottomBorderedBigParticles();
  1126. auto topParts = downFld->GetTopBorderedBigParticles();
  1127. auto downParts = BorderPart::ConvertPartToBorderPart(bottomParts);
  1128. auto upParts= BorderPart::ConvertPartToBorderPart(topParts);
  1129. for (auto downprt : downParts)
  1130. {
  1131. for (auto upprt : upParts)
  1132. {
  1133. if (downprt->myPart->IsConnected(upprt->myPart.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::DOWN))
  1134. {
  1135. if (downprt->headerParticle != NULL)
  1136. {
  1137. if (upprt->headerParticle == NULL)
  1138. {
  1139. upprt->headerParticle = downprt->headerParticle;
  1140. mapMergeParticles[downprt->headerParticle].push_back(upprt->myPart);
  1141. }
  1142. }
  1143. else
  1144. {
  1145. if (upprt->headerParticle != NULL)
  1146. {
  1147. downprt->headerParticle = upprt->headerParticle;
  1148. mapMergeParticles[upprt->headerParticle].push_back(downprt->myPart);
  1149. }
  1150. else
  1151. {
  1152. downprt->headerParticle = downprt->myPart.get();
  1153. upprt->headerParticle = downprt->myPart.get();
  1154. mapMergeParticles[downprt->myPart.get()].push_back(upprt->myPart);
  1155. }
  1156. }
  1157. }
  1158. }
  1159. }
  1160. }
  1161. //find neighbor field on the right.
  1162. auto rightFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::RIGHT);
  1163. if (rightFld != nullptr)
  1164. {
  1165. auto rParts = centerfld->GetRightBorderedBigParticles();
  1166. auto lParts = rightFld->GetLeftBorderedBigParticles();
  1167. auto rightParts = BorderPart::ConvertPartToBorderPart(rParts);
  1168. auto leftParts = BorderPart::ConvertPartToBorderPart(lParts);
  1169. for (auto rightprt : rightParts)
  1170. {
  1171. for (auto leftprt : leftParts)
  1172. {
  1173. if (rightprt->myPart->IsConnected(leftprt->myPart.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::RIGHT))
  1174. {
  1175. if (rightprt->headerParticle != NULL)
  1176. {
  1177. if (leftprt->headerParticle == NULL)
  1178. {
  1179. leftprt->headerParticle = rightprt->headerParticle;
  1180. mapMergeParticles[rightprt->headerParticle].push_back(leftprt->myPart);
  1181. }
  1182. }
  1183. else
  1184. {
  1185. if (leftprt->headerParticle != NULL)
  1186. {
  1187. rightprt->headerParticle = leftprt->headerParticle;
  1188. mapMergeParticles[leftprt->headerParticle].push_back(rightprt->myPart);
  1189. }
  1190. else
  1191. {
  1192. rightprt->headerParticle = rightprt->myPart.get();
  1193. leftprt->headerParticle = rightprt->myPart.get();
  1194. mapMergeParticles[rightprt->myPart.get()].push_back(leftprt->myPart);
  1195. }
  1196. }
  1197. }
  1198. }
  1199. }
  1200. }
  1201. }
  1202. /*for (auto particle : mapMergeParticles)
  1203. {
  1204. }*/
  1205. static int partTagId;
  1206. for (auto pair : mapMergeParticles)
  1207. {
  1208. struct EleAreaPercentage
  1209. {
  1210. EleAreaPercentage(double p, CElementChemistryPtr e)
  1211. {
  1212. areaPercentage = p;
  1213. eleData = e;
  1214. }
  1215. double areaPercentage;
  1216. CElementChemistryPtr eleData;
  1217. };
  1218. auto newPart = COTSParticlePtr(new COTSParticle());
  1219. COTSSegmentsList newSegs;
  1220. auto p = pair.first;
  1221. newPart->SetSEMPos(p->GetSEMPos());
  1222. //firstly,we sum up all the merged particles's area and get the represent string.
  1223. std::string partsStr = std::to_string(p->GetFieldId()) + ":" + std::to_string(p->GetAnalysisId());
  1224. double allPartArea = p->GetActualArea();//Get the first particle's area.
  1225. for (auto other : pair.second)// Get the total area of all these particles for the use of ele calcu.
  1226. {
  1227. partsStr += "," + std::to_string(other->GetFieldId()) + ":" + std::to_string(other->GetAnalysisId());//Get the subparticles string such as "1:1,2:1" etc.
  1228. allPartArea += other->GetActualArea();//Get other particle's area
  1229. }
  1230. // calculate all the new segment's position.
  1231. std::vector <COTSParticle*> allSubParts;
  1232. allSubParts.push_back(p);
  1233. for (auto other : pair.second)// Get the total area of all these particles for the use of ele calcu.
  1234. {
  1235. allSubParts.push_back(other.get());
  1236. }
  1237. for (auto subp : allSubParts)
  1238. {
  1239. int fid = subp->GetFieldId();
  1240. CPoint myFldPos;
  1241. for (auto f : allFields)//find this particle's filed.
  1242. {
  1243. if (f->GetId() == fid)
  1244. {
  1245. myFldPos = f->GetPosition();
  1246. }
  1247. }
  1248. int fldWidth = allFields[0]->Width;
  1249. int fldHeight = allFields[0]->Height;
  1250. CPoint fldLeftUpPos = CPoint(myFldPos.x + fldWidth / 2 , myFldPos.y + fldHeight / 2 );
  1251. for (auto s : subp->GetFeature()->GetSegmentsList())
  1252. {
  1253. COTSSegmentPtr newseg = COTSSegmentPtr(new COTSSegment());
  1254. newseg->SetStart(s->GetStart() + fldLeftUpPos.x);
  1255. 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.
  1256. newseg->SetLength(s->GetLength());
  1257. newSegs.push_back(newseg);
  1258. }
  1259. }
  1260. COTSFeaturePtr newFeature = COTSFeaturePtr(new COTSFeature());
  1261. newFeature->SetSegmentsList(newSegs);
  1262. newPart->SetFeature(newFeature);
  1263. newPart->CalCoverRectFromSegment();
  1264. //second, we get all the element data and their area percentage .
  1265. std::map<std::string, std::vector<EleAreaPercentage>> mapEleData;
  1266. CPosXrayPtr pXray1 = p->GetXrayInfo();
  1267. if (pXray1 != nullptr)
  1268. {
  1269. for (auto ele : pXray1->GetElementQuantifyData())
  1270. {
  1271. mapEleData[ele->GetName().GetBuffer()].push_back(EleAreaPercentage(p->GetActualArea() / allPartArea, ele));
  1272. }
  1273. }
  1274. for (auto other : pair.second)
  1275. {
  1276. auto otherXray = other->GetXrayInfo();
  1277. if (otherXray != nullptr)
  1278. {
  1279. for (auto eledata : otherXray->GetElementQuantifyData())
  1280. {
  1281. mapEleData[eledata->GetName().GetBuffer()].push_back(EleAreaPercentage(other->GetActualArea() / allPartArea, eledata));
  1282. }
  1283. }
  1284. }
  1285. // third,we calculate all the element's new percentage data and get a new element chemistry list.
  1286. CElementChemistriesList newCheList;
  1287. for (auto eledata : mapEleData)
  1288. {
  1289. CElementChemistryPtr newEleche = CElementChemistryPtr(new CElementChemistry());
  1290. newEleche->SetName(CString(eledata.first.c_str()));
  1291. double newPercentage = 0;
  1292. for (auto d : eledata.second)
  1293. {
  1294. newPercentage += d.areaPercentage * d.eleData->GetPercentage();
  1295. }
  1296. newEleche->SetPercentage(newPercentage);
  1297. newCheList.push_back(newEleche);
  1298. }
  1299. CPosXrayPtr xray(new CPosXray());
  1300. xray->SetElementQuantifyData(newCheList);
  1301. newPart->SetXrayInfo(xray);
  1302. newPart->SetConnectedParticlesSequentialString(partsStr);
  1303. newPart->SetActualArea(allPartArea);
  1304. partTagId++;
  1305. newPart->SetParticleId(partTagId);
  1306. newPart->SetAnalysisId(partTagId);
  1307. std::string name = p->GetClassifyName();
  1308. newPart->SetClassifyName(name);
  1309. newPart->SetColor(p->GetColor());
  1310. mergedParts.push_back(newPart);
  1311. }
  1312. return true;
  1313. }
  1314. }