OnLineCardPageIdentifier1.cpp 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606
  1. #include "stdafx.h"
  2. #include "OnLineCardPageIdentifier.h"
  3. #include "OnLineCardSchemaStruct.h"
  4. #include "schema_struct.h"
  5. #include "schema_struct_auto.h"
  6. #include <iostream>
  7. #include <vector>
  8. #include <string>
  9. #include <sstream>
  10. #include <zxing\LuminanceSource.h>
  11. #include "GrayImageSource.h"
  12. #include <zxing\Exception.h>
  13. #include <zxing\qrcode\QRCodeReader.h>
  14. #include <zxing\qrcode\QRCodeReader.h>
  15. #include <zxing\BinaryBitmap.h>
  16. #include <zxing\common\GlobalHistogramBinarizer.h>
  17. #include <zxing\Binarizer.h>
  18. #include <zxing\qrcode\QRCodeReader.h>
  19. #include "schema_struct.h"
  20. #include <map>
  21. #include <..\zxinglib\zxing\ReaderException.h>
  22. #include "Util.h"
  23. #include "../lib/myqr/include/GetQRCode.h"
  24. #include "opencv2/opencv.hpp"
  25. #include <chrono>
  26. #include <ctime>
  27. #include "ParseQRInfo.h"
  28. /********************************************************
  29. * @function :
  30. * @brief : 红色分析判断的客户端交互接口(阶段1:实现红色像素的数量统计 阶段2:实现红色的形状判断 目前只实现阶段1
  31. * @input : Mat img_input; // 切割好的打分框区域 精准区域
  32. int cols; // 该打分框的个数
  33. int type; // 打分框所属(非在线模板解答0 非在线模板填空1 在线模板解答2 在线模板填空3 )
  34. int mutil: // 是否是组合型包含(个位+十位) 打分框
  35. int * a_res; // 存放结果的数组
  36. int len; // a_res的长度 len应该等于cols
  37. * @output : int ret; // 0: succ 1: faild 2: img.empty||img.error
  38. * @return : int ret;
  39. * @author : Duste 2020/03/27 10:07
  40. * @History :
  41. *********************************************************/
  42. __declspec(dllimport) int api_red_check_analysis(cv::Mat&img, int cols, int type, int mutil, int * arr_result, int arr_len);
  43. /********************************************************
  44. * @function :
  45. * @brief : DTK的定位点查找
  46. * @input : @见函数说明 给定图像+模板定位点位置信息,
  47. * @output :
  48. * @return : int,(0:succ) (-1:input error) (-2:img.empty||img type.error) (-3:not found)
  49. * @author : Duste 2020/07/02 15:33
  50. * @History :
  51. *********************************************************/
  52. int api_location_anchor_points(const cv::Mat&img_gray, // 输入的灰度图像
  53. const std::vector<cv::Rect>&vec_lcpts, // json内的定位点信息
  54. const cv::Size&mb_size, // json内模板图像的宽高
  55. std::vector<cv::Rect>&vec_tops, // 接受找到的上面三个定位点 下标访问
  56. std::vector<cv::Rect>&vec_bottoms, // 接受找到的下面的两个定位点 下标访问
  57. std::vector<int>&vec_len, // 解决不同版本内存权限问题
  58. int&dir, // 接受识别到的本图像的方向(0123-上下左右)
  59. int top_number, // 知心慧学 top_number=3或4 在线模板 top_number=3
  60. int need_bottom // 是否需要查找bottom定位点
  61. );// Vec_lcpts: json内的两页内所有的定位点的矩形信息合集
  62. #if _DEBUG
  63. #pragma comment(lib,"../lib/myqr/debug/GetQR.lib")
  64. #pragma comment(lib,"../Win32/Debug/client_interactive_api_d.lib")
  65. #else
  66. #pragma comment(lib,"../lib/myqr/Release/GetQR.lib")
  67. #pragma comment(lib,"../Win32/Release/client_interactive_api.lib")
  68. #endif
  69. using namespace cv;
  70. using namespace std;
  71. using namespace zxing;
  72. namespace OnLineCard{
  73. enum SelectType{
  74. //未选中
  75. UNSELECT,
  76. //不确定
  77. UNCERTAIN,
  78. //选中
  79. SELECTED
  80. };
  81. #define MAX_ITEM 64
  82. enum
  83. {
  84. JIEDATITYPE = 0,
  85. TIANKONGTITYPE = 1,
  86. };
  87. void SetTBLR(SchemaQuestionScore& score)
  88. {
  89. score.lt.x = score.lb.x = (int)(0.5 + score.centerx - score.width / 2.0);
  90. score.rt.x = score.rb.x = (int)(0.5 + score.centerx + score.width / 2.0);
  91. score.lt.y = score.rt.y = (int)(0.5 + score.centery - score.height / 2.0);
  92. score.lb.y = score.rb.y = (int)(0.5 + score.centery + score.height / 2.0);
  93. }
  94. COnLineCardPageIdentifier::COnLineCardPageIdentifier()
  95. {
  96. m_pTemplate = nullptr;
  97. src = NULL;
  98. feedDirection = ROTATE_0;
  99. m_strQrClass = "";
  100. m_bUseQr = false;
  101. }
  102. COnLineCardPageIdentifier::~COnLineCardPageIdentifier()
  103. {
  104. }
  105. // 识别
  106. int COnLineCardPageIdentifier::Identify()
  107. {
  108. try{
  109. int r = Identify_impl();
  110. if (r == identify::result::IDF_SUCCESS){
  111. omr_result->identified = TRUE;
  112. omr_result->identify_msg = "识别成功";
  113. return r;
  114. }
  115. }catch (std::exception &e){
  116. return identify::result::IDF_FAILURE;
  117. }
  118. return identify::result::IDF_CANNOT_MATCH_TEMPLATE;
  119. }
  120. void COnLineCardPageIdentifier::SetScanMode(int type)
  121. {
  122. m_scantype = type;
  123. }
  124. void COnLineCardPageIdentifier::SetUseQr(std::string strQr, bool bUse)
  125. {
  126. m_bUseQr = bUse;
  127. m_strQrClass = strQr;
  128. }
  129. void COnLineCardPageIdentifier::SetPaperMode(int mode)
  130. {
  131. m_paper_mode = mode;
  132. }
  133. void COnLineCardPageIdentifier::SetIdentiforMode(int subject, int mode)
  134. {
  135. m_subjectMode = subject;
  136. m_identiforMode = mode;
  137. }
  138. #define ROUND_SCALE(x) ((int)(x*scale+0.5))
  139. #define ROUND_SCALE_ARRAY(_dst,_src,count) \
  140. for (int i=0;i<count;i++)\
  141. {\
  142. _dst[i]=((int)(_src[i]*scale+0.5));\
  143. }
  144. void COnLineCardPageIdentifier::myscale_shema_param0(const schema_const_param& default_schema_const_param, schema_const_param& schema_param, double scale){
  145. schema_param.main_locate_point = default_schema_const_param.main_locate_point;
  146. ROUND_SCALE_ARRAY(schema_param.minw, default_schema_const_param.minw, 4);
  147. ROUND_SCALE_ARRAY(schema_param.maxw, default_schema_const_param.maxw, 4);
  148. ROUND_SCALE_ARRAY(schema_param.minh, default_schema_const_param.minh, 4);
  149. ROUND_SCALE_ARRAY(schema_param.maxh, default_schema_const_param.maxh, 4);
  150. }
  151. //旋转指定区域
  152. int COnLineCardPageIdentifier::xuanzhuan(IplImage * src0, IplImage * &dst_img, CvRect myRect)
  153. {
  154. cvSetImageROI(src0, myRect);
  155. IplImage* result = cvCreateImage(cvSize(myRect.width, myRect.height), src0->depth, src0->nChannels);
  156. cvCopy(src0, result, NULL);
  157. cvResetImageROI(src0);
  158. dst_img = cvCreateImage(cvSize(myRect.height, myRect.width), src0->depth, src0->nChannels);
  159. cvTranspose(result, dst_img);
  160. cvFlip(dst_img, NULL, 1);
  161. cvReleaseImage(&result);
  162. return identify::result::IDF_SUCCESS;
  163. }
  164. inline int get_distance(CvPoint2D32f p1, CvPoint2D32f p2){
  165. double dy = p2.y - p1.y;
  166. double dx = p2.x - p1.x;
  167. return static_cast<int>(sqrt(dx*dx + dy*dy));
  168. }
  169. // 纠偏后的新坐标点
  170. CvPoint2D32f get_new_point(CvPoint2D32f ptOld, int iWidth, int iHight, float thir)
  171. {
  172. // 转图像中心坐标系
  173. CvPoint2D32f ptNew, ptNew_, ptOld_;
  174. CvPoint2D32f ptMid = cvPoint2D32f(iWidth / 2.0, iHight / 2.0);
  175. ptOld_.x = ptOld.x - ptMid.x;
  176. ptOld_.y = -(ptOld.y - ptMid.y);
  177. float thetia = atan2(float(ptOld_.y), float(ptOld_.x));
  178. float angleOld = static_cast<int>( thetia * 180.0 / CV_PI);
  179. float angleNew = angleOld - thir;
  180. double dx = cos(angleNew*CV_PI / 180.0)*ptOld_.y / sin(angleOld*CV_PI / 180.0);
  181. double dy = sin(angleNew*CV_PI / 180.0)*ptOld_.y / sin(angleOld*CV_PI / 180.0);
  182. ptNew_.x = dx > 0 ? (dx + 0.5) : (dx - 0.5);
  183. ptNew_.y = dy > 0 ? (dy + 0.5) : (dy - 0.5);
  184. // 转回原坐标系坐标
  185. // 这里需要重新计算 中心点坐标(计算四个顶点后计算外接矩形再确定中心点)
  186. // 这里简化处理 因为只需要处理 90,180,270,等固定角度旋转
  187. if (90 == abs(thir) || 270 == abs(thir))
  188. ptMid = cvPoint2D32f(iHight / 2.0, iWidth / 2.0);
  189. ptNew.x = ptNew_.x + ptMid.x;
  190. ptNew.y = ptMid.y - ptNew_.y;
  191. return ptNew;
  192. }
  193. bool sort_locate_point_by_x_pt2f(const CvPoint2D32f c1, const CvPoint2D32f c2)
  194. {
  195. return c1.x < c2.x;
  196. }
  197. inline void sort_point_list(CvPoint2D32f * points_src){
  198. int max_y = std::max(std::max(points_src[0].y, points_src[1].y), points_src[2].y);
  199. vector<CvPoint2D32f> vec_one, vec_two;
  200. for (int i = 0; i < 3; i++)
  201. {
  202. CvPoint2D32f tmp = points_src[i];
  203. if (tmp.y > max_y / 2)
  204. vec_two.push_back(tmp);
  205. else
  206. vec_one.push_back(tmp);
  207. }
  208. std::sort(vec_one.begin(), vec_one.end(), sort_locate_point_by_x_pt2f);
  209. for (std::size_t i = 0; i < vec_one.size(); i++)
  210. points_src[i] = vec_one[i];
  211. points_src[vec_one.size()] = vec_two[0];
  212. return;
  213. }
  214. inline cv::Mat IplImageToMat(const IplImage* image, bool copyData = false)
  215. {
  216. if (!image)
  217. return cv::Mat();
  218. int imageDepth = IPL2CV_DEPTH(image->depth);
  219. int type = CV_MAKETYPE(imageDepth, image->nChannels);
  220. cv::Mat ret(image->height, image->width, type, image->imageData, image->widthStep);
  221. return copyData ? ret.clone() : ret;
  222. }
  223. int COnLineCardPageIdentifier::JiaoZheng_20200418_first(IplImage * src0_, IplImage * &dst_img,
  224. const vector<CvRect> & top_locate_point_list, const vector<CvRect> & bottom_locate_point_list, const vector<CvRect> & word_location_point_list,
  225. int dir)
  226. {
  227. // printf("线程:%s JiaoZheng_20200418_first1 \n", std::to_string(GetCurrentThreadId()).c_str());
  228. int bottom_id = 0;
  229. if (3 == dir)
  230. bottom_id = 1;
  231. CvPoint2D32f points_src[3] = {
  232. cvPoint2D32f(top_locate_point_list[0].x + top_locate_point_list[0].width / 2.0, top_locate_point_list[0].y + top_locate_point_list[0].height / 2.0),
  233. cvPoint2D32f(top_locate_point_list[top_locate_point_list.size() - 1].x + top_locate_point_list[top_locate_point_list.size() - 1].width / 2.0, top_locate_point_list[top_locate_point_list.size() - 1].y + top_locate_point_list[top_locate_point_list.size() - 1].height / 2.0),
  234. cvPoint2D32f(bottom_locate_point_list[bottom_id].x + bottom_locate_point_list[bottom_id].width / 2.0, bottom_locate_point_list[bottom_id].y + bottom_locate_point_list[bottom_id].height / 2.0)
  235. };
  236. IplImage * src0 = nullptr;
  237. // printf("线程:%s JiaoZheng_20200418_first2 \n", std::to_string(GetCurrentThreadId()).c_str());
  238. try{
  239. src0 = cvCloneImage(src0_);
  240. }
  241. catch (cv::Exception&e){
  242. // printf("线程:%s cvCloneImage %s\n", std::to_string(GetCurrentThreadId()).c_str(), e.what());
  243. }
  244. // printf("线程:%s JiaoZheng_20200418_first3 \n", std::to_string(GetCurrentThreadId()).c_str());
  245. // 0:上 旋转0度 1:下 旋转180度 2:左 旋转90度 3:右 旋转270度 如果不对请自行调整
  246. CvPoint2D32f points_src_rotate[3] = { points_src[0], points_src[1], points_src[2] };
  247. // printf("线程:%s JiaoZheng_20200418_first cvCreateImage dir=%d %d %d %d %d \n", std::to_string(GetCurrentThreadId()).c_str(), dir, src0->width, src0->height, src0->depth, src0->nChannels);
  248. float angle = 0.0;
  249. if (0 == dir) {
  250. angle = 0.0;
  251. dst_img = cvCreateImage(cvSize(src0->width, src0->height), src0->depth, src0->nChannels);
  252. }
  253. else if (1 == dir){
  254. angle = 180;
  255. //cvFlip(src0,src0,1);
  256. dst_img = cvCreateImage(cvSize(src0->width, src0->height), src0->depth, src0->nChannels);
  257. }
  258. else if (2 == dir) {
  259. angle = 90;
  260. dst_img = cvCreateImage(cvSize(src0->height, src0->width), src0->depth, src0->nChannels);
  261. }
  262. else if (3 == dir) {
  263. angle = 270;
  264. dst_img = cvCreateImage(cvSize(src0->height, src0->width), src0->depth, src0->nChannels);
  265. }
  266. else
  267. return identify::result::IDF_FAILURE;
  268. // printf("线程:%s JiaoZheng_20200418_first cvCreateImage end \n", std::to_string(GetCurrentThreadId()).c_str());
  269. if (angle > 1.0){
  270. for (int i = 0; i < 3; i++){
  271. points_src_rotate[i] = get_new_point(points_src_rotate[i], src0->width, src0->height, angle);
  272. }
  273. }
  274. // printf("线程:%s JiaoZheng_20200418_first4 \n", std::to_string(GetCurrentThreadId()).c_str());
  275. CvPoint2D32f points_dst[3];
  276. for (int i = 0; i < 3; i++){
  277. CvPoint2D32f pt_src_tmp = points_src_rotate[i];
  278. int nearst_id = -1;
  279. int nearst_distance = 100000000;
  280. for (std::size_t j = 0; j < word_location_point_list.size(); j++){
  281. CvPoint2D32f pt_dst_tmp = cvPoint2D32f(word_location_point_list[j].x + word_location_point_list[j].width / 2.0, word_location_point_list[j].y + word_location_point_list[j].height / 2.0);
  282. int distance = get_distance(pt_src_tmp, pt_dst_tmp);
  283. if (distance < nearst_distance){
  284. nearst_distance = distance;
  285. nearst_id = j;
  286. }
  287. }
  288. if (-1 == nearst_id)
  289. return identify::result::IDF_FAILURE;
  290. points_dst[i] = cvPoint2D32f(word_location_point_list[nearst_id].x + word_location_point_list[nearst_id].width / 2.0, word_location_point_list[nearst_id].y + word_location_point_list[nearst_id].height / 2.0);;
  291. }
  292. // printf("线程:%s JiaoZheng_20200418_first5 \n", std::to_string(GetCurrentThreadId()).c_str());
  293. CvMat * warp_mat = cvCreateMat(2, 3, CV_32FC1);
  294. cvGetAffineTransform(points_src, points_dst, warp_mat);
  295. cvWarpAffine(src0, dst_img, warp_mat, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS, cvScalarAll(255));
  296. cvReleaseMat(&warp_mat);
  297. #if _DEBUG
  298. cvSaveImage("D:\\show\\1.png", src0);
  299. cvSaveImage("D:\\show\\2.png", dst_img);
  300. #endif
  301. cvReleaseImage(&src0);
  302. // printf("线程:%s JiaoZheng_20200418_first6 \n", std::to_string(GetCurrentThreadId()).c_str());
  303. return identify::result::IDF_SUCCESS;
  304. }
  305. int COnLineCardPageIdentifier::JiaoZheng_20200418_second_four(IplImage * src0_, IplImage * &dst_img,
  306. const vector<CvRect> & top_locate_point_list, const vector<CvRect> & bottom_locate_point_list, const vector<CvRect> & word_location_point_list,
  307. int dir)
  308. {
  309. //CV_Assert(top_locate_point_list.size()>2);
  310. //CV_Assert(bottom_locate_point_list.size());
  311. CvPoint2D32f points_src[4] = {
  312. cvPoint2D32f(top_locate_point_list[0].x + top_locate_point_list[0].width / 2.0, top_locate_point_list[0].y + top_locate_point_list[0].height / 2.0),
  313. cvPoint2D32f(top_locate_point_list[top_locate_point_list.size() - 1].x + top_locate_point_list[top_locate_point_list.size() - 1].width / 2.0, top_locate_point_list[top_locate_point_list.size() - 1].y + top_locate_point_list[top_locate_point_list.size() - 1].height / 2.0),
  314. cvPoint2D32f(bottom_locate_point_list[0].x + bottom_locate_point_list[0].width / 2.0, bottom_locate_point_list[0].y + bottom_locate_point_list[0].height / 2.0),
  315. cvPoint2D32f(bottom_locate_point_list[1].x + bottom_locate_point_list[1].width / 2.0, bottom_locate_point_list[1].y + bottom_locate_point_list[1].height / 2.0)
  316. };
  317. IplImage * src0 = cvCloneImage(src0_);
  318. // 0:上 旋转0度 1:下 旋转180度 2:左 旋转90度 3:右 旋转270度 如果不对请自行调整
  319. CvPoint2D32f points_src_rotate[4] = { points_src[0], points_src[1], points_src[2], points_src[3] };
  320. float angle = 0.0;
  321. if (0 == dir) {
  322. angle = 0.0;
  323. dst_img = cvCreateImage(cvSize(src0->width, src0->height), src0->depth, src0->nChannels);
  324. }
  325. else if (1 == dir){
  326. angle = 180;
  327. //cvFlip(src0,src0,1);
  328. dst_img = cvCreateImage(cvSize(src0->width, src0->height), src0->depth, src0->nChannels);
  329. }
  330. else if (2 == dir) {
  331. angle = 90;
  332. dst_img = cvCreateImage(cvSize(src0->height, src0->width), src0->depth, src0->nChannels);
  333. }
  334. else if (3 == dir) {
  335. angle = 270;
  336. dst_img = cvCreateImage(cvSize(src0->height, src0->width), src0->depth, src0->nChannels);
  337. }
  338. else
  339. return identify::result::IDF_FAILURE;
  340. if (angle > 1.0){
  341. for (int i = 0; i < 4; i++){
  342. points_src_rotate[i] = get_new_point(points_src_rotate[i], src0->width, src0->height, angle);
  343. }
  344. }
  345. CvPoint2D32f points_dst[4];
  346. for (std::size_t i = 0; i < 4; i++){
  347. CvPoint2D32f pt_src_tmp = points_src_rotate[i];
  348. int nearst_id = -1;
  349. int nearst_distance = 100000000;
  350. for (std::size_t j = 0; j < word_location_point_list.size(); j++){
  351. CvPoint2D32f pt_dst_tmp = cvPoint2D32f(word_location_point_list[j].x + word_location_point_list[j].width / 2.0, word_location_point_list[j].y + word_location_point_list[j].height / 2.0);
  352. int distance = get_distance(pt_src_tmp, pt_dst_tmp);
  353. if (distance < nearst_distance){
  354. nearst_distance = distance;
  355. nearst_id = j;
  356. }
  357. }
  358. if (-1 == nearst_id)
  359. return identify::result::IDF_FAILURE;
  360. points_dst[i] = cvPoint2D32f(word_location_point_list[nearst_id].x + word_location_point_list[nearst_id].width / 2.0, word_location_point_list[nearst_id].y + word_location_point_list[nearst_id].height / 2.0);;
  361. }
  362. Point2f pts[4];
  363. for (int i = 0; i < 4; i++)
  364. pts[i] = Point2f(points_src[i].x, points_src[i].y);
  365. Point2f ptd[4];
  366. for (int i = 0; i < 4; i++)
  367. ptd[i] = Point2f(points_dst[i].x, points_dst[i].y);
  368. cv::Mat warp_mat(cv::Size(3, 3), CV_32FC1);
  369. warp_mat = getPerspectiveTransform(pts, ptd);
  370. CvMat b = warp_mat;
  371. cvWarpPerspective(src0_, dst_img, &b, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS, cvScalarAll(255));
  372. #if _DEBUG
  373. cvSaveImage("D:\\show\\1.png", src0);
  374. cvSaveImage("D:\\show\\2.png", dst_img);
  375. #endif
  376. cvReleaseImage(&src0);
  377. return identify::result::IDF_SUCCESS;
  378. }
  379. int COnLineCardPageIdentifier::JiaoZheng_20200418_second_five(IplImage * src0_, IplImage * &dst_img,
  380. const vector<CvRect> & top_locate_point_list, const vector<CvRect> & bottom_locate_point_list, const vector<CvRect> & word_location_point_list,
  381. int dir)
  382. {
  383. //CV_Assert(top_locate_point_list.size()>2);
  384. //CV_Assert(bottom_locate_point_list.size());
  385. CvPoint2D32f points_src[5] = {
  386. cvPoint2D32f(top_locate_point_list[0].x + top_locate_point_list[0].width / 2.0, top_locate_point_list[0].y + top_locate_point_list[0].height / 2.0),
  387. cvPoint2D32f(top_locate_point_list[1].x + top_locate_point_list[1].width / 2.0, top_locate_point_list[1].y + top_locate_point_list[1].height / 2.0),
  388. cvPoint2D32f(top_locate_point_list[top_locate_point_list.size() - 1].x + top_locate_point_list[top_locate_point_list.size() - 1].width / 2.0, top_locate_point_list[top_locate_point_list.size() - 1].y + top_locate_point_list[top_locate_point_list.size() - 1].height / 2.0),
  389. cvPoint2D32f(bottom_locate_point_list[0].x + bottom_locate_point_list[0].width / 2.0, bottom_locate_point_list[0].y + bottom_locate_point_list[0].height / 2.0),
  390. cvPoint2D32f(bottom_locate_point_list[1].x + bottom_locate_point_list[1].width / 2.0, bottom_locate_point_list[1].y + bottom_locate_point_list[1].height / 2.0)
  391. };
  392. IplImage * src0 = cvCloneImage(src0_);
  393. // 0:上 旋转0度 1:下 旋转180度 2:左 旋转90度 3:右 旋转270度 如果不对请自行调整
  394. CvPoint2D32f points_src_rotate[5] = { points_src[0], points_src[1], points_src[2], points_src[3], points_src[4] };
  395. float angle = 0.0;
  396. if (0 == dir) {
  397. angle = 0.0;
  398. dst_img = cvCreateImage(cvSize(src0->width, src0->height), src0->depth, src0->nChannels);
  399. }
  400. else if (1 == dir){
  401. angle = 180;
  402. //cvFlip(src0,src0,1);
  403. dst_img = cvCreateImage(cvSize(src0->width, src0->height), src0->depth, src0->nChannels);
  404. }
  405. else if (2 == dir) {
  406. angle = 90;
  407. dst_img = cvCreateImage(cvSize(src0->height, src0->width), src0->depth, src0->nChannels);
  408. }
  409. else if (3 == dir) {
  410. angle = 270;
  411. dst_img = cvCreateImage(cvSize(src0->height, src0->width), src0->depth, src0->nChannels);
  412. }
  413. else
  414. return identify::result::IDF_FAILURE;
  415. if (angle > 1.0){
  416. for (int i = 0; i < 5; i++){
  417. points_src_rotate[i] = get_new_point(points_src_rotate[i], src0->width, src0->height, angle);
  418. }
  419. }
  420. CvPoint2D32f points_dst[5];
  421. for (std::size_t i = 0; i < 5; i++){
  422. CvPoint2D32f pt_src_tmp = points_src_rotate[i];
  423. int nearst_id = -1;
  424. int nearst_distance = 100000000;
  425. for (std::size_t j = 0; j < word_location_point_list.size(); j++){
  426. CvPoint2D32f pt_dst_tmp = cvPoint2D32f(word_location_point_list[j].x + word_location_point_list[j].width / 2.0, word_location_point_list[j].y + word_location_point_list[j].height / 2.0);
  427. int distance = get_distance(pt_src_tmp, pt_dst_tmp);
  428. if (distance < nearst_distance){
  429. nearst_distance = distance;
  430. nearst_id = j;
  431. }
  432. }
  433. if (-1 == nearst_id)
  434. return identify::result::IDF_FAILURE;
  435. points_dst[i] = cvPoint2D32f(word_location_point_list[nearst_id].x + word_location_point_list[nearst_id].width / 2.0, word_location_point_list[nearst_id].y + word_location_point_list[nearst_id].height / 2.0);;
  436. }
  437. Point2f pts[5];
  438. for (int i = 0; i < 5; i++)
  439. pts[i] = Point2f(points_src[i].x, points_src[i].y);
  440. Point2f ptd[5];
  441. for (int i = 0; i < 5; i++)
  442. ptd[i] = Point2f(points_dst[i].x, points_dst[i].y);
  443. cv::Mat warp_mat(cv::Size(3, 3), CV_32FC1);
  444. warp_mat = getPerspectiveTransform(pts, ptd);
  445. CvMat b = warp_mat;
  446. cvWarpPerspective(src0_, dst_img, &b, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS, cvScalarAll(255));
  447. #if _DEBUG
  448. cvSaveImage("D:\\show\\1.png", src0);
  449. cvSaveImage("D:\\show\\2.png", dst_img);
  450. #endif
  451. cvReleaseImage(&src0);
  452. return identify::result::IDF_SUCCESS;
  453. }
  454. int COnLineCardPageIdentifier::JiaoZheng_20200418(IplImage * src0, IplImage * &dst_img,
  455. const vector<CvRect> & top_locate_point_list, const vector<CvRect> & bottom_locate_point_list, const vector<CvRect> & word_location_point_list,
  456. int dir, double scale_x, double scale_y, int word_w, int word_h)
  457. {
  458. int len_l = top_locate_point_list.size();
  459. if (len_l != 3 || bottom_locate_point_list.size() != 2)
  460. return identify::result::IDF_FAILURE;
  461. // 中心点
  462. CvPoint2D32f points_src_[3] = {
  463. cvPoint2D32f(top_locate_point_list[0].x + top_locate_point_list[0].width / 2.0, top_locate_point_list[0].y + top_locate_point_list[0].height / 2.0),
  464. cvPoint2D32f(top_locate_point_list[len_l - 1].x + top_locate_point_list[len_l - 1].width / 2.0, top_locate_point_list[len_l - 1].y + top_locate_point_list[len_l - 1].height / 2.0),
  465. cvPoint2D32f(bottom_locate_point_list[0].x + bottom_locate_point_list[0].width / 2.0, bottom_locate_point_list[0].y + bottom_locate_point_list[0].height / 2.0)
  466. };
  467. cv::Size location_size[3] = {
  468. cvSize(top_locate_point_list[0].width, top_locate_point_list[0].height),
  469. cvSize(top_locate_point_list[len_l - 1].width, top_locate_point_list[len_l - 1].height),
  470. cvSize(bottom_locate_point_list[0].width, bottom_locate_point_list[0].height)
  471. };
  472. // 0:上 旋转0度 1:下 旋转180度 2:左 旋转90度 3:右 旋转270度 如果不对请自行调整
  473. CvPoint2D32f points_src_rotate[3] = { points_src_[0], points_src_[1], points_src_[2] };
  474. float angle = 0.0;
  475. int use_w = src0->width;
  476. int use_h = src0->height;
  477. if (0 == dir) {
  478. angle = 0.0;
  479. }
  480. else if (1 == dir){
  481. angle = 180;
  482. }
  483. else if (2 == dir) {
  484. angle = 90;
  485. swap(use_w, use_h);
  486. for (int i = 0; i < 3; i++)
  487. swap(location_size[i].width, location_size[i].height);
  488. }
  489. else if (3 == dir) {
  490. angle = 270;
  491. swap(use_w, use_h);
  492. for (int i = 0; i < 3; i++)
  493. swap(location_size[i].width, location_size[i].height);
  494. }
  495. else
  496. return identify::result::IDF_FAILURE;
  497. if (angle > 1.0){
  498. for (int i = 0; i < 3; i++){
  499. points_src_rotate[i] = get_new_point(points_src_rotate[i], src0->width, src0->height, angle);
  500. }
  501. }
  502. // 按照从左到右 从上到下 排序
  503. sort_point_list(points_src_rotate);
  504. // 推算边缘点
  505. CvPoint2D32f points_src[3] = {
  506. cvPoint2D32f(points_src_rotate[0].x - (location_size[0].width / 2.0 + 0.55), top_locate_point_list[0].y - (location_size[0].height / 2.0 + 0.55)),
  507. cvPoint2D32f(points_src_rotate[1].x + (location_size[1].width / 2.0 + 0.55), top_locate_point_list[1].y - (location_size[1].height / 2.0 + 0.55)),
  508. cvPoint2D32f(points_src_rotate[2].x - (location_size[2].width / 2.0 + 0.55), top_locate_point_list[2].y + (location_size[2].height / 2.0 + 0.55))
  509. };
  510. // 寻找json数据的对应定位点坐标
  511. CvPoint2D32f points_dst_[3]; // 中心点
  512. CvPoint2D32f points_dst[3]; // 边缘点
  513. for (std::size_t i = 0; i < 3; i++){
  514. CvPoint2D32f pt_src_tmp = points_src_rotate[i];
  515. int nearst_id = -1;
  516. int nearst_distance = 100000000;
  517. for (std::size_t j = 0; j < word_location_point_list.size(); j++){
  518. CvRect rc_tmp;
  519. rc_tmp.x = word_location_point_list[j].x*scale_x + 0.55;
  520. rc_tmp.y = word_location_point_list[j].y*scale_y + 0.55;
  521. rc_tmp.width = word_location_point_list[j].width*scale_x + 0.55;
  522. rc_tmp.height = word_location_point_list[j].height*scale_y + 0.55;
  523. CvPoint2D32f pt_dst_tmp = cvPoint2D32f(rc_tmp.x + rc_tmp.width / 2.0, rc_tmp.y + rc_tmp.height / 2.0);
  524. int distance = get_distance(pt_src_tmp, pt_dst_tmp);
  525. if (distance < nearst_distance){
  526. nearst_distance = distance;
  527. nearst_id = j;
  528. }
  529. }
  530. if (-1 == nearst_id)
  531. return identify::result::IDF_FAILURE;
  532. points_dst_[i] = cvPoint2D32f(word_location_point_list[nearst_id].x + word_location_point_list[nearst_id].width / 2.0, word_location_point_list[nearst_id].y + word_location_point_list[nearst_id].height / 2.0);;
  533. if (0 == i)
  534. points_dst[i] = cvPoint2D32f(word_location_point_list[nearst_id].x, word_location_point_list[nearst_id].y);
  535. else if (1 == i)
  536. points_dst[i] = cvPoint2D32f(word_location_point_list[nearst_id].x + word_location_point_list[nearst_id].width, word_location_point_list[nearst_id].y);
  537. else
  538. points_dst[i] = cvPoint2D32f(word_location_point_list[nearst_id].x, word_location_point_list[nearst_id].y + word_location_point_list[nearst_id].height);
  539. }
  540. #define GET_SCALE 1
  541. #if GET_SCALE
  542. int len = 3;
  543. int dis_w = get_distance(points_src_rotate[1], points_src_rotate[0]);
  544. int dis_h = get_distance(points_src_rotate[0], points_src_rotate[2]);
  545. int dis_ww = get_distance(points_dst[1], points_dst[0]);
  546. int dis_hw = get_distance(points_dst[0], points_dst[2]);
  547. double s_x = dis_w*1.0 / dis_ww*1.0;
  548. double s_y = dis_h*1.0 / dis_hw*1.0;
  549. int add_left = points_src_rotate[0].x - points_dst[0].x*s_x;
  550. int dis_w_right = (word_w - points_dst[1].x)*s_x;
  551. int dis_s_right = use_w - points_src_rotate[1].x;
  552. int add_right = dis_s_right - dis_w_right;
  553. int add_top = points_src_rotate[0].y - points_dst[0].y*s_y;
  554. int dis_w_bottom = (word_h - points_dst[1].y)*s_y;
  555. int dis_s_bottom = use_h - points_src_rotate[1].y;
  556. int add_bottom = dis_s_bottom - dis_w_bottom;
  557. IplImage* lp_after_warp = NULL;
  558. int xn_w = use_w - add_left - add_right;
  559. int xn_h = use_h - add_bottom - add_top;
  560. CvRect rc_box;//= cvRect(add_left,add_top,src0->width-add_left-add_right,src0->height-add_top-add_bottom);
  561. // 旋转矩形
  562. // 新的水平坐标点更新回 切割后的原图
  563. points_src_[0] = cvPoint2D32f(points_src_rotate[0].x - add_left, points_src_rotate[0].y - add_top);
  564. points_src_[1] = cvPoint2D32f(points_src_rotate[1].x - add_left, points_src_rotate[1].y - add_top);
  565. points_src_[2] = cvPoint2D32f(points_src_rotate[2].x - add_left, points_src_rotate[2].y - add_top);
  566. angle = 0.0;
  567. if (0 == dir) {
  568. rc_box = cvRect(add_left, add_top, src0->width - add_left - add_right, src0->height - add_top - add_bottom);
  569. lp_after_warp = cvCreateImage(cvSize(rc_box.width, rc_box.height), src0->depth, src0->nChannels);
  570. dst_img = cvCreateImage(cvSize(lp_after_warp->width, lp_after_warp->height), lp_after_warp->depth, lp_after_warp->nChannels);
  571. }
  572. else if (1 == dir){
  573. angle = -180;
  574. rc_box = cvRect(add_right, add_bottom, src0->width - add_left - add_right, src0->height - add_top - add_bottom);
  575. lp_after_warp = cvCreateImage(cvSize(rc_box.width, rc_box.height), src0->depth, src0->nChannels);
  576. dst_img = cvCreateImage(cvSize(lp_after_warp->width, lp_after_warp->height), lp_after_warp->depth, lp_after_warp->nChannels);
  577. }
  578. else if (2 == dir) {
  579. angle = -90;
  580. rc_box = cvRect(add_top, add_right, src0->width - add_top - add_bottom, src0->height - add_right - add_left);
  581. lp_after_warp = cvCreateImage(cvSize(rc_box.width, rc_box.height), src0->depth, src0->nChannels);
  582. dst_img = cvCreateImage(cvSize(lp_after_warp->height, lp_after_warp->width), lp_after_warp->depth, lp_after_warp->nChannels);
  583. }
  584. else if (3 == dir) {
  585. angle = -270;
  586. rc_box = cvRect(add_bottom, add_left, src0->width - add_bottom - add_top, src0->height - add_left - add_right);
  587. lp_after_warp = cvCreateImage(cvSize(rc_box.width, rc_box.height), src0->depth, src0->nChannels);
  588. dst_img = cvCreateImage(cvSize(lp_after_warp->height, lp_after_warp->width), lp_after_warp->depth, lp_after_warp->nChannels);
  589. }
  590. else
  591. return identify::result::IDF_FAILURE;
  592. if (angle < -1.0){
  593. for (int i = 0; i < 3; i++){
  594. points_src_[i] = get_new_point(points_src_[i], xn_w, xn_h, angle);
  595. }
  596. }
  597. cvSetImageROI(src0, rc_box);
  598. cvCopy(src0, lp_after_warp, NULL);
  599. #if _DEBUG
  600. cvSaveImage("D:\\show\\0.png", lp_after_warp);
  601. #endif
  602. cvResetImageROI(src0);
  603. //}
  604. use_w = lp_after_warp->width;
  605. use_h = lp_after_warp->height;
  606. if (dir > 1)
  607. swap(use_h, use_w);
  608. double d_x = use_w*1.0 / word_w;
  609. double d_y = use_h*1.0 / word_h;
  610. points_dst_[0] = cvPoint2D32f(points_dst_[0].x*d_x + 0.55, points_dst_[0].y*d_y + 0.55);
  611. points_dst_[1] = cvPoint2D32f(points_dst_[1].x*d_x + 0.55, points_dst_[1].y*d_y + 0.55);
  612. points_dst_[2] = cvPoint2D32f(points_dst_[2].x*d_x + 0.55, points_dst_[2].y*d_y + 0.55);
  613. #endif
  614. CvMat * warp_mat = cvCreateMat(2, 3, CV_32FC1);
  615. cvGetAffineTransform(points_src_, points_dst_, warp_mat);
  616. cvWarpAffine(lp_after_warp, dst_img, warp_mat, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS, cvScalarAll(255));
  617. cvReleaseMat(&warp_mat);
  618. cvReleaseImage(&lp_after_warp);
  619. #if _DEBUG
  620. cvSaveImage("D:\\show\\1.png", src0);
  621. cvSaveImage("D:\\show\\2.png", dst_img);
  622. #endif
  623. return identify::result::IDF_SUCCESS;
  624. }
  625. int COnLineCardPageIdentifier::MyFindDingWeiDian(IplImage * src_gray_img, CvMemStorage* storage, schema_const_param &schema_param, vector<CvRect>& locate_point_list, int & _dir)
  626. {
  627. CvRect roi[4] = {//上下左右 roi
  628. cvRect(0, 0, src_gray_img->width, min(300, src_gray_img->height)),
  629. cvRect(0, src_gray_img->height - min(300, src_gray_img->height) - 1, src_gray_img->width, min(300, src_gray_img->height)),
  630. cvRect(0, 0, min(200, src_gray_img->width), src_gray_img->height),
  631. cvRect(src_gray_img->width - min(300, src_gray_img->width) - 1, 0, min(300, src_gray_img->width), src_gray_img->height)
  632. };
  633. CvSeq * contour = NULL;
  634. for (int dir = 0; dir < 4; dir++)
  635. {
  636. if (dir < 2)myscale_shema_param0(m_default_schema_param, schema_param, src_gray_img->width / (double)m_default_schema_param.shijuanwidth);
  637. else myscale_shema_param0(m_default_schema_param, schema_param, src_gray_img->height / (double)m_default_schema_param.shijuanwidth);
  638. IplImage * binary_img = cvCreateImage(cvSize(roi[dir].width + 10, roi[dir].height + 10), IPL_DEPTH_8U, 1);
  639. IplImage * binary_img2 = cvCreateImage(cvSize(roi[dir].width + 10, roi[dir].height + 10), IPL_DEPTH_8U, 1);
  640. cvSet(binary_img, cvScalarAll(0));
  641. cvSetImageROI(src_gray_img, roi[dir]);
  642. cvSetImageROI(binary_img, cvRect(5, 5, roi[dir].width, roi[dir].height));
  643. cvThreshold(src_gray_img, binary_img, 180, 255, CV_THRESH_BINARY_INV);
  644. cvResetImageROI(binary_img);
  645. int an = 2;
  646. IplConvKernel * element = cvCreateStructuringElementEx(an * 2 + 1, an * 2 + 1, an, an, CV_SHAPE_RECT, 0);//创建结构元素
  647. cvErode(binary_img, binary_img2, element, 1);//腐蚀图像
  648. cvDilate(binary_img2, binary_img2, element, 1);//膨胀图像
  649. cvSub(binary_img, binary_img2, binary_img2);
  650. cvDilate(binary_img, binary_img, element, 1);//膨胀图像
  651. cvErode(binary_img, binary_img, element, 1);//腐蚀图像
  652. cvSub(binary_img, binary_img2, binary_img);
  653. if (element)
  654. cvReleaseStructuringElement(&element);
  655. #ifdef _DEBUG
  656. cvSaveImage("D:\\Debug.jpg",binary_img);
  657. #endif
  658. int contours = cvFindContours(binary_img, storage, &contour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(roi[dir].x - 5, roi[dir].y - 5));
  659. cvReleaseImage(&binary_img);
  660. cvReleaseImage(&binary_img2);
  661. int validCount = 0;
  662. vector<CvContour *> contourList;
  663. for (CvContour * c = (CvContour *)contour; c != 0; c = (CvContour *)c->h_next)
  664. {
  665. locate_point_list.clear();
  666. if (c->rect.width > schema_param.maxw[dir])continue;
  667. if (c->rect.height > schema_param.maxh[dir])continue;
  668. if (c->rect.width < schema_param.minw[dir] * 0.7)continue;
  669. if (c->rect.height < schema_param.minh[dir] * 0.7)continue;
  670. cvSetImageROI(src_gray_img, c->rect);
  671. int area = GetBlackArea(src_gray_img);
  672. if (area < c->rect.width*c->rect.height*0.75)continue;
  673. validCount++;
  674. contourList.push_back(c);
  675. }
  676. cvResetImageROI(src_gray_img);
  677. std::vector<CvRect> list;
  678. for (int i = 0; i < contourList.size(); i++)
  679. {
  680. CvRect rect = contourList[i]->rect;
  681. list.push_back(rect);
  682. }
  683. CvRect rect;
  684. float k = 0, b = 0;
  685. if (dir == 0 || dir == 1){
  686. std::sort(list.begin(), list.end(), sort_locate_point_by_x);
  687. }
  688. else {
  689. std::sort(list.begin(), list.end(), sort_locate_point_by_y);
  690. }
  691. if (list.size() > 1)
  692. {
  693. bool bV = false;
  694. locate_point_list.push_back(list[0]);
  695. if (list[list.size() - 1].x - list[0].x>-0.00001 && list[list.size() - 1].x - list[0].x<0.00001)
  696. {
  697. bV = true;
  698. }
  699. else{
  700. k = (list[list.size() - 1].y - list[0].y + 0.0) / (list[list.size() - 1].x - list[0].x);
  701. }
  702. b = list[0].y - k*list[0].x;
  703. for (int i = 1; i < list.size() - 1; i++)
  704. {
  705. if (dir == 0 || dir == 1){
  706. int y = k*list[i].x + b;
  707. if (fabs(y - list[i].y + 0.0) < list[i].height / 2)
  708. locate_point_list.push_back(list[i]);
  709. }
  710. else
  711. {
  712. int x = 0;
  713. if (bV){
  714. x = list[0].x;
  715. }
  716. else{
  717. x =(list[i].y - b) / k;
  718. }
  719. if (fabs(x - list[i].x + 0.0) < list[i].width / 2)
  720. locate_point_list.push_back(list[i]);
  721. }
  722. }
  723. locate_point_list.push_back(list[list.size() - 1]);
  724. }
  725. if (locate_point_list.size() == 3)
  726. {
  727. _dir = dir;
  728. if (MyFindBottomDingWeiDian(src_gray_img, storage, schema_param, locate_point_list, dir) == identify::result::IDF_FAILURE)
  729. return identify::result::IDF_FAILURE;
  730. return identify::result::IDF_SUCCESS;
  731. break;
  732. }
  733. }
  734. return identify::result::IDF_FAILURE;
  735. }
  736. //查找定位点
  737. int COnLineCardPageIdentifier::MyFindBottomDingWeiDian(IplImage * src_gray_img, CvMemStorage* storage, schema_const_param &schema_param, vector<CvRect>& locate_point_list, int & _dir)
  738. {
  739. CvRect roi[4] = {//上下左右 roi
  740. cvRect(0, 0, src_gray_img->width, min(300, src_gray_img->height)),
  741. cvRect(0, src_gray_img->height - min(300, src_gray_img->height) - 1, src_gray_img->width, min(300, src_gray_img->height)),
  742. cvRect(0, 0, min(300, src_gray_img->width), src_gray_img->height),
  743. cvRect(src_gray_img->width - min(300, src_gray_img->width) - 1, 0, min(300, src_gray_img->width), src_gray_img->height)
  744. };
  745. CvSeq * contour = NULL;
  746. int dir;
  747. _dir > 1 ? dir = 3 - _dir + 2 : dir = 1 - _dir;
  748. m_vecBottom.clear();
  749. //for (int dir=0;dir<4;dir++)
  750. {
  751. IplImage * binary_img = cvCreateImage(cvSize(roi[dir].width + 10, roi[dir].height + 10), IPL_DEPTH_8U, 1);
  752. IplImage * binary_img2 = cvCreateImage(cvSize(roi[dir].width + 10, roi[dir].height + 10), IPL_DEPTH_8U, 1);
  753. cvSet(binary_img, cvScalarAll(0));
  754. cvSetImageROI(src_gray_img, roi[dir]);
  755. cvSetImageROI(binary_img, cvRect(5, 5, roi[dir].width, roi[dir].height));
  756. cvThreshold(src_gray_img, binary_img, 180, 255, CV_THRESH_BINARY_INV);
  757. cvResetImageROI(binary_img);
  758. int an = 2;
  759. IplConvKernel * element = cvCreateStructuringElementEx(an * 2 + 1, an * 2 + 1, an, an, CV_SHAPE_RECT, 0);//创建结构元素
  760. cvErode(binary_img, binary_img2, element, 1);//腐蚀图像
  761. cvDilate(binary_img2, binary_img2, element, 1);//膨胀图像
  762. cvSub(binary_img, binary_img2, binary_img2);
  763. cvDilate(binary_img, binary_img, element, 1);//膨胀图像
  764. cvErode(binary_img, binary_img, element, 1);//腐蚀图像
  765. cvSub(binary_img, binary_img2, binary_img);
  766. int contours = cvFindContours(binary_img, storage, &contour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(roi[dir].x - 5, roi[dir].y - 5));
  767. if (element)
  768. cvReleaseStructuringElement(&element);
  769. cvReleaseImage(&binary_img);
  770. cvReleaseImage(&binary_img2);
  771. int validCount = 0;
  772. vector<CvContour *> contourList;
  773. for (CvContour * c = (CvContour *)contour; c != 0; c = (CvContour *)c->h_next)
  774. {
  775. if (c->rect.width > schema_param.maxw[dir])continue;
  776. if (c->rect.height > schema_param.maxh[dir])continue;
  777. if (c->rect.width < schema_param.minw[dir] * 0.7)continue;
  778. if (c->rect.height < schema_param.minh[dir] * 0.7)continue;
  779. cvSetImageROI(src_gray_img, c->rect);
  780. //cvSaveImage( "d://debug1.jpg", binary_img );
  781. int area = GetBlackArea(src_gray_img);
  782. if (area < c->rect.width*c->rect.height*0.75)continue;
  783. validCount++;
  784. contourList.push_back(c);
  785. }
  786. cvResetImageROI(src_gray_img);
  787. if (contourList.size() == 2){
  788. for (std::size_t i = 0; i < contourList.size(); i++){
  789. CvRect rect = contourList[i]->rect;
  790. m_vecBottom.push_back(rect);
  791. }
  792. if (m_vecBottom.size() > 1){
  793. std::sort(m_vecBottom.begin(), m_vecBottom.end(), sort_locate_point_by_x);
  794. if (_dir == 1)
  795. std::sort(m_vecBottom.begin(), m_vecBottom.end(), sort_locate_point_by_x);
  796. }
  797. return identify::result::IDF_SUCCESS;
  798. }
  799. else
  800. {
  801. return identify::result::IDF_FAILURE;
  802. }
  803. }
  804. }
  805. int COnLineCardPageIdentifier::OnLineCardJZ(IplImage* src, IplImage* &dst_img, int nPageIndex, int dir, std::vector<CvRect>&main_locate_point, int &wordloctlt)
  806. {
  807. int ret = identify::result::IDF_FAILURE;
  808. std::vector<CvRect> vec_locations_word; vec_locations_word.clear();
  809. double tt[4] = { 0.0, 0.0, 0.0, 0.0 };
  810. if (!m_pTemplate->pages.empty()){
  811. PageTemplate&page = m_pTemplate->pages[nPageIndex];
  812. std::vector<Location>& lcs = page.location;
  813. for (std::size_t j = 0; j < lcs.size(); j++){
  814. CvRect rc_tmp;
  815. rc_tmp.x = lcs[j].pos.x;
  816. rc_tmp.y = lcs[j].pos.y;
  817. rc_tmp.width = lcs[j].pos.w;
  818. rc_tmp.height = lcs[j].pos.h;
  819. vec_locations_word.push_back(rc_tmp);
  820. }
  821. wordloctlt = vec_locations_word[3].y - vec_locations_word[0].y;
  822. double word_w = page.w;
  823. double word_h = page.h;
  824. tt[2] = word_w;
  825. tt[3] = word_h;
  826. // resize point
  827. int mb_w = src->width;
  828. int mb_h = src->height;
  829. if (2 == dir || 3 == dir) //
  830. swap(mb_h, mb_w);
  831. float rio_w = mb_w*1.0 / word_w*1.0;
  832. float rio_h = mb_h*1.0 / word_h*1.0;
  833. tt[0] = rio_w;
  834. tt[1] = rio_h;
  835. for (std::size_t i = 0; i < vec_locations_word.size(); i++){
  836. vec_locations_word[i].x = vec_locations_word[i].x*rio_w + 0.55;
  837. vec_locations_word[i].y = vec_locations_word[i].y*rio_h + 0.55;
  838. vec_locations_word[i].width = vec_locations_word[i].width*rio_w + 0.55;
  839. vec_locations_word[i].height = vec_locations_word[i].height*rio_h + 0.55;
  840. }
  841. }
  842. if (2 == m_vecBottom.size() && vec_locations_word.size()>3){
  843. try{
  844. // 三点校正
  845. ret = JiaoZheng_20200418_first(src, dst_img, main_locate_point, m_vecBottom, vec_locations_word, dir);
  846. }
  847. catch (cv::Exception &e)
  848. {
  849. return identify::result::IDF_FAILURE;
  850. }
  851. }
  852. return ret;
  853. }
  854. void COnLineCardPageIdentifier::InitScoreArea(SchemaQuestionScore&sqs, const ScoreBox&box, CvPoint offset, double w_scale, double h_scale)
  855. {
  856. // 打分框类型
  857. sqs.scoreBox.type = box.type;
  858. // 填空题打分类型
  859. sqs.scoreBox.vctScore = box.vctScore;
  860. // 上限
  861. sqs.scoreBox.limit = box.limit;
  862. // 最后一个格子是否为小数(0.5分)
  863. sqs.scoreBox.bPoint = box.bPoint;
  864. // 位置
  865. sqs.scoreBox.pos.x = offset.x + box.pos.x * w_scale;
  866. sqs.scoreBox.pos.y = offset.y + box.pos.y * h_scale;
  867. sqs.scoreBox.pos.w = box.pos.w *w_scale;
  868. sqs.scoreBox.pos.h = box.pos.h *h_scale;
  869. }
  870. int COnLineCardPageIdentifier::GetQrCode(IplImage* dst_gray_img, schema_const_param &schema_param, vector<CvRect> &main_locate_point, vector<SchemaCode> &codev, string& paper_id, string& student_code)
  871. {
  872. std::string result;
  873. CvRect myRect = cvRect(max(0, min(dst_gray_img->width - schema_param.paper_bar_width1, main_locate_point[0].x + schema_param.paper_bar_offsetx1)),
  874. max(0, min(dst_gray_img->height - schema_param.paper_bar_height1, main_locate_point[0].y + schema_param.paper_bar_offsety1)),
  875. schema_param.paper_bar_width1,
  876. schema_param.paper_bar_height1);
  877. IplImage* mydst_gray_img = NULL;
  878. xuanzhuan(dst_gray_img, mydst_gray_img, myRect);
  879. #ifdef _DEBUG
  880. cv::imwrite("D:\\TTTTT.JPG", cvarrToMat(mydst_gray_img));
  881. #endif
  882. cv::Mat matDstGray = cvarrToMat(mydst_gray_img);
  883. int ret = 0;
  884. try{
  885. ret = GetQR(matDstGray, result);
  886. }
  887. catch (cv::Exception&e){
  888. const char* what = e.what();
  889. int n = 0;
  890. ++n;
  891. }
  892. //CParseQRInfo test;
  893. //char result[512] = { 0 };
  894. //bool bRet = test.parseQRInfo(matDstGray, result, sizeof(result)/sizeof(char));
  895. //if (/*ret == identify::result::IDF_SUCCESS*/bRet){
  896. if (ret == identify::result::IDF_SUCCESS){
  897. SchemaCode qrcode1;
  898. qrcode1.centerx = (main_locate_point[0].x + schema_param.paper_bar_offsetx1) + schema_param.paper_bar_width1 / 2.0;
  899. qrcode1.centery = (main_locate_point[0].y + schema_param.paper_bar_offsety1) + schema_param.paper_bar_height1 / 2.0;
  900. qrcode1.width = schema_param.paper_bar_width1;
  901. qrcode1.height = schema_param.paper_bar_height1;
  902. qrcode1.index = obj_index++;
  903. qrcode1.nQrOrBar = 1;
  904. qrcode1.isXuanZhuan = false;
  905. student_code = result;
  906. qrcode1.strContent = result;
  907. codev.push_back(qrcode1);
  908. }
  909. if (mydst_gray_img != NULL)cvReleaseImage(&mydst_gray_img);
  910. return identify::result::IDF_SUCCESS;
  911. }
  912. void COnLineCardPageIdentifier::CalcPos(const Pos&pos, const CvPoint& offset, double w_scale, double h_scale, int &center_x, int &center_y, int &w, int &h)
  913. {
  914. w = static_cast<int>(pos.w*w_scale);
  915. h = static_cast<int>(pos.h*h_scale);
  916. center_x = static_cast<int>(offset.x + pos.x*w_scale + pos.w*w_scale / 2);
  917. center_y = static_cast<int>(offset.y + pos.y*h_scale + pos.h*h_scale / 2);
  918. }
  919. void COnLineCardPageIdentifier::SetPageDefault(SchemaPage& page)
  920. {
  921. page.codeCount = 0;
  922. page.cutAreaCount = 0;
  923. page.group_spacer = ';';
  924. page.groupCount = 0;
  925. page.stuItemCount = 0;
  926. page.stuGroupsCount = 0;
  927. page.hei_du_ling_min_du = 4;
  928. page.height = 0;
  929. page.index = 0;
  930. page.itemCount = 0;
  931. page.locateAreaCount = 0;
  932. page.locateCrossCount = 0;
  933. page.locatePointCount = 0;
  934. page.option_spacer = ',';
  935. page.question_score_count = 0;
  936. page.unselect_char = '*';
  937. page.user_float_heidu = 4;
  938. page.width = 0;
  939. page.worthwhileBlk = 4;
  940. }
  941. void COnLineCardPageIdentifier::SetTemplate(OnLineCard::PaperTemplate*pT)
  942. {
  943. m_pTemplate = pT;
  944. int dingweidianH;
  945. int dingweidianW;
  946. if (m_pTemplate->pages[1].location.size() >= 5){
  947. int m = 0;
  948. dingweidianH = m_pTemplate->pages[1].location[m].pos.h;
  949. dingweidianW = m_pTemplate->pages[1].location[m].pos.w;
  950. m_default_schema_param.main_locate_point_distance = m_pTemplate->pages[1].location[m + 2].pos.x - m_pTemplate->pages[1].location[m].pos.x;
  951. m_default_schema_param.shijuanwidth = m_pTemplate->pages[1].w;
  952. for (int i = 0; i < 4; i++)
  953. {
  954. if (i < 2)
  955. {
  956. m_default_schema_param.minw[i] = dingweidianW*0.75;
  957. m_default_schema_param.maxw[i] = dingweidianW*1.25;
  958. m_default_schema_param.minh[i] = dingweidianH*0.75;
  959. m_default_schema_param.maxh[i] = dingweidianH*1.25;
  960. }
  961. else
  962. {
  963. m_default_schema_param.minw[i] = dingweidianH*0.75;
  964. m_default_schema_param.maxw[i] = dingweidianH*1.25;
  965. m_default_schema_param.minh[i] = dingweidianW*0.75;
  966. m_default_schema_param.maxh[i] = dingweidianW*1.25;
  967. }
  968. }
  969. }
  970. }
  971. int COnLineCardPageIdentifier::createSchema(const IplImage* src, IplImage* *dst, SchemaPage* *pageSchema, const char * result_path, bool flag, std::string qr, bool bUseQr)
  972. {
  973. if (!m_pTemplate || m_pTemplate->pages.size() < 2 || m_pTemplate->pages[1].location.size() < 5){
  974. return identify::result::IDF_FAILURE;
  975. }
  976. int wordloctlt = 0;
  977. int schoolCardStatus = m_pTemplate->schoolCardStatus;
  978. obj_index = 0;
  979. CvRect roi_old = cvGetImageROI(src);
  980. IplImage * src0 = (IplImage *)src;
  981. int ret;
  982. int mode_type = 1115;
  983. vector<SchemaGroup> groupv;
  984. vector<SchemaItem> itemv;
  985. vector<SchemaQuestionScore> scorev;
  986. vector<SchemaCode> codev;
  987. vector<SchemaCutArea> cutareav;
  988. vector<SchemaLocatePoint> questionLocatePoints;
  989. vector<KEGUANTI_QUESTION_LOCATEPOINT_INDEX> keguanti_question_locatepoint_indexs;
  990. vector<SchemaGroup> stuGroupv;
  991. vector<SchemaItem> stuItemv;
  992. vector<KEGUANTI_QUESTION_LOCATEPOINT_INDEX> stuIndex;
  993. vector<KEGUANTI_QUESTION_LOCATEPOINT_INDEX> quekaoIndex;
  994. CvMemStorage* storage = cvCreateMemStorage(0);
  995. IplImage * src_gray_img;
  996. int question_count = 0;
  997. int itemcount = 0;
  998. cvResetImageROI((IplImage*)src0);
  999. src_gray_img = cvCreateImage(cvGetSize(src0), IPL_DEPTH_8U, 1);
  1000. cvCvtColor(src0, src_gray_img, CV_BGR2GRAY);
  1001. schema_const_param schema_param;
  1002. std::vector<CvRect> main_locate_point;
  1003. CvPoint offset_pt;
  1004. int dir;//上下左右
  1005. // printf("线程:%s 第一次查找定位点\n", std::to_string(GetCurrentThreadId()).c_str());
  1006. ret = MyFindDingWeiDian(src_gray_img, storage, schema_param, main_locate_point, dir);
  1007. if (ret != identify::result::IDF_SUCCESS){
  1008. cv::Size sz;
  1009. std::vector<cv::Rect> vec_locations_word; vec_locations_word.clear();
  1010. if (!m_pTemplate->pages.empty()){
  1011. PageTemplate&page = m_pTemplate->pages[1];
  1012. sz = cv::Size(page.w, page.h);
  1013. std::vector<Location>& lcs = page.location;
  1014. for (std::size_t j = 0; j < lcs.size(); j++){
  1015. cv::Rect rc_tmp;
  1016. rc_tmp.x = lcs[j].pos.x;
  1017. rc_tmp.y = lcs[j].pos.y;
  1018. rc_tmp.width = lcs[j].pos.w;
  1019. rc_tmp.height = lcs[j].pos.h;
  1020. vec_locations_word.push_back(rc_tmp);
  1021. }
  1022. }
  1023. std::vector<cv::Rect> vec_tops; // 接受找到的上面三个定位点 下标访问
  1024. std::vector<cv::Rect> vec_bottoms; // 接受找到的下面的两个定位点 下标访问
  1025. std::vector<int> vec_len;
  1026. vec_tops.resize(100);
  1027. vec_bottoms.resize(100);
  1028. vec_len.push_back(-1);
  1029. vec_len.push_back(-1);
  1030. #ifdef _DEBUG
  1031. cv::imwrite("D:\\src_gray_img.png", cv::cvarrToMat(src_gray_img));
  1032. #endif
  1033. int nRet = api_location_anchor_points(cv::cvarrToMat(src_gray_img), vec_locations_word, sz, vec_tops, vec_bottoms, vec_len, dir, 3, 1);
  1034. if (nRet == 0 && vec_len.size()==2 && vec_len[0] == 3 && vec_len[1] == 2){
  1035. main_locate_point.clear();
  1036. m_vecBottom.clear();
  1037. for (std::size_t i = 0; i < vec_tops.size() && i < vec_len[0]; ++i){
  1038. main_locate_point.push_back(vec_tops[i]);
  1039. }
  1040. for (std::size_t i = 0; i < vec_bottoms.size() && i < vec_len[1]; ++i){
  1041. m_vecBottom.push_back(vec_bottoms[i]);
  1042. }
  1043. }
  1044. else{
  1045. cvReleaseMemStorage(&storage);
  1046. cvReleaseImage(&src_gray_img);
  1047. cvSetImageROI(src0, roi_old);
  1048. return identify::result::IDF_TEMPLATE_NOTFOUND_ENOUGH_LOCATEPOINT;
  1049. }
  1050. }
  1051. IplImage * dst_img = NULL;
  1052. // printf("线程:%s 开始矫正\n", std::to_string(GetCurrentThreadId()).c_str());
  1053. ret = OnLineCardJZ(src0, dst_img, 1, dir, main_locate_point, wordloctlt);
  1054. if (ret != identify::result::IDF_SUCCESS){
  1055. cvReleaseMemStorage(&storage);
  1056. cvReleaseImage(&src_gray_img);
  1057. cvSetImageROI(src0, roi_old);
  1058. if (dst_img)cvReleaseImage(&dst_img);
  1059. return identify::result::IDF_TEMPLATE_NOTFOUND_ENOUGH_LOCATEPOINT;
  1060. }
  1061. //printf("线程:%s 矫正结束\n", std::to_string(GetCurrentThreadId()).c_str());
  1062. IplImage* dst_gray_img = cvCreateImage(cvGetSize(dst_img), IPL_DEPTH_8U, 1);
  1063. cvCvtColor(dst_img, dst_gray_img, CV_BGR2GRAY);
  1064. int locate_point_count = main_locate_point.size();
  1065. main_locate_point.clear();
  1066. //printf("线程:%s 第二次查找定位点\n", std::to_string(GetCurrentThreadId()).c_str());
  1067. ret = MyFindDingWeiDian(dst_gray_img, storage, schema_param, main_locate_point, dir);
  1068. std::sort(main_locate_point.begin(), main_locate_point.end(), sort_locate_point_by_x);
  1069. if (ret != identify::result::IDF_SUCCESS || main_locate_point.size() != locate_point_count){
  1070. cv::Size sz;
  1071. std::vector<cv::Rect> vec_locations_word; vec_locations_word.clear();
  1072. if (!m_pTemplate->pages.empty()){
  1073. PageTemplate&page = m_pTemplate->pages[1];
  1074. sz = cv::Size(page.w, page.h);
  1075. std::vector<Location>& lcs = page.location;
  1076. for (std::size_t j = 0; j < lcs.size(); j++){
  1077. cv::Rect rc_tmp;
  1078. rc_tmp.x = lcs[j].pos.x;
  1079. rc_tmp.y = lcs[j].pos.y;
  1080. rc_tmp.width = lcs[j].pos.w;
  1081. rc_tmp.height = lcs[j].pos.h;
  1082. vec_locations_word.push_back(rc_tmp);
  1083. }
  1084. }
  1085. std::vector<cv::Rect> vec_tops; // 接受找到的上面三个定位点 下标访问
  1086. std::vector<cv::Rect> vec_bottoms; // 接受找到的下面的两个定位点 下标访问
  1087. std::vector<int> vec_len;
  1088. vec_tops.resize(100);
  1089. vec_bottoms.resize(100);
  1090. vec_len.push_back(-1);
  1091. vec_len.push_back(-1);
  1092. int nRet = api_location_anchor_points(cv::cvarrToMat(src_gray_img), vec_locations_word, sz, vec_tops, vec_bottoms, vec_len, dir, 3, 1);
  1093. if (nRet == 0 && vec_len.size() == 2 && vec_len[0] == 3 && vec_len[1] == 2){
  1094. main_locate_point.clear();
  1095. m_vecBottom.clear();
  1096. for (std::size_t i = 0; i < vec_tops.size() && i < vec_len[0]; ++i){
  1097. main_locate_point.push_back(vec_tops[i]);
  1098. }
  1099. for (std::size_t i = 0; i < vec_bottoms.size() && i < vec_len[1]; ++i){
  1100. m_vecBottom.push_back(vec_bottoms[i]);
  1101. }
  1102. }
  1103. else{
  1104. cvReleaseMemStorage(&storage);
  1105. cvReleaseImage(&src_gray_img);
  1106. cvReleaseImage(&dst_gray_img);
  1107. cvSetImageROI(src0, roi_old);
  1108. if (dst_img)cvReleaseImage(&dst_img);
  1109. return identify::result::IDF_TEMPLATE_NOTFOUND_ENOUGH_LOCATEPOINT;
  1110. }
  1111. }
  1112. double fffx = (main_locate_point[main_locate_point.size() - 1].x - main_locate_point[0].x)*(main_locate_point[main_locate_point.size() - 1].x - main_locate_point[0].x);
  1113. double fffy = (main_locate_point[main_locate_point.size() - 1].y - main_locate_point[0].y)*(main_locate_point[main_locate_point.size() - 1].y - main_locate_point[0].y);
  1114. double w_scale = (sqrt(fffx + fffy)) / m_default_schema_param.main_locate_point_distance;
  1115. double Hfffx = (m_vecBottom[0].x - main_locate_point[0].x)*(m_vecBottom[0].x - main_locate_point[0].x);
  1116. double Hfffy = (m_vecBottom[0].y - main_locate_point[0].y)*(m_vecBottom[0].y - main_locate_point[0].y);
  1117. double h_scale = (sqrt(Hfffx + Hfffy)) / wordloctlt;
  1118. {
  1119. //保存
  1120. SchemaLocatePoint leftp;
  1121. leftp.centerx = main_locate_point[0].x + main_locate_point[0].width / 2.0;
  1122. leftp.centery = main_locate_point[0].y + main_locate_point[0].height / 2.0;
  1123. leftp.width = main_locate_point[0].width;
  1124. leftp.height = main_locate_point[0].height;
  1125. questionLocatePoints.push_back(leftp);
  1126. SchemaLocatePoint rightp;
  1127. rightp.centerx = main_locate_point[2].x + main_locate_point[2].width / 2.0;
  1128. rightp.centery = main_locate_point[2].y + main_locate_point[2].height / 2.0;
  1129. rightp.width = main_locate_point[2].width;
  1130. rightp.height = main_locate_point[2].height;
  1131. questionLocatePoints.push_back(rightp);
  1132. leftp.centerx = m_vecBottom[0].x + m_vecBottom[0].width / 2.0;
  1133. leftp.centery = m_vecBottom[0].y + m_vecBottom[0].height / 2.0;
  1134. leftp.width = m_vecBottom[0].width;
  1135. leftp.height = m_vecBottom[0].height;
  1136. questionLocatePoints.push_back(leftp);
  1137. rightp.centerx = m_vecBottom[1].x + m_vecBottom[1].width / 2.0;
  1138. rightp.centery = m_vecBottom[1].y + m_vecBottom[1].height / 2.0;
  1139. rightp.width = m_vecBottom[1].width;
  1140. rightp.height = m_vecBottom[1].height;
  1141. questionLocatePoints.push_back(rightp);
  1142. }
  1143. int page_index = 1;
  1144. string paper_id = "";
  1145. string student_code = "";
  1146. bool is_stu_id = false;
  1147. offset_pt.x = main_locate_point[0].x;
  1148. offset_pt.y = main_locate_point[0].y;
  1149. bool front_page = false;
  1150. //判断前后页
  1151. if ((main_locate_point[1].x - main_locate_point[0].x) < (main_locate_point[2].x - main_locate_point[1].x)){//正面
  1152. front_page = true;
  1153. }
  1154. else{
  1155. front_page = false;
  1156. }
  1157. PageTemplate&page = m_pTemplate->pages[front_page ? 1 : 2];
  1158. // 正面
  1159. if (front_page){
  1160. SchemaCode code_tmp;
  1161. std::vector<std::string> split_qr;
  1162. split(qr, (std::string)",", &split_qr);
  1163. if (split_qr.size() > 0){
  1164. if (split_qr.size() > 0)
  1165. paper_id = split_qr[0];
  1166. if (bUseQr){
  1167. if (split_qr.size() > 2){
  1168. std::vector<std::string> _vct;
  1169. split(split_qr[2], std::string("@"), &_vct);
  1170. if (_vct.size() > 1){
  1171. student_code = _vct[2];
  1172. is_stu_id = !student_code.empty();
  1173. }
  1174. }
  1175. }
  1176. }
  1177. if (bUseQr&&!student_code.empty())
  1178. {
  1179. SchemaCode code;
  1180. code.index = obj_index++;
  1181. code.nQrOrBar = 1;
  1182. code.strContent = paper_id + "_" + student_code + "_3_1";
  1183. codev.push_back(code);
  1184. }
  1185. else{
  1186. // 二维码
  1187. schema_param.paper_bar_width1 = page.QrCode.w*w_scale;
  1188. schema_param.paper_bar_height1 = page.QrCode.h*h_scale;
  1189. schema_param.paper_bar_offsetx1 = page.QrCode.x*w_scale;
  1190. schema_param.paper_bar_offsety1 = page.QrCode.y*h_scale;
  1191. if (m_pTemplate->useQrCode){
  1192. std::size_t nOld = codev.size();
  1193. ret = GetQrCode(dst_gray_img, schema_param, main_locate_point, codev, paper_id, student_code);
  1194. if (ret != identify::result::IDF_SUCCESS){
  1195. cvReleaseMemStorage(&storage);
  1196. cvReleaseImage(&src_gray_img);
  1197. cvReleaseImage(&dst_img);
  1198. cvReleaseImage(&dst_gray_img);
  1199. return identify::result::IDF_TEMPLATE_QRCODE_ILLEGAL;
  1200. }
  1201. if (nOld < codev.size()){
  1202. vector<std::string> splits;
  1203. split(student_code, (std::string)"@", &splits);
  1204. if (splits.size() == 1){
  1205. paper_id = splits[0];
  1206. codev[codev.size() - 1].strContent = paper_id + "_6582234412574771625_3_1_12_1_1_1";
  1207. student_code = "";
  1208. }
  1209. else if (splits.size() >= 2){
  1210. student_code = splits[0];
  1211. paper_id = splits[1];
  1212. codev[codev.size() - 1].strContent = paper_id + "_" + student_code + "_3_1";
  1213. }
  1214. if (student_code.empty()){
  1215. if (!codev.empty())
  1216. code_tmp = codev[0];
  1217. codev.clear();
  1218. }
  1219. }
  1220. is_stu_id = !student_code.empty();
  1221. }
  1222. // 二维码中含有学生信息 不需要识别条形码和填涂考号
  1223. if (!m_pTemplate->useQrCode)
  1224. {
  1225. //条形码
  1226. schema_param.paper_bar_width1 = page.studentcode_bar.w*w_scale;
  1227. schema_param.paper_bar_height1 = page.studentcode_bar.h*h_scale;
  1228. schema_param.paper_bar_offsetx1 = page.studentcode_bar.x*w_scale;
  1229. schema_param.paper_bar_offsety1 = page.studentcode_bar.y*h_scale;
  1230. if (schema_param.paper_bar_width1 != 0 && schema_param.paper_bar_height1 != 0){
  1231. auto oldSize = codev.size();
  1232. ret = GetQrCode(dst_gray_img, schema_param, main_locate_point, codev, paper_id, student_code);
  1233. if (ret != identify::result::IDF_SUCCESS){
  1234. cvReleaseMemStorage(&storage);
  1235. cvReleaseImage(&src_gray_img);
  1236. cvReleaseImage(&dst_img);
  1237. cvReleaseImage(&dst_gray_img);
  1238. return identify::result::IDF_TEMPLATE_QRCODE_ILLEGAL;;
  1239. }
  1240. if (oldSize < codev.size()){
  1241. codev[codev.size() - 1].strContent = paper_id + "_" + student_code + "_3_1_12_1_1_1";
  1242. }
  1243. }
  1244. //填涂考号
  1245. if (student_code.empty())
  1246. {
  1247. int q_item_count = 10;
  1248. int itemcount = 0;
  1249. int question_count = 0;
  1250. int x, y, w, h;
  1251. for (std::size_t j = 0; j < page.studentcode_fill.size(); j++)
  1252. {
  1253. SchemaGroup group;
  1254. group.question_code = "";
  1255. group.index = question_count++;
  1256. group.question_index = question_count;
  1257. group.itemCount = q_item_count;
  1258. group.itemIndex.clear();
  1259. group.omr_out_type = OMR_OUT_TYPE::OMR_OUT_TYPE_SINGLE_NONSTRICT;
  1260. for (std::size_t m = 0; m < page.studentcode_fill[j].size(); m++)
  1261. {
  1262. group.itemIndex.push_back(itemcount + m);
  1263. SchemaItem item;
  1264. CalcPos(page.studentcode_fill[j][m].pos, offset_pt, w_scale, h_scale, item.centerx, item.centery, item.width, item.height);
  1265. std::string&optName = page.studentcode_fill[j][m].optName;
  1266. if (!optName.empty())
  1267. item.OutChar = std::stoi(optName) + '0';
  1268. item.keguanti_question_locate_point_index = stuIndex.size();
  1269. stuItemv.push_back(item);
  1270. }
  1271. if (j == 0){
  1272. x = stuItemv[0].centerx - stuItemv[0].width / 2 - 35;
  1273. y = stuItemv[0].centery - stuItemv[0].height - stuItemv[0].height / 2 - 105;
  1274. }
  1275. else if (j == page.studentcode_fill.size() - 1){
  1276. SchemaItem &item = stuItemv[stuItemv.size() - 1];
  1277. int tx = item.centerx + item.width / 2 + 35;
  1278. int ty = item.centery + item.height / 2 + 5;
  1279. w = tx - x;
  1280. h = ty - y;
  1281. }
  1282. itemcount += q_item_count;
  1283. stuGroupv.push_back(group);
  1284. }
  1285. KEGUANTI_QUESTION_LOCATEPOINT_INDEX kqli;
  1286. kqli[0] = 0;
  1287. kqli[1] = 1;
  1288. kqli[2] = 2;
  1289. kqli[3] = 3;
  1290. stuIndex.push_back(kqli);
  1291. code_tmp.centerx = x + w / 2;
  1292. code_tmp.centery = y + h / 2;
  1293. code_tmp.width = w;
  1294. code_tmp.height = h;
  1295. code_tmp.index = obj_index++;
  1296. code_tmp.nQrOrBar = 1;
  1297. code_tmp.strContent = paper_id + "_6582234412574771625_3_1_12_1_1_1";
  1298. codev.clear();
  1299. codev.push_back(code_tmp);
  1300. }
  1301. }
  1302. }
  1303. // 缺考标记
  1304. {
  1305. schema_param.quekao_item_offset_x = main_locate_point[0].x + page.absent.x*w_scale;
  1306. schema_param.quekao_item_offset_y = main_locate_point[0].y + page.absent.y*h_scale;
  1307. schema_param.quekao_item_width = page.absent.w*w_scale;
  1308. schema_param.quekao_item_height = page.absent.h*h_scale;
  1309. KEGUANTI_QUESTION_LOCATEPOINT_INDEX QKIndex;
  1310. QKIndex[0] = 0;
  1311. QKIndex[1] = 1;
  1312. QKIndex[2] = 2;
  1313. QKIndex[3] = 3;
  1314. quekaoIndex.push_back(QKIndex);
  1315. }
  1316. }
  1317. for (std::size_t i = 0; i < page.vctQuestions.size(); ++i){
  1318. question&qt = page.vctQuestions[i];
  1319. if (qt.type == 0 || qt.type == 8){//选
  1320. SchemaGroup group;
  1321. question_count++;
  1322. group.question_code = qt.id;
  1323. static int id = 0; id = std::stoi(qt.id);
  1324. group.index = obj_index++;
  1325. group.question_index = question_count;
  1326. group.itemCount = qt.opt.size();
  1327. group.itemIndex.clear();
  1328. group.omr_out_type = qt.type == 0 ? OMR_OUT_TYPE::OMR_OUT_TYPE_SINGLE_NONSTRICT : OMR_OUT_TYPE::OMR_OUT_TYPE_MUTIL_STRICT;
  1329. for (int q = 0; q < group.itemCount; q++)
  1330. {
  1331. group.itemIndex.push_back(itemcount + q);
  1332. SchemaItem item;
  1333. CalcPos(qt.opt[q].pos, offset_pt, w_scale, h_scale, item.centerx, item.centery, item.width, item.height);
  1334. if (!qt.opt[q].optName.empty())
  1335. item.OutChar = qt.opt[q].optName.at(0);
  1336. item.keguanti_question_locate_point_index = keguanti_question_locatepoint_indexs.size();
  1337. itemv.push_back(item);
  1338. }
  1339. itemcount += group.itemCount;
  1340. groupv.push_back(group);
  1341. KEGUANTI_QUESTION_LOCATEPOINT_INDEX kqli;
  1342. kqli[0] = 0;
  1343. kqli[1] = 1;
  1344. kqli[2] = 2;
  1345. kqli[3] = 3;
  1346. keguanti_question_locatepoint_indexs.push_back(kqli);
  1347. }
  1348. else if (qt.type == 1 || qt.type == 2){
  1349. //通过linkparm判断第一次还是第二次出现(第一次一定小于2)
  1350. if (qt.cut.linkparm < 2){
  1351. SchemaQuestionScore sqs;
  1352. cv::warpAffine;
  1353. sqs.type = QUESTION_SCORE_TYPE::MUTIL_SCORE;
  1354. InitScoreArea(sqs, qt.scoreBox, offset_pt, w_scale, h_scale);
  1355. if (sqs.scoreBox.type == 1){ // 1-16
  1356. sqs.count = sqs.scoreBox.limit;
  1357. if (sqs.scoreBox.bPoint)
  1358. sqs.count++;
  1359. }
  1360. else if (sqs.scoreBox.type == 2){ // 29/49
  1361. sqs.count = ((int)sqs.scoreBox.limit / 10) + 12;// 12 = 各位数字+ 十/个位标记 10+2
  1362. if (sqs.scoreBox.bPoint)
  1363. sqs.count++;
  1364. }
  1365. sqs.scoreBox.bXuanZuoTi = (qt.type == 2);
  1366. sqs.index = obj_index++;
  1367. if (!qt.id.empty())
  1368. sqs.question_index = std::stoi(qt.id);
  1369. sqs.question_code = qt.id;
  1370. if (qt.type ==2){
  1371. sqs.question_code_all = qt.all_id;
  1372. }
  1373. sqs.valid_option_count = 0;
  1374. sqs.questionLocatePointIndex[0] = 0;
  1375. sqs.questionLocatePointIndex[1] = 1;
  1376. CalcPos(qt.cut.pos, offset_pt, w_scale, h_scale, sqs.centerx, sqs.centery, sqs.width, sqs.height);
  1377. if (qt.type == 2){
  1378. sqs.questionLocatePointIndex[2] = 2;
  1379. sqs.questionLocatePointIndex[3] = 3;
  1380. for (int v = 0; v < qt.selTotal && qt.opt.size() == qt.selTotal; v++)
  1381. {
  1382. CalcPos(qt.opt[v].pos, offset_pt, w_scale, h_scale, sqs.item[v].centerx, sqs.item[v].centery, sqs.item[v].width, sqs.item[v].height);
  1383. }
  1384. sqs.countItemSelete = qt.selItem;
  1385. sqs.countNum = qt.selTotal;
  1386. SetTBLR(sqs);
  1387. }
  1388. SchemaCutArea cutarea;
  1389. cutarea.index = obj_index++;
  1390. cutarea.area_name = sqs.question_code;
  1391. cutarea.isfirst = TRUE;
  1392. cutarea.question_index = sqs.question_index;
  1393. cutarea.iscut = TRUE;
  1394. CalcPos(qt.cut.pos, offset_pt, w_scale, h_scale, cutarea.centerx, cutarea.centery, cutarea.width, cutarea.height);
  1395. SetTBLR(sqs);
  1396. scorev.push_back(sqs);
  1397. cutareav.push_back(cutarea);
  1398. }else{
  1399. SchemaCutArea cutarea;
  1400. CalcPos(qt.cut.pos, offset_pt, w_scale, h_scale, cutarea.centerx, cutarea.centery, cutarea.width, cutarea.height);
  1401. cutarea.index = obj_index++;
  1402. cutarea.isfirst = FALSE;
  1403. cutarea.question_index = -1;
  1404. cutarea.area_name = qt.id;
  1405. cutareav.push_back(cutarea);
  1406. }
  1407. }else if (qt.type == 3){
  1408. //填空(不会跨页,所以可以直接处理)
  1409. SchemaQuestionScore sqs;
  1410. sqs.type = QUESTION_SCORE_TYPE::SINGLE_SCORE;
  1411. InitScoreArea(sqs, qt.scoreBox, offset_pt, w_scale, h_scale);
  1412. sqs.count = sqs.scoreBox.vctScore.size();
  1413. if (sqs.count <= 0)
  1414. sqs.count = 1;
  1415. sqs.index = obj_index++;
  1416. if (!qt.id.empty())
  1417. sqs.question_index = std::stoi(qt.id);
  1418. sqs.question_code = qt.id;
  1419. sqs.valid_option_count = 0;
  1420. sqs.questionLocatePointIndex[0] = 0;
  1421. sqs.questionLocatePointIndex[1] = 1;
  1422. CalcPos(qt.cut.pos, offset_pt, w_scale, h_scale, sqs.centerx, sqs.centery, sqs.width, sqs.height);
  1423. SchemaCutArea cutarea;
  1424. cutarea.index = obj_index++;
  1425. cutarea.area_name = sqs.question_code;
  1426. cutarea.isfirst = TRUE;
  1427. cutarea.question_index = question_count;
  1428. cutarea.iscut = FALSE;
  1429. CalcPos(qt.cut.pos, offset_pt, w_scale, h_scale, cutarea.centerx, cutarea.centery, cutarea.width, cutarea.height);
  1430. SetTBLR(sqs);
  1431. scorev.push_back(sqs);
  1432. cutareav.push_back(cutarea);
  1433. }
  1434. }
  1435. SchemaPage* schemapage = (SchemaPage*) new SchemaPage;
  1436. SetPageDefault(*schemapage);
  1437. schemapage->schoolCardStatus = m_pTemplate->schoolCardStatus;
  1438. schemapage->isZaiXianDaTiKa = true;
  1439. schemapage->index = front_page ? page_index : 2;
  1440. strcpy(schemapage->paper_id, paper_id.c_str());
  1441. strcpy(schemapage->student_code, student_code.c_str());
  1442. schemapage->is_stu_id = is_stu_id;
  1443. schemapage->width = dst_img->width;
  1444. schemapage->height = dst_img->height;
  1445. schemapage->locatePointCount = main_locate_point.size();
  1446. schemapage->questionLocatePoints = questionLocatePoints;
  1447. schemapage->keguantiQuestionLocatePointIndexs = keguanti_question_locatepoint_indexs;
  1448. schemapage->stuQuestionLocatePointIndex = stuIndex;
  1449. schemapage->codeCount = codev.size();
  1450. schemapage->groupCount = groupv.size();
  1451. schemapage->itemCount = itemv.size();
  1452. schemapage->stuGroupsCount = stuGroupv.size();
  1453. schemapage->stuItemCount = stuItemv.size();
  1454. schemapage->question_score_count = scorev.size();
  1455. schemapage->cutAreaCount = cutareav.size();
  1456. int offset = sizeof(SchemaPage);
  1457. for (std::size_t i = 0; i < main_locate_point.size(); i++)
  1458. {
  1459. SchemaLocatePoint slp;
  1460. slp.centerx = main_locate_point[i].x + main_locate_point[i].width / 2.;
  1461. slp.centery = main_locate_point[i].y + main_locate_point[i].height / 2.;
  1462. slp.width = main_locate_point[i].width;
  1463. slp.height = main_locate_point[i].height;
  1464. schemapage->locatePoints.push_back(slp);
  1465. }
  1466. schemapage->codes = codev;
  1467. schemapage->groups = groupv;
  1468. schemapage->items = itemv;
  1469. schemapage->question_score = scorev;
  1470. schemapage->cutAreas = cutareav;
  1471. schemapage->mode_type = mode_type;
  1472. schemapage->stuGroups = stuGroupv;
  1473. schemapage->stuItems = stuItemv;
  1474. schemapage->schoolCardStatus = schoolCardStatus;
  1475. //缺考标记相关参数
  1476. schemapage->quekao.centerx = schema_param.quekao_item_offset_x + schema_param.quekao_item_width / 2;
  1477. schemapage->quekao.centery = schema_param.quekao_item_offset_y + schema_param.quekao_item_height / 2;
  1478. schemapage->quekao.width = schema_param.quekao_item_width;
  1479. schemapage->quekao.height = schema_param.quekao_item_height;
  1480. schemapage->quekaoLocatePointIndex = quekaoIndex;
  1481. *pageSchema = schemapage;
  1482. if (flag){
  1483. DrawSchema(dst_img, schemapage);
  1484. cvSaveImage(result_path, dst_img);
  1485. }
  1486. cvReleaseMemStorage(&storage);
  1487. cvReleaseImage(&src_gray_img);
  1488. *dst = dst_img;
  1489. cvReleaseImage(&dst_gray_img);
  1490. {
  1491. cvSetImageROI(src0, roi_old);
  1492. return identify::result::IDF_SUCCESS;
  1493. }
  1494. //printf("线程:%s 单页识别结束\n", std::to_string(GetCurrentThreadId()).c_str());
  1495. }
  1496. std::string getTimeStamp()
  1497. {
  1498. std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
  1499. auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
  1500. std::time_t timestamp = tmp.count();
  1501. return std::move(std::to_string(timestamp));
  1502. }
  1503. //按照指定模板进行识别
  1504. int COnLineCardPageIdentifier::Identify_impl()
  1505. {
  1506. int ret = BaseCheck();
  1507. if (ret != identify::result::IDF_SUCCESS)return ret;
  1508. InitalizeTempVar();
  1509. CvMemStorage* storage = cvCreateMemStorage(0);
  1510. IplImage * dst = NULL;
  1511. SchemaPage* pschemaPage = NULL;
  1512. std::string path = std::string("d:\\image\\") + getTimeStamp() + ".jpg";
  1513. ret = createSchema(src, &dst, &pschemaPage, path.c_str(), true, m_strQrClass, m_bUseQr);
  1514. if (ret != identify::result::IDF_SUCCESS){
  1515. if (dst != NULL)cvReleaseImage(&dst);
  1516. cvReleaseMemStorage(&storage);
  1517. return ret;
  1518. }
  1519. SchemaPage& schemaPage = *pschemaPage;
  1520. /************************读取学生学号*********************************************/
  1521. ret = ReadStudentID(schemaPage, dst);
  1522. if (ret != identify::result::IDF_SUCCESS) {
  1523. if (dst != NULL)cvReleaseImage(&dst);
  1524. cvReleaseMemStorage(&storage);
  1525. if (pschemaPage != NULL)delete pschemaPage;
  1526. return ret;
  1527. }
  1528. /************************查找题目定位点,准备定位数据*********************************************/
  1529. ret = FindQuestionLocatePoints(schemaPage, dst);
  1530. if (ret != identify::result::IDF_SUCCESS) {
  1531. if (dst != NULL)cvReleaseImage(&dst);
  1532. cvReleaseMemStorage(&storage);
  1533. if (pschemaPage != NULL)delete pschemaPage;
  1534. return ret;
  1535. }
  1536. /************************客观题()*********************************************/
  1537. ret = ReadKeGuanTi(schemaPage, dst);
  1538. if (ret != identify::result::IDF_SUCCESS) {
  1539. if (dst != NULL)cvReleaseImage(&dst);
  1540. cvReleaseMemStorage(&storage);
  1541. if (pschemaPage != NULL)delete pschemaPage;
  1542. return ret;
  1543. }
  1544. /************************阅卷读取*************************************************/
  1545. ret = ReadQuestionScore(schemaPage, dst, storage);
  1546. if (ret != identify::result::IDF_SUCCESS) {
  1547. if (dst != NULL)cvReleaseImage(&dst);
  1548. cvReleaseMemStorage(&storage);
  1549. if (pschemaPage != NULL)delete pschemaPage;
  1550. return ret;
  1551. }
  1552. /************************根据给定区域切割图片*********************************************/
  1553. ret = ClipImg(schemaPage, dst);
  1554. if (ret != identify::result::IDF_SUCCESS) {
  1555. if (dst != NULL)cvReleaseImage(&dst);
  1556. cvReleaseMemStorage(&storage);
  1557. if (pschemaPage != NULL)delete pschemaPage;
  1558. return ret;
  1559. }
  1560. /************************读取缺考标记*********************************************/
  1561. omr_result->quekaoFlag = ReadQuekaoFlag(schemaPage, dst);
  1562. omr_result->card_index = schemaPage.index;
  1563. if (dst != NULL)cvReleaseImage(&dst);
  1564. cvReleaseMemStorage(&storage);
  1565. if (pschemaPage != NULL)delete pschemaPage;
  1566. return identify::result::IDF_SUCCESS;
  1567. }
  1568. //获取全局背景灰度,通过统计所有涂点周围像素(灰度不小于160)的灰度平均值获得
  1569. int COnLineCardPageIdentifier::caculate_global_background(SchemaPage& schemaPage, IplImage * dst, bool flag)
  1570. {
  1571. int global_background_color_count = 0;
  1572. int global_background_color_avg = 0;
  1573. int totalCount = 0;
  1574. if (!flag)
  1575. {
  1576. totalCount = schemaPage.itemCount;
  1577. }
  1578. else
  1579. {
  1580. totalCount = schemaPage.stuItemCount;
  1581. }
  1582. for (int i = 0; i<totalCount; i++)
  1583. {
  1584. SchemaItem & item = m_items[i];
  1585. RESULT_RECT result_rect;
  1586. GetRect(item.centerx, item.centery, item.width, item.height, result_rect);
  1587. int left = int(result_rect.x);
  1588. int top = int(result_rect.y);
  1589. int right = int(result_rect.x + result_rect.width);
  1590. int bottom = int(result_rect.y + result_rect.height);
  1591. int vtop = (int)(result_rect.y - (result_rect.width + 2) / 4);
  1592. int vbottom = (int)(result_rect.y + result_rect.height + (result_rect.height + 2) / 4);
  1593. int background_color_count = 0, background_color_avg = 0;
  1594. int gray;
  1595. int(*GetGray)(const IplImage * dst, int x, int y);
  1596. if (dst->nChannels == 1)GetGray = GetGrayValue;
  1597. else GetGray = GetBGRGray;
  1598. for (int x = left; x <= right; x++)
  1599. {
  1600. gray = GetGray(dst, x, vtop);
  1601. if (gray>160){ background_color_avg += gray; background_color_count++; }
  1602. gray = GetGray(dst, x, vbottom);
  1603. if (gray>160){ background_color_avg += gray; background_color_count++; }
  1604. }
  1605. background_color_avg = background_color_count>0 ? background_color_avg / background_color_count : 0;
  1606. if (background_color_avg>100){
  1607. global_background_color_avg += background_color_avg;
  1608. global_background_color_count++;
  1609. }
  1610. }
  1611. global_background_color_avg = global_background_color_count>0 ? global_background_color_avg / global_background_color_count : 0;
  1612. return global_background_color_avg;
  1613. }
  1614. //计算涂点属性
  1615. inline int COnLineCardPageIdentifier::caculate_cell_property(SchemaPage& schemaPage, SchemaItem &item, IplImage * dst, int &cellSize, double &refArea, double &avg_gray, int &grayNum, double &mo_hu_du, double &gao_heidu,/*字符区域大小*/int &char_area_size,/*字符区域黑色点数(统计选项的字符区域的灰度判定为黑色的点数,用于统计试卷该选项全局的平均字符黑点数)*/int& char_area_black_count,/*字符区域黑色点的平均灰度*/double& char_area_avg_gray, int flag)
  1616. {
  1617. int gray;//临时变量,灰度值
  1618. //int tembackcolor=max((anlyseResult.globalBackGround+200)/2,200);
  1619. int tembackcolor = (flag == 0) ? max(anlyseResult.backGroundGray - 20, 200) : max(anlyseResult.backGroundGray - 20, 0);
  1620. //获取灰度的函数指针
  1621. int(*ggray)(const IplImage * dst, int x, int y);
  1622. if (dst->nChannels == 1)ggray = GetGrayValue;
  1623. else ggray = GetBGRGray;
  1624. int left = int(item.centerx - item.width / 2. + 0.5);
  1625. int top = int(item.centery - item.height / 2. + 0.5);
  1626. int right = int(item.centerx + item.width / 2. + 0.5);
  1627. int bottom = int(item.centery + item.height / 2. + 0.5);
  1628. int vtop = (int)(item.centery - item.height*0.75 + 0.5);
  1629. /*******************统计字符区域属性************************************/
  1630. int cl = left + ((right - left + 1) + 2) / 4;
  1631. int cr = left + ((right - left + 1) * 3 + 2) / 4;
  1632. int ct = top;
  1633. int cb = bottom;
  1634. //初始化字符区域参数
  1635. char_area_avg_gray = 0;
  1636. char_area_black_count = 0;
  1637. char_area_size = 0;
  1638. for (int y = ct; y <= cb; y++)
  1639. {
  1640. for (int x = cl; x <= cr; x++)
  1641. {
  1642. gray = ggray(dst, x, y);
  1643. char_area_size++;
  1644. if (gray<tembackcolor){
  1645. char_area_avg_gray += gray;
  1646. char_area_black_count++;
  1647. }
  1648. }
  1649. }
  1650. if (char_area_black_count>0)char_area_avg_gray /= char_area_black_count;
  1651. //灰度直方图
  1652. int gray256[260];
  1653. cellSize = 0;
  1654. memset(gray256, 0, sizeof(gray256));
  1655. double yRatio, xRatio, maxRatio;
  1656. double dist;
  1657. if (item.width >= item.height){
  1658. xRatio = 1.0;
  1659. yRatio = (item.width / (double)item.height)*(1 - 0.3);
  1660. if (yRatio<1)yRatio = 1;
  1661. }
  1662. else{
  1663. yRatio = 1.0;
  1664. xRatio = (item.height / (double)item.width)*(1 - 0.3);
  1665. if (xRatio<1)xRatio = 1;
  1666. }
  1667. double yd = item.centery;
  1668. double xd = item.centerx;
  1669. //理想点数
  1670. int li_xiang_dian_shu = item.width*item.height;
  1671. double dx;
  1672. double dy;
  1673. maxRatio = sqrt(xRatio*xRatio + yRatio*yRatio);
  1674. xRatio = xRatio / (item.width / 2.0);
  1675. yRatio = yRatio / (item.height / 2.0);
  1676. xRatio = xRatio / maxRatio;
  1677. yRatio = yRatio / maxRatio;
  1678. refArea = 0;
  1679. double distSum = 0;
  1680. avg_gray = 0;
  1681. grayNum = 0;
  1682. for (int y = top; y <= bottom; y++)
  1683. {
  1684. for (int x = left; x <= right; x++)
  1685. {
  1686. gray = ggray(dst, x, y);
  1687. gray256[gray]++;
  1688. cellSize++;
  1689. if (gray<tembackcolor){
  1690. avg_gray += gray;
  1691. grayNum++;
  1692. }
  1693. dx = (x - xd)*xRatio;
  1694. dy = (y - yd)*xRatio;
  1695. dist = sqrt(dx*dx + dy*dy);
  1696. gray = max(0, 255 - gray - 32);
  1697. refArea += gray*(1 - dist);
  1698. distSum += (1 - dist);
  1699. }
  1700. }
  1701. avg_gray = grayNum>0 ? avg_gray / grayNum : 256;
  1702. refArea /= 16;
  1703. refArea /= distSum;
  1704. //灰度上限
  1705. int grayUp = 256 - schemaPage.hei_du_ling_min_du * 16;
  1706. mo_hu_du = 0;
  1707. //合格黑点数
  1708. int he_ge_hei_dian_shu = 0;
  1709. for (int i = 0; i<grayUp; i++)
  1710. {
  1711. if (gray256[i]>0){
  1712. he_ge_hei_dian_shu += gray256[i];
  1713. mo_hu_du += (i*gray256[i]);
  1714. }
  1715. }
  1716. mo_hu_du = he_ge_hei_dian_shu>0 ? mo_hu_du / he_ge_hei_dian_shu : 0;
  1717. double ping_jun_hei_du = he_ge_hei_dian_shu>0 ? mo_hu_du / he_ge_hei_dian_shu : 0;
  1718. int i, endpos, flag = 0;
  1719. int hui_du_he = 0;
  1720. int hei_dian_shu = 0;
  1721. for (i = 0, endpos = grayUp; i<endpos; i++)
  1722. {
  1723. if (gray256[i]>0){
  1724. if (flag == 0){
  1725. flag = 1;
  1726. endpos = min(grayUp, i + 40);
  1727. }
  1728. hui_du_he += (i*gray256[i]);
  1729. hei_dian_shu += gray256[i];
  1730. }
  1731. if (hei_dian_shu>li_xiang_dian_shu*0.1 || hei_dian_shu >= he_ge_hei_dian_shu*0.3 + 1){
  1732. endpos = i;
  1733. i++;
  1734. break;
  1735. }
  1736. }
  1737. for (; i<grayUp&&hei_dian_shu<dianshu_meihaomi*dianshu_meihaomi / 4 + 1 && hei_dian_shu<hei_dian_shu >= he_ge_hei_dian_shu*0.3 + 1; i++)
  1738. {
  1739. hui_du_he += (i*gray256[i]);
  1740. hei_dian_shu += gray256[i];
  1741. }
  1742. endpos = i;
  1743. gao_heidu = hei_dian_shu>0 ? (255 - hui_du_he / hei_dian_shu) / 16 : 0;
  1744. refArea /= max(1.0, gao_heidu - 2);
  1745. /************确定清晰度**************************/
  1746. int pnt_high = 0, pnt_low = 0, pnt_mid = 0;
  1747. int pos1, pos2, pos3;
  1748. double minRatio = 0.3; maxRatio = 0.8;
  1749. pos1 = 255 - (int)(gao_heidu*maxRatio * 16);
  1750. for (i = 0; i <= pos1; i++)
  1751. {
  1752. pnt_high += gray256[i];
  1753. }
  1754. for (i = pos1 + 1; i<grayUp&&pnt_high<dianshu_meihaomi*dianshu_meihaomi / 4 + 1; i++)
  1755. {
  1756. pnt_high += gray256[i];
  1757. }
  1758. pos1 = i;
  1759. pos2 = hei_dian_shu*minRatio >= 3 ? 255 - (int)(gao_heidu*minRatio * 16) : 255 - 48;
  1760. pos3 = min(pos2, pos1 + 16);
  1761. for (i = pos1 + 1; i <= pos3; i++)
  1762. {
  1763. pnt_mid += gray256[i];
  1764. }
  1765. for (i = pos3 + 1; i <= pos2; i++)
  1766. {
  1767. pnt_low += gray256[i];
  1768. }
  1769. if (pnt_high == 0){ return identify::result::IDF_DIVIDE_ZERO; }
  1770. mo_hu_du = ((pnt_low + pnt_mid) / (double)pnt_high + pnt_low / (double)(pnt_high + pnt_mid)) / 2.0;
  1771. mo_hu_du = min(mo_hu_du, gao_heidu - ping_jun_hei_du);
  1772. mo_hu_du = min(min(gao_heidu, mo_hu_du), 5.0);
  1773. return identify::result::IDF_SUCCESS;
  1774. }
  1775. //分析全局属性
  1776. int COnLineCardPageIdentifier::analyseOmrPanoramic(SchemaPage& schemaPage)
  1777. {
  1778. int i;
  1779. int lev;
  1780. int count; // 所有涂点个数
  1781. int numAblePnt; // 参与分析的点的个数
  1782. double maxblk[16];
  1783. int numBlk[16];
  1784. double maxBlur[16];
  1785. int numBlur[16];
  1786. double stdBlur;
  1787. memset(numBlk, 0, sizeof(int) * 16);
  1788. memset(maxblk, 0, sizeof(double) * 16);
  1789. memset(maxBlur, 0, sizeof(double) * 16);
  1790. memset(numBlur, 0, sizeof(int) * 16);
  1791. count = schemaPage.itemCount;
  1792. numAblePnt = 0;
  1793. double& m_avaMaxBlk = anlyseResult.avgMax_heidu;
  1794. double& m_benchmarkBlk = anlyseResult.benchmarkBlk;
  1795. double& m_benchmarkArea = anlyseResult.benchmarkAreaBlk;
  1796. m_avaMaxBlk = 0;
  1797. for (i = 0; i<count; i++)
  1798. {
  1799. OnlineAnalyseResult::itemResult& itemr = anlyseResult.itemAnalyseResult[i];
  1800. if (itemr.heidu < m_benchmarkBlk || itemr.xiangdui_value < m_benchmarkArea)
  1801. continue;
  1802. if (itemr.heidu< 4.0) continue;
  1803. m_avaMaxBlk += itemr.heidu;
  1804. numAblePnt++;
  1805. lev = int(itemr.heidu);
  1806. maxblk[lev] += itemr.heidu;
  1807. numBlk[lev] ++;
  1808. lev = int(itemr.mohudu);
  1809. maxBlur[lev] += itemr.mohudu;
  1810. numBlur[lev] ++;
  1811. //pPnt++;
  1812. }
  1813. if (numAblePnt > 0)
  1814. m_avaMaxBlk /= numAblePnt;
  1815. else
  1816. m_avaMaxBlk = 12;
  1817. int stdId = 2;
  1818. int numStd = numBlk[0] + numBlk[1] + numBlk[2];
  1819. int numCur = numStd;
  1820. for (i = 3; i<16; i++)
  1821. {
  1822. numCur += (numBlk[i] - numBlk[i - 3]);
  1823. if (numCur > numStd)
  1824. {
  1825. numStd = numCur;
  1826. stdId = i;
  1827. }
  1828. }
  1829. double stdBlk = maxblk[stdId - 2] + maxblk[stdId - 1] + maxblk[stdId];
  1830. if (numStd > 0)
  1831. stdBlk /= numStd;
  1832. else
  1833. stdBlk = 12;
  1834. // 计算模糊度代表值
  1835. stdId = 2;
  1836. numStd = numBlur[0] + numBlur[1] + numBlur[2];
  1837. numCur = numStd;
  1838. for (i = 3; i<16; i++)
  1839. {
  1840. numCur += (numBlur[i] - numBlur[i - 3]);
  1841. if (numCur > numStd)
  1842. {
  1843. numStd = numCur;
  1844. stdId = i;
  1845. }
  1846. }
  1847. stdBlur = maxBlur[stdId - 2] + maxBlur[stdId - 1] + maxBlur[stdId];
  1848. if (numStd > 0)
  1849. stdBlur /= numStd;
  1850. else
  1851. stdBlur = 0;
  1852. if (stdBlur < 2.0)
  1853. stdBlk -= stdBlur;
  1854. else
  1855. stdBlk -= 2.0;
  1856. if (stdBlk < m_avaMaxBlk)
  1857. m_avaMaxBlk = stdBlk;
  1858. //统计字符区域的黑色点数的平均值
  1859. if (schemaPage.groupCount>0){
  1860. int max_item = 1;
  1861. for (int gi = 0; gi<schemaPage.groupCount; gi++)
  1862. {
  1863. if (schemaPage.groups[gi].itemCount>max_item) max_item = schemaPage.groups[gi].itemCount;
  1864. }
  1865. int * char_area_black_counts = new int[schemaPage.groupCount];
  1866. int cur_item_count;//当前选项的数量
  1867. for (int ii = 0; ii<max_item; ii++)
  1868. {
  1869. cur_item_count = 0;
  1870. long total_char_area_size = 0;
  1871. for (int gi = 0; gi<schemaPage.groupCount; gi++)
  1872. {
  1873. if (schemaPage.groups[gi].itemCount>ii){
  1874. OnlineAnalyseResult::itemResult& ir = anlyseResult.itemAnalyseResult[schemaPage.groups[gi].itemIndex[ii]];
  1875. char_area_black_counts[cur_item_count] = ir.char_area_black_count;
  1876. total_char_area_size += ir.char_area_size;
  1877. cur_item_count++;
  1878. }
  1879. }
  1880. double char_area_size_avg = total_char_area_size / (double)cur_item_count;
  1881. int temp;
  1882. for (int m = 0; m<cur_item_count; m++)
  1883. {
  1884. for (int n = m + 1; n<cur_item_count; n++)
  1885. {
  1886. if (char_area_black_counts[n]<char_area_black_counts[m]){
  1887. temp = char_area_black_counts[n];
  1888. char_area_black_counts[n] = char_area_black_counts[m];
  1889. char_area_black_counts[m] = temp;
  1890. }
  1891. }
  1892. }
  1893. anlyseResult.char_area_black_avg[ii] = -1;
  1894. if (char_area_black_counts[0] * 1.4<char_area_black_counts[cur_item_count - 1] || char_area_black_counts[cur_item_count - 1] * 1.4<char_area_size_avg){
  1895. double t = char_area_black_counts[0] * 1.15;
  1896. double f = 0;
  1897. int count__ = 0;
  1898. for (int l = 0; l<cur_item_count; l++)
  1899. {
  1900. if (t<char_area_black_counts[l]){
  1901. f += char_area_black_counts[l];
  1902. count__++;
  1903. }
  1904. }
  1905. anlyseResult.char_area_black_avg[ii] = f / count__;
  1906. }
  1907. }
  1908. delete[]char_area_black_counts;
  1909. }
  1910. return(0);
  1911. }
  1912. /************************************************************************/
  1913. /* 生成OMR涂点字符串(客观题填涂判断) 2014年11月28日10:54:19 */
  1914. /************************************************************************/
  1915. int COnLineCardPageIdentifier::GenerateOmrStr(SchemaPage& schemaPage)
  1916. {
  1917. char omr_str[512] = { 0 };
  1918. int omr_str_len = 0;
  1919. omr_result->group_result.resize(schemaPage.groups.size());
  1920. identify::result::KEGUANTI_RESULT* ketuanti_result_value = omr_result->group_result.data();
  1921. //标记是否被选中
  1922. SelectType sel[MAX_ITEM];
  1923. for (int groupIndex = 0; groupIndex<schemaPage.groupCount; groupIndex++)
  1924. {
  1925. omr_str_len = 0;
  1926. memset(sel, UNSELECT, sizeof(sel));
  1927. SchemaGroup& group = schemaPage.groups[groupIndex];
  1928. int pn = min(MAX_ITEM, group.itemCount);
  1929. OnlineAnalyseResult::itemResult* results[MAX_ITEM];
  1930. SchemaItem* items[MAX_ITEM] = { nullptr };
  1931. int maxgraynum = -1;
  1932. int mingraynum = 0xffffff;
  1933. for (int i = 0; i<pn; i++){
  1934. results[i] = &anlyseResult.itemAnalyseResult[group.itemIndex[i]];
  1935. items[i] = &m_items[group.itemIndex[i]];
  1936. if (results[i]->gray_num > maxgraynum)maxgraynum = results[i]->gray_num;
  1937. if (results[i]->gray_num < mingraynum)mingraynum = results[i]->gray_num;
  1938. }
  1939. int selected_num = 0;
  1940. int unselect_num = 0;
  1941. int uncertain_num = 0;
  1942. for (int i = 0; i<pn; i++)
  1943. {
  1944. //被涂面积小于10%
  1945. if (results[i]->gray_num<results[i]->cell_size*0.1){
  1946. sel[i] = UNSELECT;
  1947. unselect_num++;
  1948. continue;
  1949. }
  1950. //面积超过65%
  1951. if (results[i]->gray_num>results[i]->cell_size*0.65){
  1952. sel[i] = SELECTED;
  1953. selected_num++;
  1954. continue;
  1955. }
  1956. //填涂面积相差达%15,将面积不小于最大点填涂面积的总面积%10的涂点视为填涂
  1957. if (maxgraynum - mingraynum>results[i]->cell_size*0.2&&results[i]->gray_num>maxgraynum - results[i]->cell_size*0.1){
  1958. sel[i] = SELECTED;
  1959. selected_num++;
  1960. continue;
  1961. }
  1962. //填涂面积相差达%50,将面积不小于最大点填涂面积的总面积%30的涂点视为填涂
  1963. if (maxgraynum - mingraynum>results[i]->cell_size*0.5&&results[i]->gray_num>maxgraynum - results[i]->cell_size*0.3){
  1964. sel[i] = SELECTED;
  1965. selected_num++;
  1966. continue;
  1967. }
  1968. if (maxgraynum - results[i]->gray_num>results[i]->cell_size*0.2){
  1969. sel[i] = UNSELECT;
  1970. unselect_num++;
  1971. continue;
  1972. }
  1973. sel[i] = UNCERTAIN;
  1974. uncertain_num++;
  1975. }
  1976. int out_count;
  1977. identify::result::OMR_QUESTION_STATE question_state = identify::result::OMR_QUESTION_STATE::OMR_QUESTION_STATE_NORMAL;
  1978. if (group.omr_out_type == OMR_OUT_TYPE::OMR_OUT_TYPE_SINGLE_NONSTRICT){
  1979. if (selected_num>1){
  1980. int maxgrayindex = -1;
  1981. int maxgraynum = -101;
  1982. for (int i = 0; i<pn; i++)
  1983. {
  1984. if (sel[i] == SELECTED)
  1985. {
  1986. if ((results[i]->gray_num - maxgraynum>100) || (abs(results[i]->gray_num - maxgraynum) <= 100 && results[i]->heidu > results[maxgrayindex]->heidu))
  1987. {
  1988. maxgraynum = results[i]->gray_num;
  1989. maxgrayindex = i;
  1990. }
  1991. }
  1992. }
  1993. for (int i = 0; i<pn; i++)
  1994. {
  1995. if (sel[i] == SELECTED&&i != maxgrayindex){
  1996. sel[i] = UNSELECT;
  1997. }
  1998. }
  1999. }
  2000. out_count = 0;
  2001. for (int i = 0; i<pn; i++){
  2002. if (sel[i] == SELECTED){
  2003. if (out_count>0)omr_str[omr_str_len++] = schemaPage.option_spacer;
  2004. omr_str[omr_str_len++] = items[i]->OutChar;
  2005. out_count++;
  2006. }
  2007. }
  2008. if (out_count == 0){
  2009. omr_str[omr_str_len++] = schemaPage.unselect_char;
  2010. question_state = identify::result::OMR_QUESTION_STATE::OMR_QUESTION_STATE_LOUTU;
  2011. }
  2012. }
  2013. else if (group.omr_out_type == OMR_OUT_TYPE::OMR_OUT_TYPE_MUTIL_STRICT)
  2014. {
  2015. out_count = 0;
  2016. for (int i = 0; i < pn; i++)
  2017. {
  2018. if (sel[i] == SELECTED){
  2019. if (out_count > 0)omr_str[omr_str_len++] = schemaPage.option_spacer;
  2020. omr_str[omr_str_len++] = items[i]->OutChar;
  2021. out_count++;
  2022. }
  2023. }
  2024. if (out_count == 0){
  2025. omr_str[omr_str_len++] = schemaPage.unselect_char;
  2026. question_state = identify::result::OMR_QUESTION_STATE::OMR_QUESTION_STATE_LOUTU;
  2027. }
  2028. }
  2029. omr_str[omr_str_len] = '\0';
  2030. ketuanti_result_value[groupIndex].question_code = group.question_code;
  2031. ketuanti_result_value[groupIndex].obj_id = static_cast<unsigned int>(group.question_index);
  2032. ketuanti_result_value[groupIndex].answer = omr_str;
  2033. ketuanti_result_value[groupIndex].question_state = identify::result::OMR_QUESTION_STATE::OMR_QUESTION_STATE_NORMAL;
  2034. ketuanti_result_value[groupIndex].omr_question_type = group.omr_out_type == OMR_OUT_TYPE::OMR_OUT_TYPE_SINGLE_NONSTRICT ?
  2035. identify::DANXUANTI :identify::DUOXUANTI ;
  2036. }
  2037. return 0;
  2038. }
  2039. // //识别指定图像
  2040. int COnLineCardPageIdentifier::Identify(const IplImage* img, void* out_result, int out_size)
  2041. {
  2042. if (out_result){
  2043. this->out_result = out_result;
  2044. this->omr_result = (identify::result::OMR_RESULT*)this->out_result;
  2045. // printf("线程:%s 缓冲区:缺考标记:%d %p\n", std::to_string(GetCurrentThreadId()).c_str(), omr_result->quekaoFlag, this->out_result);
  2046. src = (IplImage*)img;
  2047. if (src == NULL)
  2048. return identify::result::IDF_FAILURE;
  2049. }else{
  2050. return identify::result::IDF_FAILURE;
  2051. }
  2052. return Identify();
  2053. }
  2054. /************************************************************************/
  2055. /* 获取区域黑色点数量 */
  2056. /************************************************************************/
  2057. int COnLineCardPageIdentifier::GetBlackArea(const IplImage * dst)
  2058. {
  2059. int blackArea = 0;
  2060. CvRect rect = cvGetImageROI(dst);
  2061. int(*GetGray)(const IplImage * dst, int x, int y);
  2062. if (dst->nChannels == 1)GetGray = GetGrayValue;
  2063. else GetGray = GetBGRGray;
  2064. for (int y = rect.y, endy = rect.y + rect.height; y<endy; y++)
  2065. {
  2066. for (int x = rect.x, endx = rect.x + rect.width; x<endx; x++)
  2067. {
  2068. if (GetGray(dst, x, y)<180)blackArea++;
  2069. }
  2070. }
  2071. return blackArea;
  2072. }
  2073. /************************************************************************/
  2074. /*
  2075. 图像基本信息检查
  2076. 最小宽高限制
  2077. 长宽比例限制 */
  2078. /************************************************************************/
  2079. int COnLineCardPageIdentifier::BaseCheck()
  2080. {
  2081. if (src->width<200 || src->height<200){
  2082. return identify::result::IDF_SIZE2SMALL;
  2083. }
  2084. if (abs(max(src->width, src->height) / (double)min(src->width, src->height) - max(21.0, 27.9) / (double)min(21.0, 27.9))>0.2){
  2085. //图像长宽比不符
  2086. return identify::result::IDF_SIDERATIO;
  2087. }
  2088. return identify::result::IDF_SUCCESS;
  2089. }
  2090. /*
  2091. 临时变量初始化
  2092. */
  2093. void COnLineCardPageIdentifier::InitalizeTempVar()
  2094. {
  2095. m_relationKey.clear();
  2096. m_relationValue.clear();
  2097. }
  2098. // 读取学生学号
  2099. int COnLineCardPageIdentifier::ReadStudentID(SchemaPage &schemaPage, IplImage * dst)
  2100. {
  2101. omr_result->qr_result.clear();
  2102. identify::result::CODE_RESULT code_rst; code_rst.qr_str = "";
  2103. code_rst.is_stu_id = false;
  2104. if (strlen(schemaPage.student_code)>0){
  2105. code_rst.qr_str = schemaPage.student_code;
  2106. code_rst.is_stu_id = schemaPage.is_stu_id;
  2107. }else{
  2108. m_items = schemaPage.stuItems;
  2109. // 计算每个填图区域图像信息
  2110. int global_background_color_avg = caculate_global_background(schemaPage, dst, true);
  2111. //模糊度,面积,高黑度,平均黑度
  2112. double mo_hu_du, refArea, gao_heidu, avg_gray;
  2113. //黑色像素数,涂点大小
  2114. int grayNum, cellSize;
  2115. //高黑度和
  2116. double topGraySum = 0;
  2117. //高黑度面积
  2118. double topGrayAreaSum = 0;
  2119. anlyseResult.backGroundGray = global_background_color_avg;
  2120. anlyseResult.cellAvgGray = 0;
  2121. anlyseResult.cellNum = 0;
  2122. int numTopGray = 0;
  2123. int ret;
  2124. item_result.resize(schemaPage.stuItemCount);
  2125. for (int i = 0; i < schemaPage.stuItemCount; i++)
  2126. {
  2127. SchemaItem & item = m_items[i];
  2128. item_result[i] = GetCVRect(item);
  2129. ret = caculate_cell_property(schemaPage, item, dst, cellSize, refArea, avg_gray, grayNum, mo_hu_du, gao_heidu, anlyseResult.itemAnalyseResult[i].char_area_size, anlyseResult.itemAnalyseResult[i].char_area_black_count, anlyseResult.itemAnalyseResult[i].char_area_avg_gray);
  2130. if (ret != identify::result::IDF_SUCCESS)return ret;
  2131. topGraySum += gao_heidu;
  2132. topGrayAreaSum += refArea;
  2133. numTopGray++;
  2134. anlyseResult.cellAvgGray += avg_gray;
  2135. anlyseResult.cellNum++;
  2136. anlyseResult.itemAnalyseResult[i].avg_gray = avg_gray;
  2137. anlyseResult.itemAnalyseResult[i].cell_size = cellSize;
  2138. anlyseResult.itemAnalyseResult[i].gray_num = grayNum;
  2139. anlyseResult.itemAnalyseResult[i].heidu = gao_heidu;
  2140. anlyseResult.itemAnalyseResult[i].mohudu = mo_hu_du;
  2141. anlyseResult.itemAnalyseResult[i].xiangdui_mianji = refArea;
  2142. anlyseResult.itemAnalyseResult[i].xiangdui_value = refArea;
  2143. }
  2144. anlyseResult.cellAvgGray = anlyseResult.cellNum > 0 ? anlyseResult.cellAvgGray / anlyseResult.cellNum : 256;
  2145. anlyseResult.avg_heidu = topGrayAreaSum > 0 ? topGraySum / topGrayAreaSum : 6;
  2146. if (numTopGray >= int(schemaPage.itemCount / 5)){
  2147. anlyseResult.float_heidu = anlyseResult.avg_heidu / schemaPage.user_float_heidu;
  2148. anlyseResult.benchmarkBlk = schemaPage.worthwhileBlk + anlyseResult.float_heidu;
  2149. }
  2150. else{
  2151. anlyseResult.benchmarkBlk = numTopGray > 0 ? min(6.0, schemaPage.worthwhileBlk + anlyseResult.avg_heidu / schemaPage.user_float_heidu) : 6;
  2152. anlyseResult.float_heidu = anlyseResult.benchmarkBlk - schemaPage.worthwhileBlk;
  2153. }
  2154. if (numTopGray > 0){
  2155. anlyseResult.avg_heidu = topGrayAreaSum / numTopGray;
  2156. anlyseResult.float_heidu = anlyseResult.avg_heidu / schemaPage.user_float_heidu;
  2157. anlyseResult.benchmarkAreaBlk = anlyseResult.float_heidu;
  2158. }
  2159. else{
  2160. anlyseResult.avg_heidu = 0;
  2161. anlyseResult.float_heidu = 0;
  2162. anlyseResult.benchmarkAreaBlk = 0.1;
  2163. }
  2164. // 获取
  2165. std::string strStudent;
  2166. if (GenerateOmrStr(schemaPage, strStudent) == 0){
  2167. code_rst.qr_str = strStudent;
  2168. }
  2169. }
  2170. if (!code_rst.qr_str.empty())
  2171. omr_result->qr_result.push_back(code_rst);
  2172. omr_result->online_card_paperid = schemaPage.paper_id;
  2173. omr_result->schoolCardStatus = schemaPage.schoolCardStatus;
  2174. if (m_bUseQr)
  2175. {
  2176. std::vector<std::string> split_qr;
  2177. split(m_strQrClass, (std::string)",", &split_qr);
  2178. if (split_qr.size()>1&& !split_qr[1].empty()){
  2179. omr_result->paper_id = std::stoi(split_qr[1]);
  2180. }
  2181. }
  2182. else{
  2183. omr_result->paper_id = -1;
  2184. }
  2185. return identify::result::IDF_SUCCESS;
  2186. }
  2187. //读取客观题分数
  2188. int COnLineCardPageIdentifier::ReadKeGuanTi(SchemaPage &schemaPage, IplImage * dst)
  2189. {
  2190. m_items = schemaPage.items;
  2191. int global_background_color_avg = caculate_global_background(schemaPage, dst);
  2192. //模糊度,面积,高黑度,平均黑度
  2193. double mo_hu_du, refArea, gao_heidu, avg_gray;
  2194. //黑色像素数,涂点大小
  2195. int grayNum, cellSize;
  2196. //高黑度和
  2197. double topGraySum = 0;
  2198. //高黑度面积
  2199. double topGrayAreaSum = 0;
  2200. anlyseResult.backGroundGray = global_background_color_avg;
  2201. anlyseResult.cellAvgGray = 0;
  2202. anlyseResult.cellNum = 0;
  2203. int numTopGray = 0;
  2204. int ret;
  2205. item_result.resize(schemaPage.itemCount);
  2206. for (int i = 0; i<schemaPage.itemCount; i++)
  2207. {
  2208. SchemaItem & item = m_items[i];
  2209. item_result[i] = GetCVRect(item);
  2210. ret = caculate_cell_property(schemaPage, item, dst, cellSize, refArea, avg_gray, grayNum, mo_hu_du, gao_heidu, anlyseResult.itemAnalyseResult[i].char_area_size, anlyseResult.itemAnalyseResult[i].char_area_black_count, anlyseResult.itemAnalyseResult[i].char_area_avg_gray);
  2211. if (ret != identify::result::IDF_SUCCESS)return ret;
  2212. topGraySum += gao_heidu;
  2213. topGrayAreaSum += refArea;
  2214. numTopGray++;
  2215. anlyseResult.cellAvgGray += avg_gray;
  2216. anlyseResult.cellNum++;
  2217. anlyseResult.itemAnalyseResult[i].avg_gray = avg_gray;
  2218. anlyseResult.itemAnalyseResult[i].cell_size = cellSize;
  2219. anlyseResult.itemAnalyseResult[i].gray_num = grayNum;
  2220. anlyseResult.itemAnalyseResult[i].heidu = gao_heidu;
  2221. anlyseResult.itemAnalyseResult[i].mohudu = mo_hu_du;
  2222. anlyseResult.itemAnalyseResult[i].xiangdui_mianji = refArea;
  2223. anlyseResult.itemAnalyseResult[i].xiangdui_value = refArea;
  2224. }
  2225. anlyseResult.cellAvgGray = anlyseResult.cellNum>0 ? anlyseResult.cellAvgGray / anlyseResult.cellNum : 256;
  2226. anlyseResult.avg_heidu = topGrayAreaSum>0 ? topGraySum / topGrayAreaSum : 6;
  2227. if (numTopGray >= int(schemaPage.itemCount / 5)){
  2228. anlyseResult.float_heidu = anlyseResult.avg_heidu / schemaPage.user_float_heidu;
  2229. anlyseResult.benchmarkBlk = schemaPage.worthwhileBlk + anlyseResult.float_heidu;
  2230. }
  2231. else{
  2232. anlyseResult.benchmarkBlk = numTopGray>0 ? min(6.0, schemaPage.worthwhileBlk + anlyseResult.avg_heidu / schemaPage.user_float_heidu) : 6;
  2233. anlyseResult.float_heidu = anlyseResult.benchmarkBlk - schemaPage.worthwhileBlk;
  2234. }
  2235. if (numTopGray>0){
  2236. anlyseResult.avg_heidu = topGrayAreaSum / numTopGray;
  2237. anlyseResult.float_heidu = anlyseResult.avg_heidu / schemaPage.user_float_heidu;
  2238. anlyseResult.benchmarkAreaBlk = anlyseResult.float_heidu;
  2239. }
  2240. else{
  2241. anlyseResult.avg_heidu = 0;
  2242. anlyseResult.float_heidu = 0;
  2243. anlyseResult.benchmarkAreaBlk = 0.1;
  2244. }
  2245. analyseOmrPanoramic(schemaPage);
  2246. GenerateOmrStr(schemaPage);
  2247. return identify::result::IDF_SUCCESS;
  2248. }
  2249. //读取填空题,和主观题分数
  2250. int COnLineCardPageIdentifier::ReadQuestionScore(SchemaPage &schemaPage, IplImage * dst, CvMemStorage* storage)
  2251. {
  2252. int tiankong_total_count = 0;
  2253. int zhuguan_total_count = 0;
  2254. for (int i = 0; i<schemaPage.question_score_count; i++){
  2255. if (schemaPage.question_score[i].type == QUESTION_SCORE_TYPE::MUTIL_SCORE)
  2256. zhuguan_total_count++;
  2257. else if (schemaPage.question_score[i].type == QUESTION_SCORE_TYPE::SINGLE_SCORE)
  2258. tiankong_total_count++;
  2259. }
  2260. omr_result->tiankongti_result.resize(tiankong_total_count);
  2261. omr_result->zhutuanti_result.resize(zhuguan_total_count);
  2262. identify::result::TIANKONGTI_RESULT*tiankongti_result_value = omr_result->tiankongti_result.data();
  2263. identify::result::ZHUGUANTI_RESULT*zhutuanti_result_value = omr_result->zhutuanti_result.data();
  2264. int score_count = 0;
  2265. int tiankong_count = 0;
  2266. for (int i = 0; i<schemaPage.question_score_count; i++){
  2267. SchemaQuestionScore& qs = schemaPage.question_score[i];
  2268. //查找题目定位点,校正题目位置
  2269. SchemaLocatePoint& lp1 = schemaPage.questionLocatePoints[qs.questionLocatePointIndex[0]];
  2270. SchemaLocatePoint& lp2 = schemaPage.questionLocatePoints[qs.questionLocatePointIndex[1]];
  2271. CvPoint2D32f point1, point2;
  2272. BOOL s1 = FindQuestionLocatePoint(dst, lp1, point1);
  2273. BOOL s2 = FindQuestionLocatePoint(dst, lp2, point2);
  2274. if (!s1&&!s2){
  2275. point1.x = lp1.centerx;
  2276. point1.y = lp1.centery;
  2277. point2.x = lp2.centerx;
  2278. point2.y = lp2.centery;
  2279. }
  2280. else if (!s1){
  2281. point1.x = lp1.centerx + (point2.x - lp2.centerx);
  2282. point1.y = lp1.centery + (point2.y - lp2.centery);
  2283. }
  2284. else if (!s2){
  2285. point2.x = lp2.centerx + (point1.x - lp1.centerx);
  2286. point2.y = lp2.centery + (point1.y - lp1.centery);
  2287. }
  2288. float data[6];
  2289. CvMat* m = caculate_scm(point1, point2, cvPoint2D32f(lp1.centerx, lp1.centery), cvPoint2D32f(lp2.centerx, lp2.centery));
  2290. memcpy(data, m->data.fl, sizeof(float) * 6);
  2291. cvReleaseMat(&m);
  2292. SchemaQuestionScore::Point * point[4] = { &qs.lt, &qs.rt, &qs.lb, &qs.rb };
  2293. float lx = -1, ly = -1;
  2294. float rx = -1, ry = -1;
  2295. double pscale = 1.0;
  2296. int redcount[30] = { 0 };
  2297. int red_in_count[30] = { 0 };
  2298. lx = data[0] * qs.lt.x + data[1] * qs.lt.y + data[2];
  2299. rx = data[0] * qs.rt.x + data[1] * qs.rt.y + data[2];
  2300. ly = data[3] * qs.lt.x + data[4] * qs.lt.y + data[5];
  2301. ry = data[3] * qs.rt.x + data[4] * qs.rt.y + data[5];
  2302. if (qs.type == QUESTION_SCORE_TYPE::MUTIL_SCORE){
  2303. if (lx >= 0 && rx >= 0){
  2304. float h = (point[2]->y - point[0]->y)*pscale;
  2305. CvRect detect_rect = cvRect(qs.scoreBox.pos.x, qs.scoreBox.pos.y, qs.scoreBox.pos.w, qs.scoreBox.pos.h);
  2306. GetZhuGuanTiRedCount(dst, detect_rect, detect_rect, redcount, red_in_count, qs.count, qs.scoreBox.type != 1 || qs.scoreBox.bPoint, false);
  2307. float score = 0;
  2308. if (qs.scoreBox.type == 1)
  2309. {
  2310. // 小数位
  2311. if (qs.scoreBox.bPoint){
  2312. if (red_in_count[0] > 25){
  2313. score += 0.5;
  2314. }
  2315. }
  2316. // 整数位
  2317. {
  2318. int red_area = 0;
  2319. int red_area_index = -1;
  2320. for (int i = (qs.scoreBox.bPoint ? 1 : 0);
  2321. i < qs.count; ++i)
  2322. {
  2323. if (red_in_count[i]>red_area){
  2324. red_area = red_in_count[i]; red_area_index = i;
  2325. }
  2326. }
  2327. if (red_area_index >= (qs.scoreBox.bPoint ? 1 : 0)){
  2328. score += (qs.scoreBox.limit - red_area_index + (qs.scoreBox.bPoint ? 1 : 0));
  2329. }
  2330. }
  2331. }
  2332. else if (qs.scoreBox.type == 2)
  2333. {
  2334. // 小数位
  2335. if (qs.scoreBox.bPoint){
  2336. if (red_in_count[0] > 25){
  2337. score += 0.5;
  2338. }
  2339. }
  2340. {
  2341. //个位数
  2342. int red_area = 0;
  2343. int red_area_index = -1;
  2344. for (int i = (qs.scoreBox.bPoint ? 1 : 0);
  2345. i < (qs.scoreBox.bPoint ? 11 : 10); ++i)
  2346. {
  2347. if (red_in_count[i]>red_area){
  2348. red_area = red_in_count[i]; red_area_index = i;
  2349. }
  2350. }
  2351. if (red_area_index >= (qs.scoreBox.bPoint ? 1 : 0)){
  2352. score += (9 - red_area_index + (qs.scoreBox.bPoint ? 1 : 0));
  2353. }
  2354. }
  2355. {
  2356. //十位数
  2357. int red_area = 0;
  2358. int red_area_index = -1;
  2359. int begin_index = (qs.scoreBox.bPoint ? 11 : 10);
  2360. for (int i = begin_index; i < begin_index + (int)(qs.scoreBox.limit / 10) + 1; ++i)
  2361. {
  2362. if (red_in_count[i] > red_area){ red_area = red_in_count[i]; red_area_index = i; }
  2363. }
  2364. if (red_area_index >= begin_index){
  2365. score = score + 10 * ((int)(qs.scoreBox.limit / 10) - (red_area_index - begin_index) + 1);
  2366. }
  2367. }
  2368. }
  2369. zhutuanti_result_value[score_count].question_score = score;
  2370. zhutuanti_result_value[score_count].question_state = identify::result::OMR_QUESTION_STATE::OMR_QUESTION_STATE_NORMAL;
  2371. }
  2372. else{
  2373. zhutuanti_result_value[score_count].question_score = 0;
  2374. zhutuanti_result_value[score_count].question_state = identify::result::OMR_QUESTION_STATE::OMR_QUESTION_STATE_WUFASHIBIE;
  2375. }
  2376. zhutuanti_result_value[score_count].obj_id = static_cast<unsigned int>(qs.question_index);
  2377. zhutuanti_result_value[score_count].question_code = qs.question_code;
  2378. zhutuanti_result_value[score_count].omr_question_type = identify::OMR_QUESTION_TYPE::ZHUTUANTI;
  2379. zhutuanti_result_value[score_count].total = 0;
  2380. zhutuanti_result_value[score_count].required = 0;
  2381. zhutuanti_result_value[score_count].xuanze = -1;
  2382. if (qs.scoreBox.bXuanZuoTi){
  2383. zhutuanti_result_value[score_count].omr_question_type = identify::OMR_QUESTION_TYPE::XUANZUOTI;
  2384. zhutuanti_result_value[score_count].total = qs.countNum;
  2385. zhutuanti_result_value[score_count].required = qs.countItemSelete;
  2386. zhutuanti_result_value[score_count].xuanze = ReadXuanZuoTi(schemaPage, qs, dst);
  2387. zhutuanti_result_value[score_count].question_code_all = qs.question_code_all;
  2388. }
  2389. score_count++;
  2390. }
  2391. else
  2392. {
  2393. if (lx >= 0 && rx >= 0){
  2394. if (qs.count > 1){
  2395. float score = 0;
  2396. int red_area_index = -1;
  2397. // 在线答题卡-填空题
  2398. if (qs.scoreBox.type == 3){
  2399. float h = (point[2]->y - point[0]->y)*pscale;
  2400. CvRect detect_rect = cvRect(qs.scoreBox.pos.x, qs.scoreBox.pos.y, qs.scoreBox.pos.w, qs.scoreBox.pos.h);
  2401. GetZhuGuanTiRedCount(dst, detect_rect, detect_rect, redcount, red_in_count, qs.count, 0, true);
  2402. int red_area = 0;
  2403. for (int m = 0; m<qs.scoreBox.vctScore.size(); m++){
  2404. if (red_in_count[m]>red_area&&red_in_count[m]>25){ red_area = red_in_count[m]; red_area_index = m; }
  2405. }
  2406. if (red_area_index >= 0 && static_cast<int>(qs.scoreBox.vctScore.size())>red_area_index){
  2407. score = qs.scoreBox.vctScore.at(qs.count - red_area_index - 1);
  2408. }
  2409. }
  2410. tiankongti_result_value[tiankong_count].question_score = score;
  2411. tiankongti_result_value[tiankong_count].omr_question_type = identify::OMR_QUESTION_TYPE::TIANKONGTI2;
  2412. }else{
  2413. float w = (rx - lx) / qs.count;
  2414. float h = (point[2]->y - point[0]->y)*pscale;
  2415. for (int j = 0; j<1; j++){
  2416. CvRect rect_detect = cvRect(qs.scoreBox.pos.x, qs.scoreBox.pos.y, qs.scoreBox.pos.w, qs.scoreBox.pos.h);
  2417. int red_area = GetTianKongTiRedCount(dst, rect_detect, rect_detect, 1, 0);
  2418. if (red_area>25){
  2419. tiankongti_result_value[tiankong_count].isright = TRUE;
  2420. break;
  2421. }
  2422. else{
  2423. tiankongti_result_value[tiankong_count].isright = FALSE;
  2424. }
  2425. }
  2426. tiankongti_result_value[tiankong_count].omr_question_type = identify::OMR_QUESTION_TYPE::TIANKONGTI;
  2427. }
  2428. }else{
  2429. tiankongti_result_value[tiankong_count].isright = FALSE;
  2430. if (qs.count > 1)
  2431. {
  2432. tiankongti_result_value[tiankong_count].omr_question_type = identify::OMR_QUESTION_TYPE::TIANKONGTI2;
  2433. }
  2434. else
  2435. {
  2436. tiankongti_result_value[tiankong_count].omr_question_type = identify::OMR_QUESTION_TYPE::TIANKONGTI;
  2437. }
  2438. }
  2439. tiankongti_result_value[tiankong_count].obj_id = static_cast<unsigned int>(qs.question_index);
  2440. tiankongti_result_value[tiankong_count].question_code = qs.question_code;
  2441. tiankong_count++;
  2442. }
  2443. }
  2444. return identify::result::IDF_SUCCESS;
  2445. }
  2446. //截取图像
  2447. int COnLineCardPageIdentifier::ClipImg(SchemaPage& schemaPage, IplImage * dst)
  2448. {
  2449. cvResetImageROI(dst);
  2450. vector<int> compression_params;
  2451. compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
  2452. compression_params.push_back(60);
  2453. omr_result->cut_area_result.resize(schemaPage.cutAreaCount);
  2454. identify::result::CUT_AREA_RESULT* cut_area_result_value = omr_result->cut_area_result.data();
  2455. for (int i = 0; i<schemaPage.cutAreaCount; i++)
  2456. {
  2457. SchemaCutArea& cutArea = schemaPage.cutAreas[i];
  2458. CvRect rect = cvRect(cutArea.centerx - cutArea.width / 2, cutArea.centery - cutArea.height / 2, cutArea.width, cutArea.height);
  2459. SaveRect(rect, cut_area_result_value[i]);
  2460. IplImage* img = CutImage(dst, rect);
  2461. vector<uchar> dst;
  2462. cv::Mat _img = cvarrToMat(img);
  2463. imencode(".jpg", _img, dst, compression_params);
  2464. cut_area_result_value[i].obj_id = cutArea.index;
  2465. cut_area_result_value[i].area_index = cutArea.index;
  2466. cut_area_result_value[i].area_sub_index = cutArea.isfirst?1:0;
  2467. cut_area_result_value[i].centerx = cutArea.centerx;
  2468. cut_area_result_value[i].centery = cutArea.centery;
  2469. cut_area_result_value[i].width = cutArea.width;
  2470. cut_area_result_value[i].height = cutArea.height;
  2471. cut_area_result_value[i].area_name = cutArea.area_name;
  2472. cut_area_result_value[i].is_cut = cutArea.iscut;
  2473. cut_area_result_value[i].img_data.resize(dst.size());
  2474. memcpy(cut_area_result_value[i].img_data.data(), &dst[0], dst.size());
  2475. cvReleaseImage(&img);
  2476. }
  2477. return identify::result::IDF_SUCCESS;
  2478. }
  2479. //获取图像指定区域红色点数(主观题) 去除长直线和周边像素 减少直线边缘偏色影响
  2480. int COnLineCardPageIdentifier::GetZhuGuanTiRedCount(IplImage * dst, const CvRect& rect, const CvRect& normal_rect, int * red_counts, int * red_in_counts, int count, int mutil, bool bTianKongTi /*= false*/)
  2481. {
  2482. if (count >= 30) return 0;
  2483. if (dst->nChannels != 3){ return 0; }
  2484. if (m_nXianXiaDaFenType == 1)
  2485. {
  2486. memset(red_counts, 0, sizeof(int)*count);
  2487. memset(red_in_counts, 0, sizeof(int)*count);
  2488. cvSetImageROI(dst, rect);
  2489. Mat img= cv::cvarrToMat(dst);
  2490. cvResetImageROI(dst);
  2491. int tempi[50] = { 0 };
  2492. try{
  2493. EnterCriticalSection(m_althom_rcs);
  2494. // printf("线程:%s 新算法判分-开始\n", std::to_string(GetCurrentThreadId()).c_str());
  2495. if (api_red_check_analysis(img, count, bTianKongTi ? 3 : 2, mutil, tempi, count) != 0)
  2496. {
  2497. // printf("线程:%s 新算法判分-错误\n", std::to_string(GetCurrentThreadId()).c_str());
  2498. LeaveCriticalSection(m_althom_rcs);
  2499. return 0;
  2500. }
  2501. // printf("线程:%s 新算法判分-结束\n", std::to_string(GetCurrentThreadId()).c_str());
  2502. LeaveCriticalSection(m_althom_rcs);
  2503. }
  2504. catch (cv::Exception&e){
  2505. // printf("线程:%s 新算法判分-异常\n", std::to_string(GetCurrentThreadId()).c_str());
  2506. LeaveCriticalSection(m_althom_rcs);
  2507. #if 0
  2508. static const char* str;
  2509. str = e.what();
  2510. #endif
  2511. }
  2512. for (int i = 0; i < count; i++)
  2513. {
  2514. red_in_counts[i] = tempi[count - i - 1];
  2515. if (red_in_counts[i] > 0)
  2516. {
  2517. red_counts[i] = 30; //目的大于25
  2518. }
  2519. }
  2520. return identify::result::IDF_SUCCESS;
  2521. }
  2522. IplImage* red = NULL;
  2523. BOOL s = GetRedBinary(dst, rect, &red);
  2524. if (!s)return 0;
  2525. IplImage * temp = cvCloneImage(red);
  2526. CvMemStorage* storage = cvCreateMemStorage();
  2527. CvSeq* contours = NULL;
  2528. cvFindContours(temp, storage, &contours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
  2529. int top = normal_rect.y - rect.y;
  2530. int bottom = normal_rect.y + normal_rect.height - 1 - rect.y;
  2531. for (CvContour * c = (CvContour *)contours; c != NULL; c = (CvContour *)c->h_next) {
  2532. CvRect b_rect = cvBoundingRect(c);
  2533. if (b_rect.y>bottom - 1 || b_rect.y + b_rect.height - 1<top + 1){
  2534. cvDrawContours(red, (CvSeq *)c, cvScalar(0), cvScalar(0), 0, CV_FILLED, 8);
  2535. }
  2536. }
  2537. cvReleaseImage(&temp);
  2538. cvReleaseMemStorage(&storage);
  2539. memset(red_counts, 0, sizeof(int)*count);
  2540. memset(red_in_counts, 0, sizeof(int)*count);
  2541. int w = red->width;
  2542. int h = red->height;
  2543. for (int x = 0; x<w; x++){
  2544. int option_index = (count - 1) - x*count / w;
  2545. for (int y = 0; y<h; y++){
  2546. if (CV_IMAGE_ELEM(red, unsigned char, y, x)){
  2547. red_counts[option_index]++;
  2548. if (y <= bottom - 1 && y >= top + 1){
  2549. red_in_counts[option_index]++;
  2550. }
  2551. }
  2552. }
  2553. }
  2554. cvReleaseImage(&red);
  2555. return identify::result::IDF_SUCCESS;
  2556. }
  2557. //获取图像指定区域红色点数(填空题)
  2558. int COnLineCardPageIdentifier::GetTianKongTiRedCount(IplImage * dst, const CvRect& rect_normal, const CvRect& rect_detect,int count,int multi)
  2559. {
  2560. CvRect rect = cvGetImageROI(dst);
  2561. if (dst->nChannels != 3){ return 0; }
  2562. if (m_nXianXiaDaFenType == 1){
  2563. cvSetImageROI(dst, rect_detect);
  2564. Mat img = cv::cvarrToMat(dst);
  2565. cvResetImageROI(dst);
  2566. int tempi[50] = { 0 };
  2567. try{
  2568. EnterCriticalSection(m_althom_rcs);
  2569. // printf("线程:%s 新算法判分-开始\n", std::to_string(GetCurrentThreadId()).c_str());
  2570. int fff = api_red_check_analysis(img, count, 3, multi, tempi, count);
  2571. if (fff != 0){
  2572. // printf("线程:%s 新算法判分-错误\n", std::to_string(GetCurrentThreadId()).c_str());
  2573. LeaveCriticalSection(m_althom_rcs);
  2574. return 0;
  2575. }
  2576. // printf("线程:%s 新算法判分-结束\n", std::to_string(GetCurrentThreadId()).c_str());
  2577. LeaveCriticalSection(m_althom_rcs);
  2578. }
  2579. catch (cv::Exception&e){
  2580. // printf("线程:%s 新算法判分-异常\n", std::to_string(GetCurrentThreadId()).c_str());
  2581. LeaveCriticalSection(m_althom_rcs);
  2582. #if 0
  2583. static const char* str;
  2584. str = e.what();
  2585. #endif
  2586. }
  2587. if (tempi[0] > 0)
  2588. return 300;
  2589. return 0;
  2590. }
  2591. int w = rect_detect.width;
  2592. int h = rect_detect.height;
  2593. IplImage * red = NULL;
  2594. BOOL r = GetRedBinary(dst, rect_detect, &red);
  2595. if (!r)return 0;
  2596. vector<CvPoint2D32f> redPoints;
  2597. for (int y = 0; y<red->height; y++)
  2598. {
  2599. unsigned char * red_row_first = (unsigned char *)(red->imageData + (y*red->widthStep));
  2600. for (int x = 0; x<red->width; x++)
  2601. {
  2602. if (red_row_first[x]){
  2603. redPoints.push_back(cvPoint2D32f(x, y));
  2604. }
  2605. }
  2606. }
  2607. CvMemStorage * storage = cvCreateMemStorage();
  2608. CvSeq* contour = NULL;
  2609. int contours = cvFindContours(red, storage, &contour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
  2610. int red_count = 0;
  2611. int normal_left = rect_normal.x - rect_detect.x;
  2612. int normal_top = rect_normal.y - rect_detect.y;
  2613. int normal_right = normal_left + rect_normal.width;
  2614. int normal_bottom = normal_top + rect_normal.height;
  2615. for (CvContour * c = (CvContour *)contour; c != NULL; c = (CvContour *)c->h_next){
  2616. float sum_x = 0;
  2617. float sum_y = 0;
  2618. int in_count = 0;
  2619. int area_c = 0;//轮廓在评分框内的点数
  2620. for (std::size_t i = 0; i<redPoints.size(); i++)
  2621. {
  2622. double distance = cvPointPolygonTest(c, redPoints[i], FALSE);
  2623. if (distance >= 0){
  2624. sum_x += redPoints[i].x;
  2625. sum_y += redPoints[i].y;
  2626. in_count++;
  2627. if (normal_left <= redPoints[i].x&&redPoints[i].x <= normal_right&&normal_top <= redPoints[i].y&&redPoints[i].y <= normal_bottom){
  2628. area_c++;
  2629. }
  2630. }
  2631. }
  2632. if (in_count == 0)continue;
  2633. if (area_c>60){
  2634. red_count += in_count;
  2635. continue;
  2636. }
  2637. //重心位置
  2638. float avg_x = sum_x / in_count;
  2639. float avg_y = sum_y / in_count;
  2640. if (normal_left <= avg_x&&avg_x <= normal_right + 30 && normal_top <= avg_y&&avg_y <= normal_bottom){
  2641. red_count += in_count;
  2642. }
  2643. }
  2644. cvReleaseImage(&red);
  2645. cvReleaseMemStorage(&storage);
  2646. return red_count;
  2647. }
  2648. int COnLineCardPageIdentifier::SaveLocateInfo(SchemaPage& schemaPage, vector<CvPoint2D32f> &relationKey, vector<CvPoint2D32f> &relationValue)
  2649. {
  2650. if (relationKey.size() >= 2)
  2651. {
  2652. m_relationKey = relationKey;
  2653. m_relationValue = relationValue;
  2654. double distance = 0;
  2655. int index1;
  2656. int index2;
  2657. for (std::size_t i = 0; i<m_relationKey.size(); i++)
  2658. {
  2659. for (std::size_t j = i + 1; j<m_relationKey.size(); j++)
  2660. {
  2661. double d = sqrt((m_relationKey[i].x - m_relationKey[j].x)*(m_relationKey[i].x - m_relationKey[j].x) + (m_relationKey[j].y - m_relationKey[i].y)*(m_relationKey[j].y - m_relationKey[i].y));
  2662. if (d>distance){ distance = d; index1 = i; index2 = j; }
  2663. }
  2664. }
  2665. //模板上的定位点
  2666. CvPoint2D32f *p11 = &m_relationKey[index1];
  2667. CvPoint2D32f *p12 = &m_relationKey[index2];
  2668. //试卷上的定位点
  2669. CvPoint2D32f*p21 = &m_relationValue[index1];
  2670. CvPoint2D32f*p22 = &m_relationValue[index2];
  2671. CvMat* m_m = caculate_scm(*p21, *p22, *p11, *p12);
  2672. memcpy(data, m_m->data.fl, sizeof(float) * 6);
  2673. cvReleaseMat(&m_m);
  2674. return identify::result::IDF_SUCCESS;
  2675. }
  2676. return identify::result::IDF_NOTFOUND_ENOUGH_LOCATEPOINT;
  2677. }
  2678. int COnLineCardPageIdentifier::GetRect(const int centerx, const int centery, const int width, const int height, RESULT_RECT& result_rect)
  2679. {
  2680. result_rect.x = centerx - width / 2;
  2681. result_rect.y = centery - height / 2;
  2682. result_rect.width = width;
  2683. result_rect.height = height;
  2684. return identify::result::IDF_FAILURE;
  2685. }
  2686. int COnLineCardPageIdentifier::GetRect2(const int centerx, const int centery, const int width, const int height, RESULT_RECT& result_rect)
  2687. {
  2688. double x = data[0] * centerx + data[1] * centery + data[2];
  2689. double y = data[3] * centerx + data[4] * centery + data[5];
  2690. result_rect.x = (int)(x - width / 2 + 0.5);
  2691. result_rect.y = (int)(y - height / 2 + 0.5);
  2692. result_rect.width = width;
  2693. result_rect.height = height;
  2694. return identify::result::IDF_SUCCESS;
  2695. }
  2696. template<class T>
  2697. void COnLineCardPageIdentifier::SaveRect(CvRect& rect, T& result)
  2698. {
  2699. result.centerx = static_cast<decltype(result.centerx)>(rect.x + rect.width / 2.0);
  2700. result.centery = static_cast<decltype(result.centery)>(rect.y + rect.height / 2.0);
  2701. result.width = static_cast<decltype(result.width)>(rect.width);
  2702. result.height = static_cast<decltype(result.height)>(rect.height);
  2703. result.angle = 0;
  2704. }
  2705. template<class T >
  2706. void COnLineCardPageIdentifier::DrawObjRect(IplImage * dst, T& obj_result, CvScalar& scalar)
  2707. {
  2708. cvDrawRect(dst,
  2709. cvPoint(obj_result.centerx - obj_result.width / 2, obj_result.centery - obj_result.height / 2),
  2710. cvPoint(obj_result.centerx + obj_result.width / 2, obj_result.centery + obj_result.height / 2),
  2711. scalar);
  2712. }
  2713. //查找题目定位点
  2714. BOOL COnLineCardPageIdentifier::FindQuestionLocatePoint(IplImage * dst, SchemaLocatePoint& lp1, CvPoint2D32f& point1)
  2715. {
  2716. int top = max(0, (int)(lp1.centery - lp1.height*1.5 + 0.5));
  2717. int bottom = min(dst->height - 1, (int)(lp1.centery + lp1.height*1.5 + 0.5));
  2718. int left = max(0, (int)(lp1.centerx - lp1.width*1.5 + 0.5));
  2719. int right = min(dst->width - 1, (int)(lp1.centerx + lp1.width*1.5 + 0.5));
  2720. int x = left;
  2721. int y = top;
  2722. int width = right - left + 1;
  2723. int height = bottom - top + 1;
  2724. if (width<1 || height<1)return FALSE;
  2725. IplImage* dst_gray_img = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
  2726. IplImage* dst_binary_img = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
  2727. CvMemStorage* storage = cvCreateMemStorage();
  2728. cvSetImageROI(dst, cvRect(x, y, width, height));
  2729. if (dst->nChannels == 3)cvCvtColor(dst, dst_gray_img, CV_BGR2GRAY);
  2730. else cvCopy(dst, dst_gray_img);
  2731. cvThreshold(dst_gray_img, dst_binary_img, 180, 255, CV_THRESH_BINARY);
  2732. int an = 2;
  2733. IplConvKernel * element = cvCreateStructuringElementEx(an * 2 + 1, an * 2 + 1, an, an, CV_SHAPE_RECT, 0);//创建结构元素
  2734. cvDilate(dst_binary_img, dst_binary_img, element, 1);//膨胀图像
  2735. cvErode(dst_binary_img, dst_binary_img, element, 1);//腐蚀图像
  2736. cvReleaseStructuringElement(&element);
  2737. CvSeq * pcontours = NULL;
  2738. int ncontours = cvFindContours(dst_binary_img, storage, &pcontours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
  2739. vector<CvContour*> quesiontLocatePoints;
  2740. for (CvContour * c = (CvContour *)pcontours; c != NULL; c = (CvContour *)c->h_next)
  2741. {
  2742. if (c->rect.width<lp1.width - 3)continue;
  2743. if (c->rect.height<lp1.height - 3)continue;
  2744. if (c->rect.width>lp1.width + 3)continue;
  2745. if (c->rect.height>lp1.height + 3)continue;
  2746. cvSetImageROI(dst_gray_img, c->rect);
  2747. int area = GetBlackArea(dst_gray_img);
  2748. if (area<c->rect.width*c->rect.height*0.75)continue;
  2749. quesiontLocatePoints.push_back(c);
  2750. }
  2751. double distance = 99999;
  2752. int min_index = -1;
  2753. for (std::size_t i = 0; i<quesiontLocatePoints.size(); i++)
  2754. {
  2755. double d = GetDistance(cvPoint2D32f(lp1.centerx - x, lp1.centery - y), cvPoint2D32f(quesiontLocatePoints[i]->rect.x + quesiontLocatePoints[i]->rect.width / 2.0, quesiontLocatePoints[i]->rect.y + quesiontLocatePoints[i]->rect.height / 2.0));
  2756. if (d<distance){ distance = d; min_index = i; }
  2757. }
  2758. if (min_index >= 0){
  2759. point1 = cvPoint2D32f(quesiontLocatePoints[min_index]->rect.x + quesiontLocatePoints[min_index]->rect.width / 2.0 + x, quesiontLocatePoints[min_index]->rect.y + quesiontLocatePoints[min_index]->rect.height / 2.0 + y);
  2760. cvReleaseImage(&dst_gray_img);
  2761. cvReleaseImage(&dst_binary_img);
  2762. cvReleaseMemStorage(&storage);
  2763. return TRUE;
  2764. }
  2765. cvReleaseImage(&dst_gray_img);
  2766. cvReleaseImage(&dst_binary_img);
  2767. cvReleaseMemStorage(&storage);
  2768. return FALSE;
  2769. }
  2770. double COnLineCardPageIdentifier::GetDistance(CvPoint2D32f point1, CvPoint2D32f point2)
  2771. {
  2772. double dx = point1.x - point2.x;
  2773. double dy = point1.y - point2.y;
  2774. return sqrt(dx*dx + dy*dy);
  2775. }
  2776. int COnLineCardPageIdentifier::FindQuestionLocatePoints(SchemaPage& schemaPage, IplImage * dst)
  2777. {
  2778. std::vector<SchemaLocatePoint>& questionLocatePoints = schemaPage.questionLocatePoints;
  2779. m_question_locate_points.resize(questionLocatePoints.size());
  2780. m_question_locate_point_found.resize(questionLocatePoints.size());
  2781. for (std::size_t i = 0; i<questionLocatePoints.size(); i++)
  2782. {
  2783. m_question_locate_point_found[i] = FindQuestionLocatePoint(dst, questionLocatePoints[i], m_question_locate_points[i]);
  2784. }
  2785. return identify::result::IDF_SUCCESS;
  2786. }
  2787. BOOL COnLineCardPageIdentifier::GetRedBinary(IplImage * dst, const CvRect& rect_detect, IplImage ** red_binary)
  2788. {
  2789. #define GETREDBINARY_DEBUG 0
  2790. #define rename_image(img,new_name) IplImage * new_name = img; img = NULL;
  2791. if (dst == NULL || dst->nChannels != 3)return FALSE;
  2792. int w = rect_detect.width;
  2793. int h = rect_detect.height;
  2794. IplImage * hsv = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 3);
  2795. //存储灰度图像和二值化图像
  2796. IplImage * gray = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 1);
  2797. IplImage * black = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 1);
  2798. IplImage * black_dilate = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 1);
  2799. //记录较浅一些的红色
  2800. IplImage * red_low = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 1);
  2801. //记录较深一些的红色
  2802. IplImage * red_high = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 1);
  2803. IplImage * red = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 1);
  2804. cvSetImageROI(dst, rect_detect);
  2805. #if GETREDBINARY_DEBUG
  2806. cvSaveImage("D:\\dst.png", dst);
  2807. #endif
  2808. cvCvtColor(dst, hsv, CV_BGR2HSV);
  2809. cvCvtColor(dst, gray, CV_BGR2GRAY);
  2810. cvAdaptiveThreshold(gray, gray, 255, CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY_INV, 9, 10.0);
  2811. rename_image(gray, binary);
  2812. #define HSV_VALUE_H hsv_row_first[hsv_offset]
  2813. #define HSV_VALUE_S hsv_row_first[hsv_offset+1]
  2814. #define HSV_VALUE_V hsv_row_first[hsv_offset+2]
  2815. const int hsv_v_valve = 255 * 50 / 100;
  2816. const int hsv_h_max_valve = 180 * 20 / 360;
  2817. const int hsv_h_min_valve = 180 * 315 / 360;
  2818. const int hsv_s_valve_1 = 255 * 23 / 100;
  2819. const int hsv_s_valve_2 = 255 * 33 / 100;
  2820. const int hsv_valve_high = 255 * (23 + 70) / 100;
  2821. const int hsv_valve_low = 255 * (13 + 60) / 100;
  2822. for (int y = 0; y<h; y++)
  2823. {
  2824. unsigned char * hsv_row_first = (unsigned char *)(hsv->imageData + y*hsv->widthStep);
  2825. unsigned char * red1_row_first = (unsigned char *)(red_low->imageData + y*red_low->widthStep);
  2826. unsigned char * red2_row_first = (unsigned char *)(red_high->imageData + y*red_high->widthStep);
  2827. unsigned char * binary_row_first = (unsigned char *)(binary->imageData + y*binary->widthStep);
  2828. unsigned char * black_row_first = (unsigned char *)(black->imageData + y*black->widthStep);
  2829. for (int x = 0, hsv_offset = 0; x<w; x++, hsv_offset += 3)
  2830. {
  2831. if (HSV_VALUE_V >= hsv_v_valve && (HSV_VALUE_H >= hsv_h_min_valve || HSV_VALUE_H <= hsv_h_max_valve)){
  2832. red1_row_first[x] = (HSV_VALUE_S >= hsv_s_valve_1 && (HSV_VALUE_S + HSV_VALUE_V) >= hsv_valve_low) ? 255 : 0;
  2833. red2_row_first[x] = (HSV_VALUE_S >= hsv_s_valve_2 && (HSV_VALUE_S + HSV_VALUE_V) >= hsv_valve_high) ? HSV_VALUE_S : 0;
  2834. }
  2835. else{
  2836. red1_row_first[x] = red2_row_first[x] = 0;
  2837. }
  2838. black_row_first[x] = (binary_row_first[x] && !red1_row_first[x]) ? 255 : 0;
  2839. }
  2840. }
  2841. #if GETREDBINARY_DEBUG
  2842. cvSaveImage("D:\\hsv.png", hsv);
  2843. cvSaveImage("D:\\red_low.png", red_low);
  2844. cvSaveImage("D:\\red_high.png", red_high);
  2845. cvSaveImage("D:\\binary.png", binary);
  2846. cvSaveImage("D:\\black.png", black);
  2847. #endif
  2848. cvThreshold(red_high, red_high, 0, 255, CV_THRESH_OTSU);
  2849. int an = 1;
  2850. IplConvKernel * element = cvCreateStructuringElementEx(an * 2 + 1, an * 2 + 1, an, an, CV_SHAPE_RECT, 0);//创建结构元素
  2851. cvDilate(binary, binary, element, 2);//膨胀图像
  2852. cvDilate(red_high, red_high, element, 2);//膨胀图像
  2853. cvDilate(black, black_dilate, element, 2);//膨胀图像
  2854. for (int y = 0; y<h; y++)
  2855. {
  2856. unsigned char * binary_row_first = (unsigned char *)(binary->imageData + y*binary->widthStep);
  2857. unsigned char * red1_row_first = (unsigned char *)(red_low->imageData + y*red_low->widthStep);
  2858. unsigned char * red2_row_first = (unsigned char *)(red_high->imageData + y*red_high->widthStep);
  2859. unsigned char * red_row_first = (unsigned char *)(red->imageData + y*red->widthStep);
  2860. unsigned char * black_dilate_row_first = (unsigned char *)(black_dilate->imageData + y*black_dilate->widthStep);
  2861. for (int x = 0; x<w; x++)
  2862. {
  2863. red_row_first[x] = (red1_row_first[x] && !black_dilate_row_first[x] && ((!binary_row_first[x]) || red2_row_first[x])) ? 255 : 0;
  2864. }
  2865. }
  2866. rename_image(red_high, red_dilate);
  2867. cvDilate(red, red_dilate, element, 2);//膨胀图像
  2868. for (int y = 0; y<h; y++)
  2869. {
  2870. unsigned char * red1_row_first = (unsigned char *)(red_low->imageData + y*red_low->widthStep);
  2871. unsigned char * red_row_first = (unsigned char *)(red->imageData + y*red->widthStep);
  2872. unsigned char * black_row_first = (unsigned char *)(black->imageData + y*black->widthStep);
  2873. unsigned char * red_dilate_row_first = (unsigned char *)(red_dilate->imageData + y*red_dilate->widthStep);
  2874. for (int x = 0; x<w; x++)
  2875. {
  2876. red_row_first[x] = (red_row_first[x] || (red_dilate_row_first[x] && (red1_row_first[x] && !black_row_first[x]))) ? 255 : 0;
  2877. }
  2878. }
  2879. cvDilate(red, red_dilate, element, 1);//膨胀图像
  2880. for (int y = 0; y<h; y++)
  2881. {
  2882. unsigned char * red_row_first = (unsigned char *)(red->imageData + y*red->widthStep);
  2883. unsigned char * black_row_first = (unsigned char *)(black->imageData + y*black->widthStep);
  2884. unsigned char * red_dilate_row_first = (unsigned char *)(red_dilate->imageData + y*red_dilate->widthStep);
  2885. for (int x = 0; x<w; x++)
  2886. {
  2887. if (!red_row_first[x] && black_row_first[x] && red_dilate_row_first[x]){
  2888. red_row_first[x] = 255;
  2889. }
  2890. }
  2891. }
  2892. cvReleaseStructuringElement(&element);
  2893. *red_binary = red;
  2894. cvReleaseImage(&hsv);
  2895. cvReleaseImage(&binary);
  2896. cvReleaseImage(&black);
  2897. cvReleaseImage(&black_dilate);
  2898. cvReleaseImage(&red_low);
  2899. cvReleaseImage(&red_dilate);
  2900. return TRUE;
  2901. }
  2902. int COnLineCardPageIdentifier::mycaculate_global_background(SchemaItem items[], int itemCount, IplImage * dst)
  2903. {
  2904. int global_background_color_count = 0;
  2905. int global_background_color_avg = 0;
  2906. for (int i = 0; i<itemCount; i++)
  2907. {
  2908. //SchemaItem & item = schemaPage.items[i];
  2909. SchemaItem & item = items[i];
  2910. RESULT_RECT result_rect;
  2911. GetRect(item.centerx, item.centery, item.width, item.height, result_rect);
  2912. int left = int(result_rect.x);
  2913. int top = int(result_rect.y);
  2914. int right = int(result_rect.x + result_rect.width);
  2915. int bottom = int(result_rect.y + result_rect.height);
  2916. int vtop = (int)(result_rect.y - (result_rect.width + 2) / 4);
  2917. int vbottom = (int)(result_rect.y + result_rect.height + (result_rect.height + 2) / 4);
  2918. int background_color_count = 0, background_color_avg = 0;
  2919. int gray;
  2920. int(*GetGray)(const IplImage * dst, int x, int y);
  2921. if (dst->nChannels == 1)GetGray = GetGrayValue;
  2922. else GetGray = GetBGRGray;
  2923. for (int x = left; x <= right; x++)
  2924. {
  2925. gray = GetGray(dst, x, vtop);
  2926. if (gray>160){ background_color_avg += gray; background_color_count++; }
  2927. gray = GetGray(dst, x, vbottom);
  2928. if (gray>160){ background_color_avg += gray; background_color_count++; }
  2929. }
  2930. background_color_avg = background_color_count>0 ? background_color_avg / background_color_count : 0;
  2931. if (background_color_avg>100){
  2932. global_background_color_avg += background_color_avg;
  2933. global_background_color_count++;
  2934. }
  2935. }
  2936. global_background_color_avg = global_background_color_count>0 ? global_background_color_avg / global_background_color_count : 0;
  2937. return global_background_color_avg;
  2938. }
  2939. //选做题分析全局属性
  2940. int COnLineCardPageIdentifier::myanalyseOmrPanoramic(OnlineAnalyseResult& anlyseResultTemp, int count)
  2941. {
  2942. int i;
  2943. int lev;
  2944. //int count; // 所有涂点个数
  2945. int numAblePnt; // 参与分析的点的个数
  2946. double maxblk[16];
  2947. int numBlk[16];
  2948. double maxBlur[16];
  2949. int numBlur[16];
  2950. double stdBlur;
  2951. memset(numBlk, 0, sizeof(int) * 16);
  2952. memset(maxblk, 0, sizeof(double) * 16);
  2953. memset(maxBlur, 0, sizeof(double) * 16);
  2954. memset(numBlur, 0, sizeof(int) * 16);
  2955. //count = schemaPage.itemCount;
  2956. numAblePnt = 0;
  2957. double& m_avaMaxBlk = anlyseResultTemp.avgMax_heidu;
  2958. double& m_benchmarkBlk = anlyseResultTemp.benchmarkBlk;
  2959. double& m_benchmarkArea = anlyseResultTemp.benchmarkAreaBlk;
  2960. m_avaMaxBlk = 0;
  2961. for (i = 0; i<count; i++)
  2962. {
  2963. OnlineAnalyseResult::itemResult& itemr = anlyseResultTemp.itemAnalyseResult[i];
  2964. if (itemr.heidu < m_benchmarkBlk || itemr.xiangdui_value < m_benchmarkArea)
  2965. continue;
  2966. if (itemr.heidu< 4.0) continue;
  2967. m_avaMaxBlk += itemr.heidu;
  2968. numAblePnt++;
  2969. lev = int(itemr.heidu);
  2970. maxblk[lev] += itemr.heidu;
  2971. numBlk[lev] ++;
  2972. lev = int(itemr.mohudu);
  2973. maxBlur[lev] += itemr.mohudu;
  2974. numBlur[lev] ++;
  2975. //pPnt++;
  2976. }
  2977. if (numAblePnt > 0)
  2978. m_avaMaxBlk /= numAblePnt;
  2979. else
  2980. m_avaMaxBlk = 12;
  2981. int stdId = 2;
  2982. int numStd = numBlk[0] + numBlk[1] + numBlk[2];
  2983. int numCur = numStd;
  2984. for (i = 3; i<16; i++)
  2985. {
  2986. numCur += (numBlk[i] - numBlk[i - 3]);
  2987. if (numCur > numStd)
  2988. {
  2989. numStd = numCur;
  2990. stdId = i;
  2991. }
  2992. }
  2993. double stdBlk = maxblk[stdId - 2] + maxblk[stdId - 1] + maxblk[stdId];
  2994. if (numStd > 0)
  2995. stdBlk /= numStd;
  2996. else
  2997. stdBlk = 12;
  2998. // 计算模糊度代表值
  2999. stdId = 2;
  3000. numStd = numBlur[0] + numBlur[1] + numBlur[2];
  3001. numCur = numStd;
  3002. for (i = 3; i<16; i++)
  3003. {
  3004. numCur += (numBlur[i] - numBlur[i - 3]);
  3005. if (numCur > numStd)
  3006. {
  3007. numStd = numCur;
  3008. stdId = i;
  3009. }
  3010. }
  3011. stdBlur = maxBlur[stdId - 2] + maxBlur[stdId - 1] + maxBlur[stdId];
  3012. if (numStd > 0)
  3013. stdBlur /= numStd;
  3014. else
  3015. stdBlur = 0;
  3016. if (stdBlur < 2.0)
  3017. stdBlk -= stdBlur;
  3018. else
  3019. stdBlk -= 2.0;
  3020. if (stdBlk < m_avaMaxBlk)
  3021. m_avaMaxBlk = stdBlk;
  3022. //统计字符区域的黑色点数的平均值
  3023. int max_item = count;
  3024. int * char_area_black_counts = new int[1];
  3025. int cur_item_count;//当前选项的数量
  3026. for (int ii = 0; ii<max_item; ii++)
  3027. {
  3028. cur_item_count = 0;
  3029. long total_char_area_size = 0;
  3030. for (int gi = 0; gi<1; gi++)
  3031. {
  3032. if (max_item > ii){
  3033. OnlineAnalyseResult::itemResult & ir = anlyseResultTemp.itemAnalyseResult[ii];
  3034. char_area_black_counts[cur_item_count] = ir.char_area_black_count;
  3035. total_char_area_size += ir.char_area_size;
  3036. cur_item_count++;
  3037. }
  3038. }
  3039. double char_area_size_avg = total_char_area_size / (double)cur_item_count;
  3040. int temp;
  3041. for (int m = 0; m<cur_item_count; m++)
  3042. {
  3043. for (int n = m + 1; n<cur_item_count; n++)
  3044. {
  3045. if (char_area_black_counts[n]<char_area_black_counts[m]){
  3046. temp = char_area_black_counts[n];
  3047. char_area_black_counts[n] = char_area_black_counts[m];
  3048. char_area_black_counts[m] = temp;
  3049. }
  3050. }
  3051. }
  3052. anlyseResultTemp.char_area_black_avg[ii] = -1;
  3053. if (char_area_black_counts[0] * 1.4<char_area_black_counts[cur_item_count - 1] || char_area_black_counts[cur_item_count - 1] * 1.4<char_area_size_avg){
  3054. double t = char_area_black_counts[0] * 1.15;
  3055. double f = 0;
  3056. int count__ = 0;
  3057. for (int l = 0; l<cur_item_count; l++)
  3058. {
  3059. if (t<char_area_black_counts[l]){
  3060. f += char_area_black_counts[l];
  3061. count__++;
  3062. }
  3063. }
  3064. anlyseResultTemp.char_area_black_avg[ii] = f / count__;
  3065. }
  3066. }
  3067. delete[]char_area_black_counts;
  3068. return(0);
  3069. }
  3070. /************************************************************************/
  3071. /* 获取选做题选择项 */
  3072. /************************************************************************/
  3073. int COnLineCardPageIdentifier::myGenerateOmrStr(OnlineAnalyseResult& anlyseResultTemp, int count)
  3074. {
  3075. int omr_str_len = 0;
  3076. //标记是否被选中
  3077. SelectType sel[MAX_ITEM] = { SelectType::UNSELECT };
  3078. int pn = min(MAX_ITEM, count);
  3079. OnlineAnalyseResult::itemResult* results[MAX_ITEM];
  3080. for (int i = 0; i<pn; i++){
  3081. results[i] = &anlyseResultTemp.itemAnalyseResult[i];
  3082. }
  3083. int maxgraynum = -1;
  3084. int mingraynum = 0xffffff;
  3085. for (int i = 0; i<pn; i++)
  3086. {
  3087. if (results[i]->gray_num>maxgraynum)maxgraynum = results[i]->gray_num;
  3088. if (results[i]->gray_num<mingraynum)mingraynum = results[i]->gray_num;
  3089. }
  3090. int selected_num = 0;
  3091. int unselect_num = 0;
  3092. int uncertain_num = 0;
  3093. for (int i = 0; i<pn; i++)
  3094. {
  3095. //被涂面积小于10%
  3096. if (results[i]->gray_num<results[i]->cell_size*0.1){
  3097. sel[i] = UNSELECT;
  3098. unselect_num++;
  3099. continue;
  3100. }
  3101. //面积超过65%
  3102. if (results[i]->gray_num>results[i]->cell_size*0.65){
  3103. sel[i] = SELECTED;
  3104. selected_num++;
  3105. continue;
  3106. }
  3107. //填涂面积相差达%15,将面积不小于最大点填涂面积的总面积%10的涂点视为填涂
  3108. if (maxgraynum - mingraynum>results[i]->cell_size*0.15&&results[i]->gray_num>maxgraynum - results[i]->cell_size*0.1){
  3109. sel[i] = SELECTED;
  3110. selected_num++;
  3111. continue;
  3112. }
  3113. //填涂面积相差达%50,将面积不小于最大点填涂面积的总面积%30的涂点视为填涂
  3114. if (maxgraynum - mingraynum>results[i]->cell_size*0.5&&results[i]->gray_num>maxgraynum - results[i]->cell_size*0.3){
  3115. sel[i] = SELECTED;
  3116. selected_num++;
  3117. continue;
  3118. }
  3119. if (maxgraynum - results[i]->gray_num>results[i]->cell_size*0.2){
  3120. sel[i] = UNSELECT;
  3121. unselect_num++;
  3122. continue;
  3123. }
  3124. sel[i] = UNCERTAIN;
  3125. uncertain_num++;
  3126. }
  3127. identify::result::OMR_QUESTION_STATE question_state = identify::result::OMR_QUESTION_STATE::OMR_QUESTION_STATE_NORMAL;
  3128. if (selected_num>1){
  3129. int maxgrayindex = -1;
  3130. int maxgraynum = -101;
  3131. for (int i = 0; i<pn; i++){
  3132. if (sel[i] == SELECTED){
  3133. if ((results[i]->gray_num - maxgraynum > 100) || (abs(results[i]->gray_num - maxgraynum) <= 100 && results[i]->heidu > results[maxgrayindex]->heidu))
  3134. {
  3135. maxgraynum = results[i]->gray_num;
  3136. maxgrayindex = i;
  3137. }
  3138. }
  3139. }
  3140. for (int i = 0; i<pn; i++){
  3141. if (sel[i] == SELECTED&&i != maxgrayindex){
  3142. sel[i] = UNSELECT;
  3143. }
  3144. }
  3145. }
  3146. for (int i = 0; i<pn; i++){
  3147. if (sel[i] == SELECTED){
  3148. return i;
  3149. }
  3150. }
  3151. return -1;
  3152. }
  3153. //读取选做题选项数据
  3154. int COnLineCardPageIdentifier::ReadXuanZuoTi(SchemaPage& schemaPage, SchemaQuestionScore& qs, IplImage * dst)
  3155. {
  3156. int global_background_color_avg = mycaculate_global_background(qs.item, qs.countNum, dst);
  3157. //模糊度,面积,高黑度,平均黑度
  3158. double mo_hu_du, refArea, gao_heidu, avg_gray;
  3159. //黑色像素数,涂点大小
  3160. int grayNum, cellSize;
  3161. //高黑度和
  3162. double topGraySum = 0;
  3163. //高黑度面积
  3164. double topGrayAreaSum = 0;
  3165. OnlineAnalyseResult anlyseResultTemp;
  3166. anlyseResultTemp.backGroundGray = global_background_color_avg;
  3167. anlyseResultTemp.cellAvgGray = 0;
  3168. anlyseResultTemp.cellNum = 0;
  3169. int numTopGray = 0;
  3170. int ret;
  3171. for (int i = 0; i<qs.countNum; i++)
  3172. {
  3173. SchemaItem & item = qs.item[i];
  3174. ret = caculate_cell_property(schemaPage, item, dst, cellSize, refArea, avg_gray, grayNum, mo_hu_du, gao_heidu, anlyseResultTemp.itemAnalyseResult[i].char_area_size, anlyseResultTemp.itemAnalyseResult[i].char_area_black_count, anlyseResultTemp.itemAnalyseResult[i].char_area_avg_gray);
  3175. if (ret != identify::result::IDF_SUCCESS)return ret;
  3176. topGraySum += gao_heidu;
  3177. topGrayAreaSum += refArea;
  3178. numTopGray++;
  3179. anlyseResultTemp.cellAvgGray += static_cast<int>(avg_gray);
  3180. anlyseResultTemp.cellNum++;
  3181. anlyseResultTemp.itemAnalyseResult[i].avg_gray = static_cast<int>(avg_gray);
  3182. anlyseResultTemp.itemAnalyseResult[i].cell_size = cellSize;
  3183. anlyseResultTemp.itemAnalyseResult[i].gray_num = grayNum;
  3184. anlyseResultTemp.itemAnalyseResult[i].heidu = gao_heidu;
  3185. anlyseResultTemp.itemAnalyseResult[i].mohudu = mo_hu_du;
  3186. anlyseResultTemp.itemAnalyseResult[i].xiangdui_mianji = refArea;
  3187. anlyseResultTemp.itemAnalyseResult[i].xiangdui_value = refArea;
  3188. }
  3189. anlyseResultTemp.cellAvgGray = anlyseResultTemp.cellNum>0 ? anlyseResultTemp.cellAvgGray / anlyseResultTemp.cellNum : 256;
  3190. anlyseResultTemp.avg_heidu = topGrayAreaSum>0 ? topGraySum / topGrayAreaSum : 6;
  3191. if (numTopGray >= int(schemaPage.itemCount / 5)){
  3192. anlyseResultTemp.float_heidu = anlyseResultTemp.avg_heidu / schemaPage.user_float_heidu;
  3193. anlyseResultTemp.benchmarkBlk = schemaPage.worthwhileBlk + anlyseResultTemp.float_heidu;
  3194. }
  3195. else{
  3196. anlyseResultTemp.benchmarkBlk = numTopGray>0 ? min(6.0, schemaPage.worthwhileBlk + anlyseResultTemp.avg_heidu / schemaPage.user_float_heidu) : 6;
  3197. anlyseResultTemp.float_heidu = anlyseResultTemp.benchmarkBlk - schemaPage.worthwhileBlk;
  3198. }
  3199. if (numTopGray>0){
  3200. anlyseResultTemp.avg_heidu = topGrayAreaSum / numTopGray;
  3201. anlyseResultTemp.float_heidu = anlyseResultTemp.avg_heidu / schemaPage.user_float_heidu;
  3202. anlyseResultTemp.benchmarkAreaBlk = anlyseResultTemp.float_heidu;
  3203. }
  3204. else{
  3205. anlyseResultTemp.avg_heidu = 0;
  3206. anlyseResultTemp.float_heidu = 0;
  3207. anlyseResultTemp.benchmarkAreaBlk = 0.1;
  3208. }
  3209. int r = myanalyseOmrPanoramic(anlyseResultTemp, qs.countNum);
  3210. r = myGenerateOmrStr(anlyseResultTemp, qs.countNum);
  3211. return r;
  3212. }
  3213. int COnLineCardPageIdentifier::GenerateOmrStr(SchemaPage& schemaPage, string &ret)
  3214. {
  3215. char omr_str[512] = { 0 };
  3216. //标记是否被选中
  3217. SelectType sel[200] = { SelectType::UNSELECT };
  3218. for (int groupIndex = 0; groupIndex<schemaPage.stuGroupsCount; groupIndex++)
  3219. {
  3220. int omr_str_len = 0;
  3221. memset(sel, UNSELECT, sizeof(sel));
  3222. SchemaGroup& group = schemaPage.stuGroups[groupIndex];
  3223. int pn = min(200, group.itemCount);
  3224. OnlineAnalyseResult::itemResult* results[200];
  3225. SchemaItem* items[200];
  3226. int maxgraynum = -1;
  3227. int mingraynum = 0xffffff;
  3228. for (int i = 0; i<pn; i++){
  3229. results[i] = &anlyseResult.itemAnalyseResult[group.itemIndex[i]];
  3230. items[i] = &m_items[group.itemIndex[i]];
  3231. if (results[i]->gray_num > maxgraynum)maxgraynum = results[i]->gray_num;
  3232. if (results[i]->gray_num < mingraynum)mingraynum = results[i]->gray_num;
  3233. }
  3234. int selected_num = 0;
  3235. int unselect_num = 0;
  3236. int uncertain_num = 0;
  3237. for (int i = 0; i<pn; i++)
  3238. {
  3239. //面积超过65%
  3240. if (results[i]->gray_num>results[i]->cell_size*0.65){
  3241. sel[i] = SELECTED;
  3242. selected_num++;
  3243. continue;
  3244. }
  3245. //填涂面积相差达%20,将面积不小于最大点填涂面积的总面积%10的涂点视为填涂
  3246. if (maxgraynum - mingraynum>results[i]->cell_size*0.2&&results[i]->gray_num>maxgraynum - results[i]->cell_size*0.1){
  3247. sel[i] = SELECTED;
  3248. selected_num++;
  3249. continue;
  3250. }
  3251. //填涂面积相差达%50,将面积不小于最大点填涂面积的总面积%30的涂点视为填涂
  3252. if (maxgraynum - mingraynum>results[i]->cell_size*0.5&&results[i]->gray_num>maxgraynum - results[i]->cell_size*0.3){
  3253. sel[i] = SELECTED;
  3254. selected_num++;
  3255. continue;
  3256. }
  3257. sel[i] = UNCERTAIN;
  3258. uncertain_num++;
  3259. }
  3260. if (group.omr_out_type == OMR_OUT_TYPE::OMR_OUT_TYPE_SINGLE_NONSTRICT){
  3261. if (selected_num > 1){
  3262. int maxgrayindex = -1;
  3263. int maxgraynum = -101;
  3264. int countSel = 0;
  3265. for (int i = 0; i < pn; i++)
  3266. {
  3267. if (sel[i] == SELECTED)
  3268. {
  3269. countSel++;
  3270. if ((results[i]->gray_num - maxgraynum>100) || (abs(results[i]->gray_num - maxgraynum) <= 100 && results[i]->heidu > results[maxgrayindex]->heidu))
  3271. {
  3272. maxgraynum = results[i]->gray_num;
  3273. maxgrayindex = i;
  3274. }
  3275. }
  3276. }
  3277. for (int i = 0; i < pn; i++)
  3278. {
  3279. if (sel[i] == SELECTED&&i != maxgrayindex){
  3280. sel[i] = UNSELECT;
  3281. }
  3282. }
  3283. }
  3284. int out_count = 0;
  3285. for (int i = 0; i < pn; i++)
  3286. {
  3287. if (sel[i] == SELECTED){
  3288. if (out_count > 0)omr_str[omr_str_len++] = schemaPage.option_spacer;
  3289. omr_str[omr_str_len++] = items[i]->OutChar;
  3290. out_count++;
  3291. }
  3292. }
  3293. if (omr_str_len != 1){
  3294. return 1;
  3295. }
  3296. omr_str[omr_str_len] = '\0';
  3297. ret += omr_str;
  3298. }
  3299. }
  3300. return 0;
  3301. }
  3302. //读取缺考标记
  3303. bool COnLineCardPageIdentifier::ReadQuekaoFlag(SchemaPage &schemaPage, IplImage * dst)
  3304. {
  3305. if (schemaPage.quekaoLocatePointIndex.size() < 1)
  3306. return false;
  3307. SchemaItem item;
  3308. item.centerx = schemaPage.quekao.centerx;
  3309. item.centery = schemaPage.quekao.centery;
  3310. item.width = schemaPage.quekao.width;
  3311. item.height = schemaPage.quekao.height;
  3312. int cellSize, grayNum, char_area_size, char_area_black_count;
  3313. double refArea, avg_gray, mo_hu_du, gao_heidu, char_area_avg_gray;
  3314. if (schemaPage.itemCount == 0 && schemaPage.stuQuestionLocatePointIndex.size() < 1)
  3315. {
  3316. anlyseResult.backGroundGray = 225;
  3317. }
  3318. int ret = caculate_cell_property(schemaPage, item, dst, cellSize, refArea, avg_gray, grayNum, mo_hu_du, gao_heidu, char_area_size, char_area_black_count, char_area_avg_gray);
  3319. OnlineAnalyseResult anlyseResult;
  3320. anlyseResult.cellAvgGray += static_cast<int>(avg_gray);
  3321. anlyseResult.cellNum++;
  3322. anlyseResult.itemAnalyseResult[0].avg_gray = static_cast<int>(avg_gray);
  3323. anlyseResult.itemAnalyseResult[0].cell_size = cellSize;
  3324. anlyseResult.itemAnalyseResult[0].gray_num = grayNum;
  3325. anlyseResult.itemAnalyseResult[0].heidu = gao_heidu;
  3326. anlyseResult.itemAnalyseResult[0].mohudu = mo_hu_du;
  3327. anlyseResult.itemAnalyseResult[0].xiangdui_mianji = refArea;
  3328. anlyseResult.itemAnalyseResult[0].xiangdui_value = refArea;
  3329. //标记是否被选中
  3330. SelectType sel = UNSELECT;
  3331. OnlineAnalyseResult::itemResult* results;
  3332. results = &anlyseResult.itemAnalyseResult[0];
  3333. int maxgraynum = -1;
  3334. int mingraynum = 0xffffff;
  3335. if (results->gray_num>maxgraynum)maxgraynum = results->gray_num;
  3336. if (results->gray_num<mingraynum)mingraynum = results->gray_num;
  3337. //面积超过65%
  3338. if (results->gray_num>results->cell_size*0.65){
  3339. sel = SELECTED;
  3340. return true;
  3341. }
  3342. //填涂面积相差达%20,将面积不小于最大点填涂面积的总面积%10的涂点视为填涂
  3343. if (maxgraynum - mingraynum>results->cell_size*0.2&&results->gray_num>maxgraynum - results->cell_size*0.1){
  3344. sel = SELECTED;
  3345. return true;
  3346. }
  3347. //填涂面积相差达%50,将面积不小于最大点填涂面积的总面积%30的涂点视为填涂
  3348. if (maxgraynum - mingraynum>results->cell_size*0.5&&results->gray_num>maxgraynum - results->cell_size*0.3){
  3349. sel = SELECTED;
  3350. return true;
  3351. }
  3352. return false;
  3353. }
  3354. void COnLineCardPageIdentifier::DrawSchema(IplImage*dst_img, SchemaPage* & pageSchema)
  3355. {
  3356. // 绘制定位点
  3357. for (std::size_t i = 0; i < pageSchema->questionLocatePoints.size(); i++){
  3358. cvDrawRect(dst_img,
  3359. cvPoint(pageSchema->questionLocatePoints[i].centerx - pageSchema->questionLocatePoints[i].width / 2.0, pageSchema->questionLocatePoints[i].centery - pageSchema->questionLocatePoints[i].height / 2.0),
  3360. cvPoint(pageSchema->questionLocatePoints[i].centerx + pageSchema->questionLocatePoints[i].width / 2.0, pageSchema->questionLocatePoints[i].centery + pageSchema->questionLocatePoints[i].height / 2.0),
  3361. cvScalar(0, 255, 0));
  3362. }
  3363. // 绘制二维码 条形码
  3364. for (std::size_t i = 0; i < pageSchema->codes.size(); i++){
  3365. cvDrawRect(dst_img,
  3366. cvPoint(pageSchema->codes[i].centerx - pageSchema->codes[i].width / 2.0, pageSchema->codes[i].centery - pageSchema->codes[i].height / 2.0),
  3367. cvPoint(pageSchema->codes[i].centerx + pageSchema->codes[i].width / 2.0, pageSchema->codes[i].centery + pageSchema->codes[i].height / 2.0),
  3368. cvScalar(0, 255, 0));
  3369. }
  3370. // 绘制
  3371. for (std::size_t i = 0; i < pageSchema->items.size(); i++){
  3372. cvDrawRect(dst_img,
  3373. cvPoint(pageSchema->items[i].centerx - pageSchema->items[i].width / 2.0, pageSchema->items[i].centery - pageSchema->items[i].height / 2.0),
  3374. cvPoint(pageSchema->items[i].centerx + pageSchema->items[i].width / 2.0, pageSchema->items[i].centery + pageSchema->items[i].height / 2.0),
  3375. cvScalar(0, 255, 0));
  3376. }
  3377. // 主观题
  3378. for (std::size_t i = 0; i < pageSchema->question_score.size(); i++){
  3379. if (pageSchema->question_score[i].scoreBox.bXuanZuoTi){
  3380. for (int j = 0; j < pageSchema->question_score[i].countNum; j++){
  3381. cvDrawRect(dst_img,
  3382. cvPoint(pageSchema->question_score[i].item[j].centerx - pageSchema->question_score[i].item[j].width / 2.0, pageSchema->question_score[i].item[j].centery - pageSchema->question_score[i].item[j].height / 2.0),
  3383. cvPoint(pageSchema->question_score[i].item[j].centerx + pageSchema->question_score[i].item[j].width / 2.0, pageSchema->question_score[i].item[j].centery + pageSchema->question_score[i].item[j].height / 2.0),
  3384. cvScalar(0, 255, 0));
  3385. }
  3386. }
  3387. cvDrawRect(dst_img,
  3388. cvPoint(pageSchema->question_score[i].centerx - pageSchema->question_score[i].width / 2.0, pageSchema->question_score[i].centery - pageSchema->question_score[i].height / 2.0),
  3389. cvPoint(pageSchema->question_score[i].centerx + pageSchema->question_score[i].width / 2.0, pageSchema->question_score[i].centery + pageSchema->question_score[i].height / 2.0),
  3390. cvScalar(0, 255, 0));
  3391. // 绘制打分区域
  3392. int x = pageSchema->question_score[i].scoreBox.pos.x,
  3393. y = pageSchema->question_score[i].scoreBox.pos.y,
  3394. w = pageSchema->question_score[i].scoreBox.pos.w,
  3395. h = pageSchema->question_score[i].scoreBox.pos.h;
  3396. cvDrawRect(dst_img,
  3397. cvPoint(x, y),
  3398. cvPoint(x + w, y + h),
  3399. cvScalar(255, 0,0));
  3400. }
  3401. // 学号
  3402. for (std::size_t i = 0; i < pageSchema->stuItems.size(); i++){
  3403. cvDrawRect(dst_img,
  3404. cvPoint(pageSchema->stuItems[i].centerx - pageSchema->stuItems[i].width / 2.0, pageSchema->stuItems[i].centery - pageSchema->stuItems[i].height / 2.0),
  3405. cvPoint(pageSchema->stuItems[i].centerx + pageSchema->stuItems[i].width / 2.0, pageSchema->stuItems[i].centery + pageSchema->stuItems[i].height / 2.0),
  3406. cvScalar(0, 255, 0));
  3407. }
  3408. //缺考标识
  3409. if (pageSchema->quekaoLocatePointIndex.size()>0){
  3410. cvDrawRect(dst_img,
  3411. cvPoint(pageSchema->quekao.centerx - pageSchema->quekao.width / 2.0, pageSchema->quekao.centery - pageSchema->quekao.height / 2.0),
  3412. cvPoint(pageSchema->quekao.centerx + pageSchema->quekao.width / 2.0, pageSchema->quekao.centery + pageSchema->quekao.height / 2.0),
  3413. cvScalar(0, 0, 255));
  3414. }
  3415. }
  3416. }