Identifier.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Identifier.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "stdafx.h"
  4. #include "Identifier.h"
  5. #include "Util.h"
  6. #include "PageMatcher.h"
  7. #include "ResultReader.h"
  8. #include "../lib/myqr/include/GetQRCode.h"
  9. namespace identify{
  10. IDENTIFIER_API int PraseQRCode_Normal2(IplImage* img, std::string & resultString)
  11. {
  12. return PraseQRCode_Normal(img, resultString);
  13. }
  14. // 这是已导出类的构造函数。
  15. // 有关类定义的信息,请参阅 Identifier.h
  16. CIdentifier::CIdentifier()
  17. {
  18. m_bUseQr = false;
  19. m_isMuBanLoaded = false;
  20. }
  21. void CIdentifier::SetUseQr(bool bUseQr, const std::string&strQr)
  22. {
  23. m_bUseQr = bUseQr;
  24. m_strQr = strQr;
  25. }
  26. int CIdentifier::Identify2(const IplImage* img, OMR_RESULT* out_result)
  27. {
  28. int ret;
  29. MATCH_REUSLT match_result;
  30. ret = pm.Identify(img, match_result);
  31. if (ret != IDF_SUCCESS){
  32. OMR_RESULT * r = (OMR_RESULT*)out_result;
  33. r->identified = FALSE;
  34. return ret;
  35. }
  36. int page_index = match_result.schema_index;
  37. cv::Mat transform(2, 3, CV_64F, match_result.transfrom);
  38. double scale = match_result.scale;
  39. ret = rr.ReadResult(img, page_index, transform, scale, match_result.dir, out_result);
  40. if (ret == IDF_SUCCESS){
  41. OMR_RESULT * r = (OMR_RESULT*)out_result;
  42. r->identified = TRUE;
  43. return ret;
  44. }
  45. return ret;
  46. }
  47. CIdentifier::~CIdentifier(void)
  48. {
  49. }
  50. void CIdentifier::LoadMuban2(const boost::shared_ptr<const ISCH_Schema>& schema)
  51. {
  52. m_schema = schema;
  53. pm.SetSchemaPages(schema);
  54. rr.SetSchemaPages(schema);
  55. m_isMuBanLoaded = true;
  56. }
  57. int CIdentifier::GetMubanPageCount()
  58. {
  59. return m_schema == NULL ? 0 : m_schema->size();
  60. }
  61. int CIdentifier::Identify3(const IplImage* img, int schema_index, const std::vector<Point2f>& muban, const std::vector<Point2f>& shijuan, OMR_RESULT* out_result)
  62. {
  63. if (muban.size() != 3 || shijuan.size() != 3)return IDF_FAILURE;
  64. MATCH_REUSLT match_result;
  65. {
  66. cv::Mat transform = getAffineTransform(muban, shijuan);
  67. Point2f src[3] = { Point2f(0, 0), Point2f(1000, 0), Point2f(0, 1000) };
  68. Point2f dst[3] = { warpAffinePoint<Point2f, Point2f>(src[0], &transform), warpAffinePoint<Point2f, Point2f>(src[1], &transform), warpAffinePoint<Point2f, Point2f>(src[2], &transform) };
  69. double d_src[3] = { GetDistance(src[0], src[1]), GetDistance(src[0], src[2]), GetDistance(src[1], src[2]) };
  70. double d_dst[3] = { GetDistance(dst[0], dst[1]), GetDistance(dst[0], dst[2]), GetDistance(dst[1], dst[2]) };
  71. double scales[3] = { d_dst[0] / d_src[0], d_dst[1] / d_src[1], d_dst[2] / d_src[2] };
  72. double dscale[3] = { abs(scales[0] - scales[1]), abs(scales[0] - scales[2]), abs(scales[1] - scales[2]) };
  73. double scale = max(max(dscale[0], dscale[1]), dscale[2]);
  74. if (scale > 0.15)return IDF_FAILURE;
  75. double m_Scaler0 = d_dst[0] / d_src[0];
  76. match_result.matched = true;
  77. match_result.schema_index = schema_index;
  78. float x = dst[1].x - dst[0].x;
  79. float y = dst[1].y - dst[0].y;
  80. int dir;
  81. if (abs(x) > abs(y)){
  82. if (x > 0){
  83. dir = ROTATION_0;
  84. }
  85. else{
  86. dir = ROTATION_180;
  87. }
  88. }
  89. else{
  90. if (y > 0){
  91. dir = ROTATION_270;
  92. }
  93. else{
  94. dir = ROTATION_90;
  95. }
  96. }
  97. match_result.dir = dir;
  98. match_result.matched_count = 0;
  99. match_result.scale = m_Scaler0;
  100. match_result.total_count = 0;
  101. memcpy(match_result.transfrom, transform.data, sizeof(match_result.transfrom));
  102. }
  103. int page_index = match_result.schema_index;
  104. cv::Mat transform(2, 3, CV_64F, match_result.transfrom);
  105. double scale = match_result.scale;
  106. int ret = rr.ReadResult(img, page_index, transform, scale, match_result.dir, out_result);
  107. if (ret == IDF_SUCCESS){
  108. OMR_RESULT * r = (OMR_RESULT*)out_result;
  109. r->identified = TRUE;
  110. return ret;
  111. }
  112. return IDF_SUCCESS;
  113. }
  114. }