OcrNode.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #include "StdAfx.h"
  2. #include "OcrNode.h"
  3. #include "OcrScreen.h"
  4. #include "common_funcs.h"
  5. #include "common_api.h"
  6. OcrNode::OcrNode(void)
  7. {
  8. m_TotalPixelsCount = 0;
  9. }
  10. OcrNode::~OcrNode(void)
  11. {
  12. ULONG i = 0;
  13. OcrLine *pNode;
  14. for(i = 0;i < m_lists.size();i++)
  15. {
  16. pNode = m_lists[i];
  17. delete pNode;
  18. }
  19. m_lists.clear();
  20. }
  21. BOOL OcrNode::GetMatchRate(ULONG64 sim)
  22. {
  23. ULONG64 matched = 0;
  24. ULONG i = 0;
  25. OcrLine *pNode;
  26. for(i = 0;i < m_lists.size();i++)
  27. {
  28. pNode = m_lists[i];
  29. matched += pNode->m_MatchCount;
  30. }
  31. if((matched*100) >= (m_TotalPixelsCount*sim))
  32. {
  33. return TRUE;
  34. }
  35. return FALSE;
  36. float retf = ((float)matched)/((float)m_TotalPixelsCount);
  37. return (ULONG)(retf*100);
  38. }
  39. void OcrNode::GetFitAreaX(COLORPOINT &topleft,COLORPOINT &topright,COLORPOINT &bottomleft,COLORPOINT &bottomright)
  40. {
  41. m_lists[0]->GetLineKey(topleft,topright);
  42. m_lists[m_lists.size() - 1]->GetLineKey(bottomleft,bottomright);
  43. }
  44. void OcrNode::GetFitArea(COLORPOINT &top1,COLORPOINT &left1,COLORPOINT &bottom1,COLORPOINT &right1)
  45. {
  46. COLORPOINT linex[2];
  47. memset(&top1,-1,sizeof(COLORPOINT));
  48. memset(&bottom1,-1,sizeof(COLORPOINT));
  49. memset(&left1,-1,sizeof(COLORPOINT));
  50. memset(&right1,-1,sizeof(COLORPOINT));
  51. ULONG i = 0;
  52. for(i = 0;i < m_lists.size();i++)
  53. {
  54. m_lists[i]->GetLineKey(linex[0],linex[1]);
  55. //top
  56. if(top1.pt.y < 0)
  57. {
  58. top1 = linex[0];
  59. }
  60. else
  61. {
  62. if(top1.pt.y > linex[0].pt.y)
  63. {
  64. top1 = linex[0];
  65. }
  66. }
  67. //bottom
  68. if(bottom1.pt.y < 0)
  69. {
  70. bottom1 = linex[0];
  71. }
  72. else
  73. {
  74. if(bottom1.pt.y < linex[0].pt.y)
  75. {
  76. bottom1 = linex[0];
  77. }
  78. }
  79. //left
  80. if(left1.pt.x < 0)
  81. {
  82. left1 = linex[0];
  83. }
  84. else
  85. {
  86. if(left1.pt.x > linex[0].pt.x)
  87. {
  88. left1 = linex[0];
  89. }
  90. }
  91. //right
  92. if(right1.pt.x < 0)
  93. {
  94. right1 = linex[1];
  95. }
  96. else
  97. {
  98. if(right1.pt.x < linex[1].pt.x)
  99. {
  100. right1 = linex[1];
  101. }
  102. }
  103. }
  104. }
  105. BOOL OcrNode::InitialLoadOcrFromFile(const TCHAR *pszFulName)
  106. {
  107. //key point
  108. COLORPOINT top1,left1,bottom1,right1;
  109. GetFitArea(top1,left1,bottom1,right1);
  110. //reFix start point
  111. for(DWORD i = 0;i < m_lists.size();i++)
  112. {
  113. m_lists[i]->m_pt.x -= left1.pt.x;
  114. m_lists[i]->m_pt.y -= top1.pt.y;
  115. }
  116. GetFitArea(top1,left1,bottom1,right1);
  117. m_size.cx = right1.pt.x - left1.pt.x + 1;
  118. m_size.cy = bottom1.pt.y - top1.pt.y + 1;
  119. //reGet FitArea
  120. GetFitAreaX(top1,left1,bottom1,right1);
  121. m_KeyPoints[0] = top1;
  122. m_KeyPoints[1] = bottom1;
  123. m_KeyPoints[2] = left1;
  124. m_KeyPoints[3] = right1;
  125. //file name
  126. //CFile file;
  127. //file.Open(pszFulName,CFile::modeRead);
  128. //CString temp = file.GetFileTitle();
  129. //temp.Replace(L".bmp",L"");
  130. string temp = pszFulName;
  131. m_NodeName = GetFileTitle(temp);
  132. //file.Close();
  133. InitialSundayLine();
  134. return TRUE;
  135. }
  136. void OcrNode::InitialSundayLine()
  137. {
  138. m_SundayIndex = 0;
  139. INT length = 0;
  140. //search longest line
  141. for(DWORD i = 0;i < m_lists.size();i++)
  142. {
  143. if(m_lists[i]->m_LimitedSize.cx > length)
  144. {
  145. length = m_lists[i]->m_LimitedSize.cx;
  146. m_SundayIndex = i;
  147. }
  148. }
  149. //do the map
  150. m_lists[m_SundayIndex]->MakeSundayLine();
  151. }
  152. BOOL OcrNode::LoadOcrFromFile(const TCHAR *pszFulName)
  153. {
  154. OCRCOLOR mask_color;
  155. //load bitmap
  156. DWORD width,height;
  157. PBYTE pBits = LoadBitMapFromFile(pszFulName,width,height);
  158. if(pBits == NULL)
  159. {
  160. return FALSE;
  161. }
  162. m_size.cx = width;
  163. m_size.cy = height;
  164. //get mask color
  165. OcrScreen screen(pBits,m_size);
  166. delete []pBits;
  167. COLORPOINT cpt;
  168. //top left
  169. cpt.pt.x = 0;
  170. cpt.pt.y = 0;
  171. screen.GetPointColor(cpt);
  172. mask_color = cpt.color;
  173. //top right
  174. cpt.pt.x = width - 1;
  175. screen.GetPointColor(cpt);
  176. if(mask_color != cpt.color)
  177. {
  178. return FALSE;
  179. }
  180. //bottom right
  181. cpt.pt.y = height - 1;
  182. screen.GetPointColor(cpt);
  183. if(mask_color != cpt.color)
  184. {
  185. return FALSE;
  186. }
  187. //bottom left
  188. cpt.pt.x = 0;
  189. screen.GetPointColor(cpt);
  190. if(mask_color != cpt.color)
  191. {
  192. return FALSE;
  193. }
  194. POINT linestartpt;
  195. pBits = new BYTE[8192*sizeof(OCRCOLOR)];//a line
  196. //cut it to each single line
  197. for(DWORD i = 0;i < height;i++)
  198. {
  199. DWORD copyedline = 0;
  200. for(DWORD j = 0;j < width;j++)
  201. {
  202. cpt.pt.x = j;
  203. cpt.pt.y = i;
  204. screen.GetPointColor(cpt);
  205. if(cpt.color != mask_color)
  206. {
  207. //got a line's start pt
  208. if(copyedline == 0)
  209. {
  210. linestartpt = cpt.pt;
  211. }
  212. memcpy(&pBits[copyedline*sizeof(OCRCOLOR)] , &(cpt.color),sizeof(OCRCOLOR));
  213. ++copyedline;
  214. }
  215. else
  216. {
  217. if(copyedline > 0)
  218. {
  219. SIZE linesize;
  220. linesize.cy = 1;
  221. linesize.cx = copyedline;
  222. //a line detected
  223. OcrLine *pLine = new OcrLine(pBits,linestartpt,linesize);
  224. m_lists.push_back(pLine);
  225. m_TotalPixelsCount += copyedline;
  226. copyedline = 0;
  227. }
  228. }
  229. }
  230. }
  231. delete []pBits;
  232. InitialLoadOcrFromFile(pszFulName);
  233. return TRUE;
  234. }
  235. void OcrNode::ClearRecord()
  236. {
  237. ULONG i = 0;
  238. OcrLine *pNode;
  239. for(i = 0;i < m_lists.size();i++)
  240. {
  241. pNode = m_lists[i];
  242. pNode->ClearRecord();
  243. }
  244. }
  245. BOOL OcrNode::CheckMainKeyMatch(OcrScreen &fullPic, POINT &start1, OCRCOLOR color, ULONG sim)
  246. {
  247. if(sim != 100)
  248. {
  249. return TRUE;
  250. }
  251. COLORPOINT temp[4];
  252. memcpy(temp,m_KeyPoints,sizeof(COLORPOINT)*4);
  253. for(int i = 0;i < 4;i++)
  254. {
  255. temp[i].pt.x += start1.x;
  256. temp[i].pt.y += start1.y;
  257. fullPic.GetPointColor(temp[i]);
  258. if(CheckTwoColors(temp[i].color,m_KeyPoints[i].color,color) == FALSE)
  259. {
  260. return FALSE;
  261. }
  262. }
  263. return TRUE;
  264. }
  265. BOOL OcrNode::CheckLineKeyMatch(OcrScreen &fullPic, POINT &start1, OCRCOLOR color, ULONG sim)
  266. {
  267. if(sim != 100)
  268. {
  269. return TRUE;
  270. }
  271. for(ULONG i = 0;i < m_lists.size();i++)
  272. {
  273. if(m_lists[i]->CheckKeyMatch(fullPic,start1,color) == FALSE)
  274. {
  275. return FALSE;
  276. }
  277. }
  278. return TRUE;
  279. }
  280. BOOL OcrNode::CheckLineMatch(OcrScreen &fullPic, POINT &start1, OCRCOLOR color, ULONG sim)
  281. {
  282. for(ULONG i = 0;i < m_lists.size();i++)
  283. {
  284. m_lists[i]->CheckMatch(fullPic,start1,color);
  285. if(GetMatchRate(sim) == FALSE)
  286. {
  287. return FALSE;
  288. }
  289. }
  290. return TRUE;
  291. }
  292. ULONG OcrNode::CheckMatch(OcrScreen &fullPic, POINT &start1, OCRCOLOR color, ULONG sim, string IN &str)
  293. {
  294. ClearRecord();
  295. if(CheckMainKeyMatch(fullPic,start1,color,sim) == TRUE)
  296. {
  297. if(CheckLineKeyMatch(fullPic,start1,color,sim) == TRUE)
  298. {
  299. if(CheckLineMatch(fullPic,start1,color,sim) == TRUE)
  300. {
  301. str = m_NodeName;
  302. return -1;
  303. }
  304. }
  305. }
  306. //do the fast jump
  307. return m_lists[m_SundayIndex]->CheckSundayLine(fullPic,start1,color);
  308. }