PageMatcher.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. #include "StdAfx.h"
  2. #include "PageMatcher.h"
  3. #include "Affine2DEstimator0.h"
  4. #include "Affine2DEstimator.h"
  5. #include "CrossDetector.h"
  6. namespace identify{
  7. CPageMatcher::CPageMatcher(void)
  8. {
  9. }
  10. CPageMatcher::~CPageMatcher(void)
  11. {
  12. }
  13. int CPageMatcher::Identifi(const IplImage * src, int shemaIndex, MATCH_REUSLT& match_result)
  14. {
  15. //旋转4个角度查找定位点(逆时针旋转 0度,90度,180度,270度) 并矫正
  16. const int rotation[4] = { ROTATION_0, ROTATION_90, ROTATION_180, ROTATION_270 };
  17. std::vector<MATCH_REUSLT> match_reuslts;
  18. int ret;
  19. for(std::size_t dir = 0; dir < 4; dir++)
  20. {
  21. MATCH_REUSLT result;
  22. ret = Identifi(src, shemaIndex, rotation[dir], result);
  23. if (ret == IDF_SUCCESS)match_reuslts.push_back(result);
  24. }
  25. match_result.matched_count = 0;
  26. int max_matched_count = 0;
  27. int max_matched_index = -1;
  28. for (std::size_t i = 0; i < match_reuslts.size(); i++)
  29. {
  30. if (max_matched_count < match_reuslts[i].matched_count){
  31. max_matched_index = i;
  32. max_matched_count = match_reuslts[i].matched_count;
  33. }
  34. }
  35. if (max_matched_index<0){
  36. return IDF_FAILURE;
  37. }
  38. match_result = match_reuslts[max_matched_index];
  39. return IDF_SUCCESS;
  40. }
  41. int CPageMatcher::Identifi(const IplImage * src, int shemaIndex, const int rotation, MATCH_REUSLT& result)
  42. {
  43. const ISCH_SCHEMA_PAGE& schemaPage = (*schemaPages)[shemaIndex];
  44. result.matched = false;
  45. int ret = BaseCheck(src, schemaPage);
  46. if (ret != IDF_SUCCESS)return ret;
  47. //估计比例
  48. double estimate_scale = sqrt((double)(src->width*src->width + src->height*src->height)) / sqrt((double)(schemaPage.width*schemaPage.width + schemaPage.height*schemaPage.height));
  49. //长宽不相等,进行长宽校验
  50. if (abs(1 - max(schemaPage.width, schemaPage.height) / (double)min(schemaPage.width, schemaPage.height))>0.15){
  51. if (rotation&(ROTATION_0 | ROTATION_180))//旋转0度或者180度
  52. {
  53. if ((schemaPage.width > schemaPage.height) != (src->width > src->height))return IDF_FAILURE;
  54. }
  55. if (rotation&(ROTATION_90 | ROTATION_270)){
  56. if ((schemaPage.width > schemaPage.height) != (src->width < src->height))return IDF_FAILURE;
  57. }
  58. }
  59. //记录对应关系属于哪个定位点、定位区、或交叉点
  60. int first_index = 0;
  61. vector<int> relationIndex;
  62. vector<CvPoint2D32f> relationKey;
  63. vector<CvPoint2D32f> relationValue;
  64. /********************************查找定位点*********************************************/
  65. ret = FindLoacteByLocatePoint(src, schemaPage, estimate_scale, rotation, relationKey, relationValue, relationIndex, first_index);
  66. first_index += schemaPage.locatePoints.size();
  67. if (ret != IDF_SUCCESS) return ret;
  68. /********************************查找模糊定位区*****************************************/
  69. ret = FindLoacteByLocateArea(src, schemaPage, estimate_scale, rotation, relationKey, relationValue, relationIndex, first_index);
  70. first_index += schemaPage.locateAreas.size();
  71. if (ret != IDF_SUCCESS) return ret;
  72. /********************************查找交叉点定位点***************************************/
  73. ret = FindLoacteByLocateCross(src, schemaPage, estimate_scale, rotation, relationKey, relationValue, relationIndex, first_index);
  74. first_index += schemaPage.locateCrosses.size();
  75. if (ret != IDF_SUCCESS) return ret;
  76. /********************************确认定位信息*********************************************/
  77. int inliner_count; cv::Mat _inliner;
  78. ret = ConfirmLocateInfo(relationKey, relationValue, inliner_count, _inliner);
  79. if (ret != IDF_SUCCESS) return ret;
  80. std::map<int, int> m;
  81. for(std::size_t kk = 0; kk < relationIndex.size(); kk++)
  82. {
  83. if (_inliner.at<unsigned char>(0, kk))m[relationIndex[kk]]++;
  84. }
  85. int matched_count = m.size();
  86. m_pageDirection = rotation;
  87. result.matched = true;
  88. result.schema_index = shemaIndex;
  89. result.dir = rotation;
  90. result.matched_count = matched_count;
  91. result.scale = m_Scaler0;
  92. result.total_count = schemaPage.locatePoints.size() + schemaPage.locateAreas.size() +schemaPage.locateCrosses.size();;
  93. memcpy(result.transfrom, m_AffineTransform.data, sizeof(result.transfrom));
  94. return IDF_SUCCESS;
  95. }
  96. int CPageMatcher::FindLoacteByLocateArea(const IplImage * src, const ISCH_SCHEMA_PAGE& schemaPage, const double estimate_scale, const int rotation, vector<CvPoint2D32f>& relationKey, vector<CvPoint2D32f>& relationValue, vector<int> &relationIndex, int first_index)
  97. {
  98. IplImage t;
  99. cvInitImageHeader(&t, cvSize(src->width, src->height), src->depth, src->nChannels, src->origin, src->align);
  100. cvSetData(&t, src->imageData, src->widthStep);
  101. IplImage * img = &t;
  102. for(std::size_t i = 0; i < schemaPage.locateAreas.size(); i++)
  103. {
  104. cvResetImageROI(img);
  105. int left, top, right, bottom;
  106. const ISCH_SCHEMA_LOCATE_AREA& locateArea = schemaPage.locateAreas[i].locate_area;
  107. const vector<KeyPoint>& locateAreaKeys = schemaPage.locateAreas[i].locateAreasKeyPoints;
  108. const CvPoint locateAreaOffset = schemaPage.locateAreas[i].locateAreasOffset;
  109. const Mat& locateAreaDescriptor = schemaPage.locateAreas[i].locateAreasDescriptor;
  110. double tw = locateArea.width + 100, th = locateArea.height + 100;
  111. switch (rotation){
  112. case ROTATION_0:
  113. left = cv::max(0, (int)(locateArea.centerx*estimate_scale - tw / 2.0 + 0.5));
  114. top = cv::max(0, (int)(locateArea.centery*estimate_scale - tw / 2.0 + 0.5));
  115. right = min(img->width, (int)(locateArea.centerx*estimate_scale + tw / 2.0 + 0.5));
  116. bottom = min(img->height, (int)(locateArea.centery*estimate_scale + th / 2.0 + 0.5));
  117. break;
  118. case ROTATION_90:
  119. left = max(0, (int)(locateArea.centery*estimate_scale - th / 2.0 + 0.5));
  120. top = max(0, (int)((schemaPage.width - locateArea.centerx)*estimate_scale - tw / 2.0 + 0.5));
  121. right = min(img->width, (int)(locateArea.centery*estimate_scale + th / 2.0 + 0.5));
  122. bottom = min(img->height, (int)((schemaPage.width - locateArea.centerx)*estimate_scale + tw / 2.0 + 0.5));
  123. break;
  124. case ROTATION_180:
  125. left = max(0, (int)((schemaPage.width - locateArea.centerx)*estimate_scale - tw / 2.0 + 0.5));
  126. top = max(0, (int)((schemaPage.height - locateArea.centery)*estimate_scale - th / 2.0 + 0.5));
  127. right = min(img->width, (int)((schemaPage.width - locateArea.centerx)*estimate_scale + tw / 2.0 + 0.5));
  128. bottom = min(img->height, (int)((schemaPage.height - locateArea.centery)*estimate_scale + th / 2.0 + 0.5));
  129. break;
  130. case ROTATION_270:
  131. left = max(0, (int)((schemaPage.height - locateArea.centery)*estimate_scale - th / 2.0 + 0.5));
  132. top = max(0, (int)(locateArea.centerx*estimate_scale - tw / 2.0 + 0.5));
  133. right = min(img->width, (int)((schemaPage.height - locateArea.centery)*estimate_scale + th / 2.0 + 0.5));
  134. bottom = min(img->height, (int)(locateArea.centerx*estimate_scale + tw / 2.0 + 0.5));
  135. break;
  136. }
  137. cvSetImageROI(img, cvRect(left, top, right - left + 1, bottom - top + 1));
  138. IplImage * grayImg = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
  139. if (img->nChannels == 3)cvCvtColor(img, grayImg, CV_BGR2GRAY);
  140. else cvCopy(img, grayImg);
  141. cvSmooth(grayImg, grayImg, CV_GAUSSIAN, 5);
  142. Mat m_m = cv::cvarrToMat(grayImg, false);
  143. std::vector<KeyPoint> keyPoints;
  144. Mat descriptor;
  145. Ptr<FeatureDetector> pDetector = SurfFeatureDetector::create();// 这里我们用了SURF特征点
  146. pDetector->detect(m_m, keyPoints);
  147. Ptr<DescriptorExtractor> pExtractor = SurfDescriptorExtractor::create(); // 提取SURF描述向量
  148. pExtractor->compute(m_m, keyPoints, descriptor);
  149. vector<DMatch> Matches;
  150. Ptr<DescriptorMatcher>pMatcher = new FlannBasedMatcher; // 使用Flann匹配算法
  151. if (descriptor.rows > 0 && locateAreaDescriptor.rows > 0)pMatcher->match(descriptor, locateAreaDescriptor, Matches);
  152. if (Matches.size() >= 3){
  153. vector<Point2f> _from;
  154. vector<Point2f> _to;
  155. for(std::size_t n = 0; n < Matches.size(); n++)
  156. {
  157. _to.push_back(keyPoints[Matches[n].queryIdx].pt);
  158. _from.push_back(locateAreaKeys[Matches[n].trainIdx].pt);
  159. }
  160. // Show result
  161. Mat _out; Mat _inliers;
  162. int su = estimateAffine2D0(_from, _to, _out, _inliers, 2);
  163. if (su > cv::max(5.0, locateAreaKeys.size()*0.1)){
  164. double centerx = locateArea.centerx - locateAreaOffset.x;//locateArea.width/2.0;
  165. double centery = locateArea.centery - locateAreaOffset.y;//locateArea.height/2.0;
  166. double x = _out.at<double>(0, 0)*centerx + _out.at<double>(0, 1)*centery + _out.at<double>(0, 2);
  167. double y = _out.at<double>(1, 0)*centerx + _out.at<double>(1, 1)*centery + _out.at<double>(1, 2);
  168. if (0 <= x&&x < grayImg->width && 0 <= y&&y < grayImg->height){
  169. relationKey.push_back(cvPoint2D32f(centerx + locateAreaOffset.x, centery + locateAreaOffset.y));
  170. relationValue.push_back(cvPoint2D32f(x + left, y + top));
  171. relationIndex.push_back(first_index + i);
  172. }
  173. }
  174. }
  175. cvReleaseImage(&grayImg);
  176. }
  177. return IDF_SUCCESS;
  178. }
  179. int CPageMatcher::ConfirmLocateInfo(vector<CvPoint2D32f>& relationKey, vector<CvPoint2D32f>& relationValue, int& inliner_count, cv::Mat& _inliner)
  180. {
  181. if (relationKey.size() < 3) return IDF_NOTFOUND_ENOUGH_LOCATEPOINT;
  182. Mat _out; Mat _inliers;
  183. vector<Point2f> _from;
  184. vector<Point2f> _to;
  185. for(std::size_t i = 0; i < relationKey.size(); i++)
  186. {
  187. _from.push_back(relationKey[i]);
  188. _to.push_back(relationValue[i]);
  189. }
  190. /*模型估计*/
  191. int su = estimateAffine2D(_from, _to, _out, _inliers, 4);
  192. if (su < 3) return IDF_FAILURE;
  193. if (su >= 3){
  194. m_AffineTransform = _out;
  195. Point2f src[3] = { Point2f(0, 0), Point2f(1000, 0), Point2f(0, 1000) };
  196. Point2f dst[3] = { warpAffinePoint<Point2f, Point2f>(src[0], &m_AffineTransform), warpAffinePoint<Point2f, Point2f>(src[1], &m_AffineTransform), warpAffinePoint<Point2f, Point2f>(src[2], &m_AffineTransform) };
  197. double d_src[3] = { GetDistance(src[0], src[1]), GetDistance(src[0], src[2]), GetDistance(src[1], src[2]) };
  198. double d_dst[3] = { GetDistance(dst[0], dst[1]), GetDistance(dst[0], dst[2]), GetDistance(dst[1], dst[2]) };
  199. double scales[3] = { d_dst[0] / d_src[0], d_dst[1] / d_src[1], d_dst[2] / d_src[2] };
  200. double dscale[3] = { abs(scales[0] - scales[1]), abs(scales[0] - scales[2]), abs(scales[1] - scales[2]) };
  201. double scale = max(max(dscale[0], dscale[1]), dscale[2]);
  202. if (scale > 0.15)return IDF_FAILURE;
  203. m_AffineTransformInverseMatrix = getAffineTransform(dst, src);
  204. m_Scaler0 = d_dst[0] / d_src[0];
  205. inliner_count = su;
  206. _inliner = _inliers;
  207. }
  208. return IDF_SUCCESS;
  209. }
  210. int CPageMatcher::FindLoacteByLocatePoint(const IplImage * src, const ISCH_SCHEMA_PAGE& schemaPage, const double estimate_scale, const int rotation, vector<CvPoint2D32f> &relationKey, vector<CvPoint2D32f>& relationValue, vector<int> &relationIndex, int first_index)
  211. {
  212. #if _DEBUG
  213. cvSaveImage("D:\\binary_cvDilate.png", src);
  214. #endif
  215. IplImage t;
  216. cvInitImageHeader(&t, cvSize(src->width, src->height), src->depth, src->nChannels, src->origin, src->align);
  217. cvSetData(&t, src->imageData, src->widthStep);
  218. IplImage * img = &t;
  219. int tw = (int)(400 * estimate_scale);
  220. int th = (int)(400 * estimate_scale);
  221. int left, top, right, bottom;
  222. CvRect rect;
  223. IplImage* dst = cvCreateImage(cvSize(tw + 10, th + 10), IPL_DEPTH_8U, 1);
  224. CvSeq* contour = NULL;
  225. CvMemStorage * storage = cvCreateMemStorage();
  226. vector<vector<CvContour*>> locatePointContour(schemaPage.locatePoints.size());
  227. for(std::size_t i = 0; i < schemaPage.locatePoints.size(); i++)
  228. {
  229. const identify::schema::ISCH_SCHEMA_LOCATE_POINT& locatePoint = schemaPage.locatePoints[i];
  230. switch (rotation){
  231. case ROTATION_0:
  232. left = max(0, (int)(locatePoint.centerx*estimate_scale - tw / 2.0 + 0.5));
  233. top = max(0, (int)(locatePoint.centery*estimate_scale - tw / 2.0 + 0.5));
  234. right = min(img->width, (int)(locatePoint.centerx*estimate_scale + tw / 2.0 + 0.5));
  235. bottom = min(img->height, (int)(locatePoint.centery*estimate_scale + th / 2.0 + 0.5));
  236. rect = cvRect(left, top, right - left, bottom - top);
  237. break;
  238. case ROTATION_90:
  239. left = max(0, (int)(locatePoint.centery*estimate_scale - tw / 2.0 + 0.5));
  240. top = max(0, (int)((schemaPage.width - locatePoint.centerx)*estimate_scale - tw / 2.0 + 0.5));
  241. right = min(img->width, (int)(locatePoint.centery*estimate_scale + tw / 2.0 + 0.5));
  242. bottom = min(img->height, (int)((schemaPage.width - locatePoint.centerx)*estimate_scale + th / 2.0 + 0.5));
  243. rect = cvRect(left, top, right - left, bottom - top);
  244. break;
  245. case ROTATION_180:
  246. left = max(0, (int)((schemaPage.width - locatePoint.centerx)*estimate_scale - tw / 2.0 + 0.5));
  247. top = max(0, (int)((schemaPage.height - locatePoint.centery)*estimate_scale - tw / 2.0 + 0.5));
  248. right = min(img->width, (int)((schemaPage.width - locatePoint.centerx)*estimate_scale + tw / 2.0 + 0.5));
  249. bottom = min(img->height, (int)((schemaPage.height - locatePoint.centery)*estimate_scale + th / 2.0 + 0.5));
  250. rect = cvRect(left, top, right - left, bottom - top);
  251. break;
  252. case ROTATION_270:
  253. left = max(0, (int)((schemaPage.height - locatePoint.centery)*estimate_scale - tw / 2.0 + 0.5));
  254. top = max(0, (int)(locatePoint.centerx*estimate_scale - tw / 2.0 + 0.5));
  255. right = min(img->width, (int)((schemaPage.height - locatePoint.centery)*estimate_scale + tw / 2.0 + 0.5));
  256. bottom = min(img->height, (int)(locatePoint.centerx*estimate_scale + th / 2.0 + 0.5));
  257. rect = cvRect(left, top, right - left, bottom - top);
  258. break;
  259. }
  260. cvSetImageROI(img, rect);
  261. cvSet(dst, cvScalarAll(255));
  262. cvSetImageROI(dst, cvRect(5, 5, rect.width, rect.height));
  263. if (img->nChannels == 3)cvCvtColor(img, dst, CV_BGR2GRAY);
  264. else cvCopy(img, dst);
  265. cvResetImageROI(dst);
  266. cvThreshold(dst, dst, 160, 255, CV_THRESH_BINARY_INV);
  267. //cvAdaptiveThreshold(dst,dst,255,CV_ADAPTIVE_THRESH_MEAN_C,CV_THRESH_BINARY,5,-40);
  268. int an = 2;
  269. IplConvKernel * element = cvCreateStructuringElementEx(an * 2 + 1, an * 2 + 1, an, an, CV_SHAPE_RECT, 0);//创建结构元素
  270. cvDilate(dst, dst, element, 1);//膨胀图像
  271. cvErode(dst, dst, element, 1);//腐蚀图像
  272. cvErode(dst, dst, element, 1);//腐蚀图像
  273. /*cvSaveImage("D:\\binary_cvErode.png",dst);*/
  274. cvDilate(dst, dst, element, 1);//膨胀图像
  275. /*cvSaveImage("D:\\binary_cvDilate.png",dst);*/
  276. cvReleaseStructuringElement(&element);
  277. int contours = cvFindContours(dst, storage, &contour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(rect.x - 5, rect.y - 5));
  278. /*cvDrawContours(dst,contour,cvScalarAll(128),cvScalarAll(128),2,1,8,cvPoint(-(rect.x-5),-(rect.y-5)));
  279. cvSaveImage("D:\\binary_Contours.png",dst);*/
  280. vector<CvContour*>& contourList = locatePointContour[i];
  281. if (rotation&(ROTATION_0 | ROTATION_180)){
  282. for (CvContour * c = (CvContour *)contour; c != 0; c = (CvContour *)c->h_next)
  283. {
  284. if (c->rect.width < min(locatePoint.width*0.8, (double)locatePoint.width - 2)*estimate_scale)continue;
  285. if (c->rect.height < min(locatePoint.height*0.8, (double)locatePoint.height - 2)*estimate_scale)continue;
  286. if (c->rect.width > max(locatePoint.width*1.2, (double)locatePoint.width + 2)*estimate_scale)continue;
  287. if (c->rect.height > max(locatePoint.height*1.2, (double)locatePoint.height + 2)*estimate_scale)continue;
  288. cvSetImageROI(img, c->rect);
  289. int area = GetBlackArea(img);
  290. if (area < c->rect.width*c->rect.height*0.75)continue;
  291. double area2 = cvContourArea(c);
  292. if (area2 < c->rect.width*c->rect.height - (c->rect.width + c->rect.height)*3.5)continue;
  293. contourList.push_back(c);
  294. }
  295. }
  296. if (rotation&(ROTATION_90 | ROTATION_270)){
  297. for (CvContour * c = (CvContour *)contour; c != 0; c = (CvContour *)c->h_next)
  298. {
  299. if (c->rect.height < min(locatePoint.width*0.8, (double)locatePoint.width - 2)*estimate_scale)continue;
  300. if (c->rect.width < min(locatePoint.height*0.8, (double)locatePoint.height - 2)*estimate_scale)continue;
  301. if (c->rect.height > max(locatePoint.width*1.2, (double)locatePoint.width + 2)*estimate_scale)continue;
  302. if (c->rect.width > max(locatePoint.height*1.2, (double)locatePoint.height + 2)*estimate_scale)continue;
  303. cvSetImageROI(img, c->rect);
  304. int area = GetBlackArea(img);
  305. if (area < c->rect.width*c->rect.height*0.75)continue;
  306. double area2 = cvContourArea(c);
  307. if (area2 < c->rect.width*c->rect.height - (c->rect.width + c->rect.height) * 3.5)continue;
  308. contourList.push_back(c);
  309. }
  310. }
  311. }
  312. for(std::size_t i = 0; i < schemaPage.locatePoints.size(); i++)
  313. {
  314. for(std::size_t j = 0; j < locatePointContour[i].size(); j++)
  315. {
  316. CvPoint2D32f* point1 = (CvPoint2D32f*)cvMemStorageAlloc(storage, sizeof(CvPoint2D32f));
  317. CvPoint2D32f* point2 = (CvPoint2D32f*)cvMemStorageAlloc(storage, sizeof(CvPoint2D32f));
  318. point1->x = schemaPage.locatePoints[i].centerx;
  319. point1->y = schemaPage.locatePoints[i].centery;
  320. CvContour& ff = *locatePointContour[i][j];
  321. point2->x = ff.rect.x + ff.rect.width / 2.;
  322. point2->y = ff.rect.y + ff.rect.height / 2.;
  323. relationKey.push_back(*point1);
  324. relationValue.push_back(*point2);
  325. relationIndex.push_back(first_index + i);
  326. }
  327. }
  328. cvReleaseMemStorage(&storage);
  329. cvReleaseImage(&dst);
  330. return IDF_SUCCESS;
  331. }
  332. inline int CPageMatcher::BaseCheck(const IplImage * src, const ISCH_SCHEMA_PAGE& schemaPage)
  333. {
  334. // cvSaveImage("d:\\d1.jpg",src);
  335. if (src->width < 200 || src->height<200){
  336. return IDF_SIZE2SMALL;
  337. }
  338. if (abs(max(src->width, src->height) / (double)min(src->width, src->height) - max(schemaPage.width, schemaPage.height) / (double)min(schemaPage.width, schemaPage.height))>0.2){
  339. //图像长宽比不符
  340. return IDF_SIDERATIO;
  341. }
  342. return IDF_SUCCESS;
  343. }
  344. int CPageMatcher::Identify(const IplImage* img, MATCH_REUSLT & match_result)
  345. {
  346. if (img == NULL) return IDF_FAILURE;
  347. if (schemaPages == NULL || schemaPages->size() < 1)return IDF_MISSING_TEMPLATES;
  348. std::vector<MATCH_REUSLT> match_results;
  349. for(std::size_t schemaIndex = 0; schemaIndex < schemaPages->size(); schemaIndex++){
  350. MATCH_REUSLT result;
  351. int r = Identifi(img, schemaIndex, result);
  352. if (r == IDF_SUCCESS){
  353. match_results.push_back(result);
  354. }
  355. }
  356. int max_gailv = 0;
  357. int matched_index = -1;
  358. for(std::size_t i = 0; i < match_results.size(); i++)
  359. {
  360. int gailv = match_results[i].matched_count - (match_results[i].total_count - match_results[i].matched_count);
  361. if (max_gailv < gailv){
  362. matched_index = i;
  363. max_gailv = gailv;
  364. }
  365. }
  366. if (matched_index < 0){ return IDF_CANNOT_MATCH_TEMPLATE; }
  367. match_result = match_results[matched_index];
  368. /*int min_unmatched_count =9999999;
  369. int min_unmatched_index =-1;
  370. for(std::size_t i=0;i<match_results.size();i++)
  371. {
  372. int unmatched_count=match_results[i].total_count-match_results[i].matched_count;
  373. if(min_unmatched_count>unmatched_count){
  374. min_unmatched_index =i;
  375. min_unmatched_count = unmatched_count;
  376. }
  377. }
  378. if(min_unmatched_index<0){return IDF_CANNOT_MATCH_TEMPLATE;}
  379. match_result = match_results[min_unmatched_index];*/
  380. return IDF_SUCCESS;
  381. }
  382. void CPageMatcher::SetSchemaPages(boost::shared_ptr<const ISCH_Schema> schema)
  383. {
  384. this->schemaPages = schema;
  385. }
  386. template<typename T1, typename T2>
  387. T2 CPageMatcher::warpAffinePoint(T1& src, Mat * map_matrix)
  388. {
  389. T2 r;
  390. r.x = map_matrix->at<double>(0, 0)*src.x + map_matrix->at<double>(0, 1)*src.y + map_matrix->at<double>(0, 2);
  391. r.y = map_matrix->at<double>(1, 0)*src.x + map_matrix->at<double>(1, 1)*src.y + map_matrix->at<double>(1, 2);
  392. return r;
  393. }
  394. template<typename T1, typename T2 >
  395. double CPageMatcher::GetDistance(T1 point1, T2 point2)
  396. {
  397. double dx = point1.x - point2.x;
  398. double dy = point1.y - point2.y;
  399. return sqrt(dx*dx + dy*dy);
  400. }
  401. int CPageMatcher::FindLoacteByLocateCross(const IplImage * src, const ISCH_SCHEMA_PAGE& schemaPage, const double estimate_scale, const int rotation, vector<CvPoint2D32f>& relationKey, vector<CvPoint2D32f>& relationValue, vector<int> &relationIndex, int first_index)
  402. {
  403. IplImage t;
  404. cvInitImageHeader(&t, cvSize(src->width, src->height), src->depth, src->nChannels, src->origin, src->align);
  405. cvSetData(&t, src->imageData, src->widthStep);
  406. IplImage * img = &t;
  407. int tw = (int)(200 * estimate_scale);
  408. int th = (int)(200 * estimate_scale);
  409. int left, top, right, bottom;
  410. CvRect rect;
  411. for(std::size_t i = 0; i < schemaPage.locateCrosses.size(); i++)
  412. {
  413. const identify::schema::ISCH_SCHEMA_LOACTE_CROSS& locateCross = schemaPage.locateCrosses[i];
  414. switch (rotation){
  415. case ROTATION_0:
  416. left = max(0, (int)(locateCross.centerx*estimate_scale - tw / 2.0 + 0.5));
  417. top = max(0, (int)(locateCross.centery*estimate_scale - tw / 2.0 + 0.5));
  418. right = min(img->width, (int)(locateCross.centerx*estimate_scale + tw / 2.0 + 0.5));
  419. bottom = min(img->height, (int)(locateCross.centery*estimate_scale + th / 2.0 + 0.5));
  420. rect = cvRect(left, top, right - left, bottom - top);
  421. break;
  422. case ROTATION_90:
  423. left = max(0, (int)(locateCross.centery*estimate_scale - tw / 2.0 + 0.5));
  424. top = max(0, (int)((schemaPage.width - locateCross.centerx)*estimate_scale - tw / 2.0 + 0.5));
  425. right = min(img->width, (int)(locateCross.centery*estimate_scale + tw / 2.0 + 0.5));
  426. bottom = min(img->height, (int)((schemaPage.width - locateCross.centerx)*estimate_scale + th / 2.0 + 0.5));
  427. rect = cvRect(left, top, right - left, bottom - top);
  428. break;
  429. case ROTATION_180:
  430. left = max(0, (int)((schemaPage.width - locateCross.centerx)*estimate_scale - tw / 2.0 + 0.5));
  431. top = max(0, (int)((schemaPage.height - locateCross.centery)*estimate_scale - tw / 2.0 + 0.5));
  432. right = min(img->width, (int)((schemaPage.width - locateCross.centerx)*estimate_scale + tw / 2.0 + 0.5));
  433. bottom = min(img->height, (int)((schemaPage.height - locateCross.centery)*estimate_scale + th / 2.0 + 0.5));
  434. rect = cvRect(left, top, right - left, bottom - top);
  435. break;
  436. case ROTATION_270:
  437. left = max(0, (int)((schemaPage.height - locateCross.centery)*estimate_scale - tw / 2.0 + 0.5));
  438. top = max(0, (int)(locateCross.centerx*estimate_scale - tw / 2.0 + 0.5));
  439. right = min(img->width, (int)((schemaPage.height - locateCross.centery)*estimate_scale + tw / 2.0 + 0.5));
  440. bottom = min(img->height, (int)(locateCross.centerx*estimate_scale + th / 2.0 + 0.5));
  441. rect = cvRect(left, top, right - left, bottom - top);
  442. break;
  443. }
  444. cvSetImageROI(img, rect);
  445. CCrossDetector crossDetector;
  446. std::vector<CvCross> crosss;
  447. crossDetector.Detect(img, crosss);
  448. int sign_=0;
  449. switch (rotation)
  450. {
  451. case ROTATION_0:
  452. if (locateCross.sign&ISCH_SCHEMA_CROSS_SIGN_UP)sign_ |= ISCH_SCHEMA_CROSS_SIGN_UP;
  453. if (locateCross.sign&ISCH_SCHEMA_CROSS_SIGN_DOWN)sign_ |= ISCH_SCHEMA_CROSS_SIGN_DOWN;
  454. if (locateCross.sign&ISCH_SCHEMA_CROSS_SIGN_LEFT)sign_ |= ISCH_SCHEMA_CROSS_SIGN_LEFT;
  455. if (locateCross.sign&ISCH_SCHEMA_CROSS_SIGN_RIGHT)sign_ |= ISCH_SCHEMA_CROSS_SIGN_RIGHT;
  456. break;
  457. case ROTATION_90:
  458. if (locateCross.sign&ISCH_SCHEMA_CROSS_SIGN_UP)sign_ |= ISCH_SCHEMA_CROSS_SIGN_LEFT;
  459. if (locateCross.sign&ISCH_SCHEMA_CROSS_SIGN_DOWN)sign_ |= ISCH_SCHEMA_CROSS_SIGN_RIGHT;
  460. if (locateCross.sign&ISCH_SCHEMA_CROSS_SIGN_LEFT)sign_ |= ISCH_SCHEMA_CROSS_SIGN_DOWN;
  461. if (locateCross.sign&ISCH_SCHEMA_CROSS_SIGN_RIGHT)sign_ |= ISCH_SCHEMA_CROSS_SIGN_UP;
  462. break;
  463. case ROTATION_180:
  464. if (locateCross.sign&ISCH_SCHEMA_CROSS_SIGN_UP)sign_ |= ISCH_SCHEMA_CROSS_SIGN_DOWN;
  465. if (locateCross.sign&ISCH_SCHEMA_CROSS_SIGN_DOWN)sign_ |= ISCH_SCHEMA_CROSS_SIGN_UP;
  466. if (locateCross.sign&ISCH_SCHEMA_CROSS_SIGN_LEFT)sign_ |= ISCH_SCHEMA_CROSS_SIGN_RIGHT;
  467. if (locateCross.sign&ISCH_SCHEMA_CROSS_SIGN_RIGHT)sign_ |= ISCH_SCHEMA_CROSS_SIGN_LEFT;
  468. break;
  469. case ROTATION_270:
  470. if (locateCross.sign&ISCH_SCHEMA_CROSS_SIGN_UP)sign_ |= ISCH_SCHEMA_CROSS_SIGN_RIGHT;
  471. if (locateCross.sign&ISCH_SCHEMA_CROSS_SIGN_DOWN)sign_ |= ISCH_SCHEMA_CROSS_SIGN_LEFT;
  472. if (locateCross.sign&ISCH_SCHEMA_CROSS_SIGN_LEFT)sign_ |= ISCH_SCHEMA_CROSS_SIGN_UP;
  473. if (locateCross.sign&ISCH_SCHEMA_CROSS_SIGN_RIGHT)sign_ |= ISCH_SCHEMA_CROSS_SIGN_DOWN;
  474. break;
  475. }
  476. for(std::size_t j = 0; j < crosss.size(); j++)
  477. {
  478. if (crosss[j].sign != sign_)continue;
  479. CvPoint2D32f point1;
  480. CvPoint2D32f point2;
  481. point1.x = locateCross.centerx;
  482. point1.y = locateCross.centery;
  483. const CvCross& ff = crosss[j];
  484. point2.x = ff.x;
  485. point2.y = ff.y;
  486. relationKey.push_back(point1);
  487. relationValue.push_back(point2);
  488. relationIndex.push_back(first_index + i);
  489. }
  490. }
  491. return IDF_SUCCESS;
  492. }
  493. }