PageIdentify.cpp 155 KB

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