OTSImageProcess.cpp 55 KB

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