OTSImageProcess.cpp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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. using namespace cv;
  8. using namespace std;
  9. namespace OTSIMGPROC
  10. {
  11. namespace
  12. {
  13. /***** 求两点间距离*****/
  14. float getDistance(Point pointO, Point pointA)
  15. {
  16. float distance;
  17. distance = powf((pointO.x - pointA.x), 2) + powf((pointO.y - pointA.y), 2);
  18. distance = sqrtf(distance);
  19. return distance;
  20. }
  21. /***** 点到直线的距离:P到AB的距离*****/
  22. //P为线外一点,AB为线段两个端点
  23. float getDist_P2L(Point pointP, Point pointA, Point pointB)
  24. {
  25. //求直线方程
  26. int A = 0, B = 0, C = 0;
  27. A = pointA.y - pointB.y;
  28. B = pointB.x - pointA.x;
  29. C = pointA.x*pointB.y - pointA.y*pointB.x;
  30. //代入点到直线距离公式
  31. float distance = 0;
  32. distance = ((float)abs(A*pointP.x + B * pointP.y + C)) / ((float)sqrtf(A*A + B * B));
  33. return distance;
  34. }
  35. int Side(Point P1, Point P2, Point point)
  36. {
  37. /*Point P1 = line.P1;
  38. Point P2 = line.P2;*/
  39. return ((P2.y - P1.y) * point.x + (P1.x - P2.x) * point.y + (P2.x*P1.y - P1.x*P2.y));
  40. }
  41. void FindInnerCircleInContour(vector<Point> contour, Point &center, int &radius)
  42. {
  43. Rect r = boundingRect(contour);
  44. int nL = r.x, nR = r.br().x; //轮廓左右边界
  45. int nT = r.y, nB = r.br().y; //轮廓上下边界
  46. double dist = 0;
  47. double maxdist = 0;
  48. for (int i = nL; i < nR; i++) //列
  49. {
  50. for (int j = nT; j < nB; j++) //行
  51. {
  52. //计算轮廓内部各点到最近轮廓点的距离
  53. dist = pointPolygonTest(contour, Point(i, j), true);
  54. if (dist > maxdist)
  55. {
  56. //求最大距离,只有轮廓最中心的点才距离最大
  57. maxdist = dist;
  58. center = Point(i, j);
  59. }
  60. }
  61. }
  62. radius = maxdist; //圆半径
  63. }
  64. BOOL GetParticleAverageChord(std::vector<Point> listEdge, double a_PixelSize, double &dPartFTD)
  65. {
  66. // safety check
  67. double nx = 0, ny = 0;
  68. Moments mu;
  69. mu = moments(listEdge, false);
  70. nx = mu.m10 / mu.m00;
  71. ny = mu.m01 / mu.m00;
  72. //circle(cvcopyImg, Point(nx, ny), 1, (255), 1);
  73. Point ptCenter = Point((int)nx, (int)ny);
  74. // coordinate transformation
  75. Point ptPosition;
  76. int radiusNum = 0;
  77. // get ferret diameter
  78. double sumFltDiameter = 0;
  79. int interval;
  80. int edgePointNum = listEdge.size();
  81. if (edgePointNum > 100)
  82. {
  83. interval = edgePointNum / 100;//get one line per 10 degree aproxemately
  84. }
  85. else
  86. {
  87. interval = 1;
  88. }
  89. for (int i = 0; i < edgePointNum; i++)
  90. {
  91. Point pt = listEdge[i];
  92. ptPosition.x = abs(pt.x - ptCenter.x);
  93. ptPosition.y = abs(pt.y - ptCenter.y);
  94. if (i % interval == 0)//calculate one line per 10 point ,so to speed up.don't calculate all the diameter.
  95. {
  96. double r1 = sqrt(pow(ptPosition.x, 2) + pow(ptPosition.y, 2));
  97. sumFltDiameter += r1;
  98. radiusNum += 1;
  99. //line(cvImageData, ptCenter, pt, Scalar(nBlackColor), nThickness, nLineType);
  100. }
  101. }
  102. if (radiusNum == 0)
  103. {
  104. dPartFTD = 0;
  105. }
  106. else
  107. {
  108. dPartFTD = a_PixelSize * sumFltDiameter / radiusNum * 2;
  109. }
  110. //imshow("feret center", cvImageData);
  111. return TRUE;
  112. }
  113. }
  114. COTSImageProcess::COTSImageProcess()
  115. {
  116. }
  117. COTSImageProcess::~COTSImageProcess()
  118. {
  119. }
  120. // use verticl line of 3 pixel to erode a image
  121. void COTSImageProcess::BErodeVertical3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  122. {
  123. WORD x, y, wcounts;
  124. if (rows <= 2 || columns <= 2)return;
  125. // top line
  126. for (x = 0; x < columns; x++)
  127. {
  128. *(target + x) = 0;
  129. }
  130. // bottom line
  131. for (x = 0; x < columns; x++)
  132. {
  133. *(target + (DWORD)(rows - 1)*columns + x) = 0;
  134. }
  135. for (y = 1; y<rows - 1; y++)
  136. {
  137. for (x = 0; x<columns; x++)
  138. {
  139. if (*(source + (DWORD)y*columns + x) == 0)
  140. {
  141. *(target + (DWORD)y*columns + x) = 0;
  142. continue;
  143. }
  144. wcounts = 0;
  145. if (*(source + (DWORD)(y - 1)*columns + x) == 255)
  146. {
  147. wcounts++;
  148. }
  149. if (*(source + (DWORD)(y + 1)*columns + x) == 255)
  150. {
  151. wcounts++;
  152. }
  153. if (wcounts == 2) *(target + (DWORD)y*columns + x) = 255;
  154. else *(target + (DWORD)y*columns + x) = 0;
  155. }
  156. }
  157. }
  158. // use left 45 degree line of 3 pixel to erode a image
  159. void COTSImageProcess::BErodeLeft45Degree3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  160. {
  161. WORD x, y, wcounts;
  162. if (rows <= 2 || columns <= 2)return;
  163. // top line
  164. for (x = 0; x < columns; x++)
  165. {
  166. *(target + x) = 0;
  167. }
  168. // bottom line
  169. for (x = 0; x < columns; x++)
  170. {
  171. *(target + (DWORD)(rows - 1)*columns + x) = 0;
  172. }
  173. // left line
  174. for (y = 0; y<rows; y++)
  175. {
  176. *(target + (DWORD)y*columns) = 0;
  177. }
  178. // right line
  179. for (y = 0; y<rows; y++)
  180. {
  181. *(target + (DWORD)y*columns + columns - 1) = 0;
  182. }
  183. for (y = 1; y < rows - 1; y++)
  184. {
  185. for (x = 1; x < columns - 1; x++)
  186. {
  187. if (*(source + (DWORD)y*columns + x) == 0)
  188. {
  189. *(target + (DWORD)y*columns + x) = 0;
  190. continue;
  191. }
  192. wcounts = 0;
  193. if (*(source + (DWORD)(y - 1)*columns + x - 1) == 255)
  194. {
  195. wcounts++;
  196. }
  197. if (*(source + (DWORD)(y + 1)*columns + x + 1) == 255)
  198. {
  199. wcounts++;
  200. }
  201. if (wcounts == 2) *(target + (DWORD)y*columns + x) = 255;
  202. else *(target + (DWORD)y*columns + x) = 0;
  203. }
  204. }
  205. }
  206. // use horizoontal line of 3 pixel to erode a image
  207. void COTSImageProcess::BErodeHorizontal3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  208. {
  209. WORD x, y, wcounts;
  210. if (rows <= 2 || columns <= 2)return;
  211. // left line
  212. for (y = 0; y<rows; y++)
  213. {
  214. *(target + (DWORD)y*columns) = 0;
  215. }
  216. // right line
  217. for (y = 0; y<rows; y++)
  218. {
  219. *(target + (DWORD)y*columns + columns - 1) = 0;
  220. }
  221. for (y = 0; y<rows; y++)
  222. {
  223. for (x = 1; x<columns - 1; x++)
  224. {
  225. if (*(source + (DWORD)y*columns + x) == 0)
  226. {
  227. *(target + (DWORD)y*columns + x) = 0;
  228. continue;
  229. }
  230. wcounts = 0;
  231. if (*(source + (DWORD)y*columns + x - 1) == 255)
  232. {
  233. wcounts++;
  234. }
  235. if (*(source + (DWORD)y*columns + x + 1) == 255)
  236. {
  237. wcounts++;
  238. }
  239. if (wcounts == 2) *(target + (DWORD)y*columns + x) = 255;
  240. else *(target + (DWORD)y*columns + x) = 0;
  241. }
  242. }
  243. }
  244. // use right 45 degree line of 3 pixel to erode a image
  245. void COTSImageProcess::BErodeRight45Degree3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  246. {
  247. WORD x, y, wcounts;
  248. if (rows <= 2 || columns <= 2)return;
  249. // top line
  250. for (x = 0; x < columns; x++)
  251. {
  252. *(target + x) = 0;
  253. }
  254. // bottom line
  255. for (x = 0; x < columns; x++)
  256. {
  257. *(target + (DWORD)(rows - 1)*columns + x) = 0;
  258. }
  259. // left line
  260. for (y = 0; y<rows; y++)
  261. {
  262. *(target + (DWORD)y*columns) = 0;
  263. }
  264. // right line
  265. for (y = 0; y<rows; y++)
  266. {
  267. *(target + (DWORD)y*columns + columns - 1) = 0;
  268. }
  269. for (y = 1; y<rows - 1; y++)
  270. {
  271. for (x = 1; x<columns - 1; x++)
  272. {
  273. if (*(source + (DWORD)y*columns + x) == 0)
  274. {
  275. *(target + (DWORD)y*columns + x) = 0;
  276. continue;
  277. }
  278. wcounts = 0;
  279. if (*(source + (DWORD)(y - 1)*columns + x + 1) == 255)
  280. {
  281. wcounts++;
  282. }
  283. if (*(source + (DWORD)(y + 1)*columns + x - 1) == 255)
  284. {
  285. wcounts++;
  286. }
  287. if (wcounts == 2) *(target + (DWORD)y*columns + x) = 255;
  288. else *(target + (DWORD)y*columns + x) = 0;
  289. }
  290. }
  291. }
  292. void COTSImageProcess::BDilateVertical3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  293. {
  294. WORD x, y;
  295. if (rows <= 2 || columns <= 2)return;
  296. // top line
  297. for (x = 0; x<columns; x++)
  298. {
  299. if (*(source + x) != 0)
  300. {
  301. *(target + x) = 0xff;
  302. }
  303. }
  304. // bottom line
  305. for (x = 0; x<columns; x++)
  306. {
  307. if (*(source + (DWORD)(rows - 1)*columns + x) != 0)
  308. {
  309. *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
  310. }
  311. }
  312. for (y = 1; y<rows - 1; y++)
  313. {
  314. for (x = 1; x<columns - 1; x++)
  315. {
  316. if (*(source + (DWORD)y*columns + x) != 0)
  317. {
  318. *(target + (DWORD)y*columns + x) = 0xff;
  319. *(target + (DWORD)(y - 1)*columns + x) = 255;
  320. *(target + (DWORD)(y + 1)*columns + x) = 255;
  321. }
  322. else *(target + (DWORD)y*columns + x) = 0;
  323. }
  324. }
  325. }
  326. void COTSImageProcess::BDilateLeft45Degree3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  327. {
  328. WORD x, y;
  329. if (rows <= 2 || columns <= 2)return;
  330. // top line
  331. for (x = 0; x<columns; x++)
  332. {
  333. if (*(source + x) != 0)
  334. {
  335. *(target + x) = 0xff;
  336. }
  337. }
  338. // bottom line
  339. for (x = 0; x<columns; x++)
  340. {
  341. if (*(source + (DWORD)(rows - 1)*columns + x) != 0)
  342. {
  343. *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
  344. }
  345. }
  346. // left line
  347. for (y = 0; y<rows; y++)
  348. {
  349. if (*(source + (DWORD)y*columns) != 0)
  350. {
  351. *(target + (DWORD)y*columns) = 0xff;
  352. }
  353. }
  354. // right line
  355. for (y = 0; y<rows; y++)
  356. {
  357. if (*(source + (DWORD)y*columns + columns - 1) != 0)
  358. {
  359. *(target + (DWORD)y*columns + columns - 1) = 0xff;
  360. }
  361. }
  362. for (y = 1; y<rows - 1; y++)
  363. {
  364. for (x = 1; x<columns - 1; x++)
  365. {
  366. if (*(source + (DWORD)y*columns + x) != 0)
  367. {
  368. *(target + (DWORD)y*columns + x) = 0xff;
  369. *(target + (DWORD)(y - 1)*columns + x - 1) = 255;
  370. *(target + (DWORD)(y + 1)*columns + x + 1) = 255;
  371. }
  372. else *(target + (DWORD)y*columns + x) = 0;
  373. }
  374. }
  375. }
  376. void COTSImageProcess::BDilateHorizontal3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  377. {
  378. WORD x, y;
  379. if (rows <= 2 || columns <= 2)return;
  380. // left line
  381. for (y = 0; y<rows; y++)
  382. {
  383. if (*(source + (DWORD)y*columns) != 0)
  384. {
  385. *(target + (DWORD)y*columns) = 0xff;
  386. }
  387. }
  388. // right line
  389. for (y = 0; y<rows; y++)
  390. {
  391. if (*(source + (DWORD)y*columns + columns - 1) != 0)
  392. {
  393. *(target + (DWORD)y*columns + columns - 1) = 0xff;
  394. }
  395. }
  396. for (y = 1; y<rows - 1; y++)
  397. {
  398. for (x = 1; x<columns - 1; x++)
  399. {
  400. if (*(source + (DWORD)y*columns + x) != 0)
  401. {
  402. *(target + (DWORD)y*columns + x) = 0xff;
  403. *(target + (DWORD)y*columns + x - 1) = 255;
  404. *(target + (DWORD)y*columns + x + 1) = 255;
  405. }
  406. else *(target + (DWORD)y*columns + x) = 0;
  407. }
  408. }
  409. }
  410. void COTSImageProcess::BDilateRight45Degree3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
  411. {
  412. WORD x, y;
  413. if (rows <= 2 || columns <= 2)return;
  414. // top line
  415. for (x = 0; x<columns; x++)
  416. {
  417. if (*(source + x) != 0)
  418. {
  419. *(target + x) = 0xff;
  420. }
  421. }
  422. // bottom line
  423. for (x = 0; x<columns; x++)
  424. {
  425. if (*(source + (DWORD)(rows - 1)*columns + x) != 0)
  426. {
  427. *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
  428. }
  429. }
  430. // left line
  431. for (y = 0; y<rows; y++)
  432. {
  433. if (*(source + (DWORD)y*columns) != 0)
  434. {
  435. *(target + (DWORD)y*columns) = 0xff;
  436. }
  437. }
  438. // right line
  439. for (y = 0; y<rows; y++)
  440. {
  441. if (*(source + (DWORD)y*columns + columns - 1) != 0)
  442. {
  443. *(target + (DWORD)y*columns + columns - 1) = 0xff;
  444. }
  445. }
  446. for (y = 1; y<rows - 1; y++)
  447. {
  448. for (x = 1; x<columns - 1; x++)
  449. {
  450. if (*(source + (DWORD)y*columns + x) != 0)
  451. {
  452. *(target + (DWORD)y*columns + x) = 0xff;
  453. *(target + (DWORD)(y - 1)*columns + x + 1) = 255;
  454. *(target + (DWORD)(y + 1)*columns + x - 1) = 255;
  455. }
  456. else *(target + (DWORD)y*columns + x) = 0;
  457. }
  458. }
  459. }
  460. void COTSImageProcess::BErode3(LPBYTE source, LPBYTE target, WORD wDegree, WORD rows, WORD columns)
  461. {
  462. WORD x, y, i, j, wcounts;
  463. if (rows == 1 || columns == 1)return;
  464. for (y = 1; y<rows - 1; y++)
  465. {
  466. for (x = 1; x<columns - 1; x++)
  467. {
  468. if (*(source + (DWORD)y*columns + x) == 0)
  469. {
  470. *(target + (DWORD)y*columns + x) = 0;
  471. continue;
  472. }
  473. wcounts = 0;
  474. for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
  475. {
  476. for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
  477. {
  478. if (*(source + (DWORD)i*columns + j) == 0)
  479. {
  480. wcounts++;
  481. }
  482. }
  483. }
  484. if (wcounts >= wDegree) *(target + (DWORD)y*columns + x) = 0;
  485. else *(target + (DWORD)y*columns + x) = 0xff;
  486. }
  487. }
  488. // top line
  489. for (x = 1; x<columns - (WORD)1; x++)
  490. {
  491. if (*(source + x) == 0)
  492. {
  493. *(target + x) = 0;
  494. continue;
  495. }
  496. wcounts = 0;
  497. for (i = 0; i <= 1; i++)
  498. {
  499. for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
  500. {
  501. if (*(source + (DWORD)i*columns + j) == 0) wcounts++;
  502. }
  503. }
  504. if (wcounts >= wDegree * 5 / 8) *(target + x) = 0;
  505. else *(target + x) = 0xff;
  506. }
  507. // bottom line
  508. for (x = 1; x<columns - 1; x++)
  509. {
  510. if (*(source + (DWORD)(rows - 1)*columns + x) == 0)
  511. {
  512. *(target + (DWORD)(rows - 1)*columns + x) = 0;
  513. continue;
  514. }
  515. wcounts = 0;
  516. for (i = (WORD)(rows - 2); i <= (WORD)(rows - 1); i++)
  517. {
  518. for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
  519. {
  520. if (*(source + (DWORD)i*columns + j) == 0) wcounts++;
  521. }
  522. }
  523. if (wcounts >= wDegree * 5 / 8) *(target + (DWORD)(rows - 1)*columns + x) = 0;
  524. else *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
  525. }
  526. // left line
  527. for (y = 1; y<rows - 1; y++)
  528. {
  529. if (*(source + (DWORD)y*columns) == 0)
  530. {
  531. *(target + (DWORD)y*columns) = 0;
  532. continue;
  533. }
  534. wcounts = 0;
  535. for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
  536. {
  537. for (j = 0; j <= 1; j++)
  538. {
  539. if (*(source + (DWORD)i*columns + j) == 0) wcounts++;
  540. }
  541. }
  542. if (wcounts >= wDegree * 5 / 8) *(target + (DWORD)y*columns) = 0;
  543. else *(target + (DWORD)y*columns) = 0xff;
  544. }
  545. // right line
  546. for (y = 1; y<rows - 1; y++)
  547. {
  548. if (*(source + (DWORD)y*columns + columns - 1) == 0)
  549. {
  550. *(target + (DWORD)y*columns + columns - 1) = 0;
  551. continue;
  552. }
  553. wcounts = 0;
  554. for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
  555. {
  556. for (j = (WORD)(columns - 2); j <= (WORD)(columns - 1); j++)
  557. {
  558. if (*(source + (DWORD)i*columns + j) == 0) wcounts++;
  559. }
  560. }
  561. if (wcounts >= wDegree * 5 / 8) *(target + (DWORD)y*columns + columns - 1) = 0;
  562. else *(target + (DWORD)y*columns + columns - 1) = 0xff;
  563. }
  564. return;
  565. }
  566. void COTSImageProcess::BDilate3(LPBYTE source, LPBYTE target, WORD wDegree, WORD rows, WORD columns)
  567. {
  568. WORD x, y, i, j, wcounts;
  569. for (y = 1; y<rows - 1; y++)
  570. {
  571. for (x = 1; x<columns - 1; x++)
  572. {
  573. if (*(source + (DWORD)y*columns + x) != 0)
  574. {
  575. *(target + (DWORD)y*columns + x) = 0xff;
  576. continue;
  577. }
  578. wcounts = 0;
  579. for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
  580. {
  581. for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
  582. {
  583. if (*(source + (DWORD)i*columns + j) != 0) wcounts++;
  584. }
  585. }
  586. if (wcounts >= wDegree) *(target + (DWORD)y*columns + x) = 0xff;
  587. else *(target + (DWORD)y*columns + x) = 0;
  588. }
  589. }
  590. // top line
  591. for (x = 1; x<columns - 1; x++)
  592. {
  593. if (*(source + x) != 0)
  594. {
  595. *(target + x) = 0xff;
  596. continue;
  597. }
  598. wcounts = 0;
  599. for (i = 0; i <= 1; i++)
  600. {
  601. for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
  602. {
  603. if (*(source + (DWORD)i*columns + j) != 0) wcounts++;
  604. }
  605. }
  606. if (wcounts >= wDegree * 5 / 8) // but does not mater, as we have border of 2 now
  607. {
  608. *(target + x) = 0xff;
  609. }
  610. else { *(target + x) = 0; }
  611. }
  612. // bottom line
  613. for (x = 1; x<columns - 1; x++)
  614. {
  615. if (*(source + (DWORD)(rows - 1)*columns + x) != 0)
  616. {
  617. *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
  618. continue;
  619. }
  620. wcounts = 0;
  621. for (i = (WORD)(rows - 2); i <= (WORD)(rows - 1); i++)
  622. {
  623. for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
  624. {
  625. if (*(source + (DWORD)i*columns + j) != 0) wcounts++;
  626. }
  627. }
  628. if (wcounts > wDegree * 5 / 8)
  629. {
  630. *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
  631. }
  632. else
  633. {
  634. *(target + (DWORD)(rows - 1)*columns + x) = 0;
  635. }
  636. }
  637. // left line
  638. for (y = 1; y<rows - 1; y++)
  639. {
  640. if (*(source + (DWORD)y*columns) != 0)
  641. {
  642. *(target + (DWORD)y*columns) = 0xff;
  643. continue;
  644. }
  645. wcounts = 0;
  646. for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
  647. {
  648. for (j = 0; j <= (WORD)1; j++)
  649. {
  650. if (*(source + (DWORD)i*columns + j) != 0) wcounts++;
  651. }
  652. }
  653. if (wcounts >= wDegree * 5 / 8)
  654. {
  655. *(target + (DWORD)y*columns) = 0xff;
  656. }
  657. else
  658. {
  659. *(target + (DWORD)y*columns) = 0;
  660. }
  661. }
  662. // right line
  663. for (y = 1; y<rows - 1; y++)
  664. {
  665. if (*(source + (DWORD)y*columns + columns - 1) != 0)
  666. {
  667. *(target + (DWORD)y*columns + columns - 1) = 0xff;
  668. continue;
  669. }
  670. wcounts = 0;
  671. for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
  672. {
  673. for (j = (WORD)(columns - 2); j <= (WORD)(columns - 1); j++)
  674. {
  675. if (*(source + (DWORD)i*columns + j) != 0) wcounts++;
  676. }
  677. }
  678. if (wcounts >= wDegree * 5 / 8)
  679. {
  680. *(target + (DWORD)y*columns + columns - 1) = 0xff;
  681. }
  682. else
  683. {
  684. *(target + (DWORD)y*columns + columns - 1) = 0;
  685. }
  686. }
  687. // four cornor points treated separately here
  688. // top-left
  689. if (*(source) != 0)
  690. {
  691. *target = 0xff;
  692. }
  693. else
  694. {
  695. wcounts = 0;
  696. if (*(source + 1) != 0) wcounts++;
  697. if (*(source + columns) != 0) wcounts++;
  698. if (*(source + columns + 1) != 0) wcounts++;
  699. // if (wcounts >= wDegree*3/8) // this is a bug here - interger division
  700. if (wcounts * 8 >= wDegree * 3)
  701. {
  702. *target = 0xff;
  703. }
  704. else
  705. {
  706. *target = 0;
  707. }
  708. }
  709. //top-right
  710. if (*(source + columns - 1) != 0)
  711. {
  712. *(target + columns - 1) = 0xff;
  713. }
  714. else
  715. {
  716. wcounts = 0;
  717. if (*(source + columns - 2) != 0) wcounts++;
  718. if (*(source + columns * 2 - 1) != 0) wcounts++;
  719. if (*(source + columns * 2 - 2) != 0) wcounts++;
  720. // if (wcounts >= wDegree*3/8) // this is a bug here - interger division
  721. if (wcounts * 8 >= wDegree * 3)
  722. {
  723. *(target + columns - 1) = 0xff;
  724. }
  725. else
  726. {
  727. *(target + columns - 1) = 0;
  728. }
  729. }
  730. //bottom-left
  731. if (*(source + (DWORD)columns * (rows - 1)) != 0)
  732. {
  733. *(target + (DWORD)columns * (rows - 1)) = 0xff;
  734. }
  735. else
  736. {
  737. wcounts = 0;
  738. if (*(source + (DWORD)columns * (rows - 1) + 1) != 0) wcounts++;
  739. if (*(source + (DWORD)columns * (rows - 2)) != 0) wcounts++;
  740. if (*(source + (DWORD)columns * (rows - 2) + 1) != 0) wcounts++;
  741. // if (wcounts >= wDegree*3/8) // this is a bug here - interger division
  742. if (wcounts * 8 >= wDegree * 3)
  743. {
  744. *(target + (DWORD)columns * (rows - 1)) = 0xff;
  745. }
  746. else
  747. {
  748. *(target + (DWORD)columns * (rows - 1)) = 0;
  749. }
  750. }
  751. //bottom-right
  752. if (*(source + (DWORD)columns * rows - 1) != 0)
  753. {
  754. *(target + (DWORD)columns * rows - 1) = 0xff;
  755. }
  756. else
  757. {
  758. wcounts = 0;
  759. if (*(source + (DWORD)columns * rows - 2) != 0) wcounts++;
  760. if (*(source + (DWORD)columns * (rows - 1) - 2) != 0) wcounts++;
  761. if (*(source + (DWORD)columns * (rows - 1) - 1) != 0) wcounts++;
  762. // if (wcounts >= wDegree*3/8) // this is a bug here - interger division
  763. if (wcounts * 8 >= wDegree * 3)
  764. {
  765. *(target + (DWORD)columns * rows - 1) = 0xff;
  766. }
  767. else
  768. {
  769. *(target + (DWORD)columns * rows - 1) = 0;
  770. }
  771. }
  772. return;
  773. }
  774. // ReZoom the picture with re-magnification
  775. BOOL COTSImageProcess::ReZoom(CString InPutPath, CString OutPutPath)
  776. {
  777. Mat cvSrcImg;
  778. string strInputPath;
  779. strInputPath = CStringA(InPutPath);
  780. // Pictures loop in folder
  781. std::vector<cv::String> ImageFolder;
  782. cv::glob(strInputPath, ImageFolder);
  783. if (ImageFolder.size() == 0)
  784. {
  785. return FALSE;
  786. }
  787. for (unsigned int nImgNum = 0; nImgNum < ImageFolder.size(); ++nImgNum) {
  788. cvSrcImg = cv::imread(ImageFolder[nImgNum], CV_LOAD_IMAGE_GRAYSCALE);
  789. // Image convolution operation
  790. //// convolution kernel
  791. float kernel[] = { -1, -1 , -1, -1 , 0, -1, -1 , -1 , -1 };
  792. cv::Mat ker = cv::Mat(nImage_Size, nImage_Size, CV_32F, &kernel);
  793. cv::Mat cvDstImg = cv::Mat(cvSrcImg.size(), cvSrcImg.type());
  794. // anchor of the kernel
  795. cv::Point anchor(-1, -1);
  796. cv::filter2D(cvSrcImg, cvDstImg, CV_32F, ker, anchor, delta, cv::THRESH_TRUNC);
  797. // Maximum Pixel Value
  798. cvDstImg = abs(cvDstImg);
  799. double minVal, maxVal;
  800. minMaxLoc(cvDstImg, &minVal, &maxVal);
  801. // Grayscale image
  802. int nReduce;
  803. Mat onesImg = Mat::ones(cvDstImg.rows, cvDstImg.cols, CV_32F) * (int)minVal;
  804. absdiff(cvDstImg, onesImg, cvDstImg);
  805. nReduce = (int)maxVal - minVal;
  806. cvDstImg = cvDstImg * nBlackColor / nReduce;
  807. // Output image convert data to int
  808. cvDstImg.convertTo(cvDstImg, CV_8U);
  809. // Process the picture to 128 pixels
  810. resize(cvDstImg, cvDstImg, Size(nPictureSize, nPictureSize));
  811. threshold(cvDstImg, cvDstImg, nProcessParam, nBlackColor, CV_THRESH_BINARY);
  812. string strOutPutPath;
  813. strOutPutPath = CStringA(OutPutPath);
  814. imwrite(strOutPutPath , cvDstImg);
  815. }
  816. return TRUE;
  817. }
  818. BOOL COTSImageProcess::CalcuParticleImagePropertes(COTSParticlePtr a_pOTSPart, double a_PixelSize)
  819. {
  820. //--------- convert this particle data to image data,construct an image only with this particle.------
  821. const int nExpand_Size = 3;
  822. const int nWhiteColor = 0;
  823. const int nThickness = 1;
  824. // lineType Type of the line
  825. const int nLineType = 8;
  826. // get rectangle of the particle
  827. CRect rect = a_pOTSPart->GetParticleRect();
  828. if (a_pOTSPart->GetArea() < 80 * 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.
  829. {
  830. double w = 0, h = 0;
  831. w = (double)rect.Width()*a_PixelSize;
  832. h = (double)rect.Height()*a_PixelSize;
  833. a_pOTSPart->SetDMax(MAX(w, h));
  834. a_pOTSPart->SetDMin(MIN(w, h));
  835. a_pOTSPart->SetDMean((w + h) / 2);
  836. a_pOTSPart->SetFeretDiameter((w + h) / 2);
  837. a_pOTSPart->SetDElong(MAX(w, h));
  838. a_pOTSPart->SetPerimeter((w+h)*2);
  839. a_pOTSPart->SetDPerp(MIN(w, h));
  840. a_pOTSPart->SetDInscr(MIN(w, h));
  841. return true;
  842. }
  843. // calculate the particle image data size, expand 3 pixel at the edge
  844. Mat particleImage = Mat::zeros(rect.Height() + nExpand_Size , rect.Width() + nExpand_Size , CV_8U);
  845. // get the segment list
  846. COTSSegmentsList listSegment = a_pOTSPart->GetFeature()->GetSegmentsList();
  847. for (auto pSegment : listSegment)
  848. {
  849. int nStart = pSegment->GetStart() - rect.left + nExpand_Size;
  850. int nEnd = pSegment->GetStart() + pSegment->GetLength() - rect.left - 1 + nExpand_Size;
  851. int nHeight = pSegment->GetHeight() - rect.top + nExpand_Size;
  852. line(particleImage, Point(nStart, nHeight), Point(nEnd, nHeight), Scalar(nBlackColor), nThickness, nLineType);
  853. }
  854. //--------abstract the contour of the particle.
  855. Mat cvcopyImg;
  856. medianBlur(particleImage, cvcopyImg, 7);//smooth the edge
  857. Mat cvContourImg = Mat::zeros(rect.Height() + nExpand_Size, rect.Width() + nExpand_Size, CV_8U);
  858. vector<vector<Point>>contours;
  859. Canny(cvcopyImg, cvcopyImg, 20, 20 * 2, 3);
  860. findContours(cvcopyImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
  861. 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.
  862. {
  863. double w = 0, h = 0;
  864. w = (double)rect.Width()*a_PixelSize;
  865. h = (double)rect.Height()*a_PixelSize;
  866. a_pOTSPart->SetDMax(MAX(w, h));
  867. a_pOTSPart->SetDMin(MIN(w, h));
  868. a_pOTSPart->SetDMean((w + h) / 2);
  869. a_pOTSPart->SetFeretDiameter((w + h) / 2);
  870. a_pOTSPart->SetDElong(MAX(w, h));
  871. a_pOTSPart->SetPerimeter((w + h) * 2);
  872. a_pOTSPart->SetDPerp(MIN(w, h));
  873. a_pOTSPart->SetDInscr(MIN(w, h));
  874. return true;
  875. }
  876. int imaxcontour = 0, imax = 0;
  877. for (unsigned int i = 0; i < contours.size(); i++) {
  878. int itmp = contourArea(contours[i]);
  879. if (imaxcontour < itmp) {
  880. imax = i;
  881. imaxcontour = itmp;
  882. }
  883. }
  884. vector<Point > listEdge = contours[imax];
  885. vector<vector<Point>>Outcontours;
  886. Outcontours.push_back(listEdge);
  887. //---------calculate the minimium rectangle
  888. auto rRect = cv::minAreaRect(listEdge);
  889. Point2f p[4];
  890. rRect.points(p);
  891. int D_MIN =getDistance(p[0], p[1]);
  892. int D_MinRecLen = 0;//minareaRect's length(the lenger side).
  893. for (int j = 0; j <= 2; j++)
  894. {
  895. //line(cvContourImg, p[j], p[(j + 1) % 4], Scalar(100, 100, 0), 2);
  896. int d = getDistance(p[j], p[j + 1]);
  897. if (d < D_MIN)
  898. {
  899. D_MIN = d;
  900. }
  901. if (d > D_MinRecLen)
  902. {
  903. D_MinRecLen = d;
  904. }
  905. }
  906. a_pOTSPart->SetDMin(D_MIN*a_PixelSize);
  907. a_pOTSPart->SetOrientation(rRect.angle);
  908. //----------calculate the perimeter
  909. double d = arcLength(listEdge, true);
  910. a_pOTSPart->SetPerimeter(d*a_PixelSize);
  911. //-----------calculate the Max Diameter. Find the min enclosing circle first ,then find the two farthest circle connected point.
  912. Point2f center; float radius;
  913. minEnclosingCircle(listEdge, center, radius);
  914. //circle(cvContourImg, center, radius, Scalar(100), 2);
  915. std::vector <Point> outContour = listEdge;
  916. std::vector <Point> rst;
  917. for (unsigned int k = 0; k < outContour.size(); k++)
  918. {
  919. Point p = outContour[k];
  920. double d = sqrt(pow((p.x - center.x), 2) + pow((p.y - center.y), 2));
  921. if (fabs(d - radius) < 0.01)
  922. {
  923. rst.push_back(p);
  924. }
  925. }
  926. double D_MAX = 0;
  927. Point lineDmax[2];
  928. for (unsigned int m = 0; m < rst.size(); m++)
  929. {
  930. Point p = rst[m];
  931. for (unsigned int n = m + 1; n < rst.size(); n++)
  932. {
  933. Point p1 = rst[n];
  934. double d = sqrt(powf((p.x - p1.x), 2) + powf((p.y - p1.y), 2));
  935. if (d > D_MAX)
  936. {
  937. D_MAX = d;
  938. lineDmax[0] = p;
  939. lineDmax[1] = p1;
  940. }
  941. }
  942. }
  943. a_pOTSPart->SetDMax(D_MAX*a_PixelSize);
  944. //--------calculate the D_PERP property using the D_MAX's two endpoints.
  945. std::vector<Point> curve1;
  946. std::vector<Point> curve2;
  947. for (unsigned int i = 0; i < outContour.size(); i++)
  948. {
  949. Point pt = outContour[i];
  950. bool start = false;
  951. int clockwise = Side(lineDmax[0], lineDmax[1], pt);// devide these points into two group ,separate into the two sides.
  952. if (clockwise > 0)
  953. {
  954. curve1.push_back(pt);
  955. }
  956. else
  957. {
  958. curve2.push_back(pt);
  959. }
  960. }
  961. double d_perp1 = 0, d_perp2 = 0;
  962. for (unsigned int i = 0; i < curve1.size(); i++)
  963. {
  964. double d = getDist_P2L(curve1[i], lineDmax[0], lineDmax[1]);
  965. if (d > d_perp1)
  966. {
  967. d_perp1 = d;
  968. }
  969. }
  970. for (unsigned int i = 0; i < curve2.size(); i++)
  971. {
  972. double d = getDist_P2L(curve2[i], lineDmax[0], lineDmax[1]);
  973. if (d > d_perp2)
  974. {
  975. d_perp2 = d;
  976. }
  977. }
  978. a_pOTSPart->SetDPerp((d_perp1 + d_perp2)*a_PixelSize);
  979. //----------find the diameter of max inscribed circle
  980. int r;
  981. Point inscribeCirclecenter;
  982. FindInnerCircleInContour(outContour, inscribeCirclecenter, r);
  983. //circle(cvContourImg, inscribeCirclecenter, r, Scalar(200));
  984. a_pOTSPart->SetDInscr(r * 2 * a_PixelSize);
  985. //---------------calculate the image other caracater: length/width realArea/minRectangeArea etc. we can use these propertes to do forward process.
  986. double minRectArea = D_MIN * D_MinRecLen*a_PixelSize*a_PixelSize;//最小外接矩形面积
  987. double fillRatio = a_pOTSPart->GetArea() / minRectArea;//实际面积与最小外接矩形面积比,that's the fill rate.
  988. double lengthWidthRatio;
  989. lengthWidthRatio = (double)D_MinRecLen / D_MIN;//长宽比
  990. //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.
  991. bool isStripShape = false;
  992. double curveLength = 0;
  993. double D_MEAN=0;
  994. Moments mu;
  995. mu = moments(listEdge, false);
  996. int nx = mu.m10 / mu.m00;
  997. int ny = mu.m01 / mu.m00;
  998. //circle(cvcopyImg, Point(nx, ny), 1, (255), 1);
  999. Point ptCenter = Point((int)nx, (int)ny);
  1000. if (pointPolygonTest(outContour, ptCenter, false) != 1)// the center point doesn't contain in the contour, we think it as curve shape.
  1001. {
  1002. isStripShape = true;
  1003. }
  1004. /*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.
  1005. {
  1006. isStripShape = true;
  1007. }*/
  1008. 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.
  1009. {
  1010. isStripShape = true;
  1011. }
  1012. if (isStripShape)
  1013. {
  1014. 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.
  1015. if (curveLength < D_MAX)
  1016. {
  1017. curveLength = D_MAX;
  1018. }
  1019. if (curveLength < MIN_DOUBLE_VALUE || a_pOTSPart->GetArea()<MIN_DOUBLE_VALUE)
  1020. {
  1021. D_MEAN = 0;
  1022. }
  1023. else
  1024. {
  1025. D_MEAN = a_pOTSPart->GetArea() / curveLength;
  1026. }
  1027. a_pOTSPart->SetDMean(D_MEAN*a_PixelSize);
  1028. a_pOTSPart->SetFeretDiameter(D_MEAN*a_PixelSize);
  1029. a_pOTSPart->SetDElong (curveLength*a_PixelSize);
  1030. }
  1031. else//it's a ball shape particle
  1032. {
  1033. curveLength = D_MAX;
  1034. double ftd = 0, maxD = 0, minD = 0, dratio = 0;
  1035. GetParticleAverageChord(outContour, a_PixelSize, ftd);
  1036. a_pOTSPart->SetDMean(ftd);
  1037. a_pOTSPart->SetFeretDiameter(ftd);
  1038. a_pOTSPart->SetDElong(curveLength*a_PixelSize);
  1039. }
  1040. return true;
  1041. }
  1042. }