OTSImageProcess.cpp 73 KB

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