PageIdentify.cpp 154 KB

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