MfcLabel.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. #pragma once
  2. #include <math.h>
  3. #include <string>
  4. #include <vector>
  5. #include <fstream>
  6. //�����MFC�ڵĻ�
  7. #ifdef AFX_WIN
  8. #include <afx.h>
  9. #include <afxwin.h>
  10. #elif OPENCV_USE
  11. #include <opencv2/opencv.hpp>
  12. #elif QT_USE
  13. #include <QtCore>
  14. #endif
  15. namespace MfcLabel {
  16. class Point
  17. {
  18. public:
  19. Point()
  20. {
  21. x = 0;
  22. y = 0;
  23. }
  24. Point(int x, int y)
  25. {
  26. this->x = x;
  27. this->y = y;
  28. }
  29. void SetZero()
  30. {
  31. x = 0;
  32. y = 0;
  33. }
  34. void offset(int x, int y)
  35. {
  36. this->x += x;
  37. this->y += y;
  38. }
  39. void SetPoint(int x, int y)
  40. {
  41. this->x = x;
  42. this->y = y;
  43. }
  44. bool operator==(const Point refer)
  45. {
  46. if (this->x == refer.x && this->y == refer.y)
  47. return true;
  48. else
  49. return false;
  50. }
  51. public:
  52. int x;
  53. int y;
  54. };
  55. class fPoint
  56. {
  57. public:
  58. fPoint(float fX, float fY)
  59. {
  60. x = fX;
  61. y = fY;
  62. }
  63. fPoint()
  64. {
  65. x = 0;
  66. y = 0;
  67. }
  68. void SetZero()
  69. {
  70. x = 0;
  71. y = 0;
  72. }
  73. void offset(float x, float y)
  74. {
  75. this->x += x;
  76. this->y += y;
  77. }
  78. void SetPoint(float x, float y)
  79. {
  80. this->x = x;
  81. this->y = y;
  82. }
  83. bool operator==(const fPoint refer)
  84. {
  85. if (fabs(this->x - refer.x) < DBL_EPSILON
  86. && fabs(this->y - refer.y) < DBL_EPSILON)
  87. return true;
  88. else
  89. return false;
  90. }
  91. #ifdef AFX_WIN
  92. CPoint TurnFloat2Int()
  93. {
  94. CPoint tmp;
  95. tmp.x = (LONG)x;
  96. tmp.y = (LONG)y;
  97. return tmp;
  98. }
  99. void operator=(POINT &Copy)
  100. {
  101. x = Copy.x;
  102. y = Copy.y;
  103. }
  104. #elif QT_USE
  105. QPoint TurnFloatToInt()
  106. {
  107. QPoint temp;
  108. temp.setX(int(x));
  109. temp.setY(int(y));
  110. return temp;
  111. }
  112. fPoint(const QPoint& point)
  113. {
  114. x = point.x();
  115. y = point.y();
  116. }
  117. void operator=(QPoint Copy)
  118. {
  119. x = Copy.rx();
  120. y = Copy.ry();
  121. }
  122. void operator()(QPoint Copy)
  123. {
  124. x = Copy.x();
  125. y = Copy.y();
  126. }
  127. #endif // AFX_WIN
  128. double GetVectorProduct(fPoint B);
  129. fPoint VectorSub(fPoint B);
  130. float x;
  131. float y;
  132. };
  133. class fRect
  134. {
  135. public:
  136. float Left;
  137. float Right;
  138. float Top;
  139. float Bottom;
  140. fRect()
  141. {
  142. this->Left = 0;
  143. this->Right = 0;
  144. this->Top = 0;
  145. this->Bottom = 0;
  146. }
  147. fRect(float Left, float Right, float Top, float Bottom)
  148. {
  149. this->Left = Left;
  150. this->Right = Right;
  151. this->Top = Top;
  152. this->Bottom = Bottom;
  153. }
  154. float Width()
  155. {
  156. return fabs(Right - Left);
  157. }
  158. float Height()
  159. {
  160. return fabs(Bottom - Top);
  161. }
  162. void SetRectEmpty()
  163. {
  164. Left = 0;
  165. Right = 0;
  166. Top = 0;
  167. Bottom = 0;
  168. }
  169. bool JudgePtInRectBoxOrNot(fPoint TestPt);
  170. #ifdef AFX_WIN
  171. CRect TrunFloat2Int()
  172. {
  173. CRect Int_Current_image_rect;
  174. Int_Current_image_rect.top = (int)Top;
  175. Int_Current_image_rect.left = (int)Left;
  176. Int_Current_image_rect.bottom = (int)Bottom;
  177. Int_Current_image_rect.right = (int)Right;
  178. return Int_Current_image_rect;
  179. }
  180. void operator=(CRect& Copy)
  181. {
  182. Left = (float)Copy.left;
  183. Top = (float)Copy.top;
  184. Right = (float)Copy.right;
  185. Bottom = (float)Copy.bottom;
  186. }
  187. #elif QT_USE
  188. fRect(const QRectF& Rect)
  189. {
  190. this->Left = Rect.left();
  191. this->Right = Rect.right();
  192. this->Top = Rect.top();
  193. this->Bottom = Rect.bottom();
  194. }
  195. QRect TrunFloat2Int()
  196. {
  197. QRect temp;
  198. temp.setLeft(int(Left));
  199. temp.setTop(int(Top));
  200. temp.setRight(int(Right)-1);
  201. temp.setBottom(int(Bottom)-1);
  202. return temp;
  203. }
  204. QRectF TrunFloat2Float()
  205. {
  206. QRectF temp;
  207. temp.setLeft(int(Left));
  208. temp.setTop(int(Top));
  209. temp.setRight(int(Right));
  210. temp.setBottom(int(Bottom));
  211. return temp;
  212. }
  213. void operator=(QRect Copy)
  214. {
  215. Left = Copy.left();
  216. Top = Copy.top();
  217. Right = Copy.right() -1 ;
  218. Bottom = Copy.bottom()-1;
  219. }
  220. void operator=(QRectF Copy)
  221. {
  222. Left = Copy.left();
  223. Top = Copy.top();
  224. Right = Copy.right()-1;
  225. Bottom = Copy.bottom()-1;
  226. }
  227. #endif // AFX_WIN
  228. void offset(float x, float y)
  229. {
  230. Left = Left + x;
  231. Right = Right + x;
  232. Top = Top + y;
  233. Bottom = Bottom + y;
  234. }
  235. void Scale(float dbScale)
  236. {
  237. Left = Left * dbScale;
  238. Top = Top * dbScale;
  239. Right = Right * dbScale;
  240. Bottom = Bottom * dbScale;
  241. //Right = Left + (Right - Left)* dbScale;
  242. //Bottom = Top + (Bottom - Top) * dbScale;
  243. }
  244. //���ص����ཻ�ľ���
  245. fRect IntersectRect(fRect& InferRect)
  246. {
  247. fRect frcRes;
  248. if (!JudgeInterOtherRect(InferRect))
  249. {
  250. frcRes.Top = 0;
  251. frcRes.Bottom = 0;
  252. frcRes.Left = 0;
  253. frcRes.Right = 0;
  254. return frcRes;
  255. }
  256. else
  257. {
  258. if (Left <= InferRect.Left)
  259. frcRes.Left = InferRect.Left;
  260. else
  261. frcRes.Left = Left;
  262. if (Right <= InferRect.Right)
  263. frcRes.Right = Right;
  264. else
  265. frcRes.Right = InferRect.Right;
  266. if (Top <= InferRect.Top)
  267. frcRes.Top = InferRect.Top;
  268. else
  269. frcRes.Top = Top;
  270. if (Bottom <= InferRect.Bottom)
  271. frcRes.Bottom = Bottom;
  272. else
  273. frcRes.Bottom = InferRect.Bottom;
  274. }
  275. return frcRes;
  276. }
  277. bool JudgeInterOtherRect(fRect& InferRect)
  278. {
  279. if (Right<InferRect.Left
  280. || Left >InferRect.Right
  281. || Top > InferRect.Bottom
  282. || Bottom < InferRect.Top)
  283. {
  284. return false;
  285. }
  286. return true;
  287. }
  288. };
  289. class Rect
  290. {
  291. public:
  292. Rect()
  293. {
  294. Left = 0;
  295. Right = 0;
  296. Top = 0;
  297. Bottom = 0;
  298. Width = 0;
  299. Height = 0;
  300. }
  301. Rect(int Left, int Top, int Width, int Height)
  302. {
  303. this->Left = Left;
  304. this->Right = Left + Width;
  305. this->Top = Top;
  306. this->Bottom = Top + Height;
  307. this->Width = Width;
  308. this->Height = Height;
  309. }
  310. void SetRectEmpty()
  311. {
  312. Left = 0;
  313. Right = 0;
  314. Top = 0;
  315. Bottom = 0;
  316. }
  317. int GetWidth()
  318. {
  319. return Right - Left;
  320. }
  321. int GetHeight()
  322. {
  323. return Bottom - Top;
  324. }
  325. void SetWidth(int nWidth)
  326. {
  327. Right = Left + nWidth;
  328. Width = nWidth;
  329. }
  330. void SetHeight(int nHeight)
  331. {
  332. Bottom = Top + nHeight;
  333. Height = nHeight;
  334. }
  335. void operator=(fRect Copy)
  336. {
  337. Left = (int)Copy.Left;
  338. Top = (int)Copy.Top;
  339. Right = (int)Copy.Right;
  340. Bottom = (int)Copy.Bottom;
  341. Width = (int)Copy.Width();
  342. Height = (int)Copy.Height();
  343. }
  344. public:
  345. int Left;
  346. int Right;
  347. int Top;
  348. int Bottom;
  349. int Width;
  350. int Height;
  351. };
  352. //�ؼ���
  353. class KeyPoint
  354. {
  355. //�ؼ��㶨λ
  356. fPoint KPoint;
  357. //�õ�����
  358. std::string Name;
  359. //�÷�
  360. float Score;
  361. //�Ƕ�
  362. float angle;
  363. //�뾶
  364. float radius;
  365. };
  366. //��״��ṹ
  367. class fRing
  368. {
  369. public:
  370. fPoint * pPoints;
  371. int m_nPointsCount;
  372. fRect RectBox;
  373. //Ĭ��Ϊ�պϵ�
  374. bool isClosed;
  375. int size()
  376. {
  377. return m_nPointsCount;
  378. }
  379. fPoint* At(int index)
  380. {
  381. if (pPoints == NULL)
  382. {
  383. return NULL;
  384. }
  385. return &pPoints[index];
  386. }
  387. ~fRing()
  388. {
  389. delete[] pPoints;
  390. pPoints = NULL;
  391. m_nPointsCount = 0;
  392. }
  393. fRing()
  394. {
  395. pPoints = NULL;
  396. m_nPointsCount = 0;
  397. isClosed = true;
  398. }
  399. //ʹ����������صĿ�������,Ҳ��ֱ�Ӷ��Ѿ�������Ring�ṹ��ֵ
  400. fRing operator=(const fRing& another)
  401. {
  402. //���֮ǰRing֮ǰ�������ݵĻ�������ɾ��
  403. if (pPoints == NULL)
  404. {
  405. delete[] pPoints;
  406. }
  407. m_nPointsCount = another.m_nPointsCount;
  408. pPoints = new fPoint[m_nPointsCount];
  409. isClosed = another.isClosed;
  410. RectBox.Left = 65535;
  411. RectBox.Top = 65535;
  412. RectBox.Bottom = 0;
  413. RectBox.Right = 0;
  414. for (int i = 0; i < m_nPointsCount; i++)
  415. {
  416. pPoints[i] = another.pPoints[i];
  417. RectBox.Left = fmin(pPoints[i].x, RectBox.Left);
  418. RectBox.Top = fmin(pPoints[i].y, RectBox.Top);
  419. RectBox.Right = fmax(pPoints[i].x, RectBox.Right);
  420. RectBox.Bottom = fmax(pPoints[i].y, RectBox.Bottom);
  421. }
  422. return *this;
  423. }
  424. //�������캯��,vector��Push_back�������ø÷���,������Ч��ֹ��ַ����
  425. fRing(const fRing& src)
  426. {
  427. m_nPointsCount = src.m_nPointsCount;
  428. pPoints = new fPoint[m_nPointsCount];
  429. for (int i = 0; i < m_nPointsCount; i++)
  430. {
  431. pPoints[i] = src.pPoints[i];
  432. }
  433. isClosed = src.isClosed;
  434. RectBox = src.RectBox;
  435. //edge_Box.fLeft = 65535;
  436. //edge_Box.fTop = 65535;
  437. //edge_Box.fBottom = 0;
  438. //edge_Box.fRight = 0;
  439. }
  440. void ComputeBox()
  441. {
  442. RectBox.Left = 65535;
  443. RectBox.Top = 65535;
  444. RectBox.Bottom = 0;
  445. RectBox.Right = 0;
  446. for (int i = 0; i < m_nPointsCount; i++)
  447. {
  448. RectBox.Left = fmin(pPoints[i].x, RectBox.Left);
  449. RectBox.Top = fmin(pPoints[i].y, RectBox.Top);
  450. RectBox.Right = fmax(pPoints[i].x, RectBox.Right);
  451. RectBox.Bottom = fmax(pPoints[i].y, RectBox.Bottom);
  452. }
  453. }
  454. float Area();
  455. bool JudgePtInRingOrNot(fPoint TestPt);
  456. bool JudgeRingIntersectOrNot(fRing& TestRing);
  457. //���������溯����
  458. bool JudgeTwoSegmentIntersect(fPoint& FirstLineStartPt, fPoint& FirstLineEndPt, fPoint& SecondLineStartPt, fPoint& SecondLineEndPt);
  459. fRect GetRectWithTwoPoint(fPoint& Pt1, fPoint& Pt2);
  460. bool JudgeRectBoxIntersectOrNot(fRect& Rect1, fRect& Rect2);
  461. fPoint GetTwoSegmentIntersectPt(fPoint FstStartPt, fPoint FstEndPt, fPoint SecStartPt, fPoint SecEndPt);
  462. fRing GetTwoRingIntersectRing(fRing& FstRing, fRing& SecRing);
  463. //�ж�a����b���˳ʱ�뷽������ʱ�뷽��
  464. bool PointCmp(const fPoint &a, const fPoint &b, const fPoint &center);
  465. //��ʱ��㼯����
  466. void ClockwiseSortPoints(std::vector<fPoint>& vPoints);
  467. };
  468. class fPolygon
  469. {
  470. public:
  471. fRing Outer;
  472. std::vector<fRing> Inner;
  473. std::string name;
  474. float score;
  475. int iInnerCount;
  476. int InnerCount()
  477. {
  478. return iInnerCount;
  479. }
  480. fPolygon()
  481. {
  482. iInnerCount = 0;
  483. score = 0;
  484. name = "unKnown";
  485. }
  486. fPolygon(const fPolygon& src)
  487. {
  488. iInnerCount = src.iInnerCount;
  489. Outer = src.Outer;
  490. Inner.resize(src.iInnerCount);
  491. name = src.name;
  492. score = src.score;
  493. for (int i = 0; i < src.iInnerCount; i++)
  494. {
  495. Inner[i] = src.Inner[i];
  496. }
  497. }
  498. ~fPolygon()
  499. {
  500. Inner.clear();
  501. iInnerCount = 0;
  502. }
  503. };
  504. class mfcLabel
  505. {
  506. public:
  507. std::string modelName = "ModeUnKnown";
  508. float score = 0;
  509. std::string filePath;
  510. std::vector<fPolygon> polygons;
  511. int iPolyCount;
  512. void Destory()
  513. {
  514. polygons.clear();
  515. iPolyCount = 0;
  516. }
  517. ~mfcLabel()
  518. {
  519. polygons.clear();
  520. iPolyCount = 0;
  521. }
  522. /***************************����opencv������������Ľ���****************************************/
  523. #ifdef OPENCV_USE
  524. //�ú�����Ӧ����cv::findcoutours �в����趨ΪCV_RETR_CCOMP�ò�����ֻ�и��ӹ�ϵ
  525. void AnalysisCvCoutour(std::vector<std::vector<cv::Point>>* Coutours //���ݱ���
  526. , std::vector<cv::Vec4i>* Coutour_hier
  527. , int heircount);//���ݼ�ϵ
  528. bool ReceiveCvCoutour(std::vector<cv::Point>* coutour, bool Notinner, MfcLabel::fRing* ring);
  529. #endif
  530. bool Empty();
  531. #ifdef AFX_WIN
  532. BOOL ShowLabelInCDC(CDC* pDC);
  533. BOOL Serialize_S();
  534. BOOL Serialize_L();
  535. //���л������л��ַ���
  536. void Serialize_StringS(CArchive& ar, std::string& szString);
  537. void Serialize_StringL(CArchive& ar, std::string& szString);
  538. //���л������л�Point������Ring
  539. void Serialize_fPointArrayS(CArchive& ar, fRing* pfsend);
  540. void Serialize_fPointArrayL(CArchive& ar, fRing* pfreceive);
  541. //���л������л�Ring�ļ���polygon
  542. void Serialize_fpolygonS(CArchive& ar, fPolygon* pfsend);
  543. void Serialize_fpolygonL(CArchive& ar, fPolygon* pfreceive);
  544. /***************************����opencv������������Ľ���****************************************/
  545. #endif // AFX_WIN
  546. };
  547. }