OTSImageProcess.cpp 61 KB

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