OTSImageProcess.cpp 61 KB

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