PixMatrix.cpp 140 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635
  1. // PixMatrix.cpp: implementation of the CPixMatrix class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "PixMatrix.h"
  6. #include "memory.h"
  7. #include "math.h"
  8. #include "String.Format.tlh"
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char THIS_FILE[]=__FILE__;
  12. #define new DEBUG_NEW
  13. #endif
  14. #define FULL_IMG_WIDTH 4000L
  15. #define FULL_IMG_HEIGHT 4000L
  16. #define PIXEL_MAX_VALUE 65536
  17. CPixMatrix* CPixMatrix::m_instance = NULL;
  18. CPixMatrix* CPixMatrix::Instance()
  19. {
  20. if(m_instance == NULL) {
  21. m_instance = new CPixMatrix(FULL_IMG_WIDTH, FULL_IMG_HEIGHT, 15, 45);
  22. }
  23. return m_instance;
  24. }
  25. CPixMatrix::CPixMatrix(int width, int height, int woffset, int hoffset, int nPixelMax)
  26. :m_nWidth(width), m_nHeight(height), m_nWOffset(woffset), m_nHOffset(hoffset), m_nPixelMax(nPixelMax)
  27. {
  28. m_BadPixelMap = NULL;
  29. m_BadNewPixelMap = NULL;
  30. m_BadPixArray = NULL;
  31. m_xLineMap = NULL;
  32. m_yLineMap = NULL;
  33. m_doubleLineMap = NULL;
  34. m_NewMap = new PIX_MAP[m_nHeight];
  35. //m_curImg = NULL;////////////////////////chenGN 2013.01.31
  36. //m_lastImg = NULL;////////////////////////chenGN 2013.01.31
  37. m_mapImg = NULL;
  38. m_curAvg = -1;
  39. m_lastAvg = -1;
  40. ZeroMemory(m_charFilename, 256);
  41. m_TempImage = new unsigned short [ m_nWidth * m_nHeight ];
  42. m_TempImage1 = new unsigned short [ m_nWidth * m_nHeight ];
  43. //nWidth = 0;
  44. //nHeight = 0;
  45. m_pBadPixNum = NULL;
  46. //m_datalen = new int [ 400 ];
  47. m_nLinePoint = 50;////×?D??μ??μ?êy, zhaoyiru, 2017.05.09
  48. m_nLineData = int( (MAXI_BADPIX_COUNT - MAX_PIX_MAP_LINE) / m_nLinePoint);//×?′ó?μ????êy, zhaoyiru, 2017.05.09
  49. m_datalen = new int[ m_nLineData ] ;
  50. memset(m_datalen, -1, sizeof(int)* m_nLineData );
  51. }
  52. CPixMatrix::~CPixMatrix()
  53. {
  54. if(m_NewMap) {
  55. delete [] m_NewMap;
  56. m_NewMap = NULL;
  57. }
  58. if(m_BadPixArray) {
  59. delete [] m_BadPixArray;
  60. m_BadPixArray = NULL;
  61. }
  62. if( m_TempImage != NULL )
  63. {
  64. delete [] m_TempImage;
  65. m_TempImage = NULL;
  66. }
  67. if( m_TempImage1 != NULL )
  68. {
  69. delete [] m_TempImage1;
  70. m_TempImage1 = NULL;
  71. }
  72. if (m_mapImg)//add by ys20180109不在析构函数中释放的话会造成内存泄漏
  73. {
  74. delete[] m_mapImg;
  75. m_mapImg = NULL;
  76. }//add end
  77. FreeBadPixelMap();
  78. //FreeBadNewPixelMap();
  79. FreeDBadLineMap();
  80. if( m_pBadPixNum != NULL )
  81. {
  82. //delete m_pBadPixNum;
  83. m_pBadPixNum = NULL;
  84. }
  85. if( m_datalen != NULL )
  86. {
  87. delete [] m_datalen;
  88. m_datalen = NULL;
  89. }
  90. }
  91. void CPixMatrix::CorrectBadPixels(WORD* image)
  92. {
  93. long line;
  94. long pix_num;
  95. long bad_pix_num;
  96. long bad_num;
  97. long *p_bad_pix_num;
  98. float new_value;
  99. long w_new_value;
  100. float divisor;
  101. WORD *p_line, *p_prev_line, *p_next_line;
  102. long unProcLineNum;
  103. long unProcTotalNum = 0;
  104. if(m_BadPixelMap == NULL){ // can't do anything without a map
  105. return;
  106. }
  107. if(m_BadPixArray == NULL) {
  108. m_BadPixArray = new long [MAXI_BADPIX_COUNT*sizeof(long)];
  109. if(m_BadPixArray == NULL) {
  110. return;
  111. }
  112. }
  113. MarkBadAdjacentPixels();
  114. for(line = 0 ; line < m_nHeight ; ++line) {
  115. m_NewMap[line].len = 0;
  116. }
  117. int nXOffset = m_nWOffset;
  118. int nYOffset = m_nHOffset;
  119. int nLeft = nXOffset;
  120. int nRight = m_nWidth - nXOffset - 1;
  121. int nTop = nYOffset;
  122. int nBottom = m_nHeight - nYOffset - 1;
  123. int nXStartPos = nLeft;
  124. int nXEndPos = nRight;
  125. int nYStartPos = nTop;
  126. int nYEndPos = nBottom;
  127. int nBadPxlInfo = 0;
  128. int nPixelPos = 0;
  129. int nIdxI = 0;
  130. float fPVSum = 0.0f;
  131. float fDivisor = 0.0f;
  132. int nAvgPxlVal = CalcGlbAvgPxlValueBySamp(image, m_nWidth, m_nHeight, nXOffset, nYOffset);
  133. //校正最上面一行的坏点的像素值
  134. if (0 < m_BadPixelMap[nTop].num_entries)
  135. {
  136. for (nIdxI = 0; nIdxI < m_BadPixelMap[nTop].num_entries; nIdxI++)
  137. {
  138. nBadPxlInfo = m_BadPixelMap[nTop].bad_pixel_num[nIdxI];
  139. nPixelPos = nBadPxlInfo & OFFSET_MASK;
  140. fPVSum = 0.0f;
  141. fDivisor = 0.0f;
  142. //左边像素[nTop, nPixelPos - 1]
  143. if ((nLeft <= (nPixelPos - 1)) && ((nBadPxlInfo & NEIGHBOR_3) == 0))
  144. {
  145. fPVSum += image[nTop * m_nWidth + nPixelPos - 1];
  146. fDivisor += 1.0f;
  147. }
  148. //左下角像素[nTop + 1, nPixelPos - 1]
  149. if ((nLeft <= (nPixelPos - 1)) && ((nTop + 1) <= nBottom) && ((nBadPxlInfo & NEIGHBOR_5) == 0))
  150. {
  151. fPVSum += 0.707f * image[(nTop + 1) * m_nWidth + nPixelPos - 1];
  152. fDivisor += 0.707f;
  153. }
  154. //右边像素[nTop, nPixelPos + 1]
  155. if (((nPixelPos + 1) <= nRight) && ((nBadPxlInfo & NEIGHBOR_4) == 0))
  156. {
  157. fPVSum += image[nTop * m_nWidth + nPixelPos + 1];
  158. fDivisor += 1.0f;
  159. }
  160. //右下角像素[nTop + 1, nPixelPos + 1]
  161. if (((nPixelPos + 1) <= nRight) && ((nTop + 1) <= nBottom) && ((nBadPxlInfo & NEIGHBOR_7) == 0))
  162. {
  163. fPVSum += 0.707f * image[(nTop + 1) * m_nWidth + nPixelPos + 1];
  164. fDivisor += 0.707f;
  165. }
  166. //下边像素[nTop + 1, nPixelPos]
  167. if (((nTop + 1) <= nBottom) && ((nBadPxlInfo & NEIGHBOR_6) == 0))
  168. {
  169. fPVSum += image[(nTop + 1) * m_nWidth + nPixelPos];
  170. fDivisor += 1.0f;
  171. }
  172. if (0.9f < fDivisor)
  173. {
  174. image[nTop * m_nWidth + nPixelPos] = (int)(fPVSum / fDivisor);
  175. }
  176. else
  177. {
  178. image[nTop * m_nWidth + nPixelPos] = nAvgPxlVal;
  179. }
  180. }
  181. }
  182. //校正最下面一行坏点的像素值
  183. if (0 < m_BadPixelMap[nBottom].num_entries)
  184. {
  185. for (nIdxI = 0; nIdxI < m_BadPixelMap[nBottom].num_entries; nIdxI++)
  186. {
  187. nBadPxlInfo = m_BadPixelMap[nBottom].bad_pixel_num[nIdxI];
  188. nPixelPos = nBadPxlInfo & OFFSET_MASK;
  189. fPVSum = 0.0f;
  190. fDivisor = 0.0f;
  191. //左边像素[nBottom, nPixelPos - 1]
  192. if ((nLeft <= (nPixelPos - 1)) && ((nBadPxlInfo & NEIGHBOR_3) == 0))
  193. {
  194. fPVSum += image[nBottom * m_nWidth + nPixelPos - 1];
  195. fDivisor += 1.0f;
  196. }
  197. //左上角像素[nBottom - 1, nPixelPos - 1]
  198. if ((nLeft <= (nPixelPos - 1)) && (nTop <= (nBottom - 1)) && ((nBadPxlInfo & NEIGHBOR_0) == 0))
  199. {
  200. fPVSum += 0.707f * image[(nBottom - 1) * m_nWidth + nPixelPos - 1];
  201. fDivisor += 0.707f;
  202. }
  203. //右边像素[nBottom, nPixelPos + 1]
  204. if (((nPixelPos + 1) <= nRight) && ((nBadPxlInfo & NEIGHBOR_4) == 0))
  205. {
  206. fPVSum += image[nBottom * m_nWidth + nPixelPos + 1];
  207. fDivisor += 1.0f;
  208. }
  209. //右上角像素[nBottom - 1, nPixelPos + 1]
  210. if (((nPixelPos + 1) <= nRight) && (nTop <= (nBottom - 1)) && ((nBadPxlInfo & NEIGHBOR_2) == 0))
  211. {
  212. fPVSum += 0.707f * image[(nBottom - 1) * m_nWidth + nPixelPos + 1];
  213. fDivisor += 0.707f;
  214. }
  215. //上边像素[nBottom - 1, nPixelPos]
  216. if ((nTop <= (nBottom - 1)) && ((nBadPxlInfo & NEIGHBOR_1) == 0))
  217. {
  218. fPVSum += image[(nBottom - 1) * m_nWidth + nPixelPos];
  219. fDivisor += 1.0f;
  220. }
  221. if (0.9f < fDivisor)
  222. {
  223. image[nBottom * m_nWidth + nPixelPos] = (int)(fPVSum / fDivisor);
  224. }
  225. else
  226. {
  227. image[nBottom * m_nWidth + nPixelPos] = nAvgPxlVal;
  228. }
  229. }
  230. }
  231. //校正最左边列和最右边列的坏点的像素值
  232. int nRow = 0;
  233. int nLeftBadPxlFlag = -1;
  234. int nRightBadPxlFlag = -1;
  235. for (nRow = nTop + 1; nRow < nBottom; nRow++)
  236. {
  237. nLeftBadPxlFlag = -1;
  238. nRightBadPxlFlag = -1;
  239. for (nIdxI = 0; nIdxI < m_BadPixelMap[nRow].num_entries; nIdxI++)
  240. {
  241. nBadPxlInfo = m_BadPixelMap[nRow].bad_pixel_num[nIdxI];
  242. nPixelPos = nBadPxlInfo & OFFSET_MASK;
  243. if (nPixelPos == nLeft)
  244. {
  245. nLeftBadPxlFlag = nIdxI;
  246. }
  247. else if (nPixelPos == nRight)
  248. {
  249. nRightBadPxlFlag = nIdxI;
  250. }
  251. }
  252. //校正最左列的坏点的像素值
  253. if (-1 < nLeftBadPxlFlag)
  254. {
  255. nBadPxlInfo = m_BadPixelMap[nRow].bad_pixel_num[nLeftBadPxlFlag];
  256. nPixelPos = nBadPxlInfo & OFFSET_MASK;
  257. fPVSum = 0.0f;
  258. fDivisor = 0.0f;
  259. //上边像素[nRow - 1, nPixelPos]
  260. if ((nTop <= (nRow - 1)) && ((nBadPxlInfo & NEIGHBOR_1) == 0))
  261. {
  262. fPVSum += image[(nRow - 1) * m_nWidth + nPixelPos];
  263. fDivisor += 1.0f;
  264. }
  265. //右上角像素[nRow - 1, nPixelPos + 1]
  266. if (((nPixelPos + 1) <= nRight) && (nTop <= (nRow - 1)) && ((nBadPxlInfo & NEIGHBOR_2) == 0))
  267. {
  268. fPVSum += 0.707f * image[(nRow - 1) * m_nWidth + nPixelPos + 1];
  269. fDivisor += 0.707f;
  270. }
  271. //右边像素[nRow, nPixelPos + 1]
  272. if (((nPixelPos + 1) <= nRight) && ((nBadPxlInfo & NEIGHBOR_4) == 0))
  273. {
  274. fPVSum += image[nRow * m_nWidth + nPixelPos + 1];
  275. fDivisor += 1.0f;
  276. }
  277. //右下角像素[nRow + 1, nPixelPos + 1]
  278. if (((nPixelPos + 1) <= nRight) && ((nRow + 1) <= nBottom) && ((nBadPxlInfo & NEIGHBOR_7) == 0))
  279. {
  280. fPVSum += 0.707f * image[(nRow + 1) * m_nWidth + nPixelPos + 1];
  281. fDivisor += 0.707f;
  282. }
  283. //下边像素[nRow + 1, nPixelPos]
  284. if (((nRow + 1) <= nBottom) && ((nBadPxlInfo & NEIGHBOR_6) == 0))
  285. {
  286. fPVSum += image[(nRow + 1) * m_nWidth + nPixelPos];
  287. fDivisor += 1.0f;
  288. }
  289. if (0.9f < fDivisor)
  290. {
  291. image[nRow * m_nWidth + nPixelPos] = (int)(fPVSum / fDivisor);
  292. }
  293. else
  294. {
  295. image[nRow * m_nWidth + nPixelPos] = nAvgPxlVal;
  296. }
  297. }
  298. //校正最右列的坏点的像素值
  299. if (-1 < nRightBadPxlFlag)
  300. {
  301. nBadPxlInfo = m_BadPixelMap[nRow].bad_pixel_num[nRightBadPxlFlag];
  302. nPixelPos = nBadPxlInfo & OFFSET_MASK;
  303. fPVSum = 0.0f;
  304. fDivisor = 0.0f;
  305. //上边像素[nRow - 1, nPixelPos]
  306. if ((nTop <= (nRow - 1)) && ((nBadPxlInfo & NEIGHBOR_1) == 0))
  307. {
  308. fPVSum += image[(nRow - 1) * m_nWidth + nPixelPos];
  309. fDivisor += 1.0f;
  310. }
  311. //左上角像素[nRow - 1, nPixelPos - 1]
  312. if ((nLeft <= (nPixelPos - 1)) && (nTop <= (nRow - 1)) && ((nBadPxlInfo & NEIGHBOR_0) == 0))
  313. {
  314. fPVSum += 0.707f * image[(nRow - 1) * m_nWidth + nPixelPos - 1];
  315. fDivisor += 0.707f;
  316. }
  317. //左边像素[nRow, nPixelPos - 1]
  318. if ((nLeft <= (nPixelPos - 1)) && ((nBadPxlInfo & NEIGHBOR_3) == 0))
  319. {
  320. fPVSum += image[nRow * m_nWidth + nPixelPos - 1];
  321. fDivisor += 1.0f;
  322. }
  323. //左下角像素[nRow + 1, nPixelPos - 1]
  324. if ((nLeft <= (nPixelPos - 1)) && ((nRow + 1) <= nBottom) && ((nBadPxlInfo & NEIGHBOR_5) == 0))
  325. {
  326. fPVSum += 0.707f * image[(nRow + 1) * m_nWidth + nPixelPos - 1];
  327. fDivisor += 0.707f;
  328. }
  329. //下边像素[nRow + 1, nPixelPos]
  330. if (((nRow + 1) <= nBottom) && ((nBadPxlInfo & NEIGHBOR_6) == 0))
  331. {
  332. fPVSum += image[(nRow + 1) * m_nWidth + nPixelPos];
  333. fDivisor += 1.0f;
  334. }
  335. if (0.9f < fDivisor)
  336. {
  337. image[nRow * m_nWidth + nPixelPos] = (int)(fPVSum / fDivisor);
  338. }
  339. else
  340. {
  341. image[nRow * m_nWidth + nPixelPos] = nAvgPxlVal;
  342. }
  343. }
  344. }
  345. //校正其他坏点的像素值
  346. for(line = nTop + 1; line < nBottom ; ++line)
  347. {
  348. /* Precalculate the column pointers */
  349. p_line = image + (line * m_nWidth);
  350. p_next_line = p_line + m_nWidth;
  351. p_prev_line = p_line - m_nWidth;
  352. p_bad_pix_num = m_BadPixelMap[line].bad_pixel_num;
  353. unProcLineNum = 0;
  354. m_NewMap[line].p_Pix = &m_BadPixArray[unProcTotalNum];
  355. for(pix_num = 0; pix_num < m_BadPixelMap[line].num_entries; ++pix_num)
  356. {
  357. bad_pix_num = *p_bad_pix_num++;
  358. bad_num = bad_pix_num & OFFSET_MASK;
  359. if (bad_num == nLeft || bad_num == nRight)
  360. {
  361. continue;
  362. }
  363. new_value = 0;
  364. divisor = 0;
  365. nPixelPos = bad_pix_num & NEIGHBOR_1;
  366. if(nPixelPos == 0)
  367. {
  368. new_value += *(p_prev_line + bad_num);
  369. divisor += 1.0f;
  370. }
  371. nPixelPos = bad_pix_num & NEIGHBOR_3;
  372. if(nPixelPos == 0 && 0 < bad_num)
  373. {
  374. new_value += *(p_line + bad_num - 1);
  375. divisor += 1.0f;
  376. }
  377. nPixelPos = bad_pix_num & NEIGHBOR_4;
  378. if(nPixelPos == 0)
  379. {
  380. new_value += *(p_line + bad_num + 1);
  381. divisor += 1.0f;
  382. }
  383. nPixelPos = bad_pix_num & NEIGHBOR_6;
  384. if(nPixelPos == 0)
  385. {
  386. new_value += *(p_next_line + bad_num);
  387. divisor += 1.0f;
  388. }
  389. if(divisor < 1.1f && divisor > 0.9f)
  390. {
  391. nPixelPos = bad_pix_num & NEIGHBOR_0;
  392. if(nPixelPos == 0 && bad_num > 0)
  393. {
  394. new_value += 0.707f * *(p_prev_line + bad_num - 1);
  395. divisor += 0.707f;
  396. }
  397. nPixelPos = bad_pix_num & NEIGHBOR_2;
  398. if(nPixelPos == 0)
  399. {
  400. new_value += 0.707f * *(p_prev_line + bad_num + 1);
  401. divisor += 0.707f;
  402. }
  403. nPixelPos = bad_pix_num & NEIGHBOR_7;
  404. if(nPixelPos == 0)
  405. {
  406. new_value += 0.707f * *(p_next_line + bad_num + 1);
  407. divisor += 0.707f;
  408. }
  409. nPixelPos = bad_pix_num & NEIGHBOR_5;
  410. if(nPixelPos == 0)
  411. {
  412. new_value += 0.707f * *(p_next_line + bad_num - 1);
  413. divisor += 0.707f;
  414. }
  415. }
  416. // At least one neighbor is good
  417. if(divisor > 1.9f)
  418. {
  419. w_new_value = (long)(new_value / divisor);
  420. //w_new_value = floor((new_value / divisor)+0.5);
  421. *(p_line + bad_num) = (WORD)w_new_value;
  422. }
  423. else
  424. { /* Added the pixel to the new bad pixel map */
  425. unProcLineNum++;
  426. m_BadPixArray[unProcTotalNum] = bad_pix_num & OFFSET_MASK;
  427. unProcTotalNum = (unProcTotalNum + 1) % MAXI_BADPIX_COUNT;
  428. }
  429. }
  430. m_NewMap[line].len = unProcLineNum;
  431. }
  432. //
  433. // Fix the remaining bad pixels until no more bad pixels are left
  434. //
  435. while(unProcTotalNum > 0)
  436. {
  437. MarkBadPixels(m_nHeight, m_NewMap);
  438. unProcTotalNum = 0;
  439. for(line = 1; line < m_nHeight - 1; ++line)
  440. {
  441. /* Precalculate the column pointers */
  442. p_line = image + line * m_nWidth;
  443. p_next_line = p_line + m_nWidth;
  444. p_prev_line = p_line - m_nWidth;
  445. p_bad_pix_num = m_NewMap[line].p_Pix;
  446. unProcLineNum = 0;
  447. m_NewMap[line].p_Pix = &m_BadPixArray[unProcTotalNum];
  448. for(pix_num = 0; pix_num < (int)m_NewMap[line].len; ++pix_num)
  449. {
  450. bad_pix_num = *p_bad_pix_num++;
  451. bad_num = bad_pix_num & OFFSET_MASK;
  452. new_value = 0;
  453. divisor = 0;
  454. if (nLeft == bad_num || nRight == bad_num)
  455. {
  456. continue;
  457. }
  458. nPixelPos = bad_pix_num & NEIGHBOR_1;
  459. if(nPixelPos == 0)
  460. {
  461. new_value += *(p_prev_line + bad_num);
  462. divisor += 1.0f;
  463. }
  464. nPixelPos = bad_pix_num & NEIGHBOR_3;
  465. if(nPixelPos == 0 && 0 < bad_num)
  466. {
  467. new_value += *(p_line + bad_num - 1);
  468. divisor += 1.0f;
  469. }
  470. nPixelPos = bad_pix_num & NEIGHBOR_4;
  471. if(nPixelPos == 0)
  472. {
  473. new_value += *(p_line + bad_num + 1);
  474. divisor += 1.0f;
  475. }
  476. nPixelPos = bad_pix_num & NEIGHBOR_6;
  477. if(nPixelPos == 0)
  478. {
  479. new_value += *(p_next_line + bad_num);
  480. divisor += 1.0f;
  481. }
  482. if(divisor < 1.1f && divisor > 0.9f)
  483. {
  484. nPixelPos = bad_pix_num & NEIGHBOR_0;
  485. if(nPixelPos == 0 && 0 < bad_num)
  486. {
  487. new_value += 0.707f * *(p_prev_line + bad_num - 1);
  488. divisor += 0.707f;
  489. }
  490. nPixelPos = bad_pix_num & NEIGHBOR_2;
  491. if(nPixelPos == 0)
  492. {
  493. new_value += 0.707f * *(p_prev_line + bad_num + 1);
  494. divisor += 0.707f;
  495. }
  496. nPixelPos = bad_pix_num & NEIGHBOR_7;
  497. if(nPixelPos == 0)
  498. {
  499. new_value += 0.707f * *(p_next_line + bad_num + 1);
  500. divisor += 0.707f;
  501. }
  502. nPixelPos = bad_pix_num & NEIGHBOR_5;
  503. if(nPixelPos == 0 && 0 < bad_num)
  504. {
  505. new_value += 0.707f * *(p_next_line + bad_num - 1);
  506. divisor += 0.707f;
  507. }
  508. }
  509. if(divisor > 1.9f)
  510. {
  511. //w_new_value = floor((new_value / divisor)+0.5);
  512. w_new_value = (long)((new_value / divisor)+0.5);
  513. *(p_line + bad_num) = (WORD)w_new_value;
  514. }
  515. else
  516. { /* Goes to the new map */
  517. unProcLineNum++;
  518. m_BadPixArray[unProcTotalNum] = bad_num;
  519. unProcTotalNum = (unProcTotalNum + 1) % MAXI_BADPIX_COUNT;
  520. }
  521. }
  522. m_NewMap[line].len = unProcLineNum;
  523. }
  524. }
  525. return;
  526. }
  527. #define CHECK_NEIGHBOR_NEW_U(a, b) \
  528. while(ju < New_Map[a].len) \
  529. { \
  530. jj = New_Map[a].p_Pix[ju] & OFFSET_MASK; \
  531. if(jj == (b))\
  532. bad_neighbor |= NEIGHBOR_1;\
  533. else if(jj == (b - 1)) \
  534. bad_neighbor |= NEIGHBOR_0; \
  535. else if(jj == (b + 1)) \
  536. bad_neighbor |= NEIGHBOR_2; \
  537. if(jj >= (b + 1)){ \
  538. ju = ju ? ju - 1 : ju;\
  539. break; }\
  540. if((ju + 1) >= New_Map[a].len) break; \
  541. ju++;\
  542. }
  543. #define CHECK_NEIGHBOR_U(a, b) \
  544. while(ju < m_BadPixelMap[a].num_entries) \
  545. { \
  546. jj = m_BadPixelMap[a].bad_pixel_num[ju] & OFFSET_MASK; \
  547. if(jj == (b))\
  548. bad_neighbor |= NEIGHBOR_1;\
  549. else if(jj == (b - 1)) \
  550. bad_neighbor |= NEIGHBOR_0; \
  551. else if(jj == (b + 1)) \
  552. bad_neighbor |= NEIGHBOR_2; \
  553. if(jj >= (b + 1)){ \
  554. ju = ju ? ju - 1 : ju;\
  555. break; } \
  556. if((ju + 1) >= m_BadPixelMap[a].num_entries) break; \
  557. ju++;\
  558. }
  559. #define CHECK_NEIGHBOR_NEW_D(a, b) \
  560. while(jd < New_Map[a].len) \
  561. { \
  562. jj = New_Map[a].p_Pix[jd] & OFFSET_MASK; \
  563. if(jj == (long)(b))\
  564. bad_neighbor |= NEIGHBOR_6;\
  565. else if(jj == (long)(b - 1)) \
  566. bad_neighbor |= NEIGHBOR_5; \
  567. else if(jj == (long)(b + 1)) \
  568. bad_neighbor |= NEIGHBOR_7; \
  569. if(jj >= (long)(b + 1)){ \
  570. jd = jd ? jd - 1 : jd;\
  571. break;} \
  572. if( ( jd + 1 ) >= New_Map[ a ].len ) break; \
  573. jd++;\
  574. }
  575. #define CHECK_NEIGHBOR_D(a, b) \
  576. while(jd < m_BadPixelMap[a].num_entries )\
  577. { \
  578. jj = m_BadPixelMap[a].bad_pixel_num[jd] & OFFSET_MASK; \
  579. if(jj == (b))\
  580. bad_neighbor |= NEIGHBOR_6;\
  581. else if(jj == (b - 1)) \
  582. bad_neighbor |= NEIGHBOR_5; \
  583. else if(jj == (b + 1)) \
  584. bad_neighbor |= NEIGHBOR_7; \
  585. if(jj >= (b + 1)){ \
  586. jd = jd ? jd - 1 : jd;\
  587. break;}\
  588. if((jd + 1) >= m_BadPixelMap[a].num_entries ) break; \
  589. jd++;\
  590. }
  591. void CPixMatrix::MarkBadPixels(int NumLines, PIX_MAP *New_Map)
  592. {
  593. long i, ju, jd;
  594. long jj;
  595. long pix_num;
  596. long *p_bad_pix;
  597. long bad_pix_num;
  598. long bad_neighbor;
  599. for(i = 1; i < NumLines - 1; ++i) {
  600. p_bad_pix = New_Map[i].p_Pix;
  601. for(pix_num = 0, ju = 0, jd = 0; pix_num < (int)New_Map[i].len; ++pix_num) {
  602. bad_pix_num = *p_bad_pix & OFFSET_MASK;
  603. bad_neighbor = 0;
  604. CHECK_NEIGHBOR_NEW_U((long)(i - 1), bad_pix_num);
  605. if((*(p_bad_pix - 1) & OFFSET_MASK) == bad_pix_num - 1)
  606. bad_neighbor |= NEIGHBOR_3;
  607. if((*(p_bad_pix + 1) & OFFSET_MASK) == bad_pix_num + 1)
  608. bad_neighbor |= NEIGHBOR_4;
  609. CHECK_NEIGHBOR_NEW_D(i + 1, bad_pix_num);
  610. *p_bad_pix++ = bad_pix_num | bad_neighbor;
  611. }
  612. }
  613. }
  614. // /****************************************************************************/
  615. // /* Use the upper byte of the bad pixel map offset to pre-mark bad pixels
  616. // that have adjacent bad pixels */
  617. //
  618. // void CPixMatrix::MarkBadAdjacentPixels()
  619. // {
  620. // long i, ju, jd;
  621. // long jj;
  622. // long pix_num;
  623. // long *p_bad_pix;
  624. // long bad_pix_num;
  625. // long bad_neighbor;
  626. //
  627. // /*
  628. // * Mark the bad pixel map for high-resolution image
  629. // */
  630. // if(m_BadPixelMap == NULL) return;
  631. // int
  632. // for(i = 1; i < (m_nHeight - 1); ++i)
  633. // {
  634. // p_bad_pix = m_BadPixelMap[i].bad_pixel_num;
  635. // for(pix_num = 0, ju = 0, jd = 0 ; pix_num < m_BadPixelMap[i].num_entries ; pix_num++)
  636. // {
  637. // bad_pix_num = *p_bad_pix & OFFSET_MASK;
  638. // bad_neighbor = 0;
  639. // CHECK_NEIGHBOR_U((long)(i - 1), bad_pix_num);
  640. // if((*(p_bad_pix - 1) & OFFSET_MASK) == bad_pix_num - 1)
  641. // {
  642. // bad_neighbor |= NEIGHBOR_3;
  643. // }
  644. // if((*(p_bad_pix + 1) & OFFSET_MASK) == bad_pix_num + 1)
  645. // {
  646. // bad_neighbor |= NEIGHBOR_4;
  647. // }
  648. // CHECK_NEIGHBOR_D((long)(i + 1), bad_pix_num);
  649. // *p_bad_pix++ = bad_pix_num | bad_neighbor;
  650. // }
  651. // }
  652. // }
  653. /****************************************************************************/
  654. /* Use the upper byte of the bad pixel map offset to pre-mark bad pixels
  655. that have adjacent bad pixels */
  656. //Modified by Alex Stocks on 2011/01/30
  657. void CPixMatrix::MarkBadAdjacentPixels()
  658. {
  659. if(m_BadPixelMap == NULL)
  660. {
  661. return;
  662. }
  663. long i, ju, jd;
  664. long jj;
  665. long pix_num;
  666. long *p_bad_pix;
  667. long bad_pix_num;
  668. long bad_neighbor;
  669. int a = 0;
  670. int b = 0;
  671. /*
  672. * Mark the bad pixel map for high-resolution image
  673. */
  674. int nXOffset = 0 < m_nWOffset ? m_nWOffset : 0;
  675. int nXStartPos = nXOffset;
  676. int nXEndPos = m_nWidth - nXOffset;
  677. int nYOffset = 0 < m_nHOffset ? m_nHOffset : 0;
  678. int nYStartPos = nYOffset;
  679. int nYEndPos = m_nHeight - nYOffset;
  680. int nLeft = nXStartPos;
  681. int nRight = nXEndPos - 1;
  682. int nTop = nYStartPos;
  683. int nBottom = nYEndPos - 1;
  684. //for(i = 1; i < (m_nHeight - 1); ++i)
  685. for (i = nYStartPos; i < nYEndPos; i++)
  686. {
  687. p_bad_pix = m_BadPixelMap[i].bad_pixel_num;
  688. for(pix_num = 0, ju = 0, jd = 0 ; pix_num < m_BadPixelMap[i].num_entries ; pix_num++)
  689. {
  690. bad_pix_num = *p_bad_pix & OFFSET_MASK;
  691. bad_neighbor = 0;
  692. //CHECK_NEIGHBOR_U((long)(i - 1), bad_pix_num);
  693. a = i - 1; //上一行
  694. b = bad_pix_num; //列
  695. //对每个坏点像素的上一行的三个位置进行检查
  696. while((nTop <= a) && (ju < m_BadPixelMap[a].num_entries))
  697. {
  698. jj = m_BadPixelMap[a].bad_pixel_num[ju] & OFFSET_MASK;
  699. if(jj == (b))
  700. {//上面
  701. bad_neighbor |= NEIGHBOR_1;
  702. }
  703. else if(nLeft <= (b - 1) && ((b - 1) == jj))
  704. {//左上
  705. bad_neighbor |= NEIGHBOR_0;
  706. }
  707. else if((b + 1) <= nRight && ((b + 1) == jj))
  708. {//右上
  709. bad_neighbor |= NEIGHBOR_2;
  710. }
  711. if((b + 1) <= jj)
  712. {
  713. ju = ju ? ju - 1 : ju;
  714. break;
  715. }
  716. if(m_BadPixelMap[a].num_entries <= (ju + 1))
  717. {
  718. break;
  719. }
  720. ju++;
  721. }
  722. //对坏点像素左边和右边进行检查
  723. if((nLeft <= (b - 1)) && ((*(p_bad_pix - 1) & OFFSET_MASK) == bad_pix_num - 1))
  724. {//左边
  725. bad_neighbor |= NEIGHBOR_3;
  726. }
  727. if(((b + 1) <= nRight) && ((*(p_bad_pix + 1) & OFFSET_MASK) == bad_pix_num + 1))
  728. {//右边
  729. bad_neighbor |= NEIGHBOR_4;
  730. }
  731. //CHECK_NEIGHBOR_D((long)(i + 1), bad_pix_num);
  732. a = i + 1; //下一行
  733. b = bad_pix_num; //列
  734. //对坏点像素的下一行进行检查
  735. while((a <= nBottom) && (jd < m_BadPixelMap[a].num_entries) )
  736. {
  737. jj = m_BadPixelMap[a].bad_pixel_num[jd] & OFFSET_MASK;
  738. if(jj == (b))
  739. {//下面
  740. bad_neighbor |= NEIGHBOR_6;
  741. }
  742. else if(nLeft <= (b - 1) && (jj == (b - 1)))
  743. {//左下
  744. bad_neighbor |= NEIGHBOR_5;
  745. }
  746. else if((b + 1) <= nRight && (jj == (b + 1)))
  747. {//右下
  748. bad_neighbor |= NEIGHBOR_7;
  749. }
  750. if(jj >= (b + 1))
  751. {
  752. jd = jd ? jd - 1 : jd;
  753. break;
  754. }
  755. if((jd + 1) >= m_BadPixelMap[a].num_entries )
  756. {
  757. break;
  758. }
  759. jd++;
  760. }
  761. //bad_neighbor记录了坏点位置8邻域内所有的坏点的位置
  762. *p_bad_pix++ = bad_pix_num | bad_neighbor;
  763. }
  764. }
  765. }
  766. bool CPixMatrix::LoadBadPixelMap(const char *fileName)
  767. {
  768. //BAD_PIXEL_ENTRY **theMap = &m_BadPixelMap;
  769. strcpy(m_charFilename, fileName);
  770. FILE *fp;
  771. int num_entries;
  772. int entry;
  773. int line_num;
  774. int file_line_num;
  775. long *p_bad_pixels;
  776. bool status = TRUE;
  777. long bad_pixel_num = 0;
  778. char *data_in = NULL;
  779. char *p_token;
  780. long num_bad_pixels = 0;
  781. /* Hopefully, the largest line in a bad pix map */
  782. // data_in = (char *)malloc(MAX_PIX_MAP_LINE);
  783. data_in = new char [MAX_PIX_MAP_LINE];
  784. if(data_in == NULL) {
  785. status = FALSE;
  786. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: malloc of data_in failed.");
  787. }
  788. fp = fopen(fileName, "rb");
  789. if(fp == NULL) {
  790. status = FALSE;
  791. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Open of file : %s failed.", fileName);
  792. }
  793. else {
  794. if(m_BadPixelMap != NULL) {
  795. FreeBadPixelMap();
  796. }
  797. //m_BadPixelMap = (BAD_PIXEL_ENTRY *)calloc(m_nHeight, sizeof(BAD_PIXEL_ENTRY));
  798. m_BadPixelMap = new BAD_PIXEL_ENTRY [m_nHeight*sizeof(BAD_PIXEL_ENTRY)];
  799. if(m_BadPixelMap == NULL) {
  800. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Malloc of m_BadPixelMap failed.");
  801. status = FALSE;
  802. }
  803. else
  804. {
  805. for (int i=0;i<m_nHeight;i++)
  806. {
  807. //addbyys20190221 参考图像组意见改动
  808. //用于修复“从小尺寸探测器更换到大尺寸探测器,而不替换匹配的full.map文件,当调用BadLineRecognize()时偶发崩溃”的问题
  809. m_BadPixelMap[i].num_entries = 0;
  810. //addend
  811. m_BadPixelMap[i].bad_pixel_num = NULL;
  812. }
  813. }
  814. }
  815. /* Data structures created OK and file is open, let's roll */
  816. if(status == TRUE) {
  817. line_num = 0;
  818. while((line_num < m_nHeight) && (status == TRUE)) {
  819. if(fgets(data_in, MAX_PIX_MAP_LINE, fp) != NULL) {
  820. // Each line in bad_pixel.map begins with "linenum,num_entries: "
  821. if(sscanf(data_in, "%d,%d: ", &file_line_num, &num_entries) == 2) {
  822. // Verify the line number read matches the line num we are on
  823. if(line_num != file_line_num) {
  824. status = FALSE;
  825. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Pixel map read error for col %d.", line_num);
  826. }
  827. // Verify that num_entries is not larger than pixels-per-line
  828. if(num_entries > m_nWidth) {
  829. status = FALSE;
  830. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Pixel map read error for col %d; "
  831. // "num_entries (%d) > line_size (%d)", line_num, num_entries, m_nWidth);
  832. }
  833. /* Tokenize the header */
  834. p_token = strtok(data_in, ":");
  835. if(status == TRUE) {
  836. /* Create the correct size bad pixel map entry */
  837. //(m_BadPixelMap)[line_num].bad_pixel_num = (long *)malloc((num_entries + 1) * sizeof(long));
  838. m_BadPixelMap[line_num].bad_pixel_num = new long [(num_entries+1)*sizeof(long)];
  839. if( (m_BadPixelMap)[line_num].bad_pixel_num == NULL) {
  840. status = FALSE;
  841. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Malloc of p_bad_pixels for line %d failed.", line_num);
  842. }
  843. else {
  844. memset( (m_BadPixelMap)[line_num].bad_pixel_num, 0, (num_entries + 1) * sizeof(long));
  845. p_bad_pixels = (m_BadPixelMap)[line_num].bad_pixel_num;
  846. (m_BadPixelMap)[line_num].num_entries = num_entries;
  847. /* Fill in the array */
  848. for(entry = 0 ; entry < num_entries ; ++entry) {
  849. /*
  850. * The remainder of each line of the Bad Pixel File consists
  851. * of numbers indicating the x-coordinate value of the bad
  852. * pixels. They should be in ascending order. None of them
  853. * should be greater than capture_config.line_size.
  854. */
  855. p_token = strtok(NULL, ",");
  856. if((p_token == NULL) ||
  857. (sscanf(p_token, "%ld,", &bad_pixel_num) != 1)) {
  858. status = FALSE;
  859. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Read failure on pixel entry %d.", entry);
  860. }
  861. else {
  862. if(bad_pixel_num > m_nWidth) {
  863. status = FALSE;
  864. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Bad value (%ld) on pixel entry %d "
  865. // "exceeds line_size (%d)",
  866. // bad_pixel_num, entry, m_nWidth);
  867. }
  868. else {
  869. p_bad_pixels[entry] = bad_pixel_num;
  870. num_bad_pixels++;
  871. }
  872. }
  873. }
  874. }
  875. }
  876. line_num++;
  877. }
  878. /* Not a bad pixel entry; This is a bad line in the file */
  879. else {
  880. if(strstr(data_in, "synthseam"))
  881. continue;
  882. else
  883. status = FALSE;
  884. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Read of entry for line_num %d failed", line_num );
  885. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, " Line_num %d is: <%s>", line_num, data_in );
  886. }
  887. }
  888. else {
  889. // file read error here or end of file.
  890. break;
  891. }
  892. }
  893. }
  894. if(status == TRUE) {
  895. if(num_bad_pixels > MAX_BAD_PIXELS) {
  896. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Bad pixel map exceeds limit (%ld) : %ld pixels",
  897. // MAX_BAD_PIXELS, num_bad_pixels);
  898. }
  899. }
  900. if(fp != NULL) {
  901. fclose(fp);
  902. }
  903. if(data_in != NULL) {
  904. delete [] data_in;
  905. }
  906. if(status != TRUE)
  907. {
  908. FreeBadPixelMap();
  909. }
  910. else
  911. {
  912. /////////////////////////新方法-基于二值图坏线识别
  913. BadLineRecognize();
  914. }
  915. return status;
  916. }
  917. bool CPixMatrix::LoadBadPixelMapChar(char* data_char, bool bNew)
  918. {
  919. //BAD_PIXEL_ENTRY **theMap = &m_BadPixelMap;
  920. // FILE *fp;
  921. int num_entries;
  922. int entry;
  923. int line_num;
  924. int file_line_num;
  925. long *p_bad_pixels;
  926. bool status = TRUE;
  927. long bad_pixel_num = 0;
  928. char *data_in = NULL;
  929. char *p_token;
  930. long num_bad_pixels = 0;
  931. // char *data_temp;
  932. long char_readcount = 0;
  933. /* Hopefully, the largest line in a bad pix map */
  934. // data_in = (char *)malloc(MAX_PIX_MAP_LINE);
  935. // data_in = new char [MAX_PIX_MAP_LINE];
  936. // if(data_in == NULL) {
  937. // status = FALSE;
  938. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: malloc of data_in failed.");
  939. // }
  940. // fp = fopen(fileName, "rb");
  941. if(data_char == NULL) {
  942. status = FALSE;
  943. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Open of file : %s failed.", fileName);
  944. }
  945. else {
  946. if(m_BadNewPixelMap != NULL) {
  947. FreeBadPixelMap();
  948. }
  949. //m_BadPixelMap = (BAD_PIXEL_ENTRY *)calloc(m_nHeight, sizeof(BAD_PIXEL_ENTRY));
  950. m_BadNewPixelMap = new BAD_PIXEL_ENTRY [m_nHeight*sizeof(BAD_PIXEL_ENTRY)];
  951. if(m_BadNewPixelMap == NULL) {
  952. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Malloc of m_BadPixelMap failed.");
  953. status = FALSE;
  954. }
  955. }
  956. /* Data structures created OK and file is open, let's roll */
  957. if(status == TRUE) {
  958. line_num = 0;
  959. while((line_num < m_nHeight) && (status == TRUE)) {
  960. data_in = strtok(data_char+char_readcount, "\n");
  961. char_readcount += long(strlen( data_in )+1);
  962. if(data_in!= NULL) {
  963. // Each line in bad_pixel.map begins with "linenum,num_entries: "
  964. if(sscanf(data_in, "%d,%d: ", &file_line_num, &num_entries) == 2) {
  965. // Verify the line number read matches the line num we are on
  966. if(line_num != file_line_num) {
  967. status = FALSE;
  968. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Pixel map read error for col %d.", line_num);
  969. }
  970. // Verify that num_entries is not larger than pixels-per-line
  971. if(num_entries > m_nWidth) {
  972. status = FALSE;
  973. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Pixel map read error for col %d; "
  974. // "num_entries (%d) > line_size (%d)", line_num, num_entries, m_nWidth);
  975. }
  976. /* Tokenize the header */
  977. p_token = strtok(data_in, ":");
  978. if(status == TRUE) {
  979. /* Create the correct size bad pixel map entry */
  980. //(m_BadPixelMap)[line_num].bad_pixel_num = (long *)malloc((num_entries + 1) * sizeof(long));
  981. m_BadNewPixelMap[line_num].bad_pixel_num = new long [(num_entries+1)*sizeof(long)];
  982. if( (m_BadNewPixelMap)[line_num].bad_pixel_num == NULL) {
  983. status = FALSE;
  984. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Malloc of p_bad_pixels for line %d failed.", line_num);
  985. }
  986. else {
  987. memset( (m_BadNewPixelMap)[line_num].bad_pixel_num, 0, (num_entries + 1) * sizeof(long));
  988. p_bad_pixels = (m_BadNewPixelMap)[line_num].bad_pixel_num;
  989. (m_BadNewPixelMap)[line_num].num_entries = num_entries;
  990. /* Fill in the array */
  991. for(entry = 0 ; entry < num_entries ; ++entry) {
  992. /*
  993. * The remainder of each line of the Bad Pixel File consists
  994. * of numbers indicating the x-coordinate value of the bad
  995. * pixels. They should be in ascending order. None of them
  996. * should be greater than capture_config.line_size.
  997. */
  998. p_token = strtok(NULL, ",");
  999. if((p_token == NULL) ||
  1000. (sscanf(p_token, "%ld,", &bad_pixel_num) != 1)) {
  1001. status = FALSE;
  1002. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Read failure on pixel entry %d.", entry);
  1003. }
  1004. else {
  1005. if(bad_pixel_num > m_nWidth) {
  1006. status = FALSE;
  1007. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Bad value (%ld) on pixel entry %d "
  1008. // "exceeds line_size (%d)",
  1009. // bad_pixel_num, entry, m_nWidth);
  1010. }
  1011. else {
  1012. p_bad_pixels[entry] = bad_pixel_num;
  1013. num_bad_pixels++;
  1014. }
  1015. }
  1016. }
  1017. }
  1018. }
  1019. line_num++;
  1020. }
  1021. /* Not a bad pixel entry; This is a bad line in the file */
  1022. else {
  1023. if(strstr(data_in, "synthseam"))
  1024. continue;
  1025. else
  1026. status = FALSE;
  1027. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Read of entry for line_num %d failed", line_num );
  1028. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, " Line_num %d is: <%s>", line_num, data_in );
  1029. }
  1030. }
  1031. else {
  1032. // file read error here or end of file.
  1033. break;
  1034. }
  1035. }
  1036. }
  1037. if(status == TRUE) {
  1038. if(num_bad_pixels > MAX_BAD_PIXELS) {
  1039. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Bad pixel map exceeds limit (%ld) : %ld pixels",
  1040. // MAX_BAD_PIXELS, num_bad_pixels);
  1041. }
  1042. }
  1043. if(status != TRUE) {
  1044. FreeBadNewPixelMap();
  1045. }
  1046. if(!bNew)
  1047. {
  1048. if(m_BadPixelMap != NULL) {
  1049. FreeBadPixelMap();
  1050. }
  1051. m_BadPixelMap = m_BadNewPixelMap;
  1052. m_BadNewPixelMap = NULL;
  1053. }
  1054. return status;
  1055. }
  1056. bool CPixMatrix::SaveBadPixelMap(const char *fileName)
  1057. {
  1058. FILE *fp;
  1059. char *data_out = NULL;
  1060. long char_count = 0;
  1061. bool status = TRUE;
  1062. long line;
  1063. long bad_pix_num;
  1064. long bad_num;
  1065. long pix_num;
  1066. long *p_bad_pix_num;
  1067. long char_length;
  1068. data_out = new char [MAXI_BADPIX_COUNT*2];
  1069. for(line = 0 ; line < m_nHeight; ++line) {
  1070. char_length = sprintf(data_out+char_count,"%d,%d:",line,m_BadPixelMap[line].num_entries);
  1071. char_count += char_length;
  1072. p_bad_pix_num = m_BadPixelMap[line].bad_pixel_num;//
  1073. for(pix_num = 0; pix_num < m_BadPixelMap[line].num_entries; ++pix_num) {
  1074. bad_pix_num = *p_bad_pix_num++;
  1075. bad_num = bad_pix_num & OFFSET_MASK;
  1076. char_length = sprintf(data_out+char_count,"%d,",bad_num);
  1077. char_count += char_length;
  1078. }
  1079. char_length = sprintf(data_out+char_count,"\n");
  1080. char_count += char_length;
  1081. }
  1082. fp = fopen(fileName, "wb");
  1083. if (fwrite(data_out,1,char_count,fp)!=char_count){
  1084. status = FALSE;
  1085. }
  1086. fclose(fp);
  1087. if(m_BadPixelMap == NULL)
  1088. {
  1089. status = FALSE;
  1090. }
  1091. else
  1092. {
  1093. //////////////////////////////////////////坏线自动识别
  1094. BadLineRecognize();
  1095. }
  1096. if(data_out != NULL) {
  1097. delete [] data_out;
  1098. }
  1099. return status;
  1100. }
  1101. int CPixMatrix::BadGridLineCorrect1( unsigned short *pImage, int nLineDirection, int nEntries, int nStartPoint, int nEndPoint, int nWidth, int nHeight)
  1102. {
  1103. if( !pImage || nEntries < m_nHOffset || nEntries > nHeight - m_nHOffset )
  1104. return -1;
  1105. memcpy( m_TempImage, pImage,sizeof(unsigned short) * m_nWidth * m_nHeight );
  1106. if( nEntries -2 < m_nHOffset)
  1107. {
  1108. for( int i = nStartPoint; i < nEndPoint; i++ )
  1109. {
  1110. pImage[ (nEntries) * nWidth + i] = m_TempImage[( nEntries +2 ) * nWidth + i] ;
  1111. }
  1112. }
  1113. else if( nEntries + 2 > nHeight - m_nHOffset )
  1114. {
  1115. for( int i = nStartPoint; i < nEndPoint; i++ )
  1116. {
  1117. pImage[ (nEntries) * nWidth + i] = m_TempImage[( nEntries -2 ) * nWidth + i] ;
  1118. }
  1119. }
  1120. else
  1121. {
  1122. for( int i = nStartPoint; i < nEndPoint; i++ )
  1123. {
  1124. pImage[ (nEntries) * nWidth + i] = 0.5*( m_TempImage[( nEntries +2 ) * nWidth + i] + m_TempImage[( nEntries -2 ) * nWidth + i] );
  1125. }
  1126. }
  1127. return 0;
  1128. }
  1129. //¨¨£¤??è¨???????ê??|ì??3????D?ê?y?ê?¨o¨2??à?|ì???ê?zhaoyiru,2017.05.09
  1130. int CPixMatrix::BadGridLineCorrect2( unsigned short *pImage, int nLineDirection, int nEntries, int nStartPoint, int nEndPoint, int nWidth, int nHeight)//¨o¨2??à
  1131. {
  1132. if( !pImage || nEntries < m_nWOffset || nEntries > nWidth - m_nWOffset )
  1133. return -1;
  1134. memcpy( m_TempImage, pImage,sizeof(unsigned short) * m_nWidth * m_nHeight );
  1135. if( nEntries -2 <= m_nWOffset)
  1136. {
  1137. for( int i = nStartPoint; i < nEndPoint; i++ )
  1138. {
  1139. pImage[i * nWidth + nEntries] =m_TempImage[( i ) * nWidth + nEntries + 2];
  1140. }
  1141. }
  1142. else if( nEntries + 2 > nHeight - m_nWOffset )
  1143. {
  1144. for( int i = nStartPoint; i < nEndPoint; i++ )
  1145. {
  1146. pImage[i * nWidth + nEntries] =m_TempImage[( i ) * nWidth + nEntries - 2];
  1147. }
  1148. }
  1149. else
  1150. {
  1151. for( int i = nStartPoint; i < nEndPoint; i++ )
  1152. {
  1153. pImage[i * nWidth + nEntries] = 0.5*( m_TempImage[( i ) * nWidth + nEntries - 2] + m_TempImage[( i ) * nWidth + nEntries + 2 ] );
  1154. }
  1155. }
  1156. return 0;
  1157. }
  1158. //¨¨£¤??è¨???o¨??ê??|ì???t???D?ê?y?ê??????|ì???ê? zhaoyiru,2017.05.09
  1159. int CPixMatrix::BadGridLineCorrect3( unsigned short *pImage, int nLineDirection, int nEntries, int nStartPoint, int nEndPoint, int nWidth, int nHeight)
  1160. {
  1161. if( !pImage || nEntries < m_nHOffset+2 || nEntries > nHeight -m_nHOffset-3 )
  1162. return -1;
  1163. if ( nStartPoint < m_nWOffset + 2 )
  1164. nStartPoint = m_nWOffset + 2;
  1165. if ( nEndPoint > m_nWOffset -3)
  1166. nEndPoint = nWidth- m_nWOffset -3;
  1167. memcpy( m_TempImage, pImage,sizeof(unsigned short) * m_nWidth * m_nHeight );
  1168. const float coef1=0.25f;
  1169. const float coef2=0.5f;//0.67f;
  1170. const float coef3=0.5f;//0.33f;
  1171. const float coef4=1.0f;
  1172. const float coef5=2.0f;
  1173. const float coef6=1.0f;
  1174. for( int i = nStartPoint; i < nEndPoint; i++ )
  1175. {
  1176. int nA,nB,nC,nA1, nB1,nC1;
  1177. nA1 = coef1*(m_TempImage[(nEntries-1)*m_nWidth + i-2]*coef4 + m_TempImage[(nEntries-1)*m_nWidth + i-1]*coef5 + m_TempImage[(nEntries-1)*m_nWidth + i]*coef6 -
  1178. m_TempImage[(nEntries+1)*m_nWidth + i]*coef4 - m_TempImage[(nEntries+1)*m_nWidth + i+1]*coef5 - m_TempImage[(nEntries+1)*m_nWidth + i +2]*coef6);
  1179. nB1 = coef1*(m_TempImage[(nEntries-1)*m_nWidth + i-1 ]*coef4 + m_TempImage[(nEntries-1)*m_nWidth + i]*coef5 + m_TempImage[(nEntries-1)*m_nWidth + i+1]*coef6-
  1180. m_TempImage[(nEntries+1)*m_nWidth + i-1 ]*coef4 - m_TempImage[(nEntries+1)*m_nWidth + i]*coef5 - m_TempImage[(nEntries+1)*m_nWidth + i+1]*coef6);
  1181. nC1 = coef1*(m_TempImage[(nEntries-1)*m_nWidth + i]*coef4 + m_TempImage[(nEntries-1)*m_nWidth + i+1]*coef5 + m_TempImage[(nEntries-1)*m_nWidth + i +2]*coef6 -
  1182. m_TempImage[(nEntries+1)*m_nWidth + i-2]*coef4 - m_TempImage[(nEntries+1)*m_nWidth + i-1]*coef5 - m_TempImage[(nEntries+1)*m_nWidth + i]*coef6);
  1183. nA = nA1*nA1;
  1184. nB = nB1*nB1;
  1185. nC = nC1*nC1;
  1186. if( nA <= nB && nA<= nC )
  1187. {
  1188. pImage[(nEntries)*m_nWidth + i] = coef1*coef2*(m_TempImage[(nEntries-1)*m_nWidth + i-2]*coef4 + m_TempImage[(nEntries-1)*m_nWidth + i-1]*coef5 + m_TempImage[(nEntries-1)*m_nWidth + i]*coef6)+
  1189. coef1*coef3*(m_TempImage[(nEntries+1)*m_nWidth + i]*coef4 + m_TempImage[(nEntries+1)*m_nWidth + i+1]*coef5 +m_TempImage[(nEntries+1)*m_nWidth + i +2]*coef6);
  1190. }
  1191. else if( nB<= nC )
  1192. {
  1193. pImage[(nEntries)*m_nWidth + i] = coef1*coef2*(m_TempImage[(nEntries-1)*m_nWidth + i-1 ]*coef4 + m_TempImage[(nEntries-1)*m_nWidth + i]*coef5 + m_TempImage[(nEntries-1)*m_nWidth + i+1]*coef6)+
  1194. coef1*coef3*( m_TempImage[(nEntries+1)*m_nWidth + i-1 ]*coef4 + m_TempImage[(nEntries+1)*m_nWidth + i]*coef5 + m_TempImage[(nEntries+1)*m_nWidth + i+1]*coef6);
  1195. }
  1196. else
  1197. {
  1198. pImage[(nEntries)*m_nWidth + i] = coef1*coef2*(m_TempImage[(nEntries-1)*m_nWidth + i]*coef4 + m_TempImage[(nEntries-1)*m_nWidth + i+1]*coef5 + m_TempImage[(nEntries-1)*m_nWidth + i +2]*coef6)+
  1199. coef1*coef3*(m_TempImage[(nEntries+1)*m_nWidth + i-2]*coef4 + m_TempImage[(nEntries+1)*m_nWidth + i-1]*coef5 + m_TempImage[(nEntries+1)*m_nWidth + i]*coef6);
  1200. }
  1201. }
  1202. return 0;
  1203. }
  1204. //¨¨£¤??è¨???o¨??ê??|ì???t???D?ê?y?ê?¨o¨2??à?|ì???ê?zhaoyiru,2017.05.09
  1205. int CPixMatrix::BadGridLineCorrect4( unsigned short *pImage, int nLineDirection, int nEntries, int nStartPoint, int nEndPoint, int nWidth, int nHeight)
  1206. {
  1207. if( !pImage || nEntries < m_nWOffset+3 || nEntries > nHeight -m_nWOffset-4 )
  1208. return -1;
  1209. if ( nStartPoint < m_nHOffset + 3 )
  1210. nStartPoint = m_nHOffset + 3;
  1211. if ( nEndPoint > m_nHOffset -4)
  1212. nEndPoint = nHeight-m_nHOffset -4;
  1213. memcpy( m_TempImage, pImage,sizeof(unsigned short) * m_nWidth * m_nHeight );
  1214. const float coef1=0.25f;
  1215. const float coef2=0.5f;//0.67f;
  1216. const float coef3=0.5f;//0.33f;
  1217. const float coef4=1.0f;
  1218. const float coef5=2.0f;
  1219. const float coef6=1.0f;
  1220. for( int i = nStartPoint; i < nEndPoint; i++ )
  1221. {
  1222. int nA,nB,nC,nA1, nB1,nC1;
  1223. nA1 = coef1*(m_TempImage[(i-2)*m_nWidth + nEntries-1]*coef4 + m_TempImage[(i-1)*m_nWidth + nEntries-1]*coef5 + m_TempImage[(i)*m_nWidth + nEntries-1]*coef6 -
  1224. m_TempImage[(i)*m_nWidth + nEntries+1]*coef4 - m_TempImage[(i+1)*m_nWidth + nEntries+1]*coef5 - m_TempImage[(i+2)*m_nWidth + nEntries +1]*coef6);
  1225. nB1 = coef1*(m_TempImage[(i-1)*m_nWidth + nEntries-1 ]*coef4 + m_TempImage[(i)*m_nWidth + nEntries-1]*coef5 + m_TempImage[(i+1)*m_nWidth + nEntries-1]*coef6-
  1226. m_TempImage[(i-1)*m_nWidth + nEntries+1 ]*coef4 - m_TempImage[(i)*m_nWidth + nEntries+1]*coef5 - m_TempImage[(i+1)*m_nWidth + nEntries+1]*coef6);
  1227. nC1 = coef1*(m_TempImage[(i)*m_nWidth + nEntries-1]*coef4 + m_TempImage[(i+1)*m_nWidth + nEntries-1]*coef5 + m_TempImage[(i+2)*m_nWidth + nEntries-1]*coef6 -
  1228. m_TempImage[(i-2)*m_nWidth + nEntries+1 ]*coef4 - m_TempImage[(i-1)*m_nWidth + nEntries+1]*coef5 - m_TempImage[(i)*m_nWidth + nEntries+1]*coef6);
  1229. nA = nA1*nA1;
  1230. nB = nB1*nB1;
  1231. nC = nC1*nC1;
  1232. if( nA <= nB && nA<= nC )
  1233. {
  1234. pImage[(i)*m_nWidth + nEntries] = coef1*coef2*(m_TempImage[(i-2)*m_nWidth + nEntries-1]*coef4 + m_TempImage[(i-1)*m_nWidth + nEntries-1]*coef5 + m_TempImage[(i)*m_nWidth + nEntries-1]*coef6)+
  1235. coef1*coef3*(m_TempImage[(i)*m_nWidth + nEntries+1]*coef4 + m_TempImage[(i+1)*m_nWidth + nEntries+1]*coef5 + m_TempImage[(i+2)*m_nWidth + nEntries +1]*coef6);
  1236. }
  1237. else if( nB<= nC )
  1238. {
  1239. pImage[(i)*m_nWidth + nEntries] = coef1*coef2*(m_TempImage[(i-1)*m_nWidth + nEntries-1 ]*coef4 + m_TempImage[(i)*m_nWidth + nEntries-1]*coef5 + m_TempImage[(i+1)*m_nWidth + nEntries-1]*coef6)+
  1240. coef1*coef3*(m_TempImage[(i-1)*m_nWidth + nEntries+1 ]*coef4 + m_TempImage[(i)*m_nWidth + nEntries+1]*coef5 + m_TempImage[(i+1)*m_nWidth + nEntries+1]*coef6 );
  1241. }
  1242. else
  1243. {
  1244. pImage[(i)*m_nWidth + nEntries] = coef1*coef2*(m_TempImage[(i)*m_nWidth + nEntries-1]*coef4 + m_TempImage[(i+1)*m_nWidth + nEntries-1]*coef5 + m_TempImage[(i+2)*m_nWidth + nEntries-1]*coef6)+
  1245. coef1*coef3*(m_TempImage[(i-2)*m_nWidth + nEntries+1 ]*coef4 + m_TempImage[(i-2)*m_nWidth + nEntries+1]*coef5 + m_TempImage[(i)*m_nWidth + nEntries+1]*coef6);
  1246. }
  1247. }
  1248. return 0;
  1249. }
  1250. void CPixMatrix::BadLineRecognize()
  1251. {
  1252. //////////////////////////////////////////坏线自动识别
  1253. long BadPixNum;
  1254. long BadNum;
  1255. memset( m_TempImage, 0, sizeof(unsigned short) * m_nWidth * m_nHeight );
  1256. memset( m_TempImage1, 0, sizeof(unsigned short) * m_nWidth * m_nHeight );
  1257. memset( m_datalen, -1, sizeof(int) * (int( (MAXI_BADPIX_COUNT - MAX_PIX_MAP_LINE) / m_nLinePoint)) );
  1258. for ( int i = m_nHOffset + 2; i < m_nHeight - m_nHOffset - 2; i++ )
  1259. {
  1260. m_pBadPixNum = m_BadPixelMap[ i ].bad_pixel_num;
  1261. if ( m_BadPixelMap[ i ].num_entries > 0 )
  1262. {
  1263. for ( int j = 0; j < m_BadPixelMap[ i ].num_entries; j++ )
  1264. {
  1265. BadPixNum = *m_pBadPixNum++;
  1266. BadNum = BadPixNum & OFFSET_MASK;
  1267. if ( BadNum > 1 && BadNum < m_nWidth - 1 )
  1268. m_TempImage[ i * m_nWidth + BadNum ] = 65535;
  1269. }
  1270. }
  1271. }
  1272. /////////////////////按行标记坏线,水平坏线
  1273. //char *data_out = NULL;改为m_datalen;
  1274. int linedirection;
  1275. //m_datalen = new char [ 40000 ];
  1276. int count = 0;
  1277. for ( int i = m_nHOffset; i < m_nHeight - m_nHOffset; i++ )
  1278. {
  1279. for ( int j = m_nWOffset + 2; j < m_nWidth - m_nWOffset - 2; j++ )
  1280. {
  1281. m_TempImage1[ i * m_nWidth + j ] = ( m_TempImage[ i * m_nWidth + j - 2 ] + m_TempImage[ i * m_nWidth + j - 1 ] + m_TempImage[ i * m_nWidth + j ] + m_TempImage[ i * m_nWidth + j + 1 ] + m_TempImage[ i * m_nWidth + j + 2 ] ) / 5;
  1282. if ( m_TempImage1[ i * m_nWidth + j ] < 40000 )
  1283. m_TempImage1[ i * m_nWidth + j ] = 0;
  1284. else
  1285. m_TempImage1[ i * m_nWidth + j ] = 65535;
  1286. }
  1287. int tempcount = 0;
  1288. for ( int p = m_nWOffset; p < m_nWidth - m_nWOffset; p++ )
  1289. {
  1290. if ( m_TempImage1[ i * m_nWidth + p ] == 65535 )
  1291. tempcount++;
  1292. }
  1293. int minX = 0;
  1294. int maxX = 0;
  1295. if ( tempcount > m_nLinePoint )//add by chen 2015-1-13 judge condition change from 300 to 50
  1296. {
  1297. for ( int k = m_nWOffset; k < m_nWidth - m_nWOffset; k++ )
  1298. {
  1299. if ( m_TempImage1[ i * m_nWidth + k ] == 65535 )
  1300. {
  1301. minX = k;
  1302. break;
  1303. }
  1304. }
  1305. for ( int k1 = m_nWidth - m_nWOffset - 1; k1 > m_nWOffset; k1-- )
  1306. {
  1307. if ( m_TempImage1[ i * m_nWidth + k1 ] == 65535 )
  1308. {
  1309. maxX = k1;
  1310. break;
  1311. }
  1312. }
  1313. linedirection = 0;
  1314. //char_length = sprintf( m_datalen + char_count, "%d,%d:%d,%d", linedirection, i, minX - 2, maxX + 2 );
  1315. //char_count += char_length;
  1316. //char_length = sprintf( m_datalen + char_count, "\n" );
  1317. //char_count += char_length;
  1318. m_datalen[ count ] = linedirection;
  1319. m_datalen[ count + 1 ] = i;
  1320. m_datalen[ count + 2 ] = minX - 2;
  1321. m_datalen[ count + 3 ] = maxX + 2;
  1322. count += 4;
  1323. }
  1324. }
  1325. ////////////////////////////按列标记坏线--竖直坏线
  1326. memset( m_TempImage1, 0, sizeof(unsigned short) * m_nWidth * m_nHeight );
  1327. for ( int i = m_nWOffset; i < m_nWidth - m_nWOffset; i++ )
  1328. {
  1329. for ( int j = m_nHOffset + 2; j < m_nHeight - m_nHOffset - 2; j++ )
  1330. {
  1331. m_TempImage1[ j * m_nWidth + i ] = ( m_TempImage[ ( j - 2 ) * m_nWidth + i ] + m_TempImage[ ( j - 1 ) * m_nWidth + i ] + m_TempImage[ j * m_nWidth + i ] + m_TempImage[ ( j + 1 ) * m_nWidth + i ] + m_TempImage[ ( j + 2 ) * m_nWidth + i ] ) / 5;
  1332. if ( m_TempImage1[ j * m_nWidth + i ] < 40000 )
  1333. m_TempImage1[ j * m_nWidth + i ] = 0;
  1334. else
  1335. m_TempImage1[ j * m_nWidth + i ] = 65535;
  1336. }
  1337. int tempcount = 0;
  1338. for ( int p = m_nHOffset; p < m_nHeight - m_nHOffset; p++ )
  1339. {
  1340. if ( m_TempImage1[ p * m_nWidth + i ] == 65535 )
  1341. tempcount++;
  1342. }
  1343. int minY = 0;
  1344. int maxY = 0;
  1345. if ( tempcount > m_nLinePoint )//add by chen 2015-1-13 judge condition change from 300 to 50
  1346. {
  1347. for ( int k = m_nHOffset; k < m_nHeight - m_nHOffset; k++ )
  1348. {
  1349. if ( m_TempImage1[ k * m_nWidth + i ] == 65535 )
  1350. {
  1351. minY = k;
  1352. break;
  1353. }
  1354. }
  1355. for ( int k1 = m_nHeight - m_nHOffset - 1; k1 > m_nHOffset; k1-- )
  1356. {
  1357. if ( m_TempImage1[ k1 * m_nWidth + i ] == 65535 )
  1358. {
  1359. maxY = k1;
  1360. break;
  1361. }
  1362. }
  1363. linedirection = 1;
  1364. //char_length = sprintf( m_datalen + char_count, "%d,%d:%d,%d", linedirection, i, minY - 2, maxY + 2 );
  1365. //char_count += char_length;
  1366. //char_length = sprintf( m_datalen + char_count, "\n" );
  1367. //char_count += char_length;
  1368. m_datalen[ count ] = linedirection;
  1369. m_datalen[ count + 1 ] = i;
  1370. m_datalen[ count + 2 ] = minY - 2;
  1371. m_datalen[ count + 3 ] = maxY + 2;
  1372. count += 4;
  1373. }
  1374. }
  1375. }
  1376. ///////////////////生成坏线标记文件,by chen G N 2013 - 01 - 16
  1377. int CPixMatrix::SaveBadLineMap(const char *fileName)
  1378. {
  1379. ////////////////////////////新方法-基于二值图,返回值: -2 坏点文件未载入;-1 坏线写失败;0 无坏线;1有坏线
  1380. if(m_BadPixelMap == NULL)
  1381. {
  1382. return -2;
  1383. }
  1384. long BadPixNum;
  1385. long BadNum;
  1386. //////////////////////////标记坏点
  1387. //if ( nWidth != m_nWidth && nHeight != m_nHeight )
  1388. //{
  1389. // unsigned short *m_TempImage = new unsigned short [ m_nWidth * m_nHeight ];
  1390. // unsigned short *m_TempImage1 = new unsigned short [ m_nWidth * m_nHeight ];
  1391. // nWidth = m_nWidth;
  1392. // nHeight = m_nHeight;
  1393. //}
  1394. memset( m_TempImage, 0, sizeof(unsigned short) * m_nWidth * m_nHeight );
  1395. memset( m_TempImage1, 0, sizeof(unsigned short) * m_nWidth * m_nHeight );
  1396. for ( int i = 2; i < m_nHeight - 2; i++ )
  1397. {
  1398. m_pBadPixNum = m_BadPixelMap[ i ].bad_pixel_num;
  1399. if ( m_BadPixelMap[ i ].num_entries > 0 )
  1400. {
  1401. for ( int j = 0; j < m_BadPixelMap[ i ].num_entries; j++ )
  1402. {
  1403. BadPixNum = *m_pBadPixNum++;
  1404. BadNum = BadPixNum & OFFSET_MASK;
  1405. if ( BadNum > 1 && BadNum < m_nWidth - 1 )
  1406. m_TempImage[ i * m_nWidth + BadNum ] = 65535;
  1407. }
  1408. }
  1409. }
  1410. /////////////////////按行标记坏线,水平坏线
  1411. char *data_out = NULL;
  1412. long char_count = 0;
  1413. long char_length;
  1414. int linedirection;
  1415. data_out = new char [ 40000 ];
  1416. for ( int i = 0; i < m_nHeight; i++ )
  1417. {
  1418. for ( int j = 2; j < m_nWidth - 2; j++ )
  1419. {
  1420. m_TempImage1[ i * m_nWidth + j ] = ( m_TempImage[ i * m_nWidth + j - 2 ] + m_TempImage[ i * m_nWidth + j - 1 ] + m_TempImage[ i * m_nWidth + j ] + m_TempImage[ i * m_nWidth + j + 1 ] + m_TempImage[ i * m_nWidth + j + 2 ] ) / 5;
  1421. if ( m_TempImage1[ i * m_nWidth + j ] < 40000 )
  1422. m_TempImage1[ i * m_nWidth + j ] = 0;
  1423. else
  1424. m_TempImage1[ j * m_nWidth + i ] = 65535;
  1425. }
  1426. int tempcount = 0;
  1427. for ( int p = 0; p < m_nWidth; p++ )
  1428. {
  1429. if ( m_TempImage1[ i * m_nWidth + p ] == 65535 )
  1430. tempcount++;
  1431. }
  1432. int minX = 0;
  1433. int maxX = 0;
  1434. if ( tempcount > 500 )
  1435. {
  1436. for ( int k = 0; k < m_nWidth; k++ )
  1437. {
  1438. if ( m_TempImage1[ i * m_nWidth + k ] == 65535 )
  1439. {
  1440. minX = k;
  1441. break;
  1442. }
  1443. }
  1444. for ( int k1 = m_nWidth - 1; k1 > 0; k1-- )
  1445. {
  1446. if ( m_TempImage1[ i * m_nWidth + k1 ] == 65535 )
  1447. {
  1448. maxX = k1;
  1449. break;
  1450. }
  1451. }
  1452. linedirection = 0;
  1453. char_length = sprintf( data_out + char_count, "%d,%d:%d,%d", linedirection, i, minX - 2, maxX + 2 );
  1454. char_count += char_length;
  1455. char_length = sprintf( data_out + char_count, "\n" );
  1456. char_count += char_length;
  1457. }
  1458. }
  1459. ////////////////////////////按列标记坏线--竖直坏线
  1460. memset( m_TempImage1, 0, sizeof(unsigned short) * m_nWidth * m_nHeight );
  1461. for ( int i = 0; i < m_nWidth; i++ )
  1462. {
  1463. for ( int j = 2; j < m_nHeight - 2; j++ )
  1464. {
  1465. m_TempImage1[ j * m_nWidth + i ] = ( m_TempImage[ ( j - 2 ) * m_nWidth + i ] + m_TempImage[ ( j - 1 ) * m_nWidth + i ] + m_TempImage[ j * m_nWidth + i ] + m_TempImage[ ( j + 1 ) * m_nWidth + i ] + m_TempImage[ ( j + 2 ) * m_nWidth + i ] ) / 5;
  1466. if ( m_TempImage1[ j * m_nWidth + i ] < 40000 )
  1467. m_TempImage1[ j * m_nWidth + i ] = 0;
  1468. else
  1469. m_TempImage1[ j * m_nWidth + i ] = 65535;
  1470. }
  1471. int tempcount = 0;
  1472. for ( int p = 0; p < m_nHeight; p++ )
  1473. {
  1474. if ( m_TempImage1[ p * m_nWidth + i ] == 65535 )
  1475. tempcount++;
  1476. }
  1477. int minY = 0;
  1478. int maxY = 0;
  1479. if ( tempcount > 500 )
  1480. {
  1481. for ( int k = 0; k < m_nHeight; k++ )
  1482. {
  1483. if ( m_TempImage1[ k * m_nWidth + i ] == 65535 )
  1484. {
  1485. minY = k;
  1486. break;
  1487. }
  1488. }
  1489. for ( int k1 = m_nHeight - 1; k1 > 0; k1-- )
  1490. {
  1491. if ( m_TempImage1[ k1 * m_nWidth + i ] == 65535 )
  1492. {
  1493. maxY = k1;
  1494. break;
  1495. }
  1496. }
  1497. linedirection = 1;
  1498. char_length = sprintf( data_out + char_count, "%d,%d:%d,%d", linedirection, i, minY - 2, maxY + 2 );
  1499. char_count += char_length;
  1500. char_length = sprintf( data_out + char_count, "\n" );
  1501. char_count += char_length;
  1502. }
  1503. }
  1504. ///////////////////////写坏线坐标/////////////////////////////
  1505. if ( char_count == 0 )
  1506. {
  1507. return 0;
  1508. }
  1509. else
  1510. {
  1511. FILE *fp;
  1512. fp = fopen(fileName, "wb");
  1513. if(m_BadPixelMap == NULL)
  1514. {
  1515. return -2;
  1516. }
  1517. if (fwrite(data_out,1,char_count,fp)!=char_count)
  1518. {
  1519. return -1;
  1520. }
  1521. fclose(fp);
  1522. }
  1523. if( data_out != NULL )
  1524. {
  1525. delete [] data_out;
  1526. data_out = NULL;
  1527. }
  1528. //if( m_TempImage != NULL )
  1529. //{
  1530. // delete [] m_TempImage;
  1531. // m_TempImage = NULL;
  1532. //}
  1533. //if( m_TempImage1 != NULL )
  1534. //{
  1535. // delete [] m_TempImage1;
  1536. // m_TempImage1 = NULL;
  1537. //}
  1538. return 1;
  1539. }
  1540. //////////////解决去栅影后的坏线振铃效应,返回-2为异常,返回1为正常去除坏线/////////////////////////////////////////
  1541. int CPixMatrix::LoadandCorrectBadLine( unsigned short *pImage, int GridDirection )
  1542. {
  1543. //FILE *fp;
  1544. int line_direction;
  1545. int line_entries;
  1546. int startpoint;
  1547. int endpoint;
  1548. //int char_count = 0;
  1549. //int char_length = 0;
  1550. int count = 0;
  1551. //char *data_in = NULL;
  1552. //data_in = new char [MAX_PIX_MAP_LINE];
  1553. if( m_datalen == NULL)
  1554. {
  1555. return -2;
  1556. }
  1557. //fp = fopen(fileName, "rt");
  1558. //if(fp == NULL)
  1559. //{
  1560. // return -2;
  1561. //}
  1562. // while( (m_datalen + char_count)!= NULL)
  1563. // {
  1564. //while( sscanf( m_datalen + char_count, "%d,%d:%d,%d", &line_direction, &line_entries, &startpoint, &endpoint ) == 4 )
  1565. //while( (m_datalen[ count ] != -1) && (count < 400) )
  1566. while( (m_datalen[ count ] != -1) && (count < m_nLineData) ) //???acoutD?óú×?′ó?μ????êy,zhaoyiru, 2017.05.09
  1567. {
  1568. line_direction = m_datalen[ count ] ;
  1569. line_entries = m_datalen[ count + 1 ];
  1570. startpoint = m_datalen[ count + 2 ];
  1571. endpoint = m_datalen[ count + 3 ];
  1572. count = count + 4;
  1573. if( line_direction == 0 && line_entries > 2 && line_entries < m_nHeight - 2 && GridDirection == 2 )//////////////////////去除水平坏线
  1574. {
  1575. int nTGray = 0, nBGray = 0, nGray = 0;
  1576. int nTDetails = 0, nBDetails = 0, nTAvg = 0, nBAvg = 0;
  1577. if ( startpoint < m_nWOffset + 2 )
  1578. startpoint = m_nWOffset + 2;
  1579. if ( endpoint > m_nWidth - m_nWOffset - 2 )
  1580. endpoint = m_nWidth - m_nWOffset - 2;
  1581. for ( int i = startpoint; i < endpoint; i++ )
  1582. {
  1583. nTAvg = 0;
  1584. for ( int j = -2; j < 3; j++ )
  1585. {
  1586. nTAvg += pImage[ ( line_entries - 1 ) * m_nWidth + i + j ];
  1587. }
  1588. nTAvg /= 5;
  1589. nTDetails = pImage[ ( line_entries - 1 ) * m_nWidth + i ] - nTAvg;
  1590. nTGray = 0.5 * pImage[ ( line_entries - 2 ) * m_nWidth + i ] + 0.25 * pImage[ ( line_entries - 2 ) * m_nWidth + i - 1 ] + 0.25 * pImage[ ( line_entries - 2 ) * m_nWidth + i + 1 ];
  1591. nBGray = 0.5 * pImage[ ( line_entries + 2 ) * m_nWidth + i ] + 0.25 * pImage[ ( line_entries + 2 ) * m_nWidth + i - 1 ] + 0.25 * pImage[ ( line_entries + 2 ) * m_nWidth + i + 1 ];
  1592. nGray = 0.75 * nTGray + 0.25 * nBGray;
  1593. //pImage[ ( line_entries - 1 ) * m_nWidth + i ] = nGray + nTDetails;
  1594. if ( (nGray + nTDetails) < 0 )
  1595. pImage[ ( line_entries - 1 ) * m_nWidth + i ] = 0;
  1596. else if ( (nGray + nTDetails) > 65535 )
  1597. pImage[ ( line_entries - 1 ) * m_nWidth + i ] = 65535;
  1598. else
  1599. pImage[ ( line_entries - 1 ) * m_nWidth + i ] = nGray + nTDetails;
  1600. }
  1601. for ( int i = startpoint; i < endpoint; i++ )
  1602. {
  1603. nBAvg = 0;
  1604. for ( int j = -2; j < 3; j++ )
  1605. {
  1606. nBAvg += pImage[ ( line_entries + 1 ) * m_nWidth + i + j ];
  1607. }
  1608. nBAvg /= 5;
  1609. nBDetails = pImage[ ( line_entries + 1 ) * m_nWidth + i ] - nBAvg;
  1610. nTGray = 0.5 * pImage[ ( line_entries - 2 ) * m_nWidth + i ] + 0.25 * pImage[ ( line_entries - 2 ) * m_nWidth + i - 1 ] + 0.25 * pImage[ ( line_entries - 2 ) * m_nWidth + i + 1 ];
  1611. nBGray = 0.5 * pImage[ ( line_entries + 2 ) * m_nWidth + i ] + 0.25 * pImage[ ( line_entries + 2 ) * m_nWidth + i - 1 ] + 0.25 * pImage[ ( line_entries + 2 ) * m_nWidth + i + 1 ];
  1612. nGray = 0.25 * nTGray + 0.75 * nBGray;
  1613. //pImage[ ( line_entries - 1 ) * m_nWidth + i ] = nGray + nBDetails;
  1614. if ( (nGray + nBDetails) < 0 )
  1615. pImage[ ( line_entries + 1 ) * m_nWidth + i ] = 0;
  1616. else if ( (nGray + nBDetails) > 65535 )
  1617. pImage[ ( line_entries + 1 ) * m_nWidth + i ] = 65535;
  1618. else
  1619. pImage[ ( line_entries + 1 ) * m_nWidth + i ] = nGray + nBDetails;
  1620. }
  1621. for ( int i = startpoint; i < endpoint; i++ )
  1622. {
  1623. pImage[ ( line_entries ) * m_nWidth + i ] = 0.25 * pImage[ ( line_entries + 1 ) * m_nWidth + i ] + 0.125 * pImage[ ( line_entries + 1 ) * m_nWidth + i - 1 ] + 0.125 * pImage[ ( line_entries + 1 ) * m_nWidth + i + 1 ] + 0.25 * pImage[ ( line_entries - 1 ) * m_nWidth + i ] + 0.125 * pImage[ ( line_entries - 1 ) * m_nWidth + i - 1 ] + 0.125 * pImage[ ( line_entries - 1 ) * m_nWidth + i + 1 ];
  1624. }
  1625. }
  1626. if( line_direction == 1 && line_entries > 2 && line_entries < m_nWidth - 2 && GridDirection == 1 )//////////////////////去除竖直坏线
  1627. {
  1628. int nLGray = 0, nRGray = 0, nGray = 0;
  1629. int nLDetails = 0, nRDetails = 0, nLAvg = 0, nRAvg = 0;
  1630. if ( startpoint < m_nHOffset + 2 )
  1631. startpoint = m_nHOffset + 2;
  1632. if ( endpoint > m_nHeight - m_nHOffset - 2 )
  1633. endpoint = m_nHeight - m_nHOffset - 2;
  1634. for ( int i = startpoint; i < endpoint; i++ )
  1635. {
  1636. nLAvg = 0;
  1637. for ( int j = -2; j < 3; j++ )
  1638. {
  1639. nLAvg += pImage[ ( i + j ) * m_nWidth + line_entries - 1 ];
  1640. }
  1641. nLAvg /= 5;
  1642. nLDetails = pImage[ i * m_nWidth + line_entries - 1 ] - nLAvg;
  1643. nLGray = 0.5 * pImage[ i * m_nWidth + line_entries - 2 ] + 0.25 * pImage[ ( i - 1 ) * m_nWidth + line_entries - 2 ] + 0.25 * pImage[ ( i + 1 ) * m_nWidth + line_entries - 2 ];
  1644. nRGray = 0.5 * pImage[ i * m_nWidth + line_entries + 2 ] + 0.25 * pImage[ ( i - 1 ) * m_nWidth + line_entries + 2 ] + 0.25 * pImage[ ( i + 1 ) * m_nWidth + line_entries + 2 ];
  1645. nGray = 0.75 * nLGray + 0.25 * nRGray;
  1646. //pImage[ i * m_nWidth + line_entries - 1 ] = nGray + nLDetails;
  1647. if ( (nGray + nLDetails) < 0 )
  1648. pImage[ i * m_nWidth + line_entries - 1 ] = 0;
  1649. else if ( (nGray + nLDetails) > 65535 )
  1650. pImage[ i * m_nWidth + line_entries - 1 ] = 65535;
  1651. else
  1652. pImage[ i * m_nWidth + line_entries - 1 ] = nGray + nLDetails;
  1653. }
  1654. for ( int i = startpoint; i < endpoint; i++ )
  1655. {
  1656. nRAvg = 0;
  1657. for ( int j = -2; j < 3; j++ )
  1658. {
  1659. nRAvg += pImage[ ( i + j ) * m_nWidth + line_entries + 1 ];
  1660. }
  1661. nRAvg /= 5;
  1662. nRDetails = pImage[ i * m_nWidth + line_entries + 1 ] - nRAvg;
  1663. nLGray = 0.5 * pImage[ i * m_nWidth + line_entries - 2 ] + 0.25 * pImage[ ( i - 1 ) * m_nWidth + line_entries - 2 ] + 0.25 * pImage[ ( i + 1 ) * m_nWidth + line_entries - 2 ];
  1664. nRGray = 0.5 * pImage[ i * m_nWidth + line_entries + 2 ] + 0.25 * pImage[ ( i - 1 ) * m_nWidth + line_entries + 2 ] + 0.25 * pImage[ ( i + 1 ) * m_nWidth + line_entries + 2 ];
  1665. nGray = 0.75 * nRGray + 0.25 * nLGray;
  1666. //pImage[ i * m_nWidth + line_entries - 1 ] = nGray + nRDetails;
  1667. if ( (nGray + nRDetails) < 0 )
  1668. pImage[ i * m_nWidth + line_entries + 1 ] = 0;
  1669. else if ( (nGray + nRDetails) > 65535 )
  1670. pImage[ i * m_nWidth + line_entries + 1 ] = 65535;
  1671. else
  1672. pImage[ i * m_nWidth + line_entries + 1 ] = nGray + nRDetails;
  1673. }
  1674. for ( int i = startpoint; i < endpoint; i++ )
  1675. {
  1676. pImage[ i * m_nWidth + line_entries ] = 0.25 * pImage[ i * m_nWidth + line_entries + 1 ] + 0.125 * pImage[ ( i - 1 ) * m_nWidth + line_entries + 1 ] + 0.125 * pImage[ ( i + 1 ) * m_nWidth + line_entries + 1 ] + 0.25 * pImage[ i * m_nWidth + line_entries - 1 ] + 0.125 * pImage[ ( i - 1 ) * m_nWidth + line_entries - 1 ] + 0.125 * pImage[ ( i + 1 ) * m_nWidth + line_entries - 1 ];
  1677. }
  1678. }
  1679. //char_count += 4;
  1680. //char_length = sprintf( m_datalen + char_count, "\n" );
  1681. //char_count += char_length;
  1682. }
  1683. // }
  1684. //if(fp != NULL)
  1685. //{
  1686. // fclose(fp);
  1687. //}
  1688. //if(data_in != NULL)
  1689. //{
  1690. // delete [] data_in;
  1691. //}
  1692. return 1;
  1693. }
  1694. bool CPixMatrix::AutoBadPixelMap(WORD *wImage)
  1695. {
  1696. char * m_charBadPixMap;
  1697. char * m_charLinePixMap;
  1698. m_charLinePixMap = new char [MAX_PIX_MAP_LINE];
  1699. m_charBadPixMap = new char [MAXI_BADPIX_COUNT];
  1700. BYTE *wImageWB = new BYTE [m_nWidth* m_nHeight];
  1701. int i,j; // for loop
  1702. DWORD Hist[16384];
  1703. DWORD count = 0;
  1704. DWORD tempsum = 0;
  1705. WORD lowvalue, highvalue;
  1706. lowvalue = highvalue = 0;
  1707. bool state = true;
  1708. for(i = 0; i< 16384; i++)
  1709. {
  1710. Hist[i] = 0;
  1711. }
  1712. //统计直方图
  1713. for(i = m_nHeight/3; i<m_nHeight*2/3; i++)
  1714. {
  1715. for(j = m_nWidth/3; j<m_nWidth*2/3; j++)
  1716. {
  1717. Hist[*(wImage+i*m_nWidth+j)]++;
  1718. count ++;
  1719. }
  1720. }
  1721. count = count/10;
  1722. for(i = 0 ; i< 16384; i++)
  1723. {
  1724. tempsum += Hist[i];
  1725. if (tempsum> count)
  1726. break;
  1727. }
  1728. lowvalue = max(i-1500, 100);
  1729. tempsum = 0;
  1730. for(i = m_nPixelMax ; i>0; i--)
  1731. {
  1732. tempsum += Hist[i];
  1733. if (tempsum> count)
  1734. break;
  1735. }
  1736. highvalue = min(i+1500, m_nPixelMax);
  1737. for(i = 0; i<m_nHeight; i++)
  1738. {
  1739. for(j = 0; j<m_nWidth; j++)
  1740. {
  1741. if((*(wImage+i*m_nWidth+j)>lowvalue)&&(*(wImage+i*m_nWidth+j)<highvalue))
  1742. {
  1743. *(wImageWB+i*m_nWidth+j) = 255;
  1744. }
  1745. else
  1746. {
  1747. *(wImageWB+i*m_nWidth+j) = 0;
  1748. }
  1749. }
  1750. }
  1751. //查找空边
  1752. int x1,x2,y1,y2;
  1753. x1 = m_nWOffset;
  1754. x2 = m_nWidth-m_nWOffset;
  1755. y1 = m_nHOffset;
  1756. y2 = m_nHeight-m_nHOffset;
  1757. /* for(i = 0; i<m_nHeight; i++)
  1758. {
  1759. if(!((*(wImageWB+m_nWidth*(m_nHeight-i-1)+m_nWidth/3)==0)&&(*(wImageWB+m_nWidth*(m_nHeight-i-1)+2*m_nWidth/3)==0)))
  1760. {
  1761. y1 = i;
  1762. break;
  1763. }
  1764. }
  1765. for(i = m_nHeight-1; i>=0; i--)
  1766. {
  1767. if(!((*(wImageWB+m_nWidth*(m_nHeight-i-1)+m_nWidth/3)==0)&&(*(wImageWB+m_nWidth*(m_nHeight-i-1)+2*m_nWidth/3)==0)))
  1768. {
  1769. y2 = i;
  1770. break;
  1771. }
  1772. }
  1773. for(j = 0; j<m_nWidth; j++)
  1774. {
  1775. if(!((*(wImageWB+m_nWidth*m_nHeight/3+j)==0)&&(*(wImageWB+2*m_nWidth*m_nHeight/3+j)==0)))
  1776. {
  1777. x1 = j;
  1778. break;
  1779. }
  1780. }
  1781. for(j = m_nWidth-1; j>=0; j--)
  1782. {
  1783. if(!((*(wImageWB+m_nWidth*m_nHeight/3+j)==0)&&(*(wImageWB+2*m_nWidth*m_nHeight/3+j)==0)))
  1784. {
  1785. x2 = j;
  1786. break;
  1787. }
  1788. }
  1789. */
  1790. long char_count;
  1791. long char_length;
  1792. long nPixcount;
  1793. nPixcount=char_count = char_length = 0;
  1794. // 每列
  1795. for(i = 0; i<y1; i++)
  1796. {
  1797. char_count += sprintf(m_charBadPixMap+char_count,"%d,0:\n",i);
  1798. }
  1799. for(i = y1; i<=y2; i++)
  1800. {
  1801. // 每行
  1802. nPixcount=char_length = 0;
  1803. for(j = x1; j <=x2; j++)
  1804. {
  1805. if(*(wImageWB+m_nWidth*i+j)==0){
  1806. //bad pix, add to charbuffer
  1807. char_length += sprintf(m_charLinePixMap+char_length,"%d,",j);
  1808. nPixcount++;
  1809. }
  1810. }
  1811. //写入buffer
  1812. if (char_count>(MAXI_BADPIX_COUNT-MAX_PIX_MAP_LINE)){
  1813. state = false;
  1814. // AfxMessageBox("Too Many bad point, maybe bad method to expose!");
  1815. //gfun_LogError("Too Many bad point, maybe bad method to expose!");
  1816. break;
  1817. }
  1818. char_count += sprintf(m_charBadPixMap+char_count,"%d,%d:",i,nPixcount);
  1819. memcpy(m_charBadPixMap+char_count,m_charLinePixMap,char_length);
  1820. char_count += char_length;
  1821. char_count += sprintf(m_charBadPixMap+char_count,"\n");
  1822. }
  1823. if(state)
  1824. {
  1825. for(i = y2+1; i<m_nHeight; i++)
  1826. {
  1827. char_count += sprintf(m_charBadPixMap+char_count,"%d,0:\n",i);
  1828. }
  1829. }
  1830. delete [] wImageWB;
  1831. if(state)
  1832. {
  1833. LoadBadPixelMapChar(m_charBadPixMap);
  1834. CombineBadPixelMap();
  1835. }
  1836. delete [] m_charLinePixMap;
  1837. delete [] m_charBadPixMap;
  1838. return state;
  1839. }
  1840. bool CPixMatrix::AutoBadPixelMap1(WORD *wImage)
  1841. {
  1842. int threshold1,threshold2;
  1843. char * m_charBadPixMap;
  1844. char * m_charLinePixMap;
  1845. m_charLinePixMap = new char [MAX_PIX_MAP_LINE];
  1846. m_charBadPixMap = new char [MAXI_BADPIX_COUNT];
  1847. BYTE *wImageWB = new BYTE [m_nWidth* m_nHeight];
  1848. int temp;
  1849. bool state = true;
  1850. int i, j;
  1851. DWORD dwMean = 0;
  1852. DWORD dwIndex = 0;
  1853. DWORD dwCts = 0;
  1854. for(i = 50; i < m_nHeight-50; i += 8) {
  1855. dwIndex = i*m_nWidth;
  1856. for(j = 50; j < m_nWidth-15; j += 8) {
  1857. dwMean += wImage[dwIndex+j];
  1858. dwCts++;
  1859. }
  1860. }
  1861. dwMean /= dwCts;
  1862. threshold1 = dwMean/3;
  1863. threshold2 = min(dwMean*2,dwMean+(m_nPixelMax-dwMean)/2);
  1864. for(i = 0; i<m_nWidth*m_nHeight; i++)
  1865. {
  1866. temp = wImage[i];// - wDarkImage[i];
  1867. if((temp<threshold1)||(temp>threshold2)) wImageWB[i] = 0;
  1868. else wImageWB[i] = 255;
  1869. }
  1870. //查找空边
  1871. int x1,x2,y1,y2;
  1872. x1 = m_nWOffset;
  1873. x2 = m_nWidth-m_nWOffset;
  1874. y1 = m_nHOffset;
  1875. y2 = m_nHeight-m_nHOffset;
  1876. long char_count;
  1877. long char_length;
  1878. long nPixcount;
  1879. nPixcount=char_count = char_length = 0;
  1880. // 每列
  1881. for(i = 0; i<y1; i++)
  1882. {
  1883. char_count += sprintf(m_charBadPixMap+char_count,"%d,0:\n",i);
  1884. }
  1885. for(i = y1; i<=y2; i++)
  1886. {
  1887. // 每行
  1888. nPixcount=char_length = 0;
  1889. for(j = x1; j <=x2; j++)
  1890. {
  1891. if(*(wImageWB+m_nWidth*i+j)==0){
  1892. //bad pix, add to charbuffer
  1893. char_length += sprintf(m_charLinePixMap+char_length,"%d,",j);
  1894. nPixcount++;
  1895. }
  1896. }
  1897. //写入buffer
  1898. if (char_count>(MAXI_BADPIX_COUNT-MAX_PIX_MAP_LINE)){
  1899. state = false;
  1900. // AfxMessageBox("Too Many bad point, maybe bad method to expose!");
  1901. //gfun_LogError("Too Many bad point, maybe bad method to expose!");
  1902. break;
  1903. }
  1904. char_count += sprintf(m_charBadPixMap+char_count,"%d,%d:",i,nPixcount);
  1905. memcpy(m_charBadPixMap+char_count,m_charLinePixMap,char_length);
  1906. char_count += char_length;
  1907. char_count += sprintf(m_charBadPixMap+char_count,"\n");
  1908. }
  1909. if(state)
  1910. {
  1911. for(i = y2+1; i<m_nHeight; i++)
  1912. {
  1913. char_count += sprintf(m_charBadPixMap+char_count,"%d,0:\n",i);
  1914. }
  1915. }
  1916. delete [] wImageWB;
  1917. if(state)
  1918. {
  1919. LoadBadPixelMapChar(m_charBadPixMap,true);
  1920. CombineBadPixelMap();
  1921. }
  1922. delete [] m_charLinePixMap;
  1923. delete [] m_charBadPixMap;
  1924. return state;
  1925. }
  1926. bool CPixMatrix::AutoBadPixelMap2(WORD *wImage)
  1927. {
  1928. char * m_charBadPixMap;
  1929. char * m_charLinePixMap;
  1930. m_charLinePixMap = new char [MAX_PIX_MAP_LINE];
  1931. m_charBadPixMap = new char [MAXI_BADPIX_COUNT];
  1932. bool state = true;
  1933. int i, j;
  1934. //查找空边
  1935. int x1,x2,y1,y2;
  1936. x1 = m_nWOffset;
  1937. x2 = m_nWidth-m_nWOffset;
  1938. y1 = m_nHOffset;
  1939. y2 = m_nHeight-m_nHOffset;
  1940. long char_count;
  1941. long char_length;
  1942. long nPixcount;
  1943. nPixcount=char_count = char_length = 0;
  1944. // 每列
  1945. for(i = 0; i<y1; i++)
  1946. {
  1947. char_count += sprintf(m_charBadPixMap+char_count,"%d,0:\n",i);
  1948. }
  1949. for(i = y1; i<y2; i++)
  1950. {
  1951. // 每行
  1952. nPixcount=char_length = 0;
  1953. for(j = x1; j <x2; j++)
  1954. {
  1955. if(*(wImage+m_nWidth*i+j)==0xffff){
  1956. //bad pix, add to charbuffer
  1957. char_length += sprintf(m_charLinePixMap+char_length,"%d,",j);
  1958. nPixcount++;
  1959. }
  1960. }
  1961. //写入buffer
  1962. if (char_count>(MAXI_BADPIX_COUNT-MAX_PIX_MAP_LINE)){
  1963. state = false;
  1964. // AfxMessageBox("Too Many bad point, maybe bad method to expose!");
  1965. //gfun_LogError("Too Many bad point, maybe bad method to expose!");
  1966. break;
  1967. }
  1968. char_count += sprintf(m_charBadPixMap+char_count,"%d,%d:",i,nPixcount);
  1969. memcpy(m_charBadPixMap+char_count,m_charLinePixMap,char_length);
  1970. char_count += char_length;
  1971. char_count += sprintf(m_charBadPixMap+char_count,"\n");
  1972. }
  1973. if(state)
  1974. {
  1975. for(i = y2; i<m_nHeight; i++)
  1976. {
  1977. char_count += sprintf(m_charBadPixMap+char_count,"%d,0:\n",i);
  1978. }
  1979. }
  1980. if(state)
  1981. {
  1982. LoadBadPixelMapChar(m_charBadPixMap,true);
  1983. CombineBadPixelMap();
  1984. }
  1985. if (m_charLinePixMap)
  1986. {
  1987. delete m_charLinePixMap;
  1988. m_charLinePixMap = NULL;
  1989. }
  1990. if (m_charBadPixMap)
  1991. {
  1992. delete m_charBadPixMap;
  1993. m_charBadPixMap = NULL;
  1994. }
  1995. return state;
  1996. }
  1997. void CPixMatrix::FreeBadPixelMap()
  1998. {
  1999. if(m_BadPixelMap != NULL) {
  2000. for(int i = 0 ; i < m_nHeight ; ++i) {
  2001. if(m_BadPixelMap[i].bad_pixel_num != NULL) {
  2002. try
  2003. {
  2004. delete [] m_BadPixelMap[i].bad_pixel_num;
  2005. }
  2006. catch(...)
  2007. {
  2008. }
  2009. m_BadPixelMap[i].bad_pixel_num = NULL;
  2010. }
  2011. }
  2012. delete m_BadPixelMap;
  2013. m_BadPixelMap = NULL;
  2014. }
  2015. }
  2016. void CPixMatrix::FreeDBadLineMap()
  2017. {
  2018. LINE_MAP2 *ptemp = NULL;
  2019. while(m_doubleLineMap)
  2020. {
  2021. ptemp = m_doubleLineMap;
  2022. m_doubleLineMap = m_doubleLineMap->nextline;
  2023. delete ptemp;
  2024. }
  2025. }
  2026. void CPixMatrix::FreeBadNewPixelMap()
  2027. {
  2028. if(m_BadNewPixelMap != NULL) {
  2029. for(int i = 0 ; i < m_nHeight ; ++i) {
  2030. if(m_BadNewPixelMap[i].bad_pixel_num != NULL) {
  2031. delete [] m_BadNewPixelMap[i].bad_pixel_num;
  2032. m_BadNewPixelMap[i].bad_pixel_num = NULL;
  2033. }
  2034. }
  2035. delete [] m_BadNewPixelMap;
  2036. m_BadNewPixelMap = NULL;
  2037. }
  2038. }
  2039. bool CPixMatrix::CombineBadPixelMap()
  2040. {
  2041. long line;
  2042. long bad_pix_num;
  2043. long bad_pix_numcount;
  2044. long bad_new_pix_num;
  2045. long bad_new_pix_numcount;
  2046. long bad_temp_pix_num;
  2047. // long bad_pix;
  2048. // long bad_new_pix;
  2049. long *p_bad_pix_num;
  2050. long *p_bad_new_pix_num;
  2051. long *p_bad_temp_pix;
  2052. if (m_BadNewPixelMap == NULL)
  2053. return TRUE;
  2054. if (m_BadPixelMap == NULL)
  2055. {
  2056. m_BadPixelMap = m_BadNewPixelMap;
  2057. m_BadNewPixelMap = NULL;
  2058. return TRUE;
  2059. }
  2060. for(line = 0 ; line < m_nHeight ; ++line)
  2061. {
  2062. //逐行合并
  2063. bad_pix_num = m_BadPixelMap[line].num_entries;
  2064. bad_new_pix_num = m_BadNewPixelMap[line].num_entries;
  2065. p_bad_pix_num = m_BadPixelMap[line].bad_pixel_num;
  2066. p_bad_new_pix_num = m_BadNewPixelMap[line].bad_pixel_num;
  2067. bad_temp_pix_num = bad_pix_numcount = bad_new_pix_numcount = 0;
  2068. if(bad_new_pix_num==0)
  2069. continue;
  2070. p_bad_temp_pix = new long [(bad_pix_num+bad_new_pix_num+1)*sizeof(long)];
  2071. memset( p_bad_temp_pix, 0, (bad_pix_num+bad_new_pix_num+1) * sizeof(long));
  2072. while (!((bad_pix_numcount>=bad_pix_num)&&(bad_new_pix_numcount>=bad_new_pix_num)))
  2073. {
  2074. if(bad_pix_numcount==bad_pix_num)
  2075. {
  2076. for(;bad_new_pix_numcount<bad_new_pix_num;bad_new_pix_numcount++)
  2077. {
  2078. *(p_bad_temp_pix+bad_temp_pix_num) = *(p_bad_new_pix_num+bad_new_pix_numcount);
  2079. bad_temp_pix_num++;
  2080. }
  2081. }
  2082. else if(bad_new_pix_numcount==bad_new_pix_num)
  2083. {
  2084. for(;bad_pix_numcount<bad_pix_num;bad_pix_numcount++)
  2085. {
  2086. *(p_bad_temp_pix+bad_temp_pix_num) = *(p_bad_pix_num+bad_pix_numcount);
  2087. bad_temp_pix_num++;
  2088. }
  2089. }
  2090. else if((*(p_bad_new_pix_num+bad_new_pix_numcount)& OFFSET_MASK)> (*(p_bad_pix_num+bad_pix_numcount)& OFFSET_MASK))
  2091. {
  2092. *(p_bad_temp_pix+bad_temp_pix_num) = *(p_bad_pix_num+bad_pix_numcount);
  2093. bad_temp_pix_num++;
  2094. bad_pix_numcount++;
  2095. }
  2096. else if((*(p_bad_new_pix_num+bad_new_pix_numcount)& OFFSET_MASK)< (*(p_bad_pix_num+bad_pix_numcount)& OFFSET_MASK))
  2097. {
  2098. *(p_bad_temp_pix+bad_temp_pix_num) = *(p_bad_new_pix_num+bad_new_pix_numcount);
  2099. bad_temp_pix_num++;
  2100. bad_new_pix_numcount++;
  2101. }
  2102. else
  2103. {
  2104. *(p_bad_temp_pix+bad_temp_pix_num) = *(p_bad_pix_num+bad_pix_numcount);
  2105. bad_temp_pix_num++;
  2106. bad_pix_numcount++;
  2107. bad_new_pix_numcount++;
  2108. }
  2109. }
  2110. delete [] m_BadPixelMap[line].bad_pixel_num;
  2111. m_BadPixelMap[line].bad_pixel_num = new long [(bad_temp_pix_num+1)*sizeof(long)];
  2112. m_BadPixelMap[line].num_entries = bad_temp_pix_num;
  2113. memcpy(m_BadPixelMap[line].bad_pixel_num,p_bad_temp_pix,(bad_temp_pix_num+1)*sizeof(long));
  2114. delete [] p_bad_temp_pix;
  2115. }
  2116. FreeBadNewPixelMap();
  2117. return TRUE;
  2118. }
  2119. bool CPixMatrix::AutoBadPixelFixed(WORD *wImage, int threshold)
  2120. {
  2121. DWORD Temp,Sum;
  2122. //指向DIB图像中的数据;
  2123. WORD* pDataByte;
  2124. //差值,与阈值比较
  2125. int idelta;
  2126. // int count;
  2127. // WORD sourcetemp[9];
  2128. int i,j,x,y,k;//for loop
  2129. for (j = m_nHOffset+2; j < m_nHeight-m_nHOffset; j++)
  2130. {
  2131. Temp = (*(wImage + j*m_nWidth - 1) + *(wImage + j*m_nWidth) + *(wImage + j*m_nWidth + 1) + *(wImage + (j-1)*m_nWidth - 1) + *(wImage + (j-1)*m_nWidth) + *(wImage + (j-1)*m_nWidth + 1)+ *(wImage + (j+1)*m_nWidth - 1) + *(wImage + (j+1)*m_nWidth) + *(wImage + (j+1)*m_nWidth + 1))/9;
  2132. for (i = m_nWOffset+2; i < m_nWidth-m_nHOffset ; i++)
  2133. {
  2134. //如果差值小于阈值,则不改变此点
  2135. pDataByte = wImage + j * m_nWidth + i;
  2136. idelta = *pDataByte - Temp;
  2137. if (idelta<0) idelta = -1*idelta;
  2138. if (idelta > threshold)
  2139. {
  2140. //如果差值大于阈值,则在邻近区域3x3找到所有满足阈值条件的点,取均值
  2141. Sum = 0;
  2142. k = 0;
  2143. for (y = j - 1; y<= j+1; y++)
  2144. {
  2145. pDataByte = (unsigned short*)(wImage + y * m_nWidth + i - 1);
  2146. for(x = i - 1; x <= i+1; x++)
  2147. {
  2148. idelta = *pDataByte - Temp;
  2149. if (idelta<0) idelta = -1*idelta;
  2150. if (idelta > threshold) break;
  2151. Sum += *pDataByte;
  2152. pDataByte++;
  2153. k++;
  2154. }
  2155. }
  2156. if (k>3) *(wImage + j * m_nWidth + i) = (WORD)Sum/k;
  2157. }
  2158. Temp = *(wImage + j * m_nWidth + i);
  2159. }
  2160. }
  2161. return TRUE;
  2162. }
  2163. bool CPixMatrix::LoadLineMap( const char *fileName )
  2164. {
  2165. FILE *fp;
  2166. long *line_direction = new long;
  2167. long *line_entries = new long;
  2168. long *startpoint = new long;
  2169. long *endpoint = new long;
  2170. char *data_in = NULL;
  2171. data_in = new char [MAX_PIX_MAP_LINE];
  2172. LINE_MAP * m_xLineMapCur = NULL;
  2173. LINE_MAP * m_yLineMapCur = NULL;
  2174. if(data_in == NULL) {
  2175. return FALSE;
  2176. }
  2177. fp = fopen(fileName, "rb");
  2178. if(fp == NULL) {
  2179. return FALSE;
  2180. }
  2181. FreeLineMap(m_xLineMap);
  2182. m_xLineMap = NULL;
  2183. FreeLineMap(m_yLineMap);
  2184. m_yLineMap = NULL;
  2185. while(fgets(data_in, MAX_PIX_MAP_LINE, fp) != NULL) {
  2186. if(sscanf(data_in, "%d,%d:%d,%d", line_direction, line_entries, startpoint, endpoint) == 4) {
  2187. // line_direction = 0;line_entries=359, startpoint=45, endpoint=2700;
  2188. if(*line_direction==0)
  2189. {
  2190. m_xLineMapCur = new LINE_MAP;
  2191. m_xLineMapCur->nextline = m_xLineMap;
  2192. m_xLineMapCur->lLine = FALSE;
  2193. m_xLineMapCur->rLine = FALSE;
  2194. m_xLineMapCur->line_entries = (WORD)*line_entries;
  2195. m_xLineMapCur->startpoint = (WORD)*startpoint;
  2196. m_xLineMapCur->endpoint = (WORD)*endpoint;
  2197. m_xLineMap = m_xLineMapCur;
  2198. }
  2199. else
  2200. {
  2201. m_yLineMapCur = new LINE_MAP;
  2202. m_yLineMapCur->nextline = m_yLineMap;
  2203. m_yLineMapCur->lLine = FALSE;
  2204. m_yLineMapCur->rLine = FALSE;
  2205. m_yLineMapCur->line_entries = (WORD)*line_entries;
  2206. m_yLineMapCur->startpoint = (WORD)*startpoint;
  2207. m_yLineMapCur->endpoint = (WORD)*endpoint;
  2208. m_yLineMap = m_yLineMapCur;
  2209. }
  2210. }
  2211. }
  2212. MarkAdjacentLine(m_xLineMap);
  2213. MarkAdjacentLine(m_yLineMap);
  2214. if(fp != NULL) {
  2215. fclose(fp);
  2216. }
  2217. if(data_in != NULL) {
  2218. delete [] data_in;
  2219. }
  2220. if(m_xLineMapCur){
  2221. FreeLineMap(m_xLineMapCur);
  2222. }
  2223. delete line_direction;
  2224. delete line_entries;
  2225. delete startpoint;
  2226. delete endpoint;
  2227. return TRUE;
  2228. }
  2229. void CPixMatrix::CorrectDBadLines(WORD *image)
  2230. {
  2231. LINE_MAP2 * m_DLineMapCur = m_doubleLineMap;
  2232. while(m_DLineMapCur)
  2233. {
  2234. if(m_DLineMapCur->nLineType == 0)
  2235. {
  2236. CorrectDXLines(image, m_DLineMapCur->line_entries, m_DLineMapCur->startpoint, m_DLineMapCur->endpoint);
  2237. }
  2238. else
  2239. {
  2240. CorrectDYLines(image, m_DLineMapCur->line_entries, m_DLineMapCur->startpoint, m_DLineMapCur->endpoint);
  2241. }
  2242. m_DLineMapCur = m_DLineMapCur->nextline;
  2243. }
  2244. }
  2245. void CPixMatrix::SwitchDBadLines(WORD *image)
  2246. {
  2247. LINE_MAP2 * m_DLineMapCur = m_doubleLineMap;
  2248. int specX = CalSpecX(image);
  2249. int specY = CalSpecY(image);
  2250. while (m_DLineMapCur)
  2251. {
  2252. if (m_DLineMapCur->nLineType == 0)
  2253. {
  2254. SwitchDXLines(image, m_DLineMapCur->line_entries, m_DLineMapCur->startpoint, m_DLineMapCur->endpoint, specX);
  2255. }
  2256. else
  2257. {
  2258. SwitchDYLines(image, m_DLineMapCur->line_entries, m_DLineMapCur->startpoint, m_DLineMapCur->endpoint, specY);
  2259. }
  2260. m_DLineMapCur = m_DLineMapCur->nextline;
  2261. }
  2262. }
  2263. // modified by shihaozhe on 2019-4-18
  2264. //below is old version
  2265. /*void CPixMatrix::CorrectDXLines(WORD *image, int nentry, int nstart, int nend)
  2266. {
  2267. int nA,nB,nC, nA1, nA2, nB1, nB2, nC1, nC2;
  2268. int nMIN;//, MID, MAX;
  2269. for(int i = nstart; i<nend; i++)
  2270. {
  2271. nA1 = image[(nentry-2)*m_nWidth + i-2] - image[(nentry-1)*m_nWidth + i-1];
  2272. nB1 = image[(nentry-2)*m_nWidth + i] - image[(nentry-1)*m_nWidth + i];
  2273. nC1 = image[(nentry-2)*m_nWidth + i+2] - image[(nentry-1)*m_nWidth + i+1];
  2274. nA2 = image[(nentry-1)*m_nWidth + i-1] - image[(nentry+2)*m_nWidth + i+2];
  2275. nB2 = image[(nentry-1)*m_nWidth + i] - image[(nentry+2)*m_nWidth + i];
  2276. nC2 = image[(nentry-1)*m_nWidth + i+1] - image[(nentry+2)*m_nWidth + i-2];
  2277. //frame1[i*2880 + 1434] = (A + B + C)/3;
  2278. nA = nA1*nA1 + nA2*nA2;
  2279. nB = nB1*nB1 + nB2*nB2;
  2280. nC = nC1*nC1 + nC2*nC2;
  2281. nMIN = nA;
  2282. if(nB<nMIN)
  2283. {
  2284. nMIN = nB;
  2285. }
  2286. if(nC<nMIN)
  2287. {
  2288. nMIN = nC;
  2289. }
  2290. if(nMIN == nA)
  2291. {
  2292. image[nentry*m_nWidth + i] = (image[(nentry-1)*m_nWidth + i-1]*2 + image[(nentry+2)*2880 + i+2])/3;
  2293. }
  2294. else if(nMIN == nB)
  2295. {
  2296. image[nentry*m_nWidth + i] = (image[(nentry-1)*m_nWidth + i]*2 + image[(nentry+2)*2880 + i])/3;
  2297. }
  2298. else //nMIN == nC
  2299. {
  2300. image[nentry*m_nWidth + i] = (image[(nentry-1)*m_nWidth + i+1]*2 + image[(nentry+2)*2880 + i-2])/3;
  2301. }
  2302. nA1 = image[(nentry+2)*m_nWidth + i+1] - image[(nentry+3)*m_nWidth + i+2];
  2303. nB1 = image[(nentry+2)*m_nWidth + i] - image[(nentry+3)*m_nWidth + i];
  2304. nC1 = image[(nentry+2)*m_nWidth + i-1] - image[(nentry+3)*m_nWidth + i-2];
  2305. nA = nA1*nA1 + nA2*nA2;
  2306. nB = nB1*nB1 + nB2*nB2;
  2307. nC = nC1*nC1 + nC2*nC2;
  2308. //frame1[i*2880 + 1434] = (A + B + C)/3;
  2309. nMIN = nA;
  2310. if(nB<nMIN)
  2311. {
  2312. nMIN = nB;
  2313. }
  2314. if(nC<nMIN)
  2315. {
  2316. nMIN = nC;
  2317. }
  2318. if(nMIN == nA)
  2319. {
  2320. image[(nentry+1)*m_nWidth + i] = (image[(nentry-1)*m_nWidth + i-2] + image[(nentry+2)*m_nWidth + i+1]*2)/3;
  2321. }
  2322. else if(nMIN == nB)
  2323. {
  2324. image[(nentry+1)*m_nWidth + i] = (image[(nentry-1)*m_nWidth + i] + image[(nentry+2)*m_nWidth + i]*2)/3;
  2325. }
  2326. else //nMIN == nC
  2327. {
  2328. image[(nentry+1)*m_nWidth + i] = (image[(nentry-1)*m_nWidth + i+2] + image[(nentry+2)*m_nWidth + i-1]*2)/3;
  2329. }
  2330. }
  2331. }
  2332. void CPixMatrix::CorrectDYLines(WORD *image, int nentry, int nstart, int nend)
  2333. {
  2334. int nA,nB,nC, nA1, nA2, nB1, nB2, nC1, nC2;
  2335. int nMIN;//, MID, MAX;
  2336. for(int i = nstart; i<nend; i++)
  2337. {
  2338. nA1 = image[(i-2)*m_nWidth + nentry-2] - image[(i-1)*m_nWidth + nentry-1];
  2339. nB1 = image[i*m_nWidth + nentry-2] - image[i*m_nWidth + nentry-1];
  2340. nC1 = image[(i+2)*m_nWidth + nentry-2] - image[(i+1)*m_nWidth + nentry-1];
  2341. nA2 = image[(i-1)*m_nWidth + nentry-1] - image[(i+2)*m_nWidth + nentry+2];
  2342. nB2 = image[i*m_nWidth + nentry-1] - image[i*m_nWidth + nentry+2];
  2343. nC2 = image[(i+1)*m_nWidth + nentry-1] - image[(i-2)*m_nWidth + nentry+2];
  2344. //frame1[i*2880 + 1434] = (A + B + C)/3;
  2345. nA = nA1*nA1 + nA2*nA2;
  2346. nB = nB1*nB1 + nB2*nB2;
  2347. nC = nC1*nC1 + nC2*nC2;
  2348. nMIN = nA;
  2349. if(nB<nMIN)
  2350. {
  2351. nMIN = nB;
  2352. }
  2353. if(nC<nMIN)
  2354. {
  2355. nMIN = nC;
  2356. }
  2357. if(nMIN == nA)
  2358. {
  2359. image[i*m_nWidth + nentry] = (image[(i-1)*m_nWidth + nentry-1]*2 + image[(i+2)*2880 + nentry+2])/3;
  2360. }
  2361. else if(nMIN == nB)
  2362. {
  2363. image[i*m_nWidth + nentry] = (image[i*m_nWidth + nentry-1]*2 + image[i*2880 + nentry+2])/3;
  2364. }
  2365. else //nMIN == nC
  2366. {
  2367. image[i*m_nWidth + nentry] = (image[(i+1)*m_nWidth + nentry-1]*2 + image[(i-2)*2880 + nentry+2])/3;
  2368. }
  2369. nA1 = image[(i+1)*m_nWidth + nentry+2] - image[(i+2)*m_nWidth + nentry+3];
  2370. nB1 = image[i*m_nWidth + nentry+2] - image[i*m_nWidth + nentry+3];
  2371. nC1 = image[(i-1)*m_nWidth + nentry+2] - image[(i-2)*m_nWidth + nentry+3];
  2372. nA = nA1*nA1 + nA2*nA2;
  2373. nB = nB1*nB1 + nB2*nB2;
  2374. nC = nC1*nC1 + nC2*nC2;
  2375. //frame1[i*2880 + 1434] = (A + B + C)/3;
  2376. nMIN = nA;
  2377. if(nB<nMIN)
  2378. {
  2379. nMIN = nB;
  2380. }
  2381. if(nC<nMIN)
  2382. {
  2383. nMIN = nC;
  2384. }
  2385. if(nMIN == nA)
  2386. {
  2387. image[i*m_nWidth + nentry+1] = (image[(i-2)*m_nWidth + nentry-1] + image[(i+1)*m_nWidth + nentry+2]*2)/3;
  2388. }
  2389. else if(nMIN == nB)
  2390. {
  2391. image[i*m_nWidth + nentry+1] = (image[i*m_nWidth + nentry-1] + image[i*m_nWidth + nentry+2]*2)/3;
  2392. }
  2393. else //nMIN == nC
  2394. {
  2395. image[i*m_nWidth + nentry+1] = (image[(i+2)*m_nWidth + nentry-1] + image[(i-1)*m_nWidth + nentry+2]*2)/3;
  2396. }
  2397. }
  2398. }*/
  2399. void matMul(float** A, int Arow, int Acol, float** x, int xrow, int xcol, float* bias, float** b)
  2400. {
  2401. for (int i = 0; i < xrow; i++)
  2402. {
  2403. for (int j = 0; j < Acol; j++)
  2404. {
  2405. *((float*)b + i * Acol + j) = 0;
  2406. for (int k = 0; k < Arow; k++)
  2407. {
  2408. *((float*)b + i * Acol + j) += (*((float*)A + k * Acol + j) * (*((float*)x + i * xcol + k)));
  2409. }
  2410. *((float*)b + i * Acol + j) += bias[j];
  2411. }
  2412. }
  2413. }
  2414. void relu(float **x, int xrow, int xcol, float** xnew)
  2415. {
  2416. for (int i = 0; i < xrow; i++)
  2417. {
  2418. for (int j = 0; j < xcol; j++)
  2419. {
  2420. *((float*)xnew + i * xcol + j) = max(*((float*)x + i * xcol + j), 0.0f);
  2421. }
  2422. }
  2423. }
  2424. int CPixMatrix::CalSpecX(WORD* image)
  2425. {
  2426. int spec = 2;
  2427. int Flag1Votes = 0;
  2428. int Flag2Votes = 0;
  2429. for (int i = 3; i < (m_nHeight - 4); i = i + 100)
  2430. {
  2431. for (int j = 0; j < m_nWidth; j++)
  2432. {
  2433. float Flag1ResLine1 = (float(image[(i - 2)* m_nWidth + j]) / 2 + float(image[(i + 2)* m_nWidth + j]) / 2);
  2434. float Flag1ResLine2 = (float(image[(i - 1)* m_nWidth + j]) / 2 + float(image[(i + 3)* m_nWidth + j]) / 2);
  2435. float Flag2ResLine1 = (float(image[(i - 3)* m_nWidth + j]) / 2 + float(image[(i + 3)* m_nWidth + j]) / 2);
  2436. float Flag2ResLine2 = (float(image[(i - 2)* m_nWidth + j]) / 2 + float(image[(i + 4)* m_nWidth + j]) / 2);
  2437. float Flag1Score = abs(Flag1ResLine1 - float(image[i * m_nWidth + j])) + abs(Flag1ResLine2 - float(image[(i + 1) * m_nWidth + j]));
  2438. float Flag2Score = abs(Flag2ResLine1 - float(image[i * m_nWidth + j])) + abs(Flag2ResLine2 - float(image[(i + 1) * m_nWidth + j]));
  2439. if (Flag1Score < Flag2Score)
  2440. Flag1Votes++;
  2441. else
  2442. Flag2Votes++;
  2443. }
  2444. }
  2445. if (Flag1Votes >= Flag2Votes)
  2446. spec = 2;
  2447. else
  2448. spec = 3;
  2449. return spec;
  2450. }
  2451. int CPixMatrix::CalSpecY(WORD* image)
  2452. {
  2453. int spec = 2;
  2454. int Flag1Votes = 0;
  2455. int Flag2Votes = 0;
  2456. for (int i = 3; i < (m_nWidth - 4); i = i + 100)
  2457. {
  2458. for (int j = 0; j < m_nHeight; j++)
  2459. {
  2460. float Flag1ResLine1 = (float(image[j * m_nWidth + (i - 2)]) / 2 + float(image[j* m_nWidth + (i + 2)]) / 2);
  2461. float Flag1ResLine2 = (float(image[j * m_nWidth + (i - 1)]) / 2 + float(image[j * m_nWidth + (i + 3)]) / 2);
  2462. float Flag2ResLine1 = (float(image[j * m_nWidth + (i - 3)]) / 2 + float(image[j * m_nWidth + (i + 3)]) / 2);
  2463. float Flag2ResLine2 = (float(image[j * m_nWidth + (i - 2)]) / 2 + float(image[j * m_nWidth + (i + 4)]) / 2);
  2464. float Flag1Score = abs(Flag1ResLine1 - float(image[j * m_nWidth + i])) + abs(Flag1ResLine2 - float(image[j * m_nWidth + (i + 1)]));
  2465. float Flag2Score = abs(Flag2ResLine1 - float(image[j * m_nWidth + i])) + abs(Flag2ResLine2 - float(image[j * m_nWidth + (i + 1)]));
  2466. if (Flag1Score < Flag2Score)
  2467. Flag1Votes++;
  2468. else
  2469. Flag2Votes++;
  2470. }
  2471. }
  2472. if (Flag1Votes >= Flag2Votes)
  2473. spec = 2;
  2474. else
  2475. spec = 3;
  2476. return spec;
  2477. }
  2478. void CPixMatrix::SwitchDXLines(WORD* image, int nentry, int nstart, int nend, int spec)
  2479. {
  2480. if (nentry < 1 || nentry >= m_nHeight) return;
  2481. if ((nentry - 1 - spec) >= 0 && (nentry + spec) < m_nHeight)
  2482. {
  2483. for (int i = nstart; i < nend; i++)
  2484. {
  2485. image[(nentry - 1) * m_nWidth + i] = WORD((float(image[(nentry - 1 - spec) * m_nWidth + i]) +
  2486. float(image[(nentry - 1 + spec) * m_nWidth + i])) / 2);
  2487. image[nentry * m_nWidth + i] = WORD((float(image[(nentry - spec) * m_nWidth + i]) +
  2488. float(image[(nentry + spec) * m_nWidth + i])) / 2);
  2489. }
  2490. }
  2491. else if ((nentry - 1 - spec) < 0)
  2492. {
  2493. for (int i = nstart; i < nend; i++)
  2494. {
  2495. image[(nentry - 1) * m_nWidth + i] = image[(nentry - 1 + spec) * m_nWidth + i];
  2496. image[nentry * m_nWidth + i] = image[(nentry + spec) * m_nWidth + i];
  2497. }
  2498. }
  2499. else
  2500. {
  2501. for (int i = nstart; i < nend; i++)
  2502. {
  2503. image[(nentry - 1) * m_nWidth + i] = image[(nentry - 1 - spec) * m_nWidth + i];
  2504. image[nentry * m_nWidth + i] = image[(nentry - spec) * m_nWidth + i];
  2505. }
  2506. }
  2507. }
  2508. void CPixMatrix::SwitchDYLines(WORD* image, int nentry, int nstart, int nend, int spec)
  2509. {
  2510. if (nentry < 1 || nentry >= m_nWidth) return;
  2511. if ((nentry - 1 - spec) >= 0 && (nentry + spec) < m_nWidth)
  2512. {
  2513. for (int i = nstart; i < nend; i++)
  2514. {
  2515. image[i * m_nWidth + (nentry - 1)] = WORD((float(image[i * m_nWidth + (nentry - 1 - spec)]) +
  2516. float(image[i * m_nWidth + (nentry - 1 + spec)])) / 2);
  2517. image[i * m_nWidth + nentry] = WORD((float(image[i * m_nWidth + (nentry - spec)]) +
  2518. float(image[i * m_nWidth + (nentry + spec)])) / 2);
  2519. }
  2520. }
  2521. else if ((nentry - 1 - spec) < 0)
  2522. {
  2523. for (int i = nstart; i < nend; i++)
  2524. {
  2525. image[i * m_nWidth + (nentry - 1)] = image[i * m_nWidth + (nentry - 1 + spec)];
  2526. image[i * m_nWidth + nentry] = image[i * m_nWidth + (nentry + spec)];
  2527. }
  2528. }
  2529. else
  2530. {
  2531. for (int i = nstart; i < nend; i++)
  2532. {
  2533. image[i * m_nWidth + (nentry - 1)] = image[i * m_nWidth + (nentry - 1 - spec)];
  2534. image[i * m_nWidth + nentry] = image[i * m_nWidth + (nentry - spec)];
  2535. }
  2536. }
  2537. }
  2538. void CPixMatrix::CorrectDXLines(WORD *image, int nentry, int nstart, int nend)
  2539. {
  2540. float w0[20][10] = { { -1.80551376e-01 , 1.48339527e-01 , 1.93434153e-01 ,-1.69102470e-01,
  2541. -8.30293637e-02 , 4.68743068e-11 ,1.07092565e-01 , 4.90719458e-02,
  2542. -1.76974159e-06 ,-2.34796443e-01 },
  2543. { 2.26791936e-01 ,-4.42808635e-02 ,-1.96939813e-03 , 1.14001209e-01,
  2544. 6.47560641e-01 , 5.78895742e-11 ,-4.89402386e-02 ,-4.71011428e-02,
  2545. 1.41082019e-06 ,4.77452935e-01 },
  2546. { 7.13725661e-01, -4.36174689e-01 ,1.00634693e-01 ,-2.00562230e-02,
  2547. 4.35445448e-01 , 4.63354817e-12 , 2.24216928e-01, -6.99454931e-02,
  2548. -2.63495911e-06 , 3.18166017e-01 },
  2549. { -1.64505637e-01, 6.80188531e-03 , 2.86472226e-01 ,-4.37072848e-01,
  2550. 1.32676203e-03 ,-1.86652083e-11 , 5.94614663e-02 ,-2.56715695e-01,
  2551. 3.52631615e-08 , 1.19374415e-02 },
  2552. { 1.92242685e-01 , 1.81255810e-02 ,-3.13366263e-01 , 1.65171353e-01,
  2553. 6.37306352e-02 ,-8.55124219e-11 , 2.06302921e-03 , 7.13230627e-02,
  2554. -8.22837481e-08 , 4.43748465e-04 },
  2555. { 2.56683926e-02 ,-1.32064094e-01 ,5.92931576e-01 ,-3.93658658e-01,
  2556. 1.89241730e-01 ,-1.02160955e-10 ,-2.60349051e-01, 6.00632605e-02,
  2557. -1.08522973e-06 , 1.38723903e-02 },
  2558. { 2.01019043e-01 , 4.60498596e-01 , 5.17829841e-01 ,-7.58273701e-01,
  2559. -1.86647317e-02 ,-1.76434531e-11, -6.46477206e-01, 4.13885018e-01,
  2560. -1.16803458e-06, -4.68910295e-01 },
  2561. { 8.62645998e-01 , 9.68335169e-01 , 8.94262963e-02 , 1.74861574e-01,
  2562. -1.85454720e-01 ,-3.19849587e-11 , 5.96511029e-01 ,6.05654463e-01,
  2563. -7.56176036e-07, -5.07831177e-01 },
  2564. { -8.71116572e-02, 3.66096894e-01 ,-6.28023440e-01 , 6.91073259e-01,
  2565. 3.01648578e-01 ,-1.15338717e-10 ,3.58207881e-01 , 6.94645261e-01,
  2566. -5.48317706e-06 ,-5.13752419e-01 },
  2567. { 2.82701627e-01 ,-1.52676216e-01 ,-1.74173102e-01 , 1.68326554e-01,
  2568. -2.20833287e-01, -1.17141445e-10 ,-2.30629014e-01 , 5.09502973e-02,
  2569. 1.03628056e-05 , 5.17466666e-02 },
  2570. { -2.93915459e-01, 2.59599255e-01, -1.57063692e-01 ,2.47658474e-01,
  2571. 2.44114791e-01 ,-4.60543803e-11 , 1.60940275e-01, 2.99402289e-01,
  2572. -5.21160106e-07 ,-6.60931206e-02 },
  2573. { -2.18481332e-01, -3.65660965e-02 ,-2.76270937e-01 , 6.37817927e-01,
  2574. -2.25210248e-01 ,-2.57545543e-11 , 5.04407490e-01 , 3.25644418e-01,
  2575. -1.67397865e-06 ,-3.38918731e-01 },
  2576. { 4.30656416e-01 ,-6.05850820e-01 , 3.56411868e-01 , 1.39871104e-01,
  2577. 1.61133433e-02, 1.95278541e-11, 4.25782008e-01 ,9.79111337e-01,
  2578. -9.56787789e-07, -3.11803801e-01 },
  2579. { -2.36239573e-01 ,-1.73015706e-01 , 3.71253420e-01 ,-4.43126520e-01,
  2580. -6.16987099e-01, -3.47112370e-11 ,-9.48308722e-02 , 2.35392910e-01,
  2581. 3.90978755e-07 ,-5.30585275e-01 },
  2582. { -9.21914745e-03 , 2.10569685e-01 , 5.86744516e-01, -3.29272382e-01,
  2583. 6.87882591e-02 ,-5.60479430e-11, -1.31813225e-01 ,-9.01748595e-02,
  2584. 1.48535772e-06 ,7.75831897e-02 },
  2585. { -3.87850229e-02 , 2.12077616e-02 , 3.81875744e-02 ,1.12848455e-01,
  2586. 1.83825444e-01, -1.18965572e-10 ,1.27163810e-01, 9.96867991e-02,
  2587. 1.84582238e-06 ,7.19917185e-02 },
  2588. { -5.18885615e-02 ,-1.74264405e-01 ,2.27000308e-01, -4.29314728e-01,
  2589. -1.17003563e-01, -2.81658523e-11 , 1.17201003e-01 ,-3.07566778e-01,
  2590. -2.01403760e-06 ,-9.42299766e-02 },
  2591. { 4.90024471e-01 , 3.71047916e-02 , 1.01772677e-01, 9.51157739e-02,
  2592. -1.71601670e-01 ,9.38432195e-11 , 6.24270660e-01, -4.71965703e-01,
  2593. -3.31362503e-07 , 4.29190240e-01 },
  2594. { 7.14228024e-02 ,-5.00837424e-02 , 1.47659839e-01 , 5.17815234e-02,
  2595. 3.52253877e-01 ,4.90728233e-11 , 2.23559528e-01 ,-2.22637845e-01,
  2596. -1.25922349e-06 , 2.04710832e-01 },
  2597. { -1.36544728e-01 ,-1.49882902e-03 , 1.16554738e-01 ,-1.07862765e-01,
  2598. 9.45728675e-02 , 2.68435951e-11 ,-2.67820297e-02, 1.89775976e-01,
  2599. -2.41099251e-06, -1.34862793e-01 } };
  2600. float w1[10][10] = { { -0.23780609 ,-0.20482226 , 0.41957468, 0.40999775, 0.38139455 ,0.49962715,
  2601. 0.08417365 , 0.29976534 ,-0.08972183 ,-0.0574983 },
  2602. { 0.07750195 ,-0.75130445 ,-0.06468749 ,-0.09033038 ,-0.18993429 ,0.82029747,
  2603. -0.61669163 , 0.01430632 , 0.07597411, 0.27505365 },
  2604. { 0.49405426, 0.01242133 ,0.25904818 ,0.41172925 ,0.07578099 ,0.06032476,
  2605. 0.46222851, 0.06112816, 0.23961187, 0.11556222 },
  2606. { 1.03532351 , 0.00237677 ,-1.41761159 , 0.2577502 ,-0.29667514 ,-0.22258016,
  2607. 1.01807614, 0.47470323 , 0.64205134 , 1.17553243 },
  2608. { -0.54341226 , 0.05698822 ,-0.63832775 ,-0.3224985, -0.08834335 ,-0.20154572,
  2609. 0.33619324 , 0.53621269 ,-0.08154711 , 0.10801987 },
  2610. { -1.43359900e-10, -2.19706358e-11 ,-7.62980145e-11 , 3.80564307e-11,
  2611. 1.43483774e-10 ,1.43472783e-10 ,-7.14971534e-11 ,-4.57497718e-11,
  2612. -7.01573521e-11 , 1.53484506e-10 },
  2613. { -0.27199681, -0.06586821 , 0.06227236 ,0.50916823 ,0.12266234 , 0.25784538,
  2614. 0.21391334 ,-0.04650493 ,-0.21507827 ,-0.81284655 },
  2615. { 0.07606676, 0.13142832 ,-0.53617744 , 0.42486831 , 0.89048667 , 0.3048935,
  2616. -0.71382582, -0.61000717 ,0.2688673 , 0.47569795 },
  2617. { 1.52424197e-07 , 6.22315298e-08 - 6.52738940e-11 ,-8.68034280e-07,
  2618. -5.31746214e-07 ,-1.78368165e-07 ,-2.11015294e-08, -1.52950988e-06,
  2619. -3.42529575e-11, 2.35200781e-07 },
  2620. { -0.9658597 ,-0.68771416, -0.14909613, -0.27739111 , 0.38529542, 0.14808103,
  2621. -0.24781566 ,-0.52709518 ,0.07968214 ,-1.09388288 } };
  2622. float w2[10][4] = { { -5.53134378e-01, -5.05213046e-02, 1.17674467e+00, 1.71470806e-10 },
  2623. { 4.66713499e-01, 4.28506910e-01, -9.50886462e-02, 1.16200494e-10 },
  2624. { 2.57455827e-01, -3.01318414e-02, -4.42330819e-01, 1.27572777e-10 },
  2625. { 4.97257812e-02 ,1.68243289e-01 ,4.46669062e-01 ,2.76339875e-11 },
  2626. { -2.00255754e-01, 3.68610428e-01 , 2.01173992e-01, -6.76783395e-11 },
  2627. { -6.26138176e-02, -5.74267944e-01, 3.32356077e-01 ,-6.16432376e-12 },
  2628. { 5.33413965e-02, -5.74108504e-02 ,-7.05800150e-01 ,-1.82851013e-10 },
  2629. { -1.06458783e+00 ,-1.52935556e-01 ,-1.45886105e+00 ,-1.01166814e-10 },
  2630. { 2.78628319e-08, 2.87125783e-01 ,-1.52646659e+00 ,-1.70692624e-10 },
  2631. { -2.66851493e-01 ,-4.95637535e-02 , 7.59249111e-01 ,-6.39687400e-11 } };
  2632. float w3[4][2] = { { 0.34269875 ,0.28160403 },{ -0.2269393 , 0.46291535 },{ 0.35116205 ,0.32428314 },{ 4.44950996e-11 ,-1.64150986e-10 } };
  2633. float b0[10] = { -0.16749457 , 0.51837033 , 0.38030122 , 0.2287536 , 0.31281455 ,-0.31270079,
  2634. -0.23713096, -0.27459556 ,-0.0780897 , 0.78092204 };
  2635. float b1[10] = { -0.19847084 , 0.51427174 ,0.16520637 , 0.02815113 ,0.14546747 ,-0.33054209,
  2636. -0.22630977 ,-0.21777402 ,-0.68041806 ,-0.51254995 };
  2637. float b2[4] = { 0.10842243 , 0.55213982 ,-0.18210444, -0.43683546 };
  2638. float b3[2] = { 0.22007598, -0.15169636 };
  2639. if (nentry < 3 || nentry >(m_nHeight - 3))
  2640. return;
  2641. for (int i = nstart; i<nend; i++)
  2642. {
  2643. if (i < 2 || i >(m_nWidth - 3))
  2644. {
  2645. image[nentry * m_nWidth + i] = image[(nentry + 1) * m_nWidth + i];
  2646. image[(nentry - 1) * m_nWidth + i] = image[(nentry - 2) * m_nWidth + i];
  2647. }
  2648. else
  2649. {
  2650. float x0[1][20];
  2651. int count = 0;
  2652. float maxP = 0;
  2653. float minP = 1e9;
  2654. for (int k = -3; k <= 2; k++)
  2655. {
  2656. if (k == -1 || k == 0) continue;
  2657. for (int l = -2; l <= 2; l++)
  2658. {
  2659. *((float*)x0 + count) = float(image[(nentry + k) * m_nWidth + (i + l)]);
  2660. count++;
  2661. maxP = max(maxP, float(image[(nentry + k) * m_nWidth + (i + l)]));
  2662. minP = min(minP, float(image[(nentry + k) * m_nWidth + (i + l)]));
  2663. }
  2664. }
  2665. if ((maxP - minP) > 2560)
  2666. {
  2667. maxP = min(maxP + 256, 65535.0f);
  2668. minP = max(minP - 256, 0.0f);
  2669. }
  2670. if((maxP - minP) < 1)maxP = minP + 1;
  2671. for (int k = 0; k < 20; k++)
  2672. {
  2673. *((float*)x0 + k) = (*((float*)x0 + k) - minP) / (maxP - minP);
  2674. }
  2675. float z1[1][20];
  2676. float x1[1][20];
  2677. float z2[1][10];
  2678. float x2[1][10];
  2679. float z3[1][5];
  2680. float x3[1][5];
  2681. float z4[1][2];
  2682. float y[1][2];
  2683. matMul((float**)w0, 20, 10, (float**)x0, 1, 20, b0, (float**)z1);
  2684. relu((float**)z1, 1, 10, (float**)x1);
  2685. matMul((float**)w1, 10, 10, (float**)x1, 1, 10, b1, (float**)z2);
  2686. relu((float**)z2, 1, 10, (float**)x2);
  2687. matMul((float**)w2, 10, 4, (float**)x2, 1, 10, b2, (float**)z3);
  2688. relu((float**)z3, 1, 4, (float**)x3);
  2689. matMul((float**)w3, 4, 2, (float**)x3, 1, 4, b3, (float**)z4);
  2690. relu((float**)z4, 1, 2, (float**)y);
  2691. float predict1 = max(min(*((float*)y) * (maxP - minP) + minP, 65535.0f), 0.0f);
  2692. float predict2 = max(min(*((float*)y + 1) * (maxP - minP) + minP, 65535.0f), 0.0f);
  2693. float flag1line1 = float(image[(nentry - 3) * m_nWidth + i]) / 2 + float(image[(nentry + 1) * m_nWidth + i]) / 2;
  2694. float flag1line2 = float(image[(nentry - 2) * m_nWidth + i]) / 2 + float(image[(nentry + 2) * m_nWidth + i]) / 2;
  2695. float flag1score = abs(flag1line1 - predict1) +
  2696. abs(flag1line2 - predict2);
  2697. if (flag1score < 30)
  2698. {
  2699. image[(nentry - 1) * m_nWidth + i] = WORD(max(min(flag1line1, 65535.0f), 0.0f));
  2700. image[nentry * m_nWidth + i] = WORD(max(min(flag1line2, 65535.0f), 0.0f));
  2701. }
  2702. else
  2703. {
  2704. image[(nentry - 1) * m_nWidth + i] = WORD(predict1);
  2705. image[nentry * m_nWidth + i] = WORD(predict2);
  2706. }
  2707. }
  2708. }
  2709. }
  2710. void CPixMatrix::CorrectDYLines(WORD *image, int nentry, int nstart, int nend)
  2711. {
  2712. float w0[20][10] = { { -1.80551376e-01 , 1.48339527e-01 , 1.93434153e-01 ,-1.69102470e-01,
  2713. -8.30293637e-02 , 4.68743068e-11 ,1.07092565e-01 , 4.90719458e-02,
  2714. -1.76974159e-06 ,-2.34796443e-01 },
  2715. { 2.26791936e-01 ,-4.42808635e-02 ,-1.96939813e-03 , 1.14001209e-01,
  2716. 6.47560641e-01 , 5.78895742e-11 ,-4.89402386e-02 ,-4.71011428e-02,
  2717. 1.41082019e-06 ,4.77452935e-01 },
  2718. { 7.13725661e-01, -4.36174689e-01 ,1.00634693e-01 ,-2.00562230e-02,
  2719. 4.35445448e-01 , 4.63354817e-12 , 2.24216928e-01, -6.99454931e-02,
  2720. -2.63495911e-06 , 3.18166017e-01 },
  2721. { -1.64505637e-01, 6.80188531e-03 , 2.86472226e-01 ,-4.37072848e-01,
  2722. 1.32676203e-03 ,-1.86652083e-11 , 5.94614663e-02 ,-2.56715695e-01,
  2723. 3.52631615e-08 , 1.19374415e-02 },
  2724. { 1.92242685e-01 , 1.81255810e-02 ,-3.13366263e-01 , 1.65171353e-01,
  2725. 6.37306352e-02 ,-8.55124219e-11 , 2.06302921e-03 , 7.13230627e-02,
  2726. -8.22837481e-08 , 4.43748465e-04 },
  2727. { 2.56683926e-02 ,-1.32064094e-01 ,5.92931576e-01 ,-3.93658658e-01,
  2728. 1.89241730e-01 ,-1.02160955e-10 ,-2.60349051e-01, 6.00632605e-02,
  2729. -1.08522973e-06 , 1.38723903e-02 },
  2730. { 2.01019043e-01 , 4.60498596e-01 , 5.17829841e-01 ,-7.58273701e-01,
  2731. -1.86647317e-02 ,-1.76434531e-11, -6.46477206e-01, 4.13885018e-01,
  2732. -1.16803458e-06, -4.68910295e-01 },
  2733. { 8.62645998e-01 , 9.68335169e-01 , 8.94262963e-02 , 1.74861574e-01,
  2734. -1.85454720e-01 ,-3.19849587e-11 , 5.96511029e-01 ,6.05654463e-01,
  2735. -7.56176036e-07, -5.07831177e-01 },
  2736. { -8.71116572e-02, 3.66096894e-01 ,-6.28023440e-01 , 6.91073259e-01,
  2737. 3.01648578e-01 ,-1.15338717e-10 ,3.58207881e-01 , 6.94645261e-01,
  2738. -5.48317706e-06 ,-5.13752419e-01 },
  2739. { 2.82701627e-01 ,-1.52676216e-01 ,-1.74173102e-01 , 1.68326554e-01,
  2740. -2.20833287e-01, -1.17141445e-10 ,-2.30629014e-01 , 5.09502973e-02,
  2741. 1.03628056e-05 , 5.17466666e-02 },
  2742. { -2.93915459e-01, 2.59599255e-01, -1.57063692e-01 ,2.47658474e-01,
  2743. 2.44114791e-01 ,-4.60543803e-11 , 1.60940275e-01, 2.99402289e-01,
  2744. -5.21160106e-07 ,-6.60931206e-02 },
  2745. { -2.18481332e-01, -3.65660965e-02 ,-2.76270937e-01 , 6.37817927e-01,
  2746. -2.25210248e-01 ,-2.57545543e-11 , 5.04407490e-01 , 3.25644418e-01,
  2747. -1.67397865e-06 ,-3.38918731e-01 },
  2748. { 4.30656416e-01 ,-6.05850820e-01 , 3.56411868e-01 , 1.39871104e-01,
  2749. 1.61133433e-02, 1.95278541e-11, 4.25782008e-01 ,9.79111337e-01,
  2750. -9.56787789e-07, -3.11803801e-01 },
  2751. { -2.36239573e-01 ,-1.73015706e-01 , 3.71253420e-01 ,-4.43126520e-01,
  2752. -6.16987099e-01, -3.47112370e-11 ,-9.48308722e-02 , 2.35392910e-01,
  2753. 3.90978755e-07 ,-5.30585275e-01 },
  2754. { -9.21914745e-03 , 2.10569685e-01 , 5.86744516e-01, -3.29272382e-01,
  2755. 6.87882591e-02 ,-5.60479430e-11, -1.31813225e-01 ,-9.01748595e-02,
  2756. 1.48535772e-06 ,7.75831897e-02 },
  2757. { -3.87850229e-02 , 2.12077616e-02 , 3.81875744e-02 ,1.12848455e-01,
  2758. 1.83825444e-01, -1.18965572e-10 ,1.27163810e-01, 9.96867991e-02,
  2759. 1.84582238e-06 ,7.19917185e-02 },
  2760. { -5.18885615e-02 ,-1.74264405e-01 ,2.27000308e-01, -4.29314728e-01,
  2761. -1.17003563e-01, -2.81658523e-11 , 1.17201003e-01 ,-3.07566778e-01,
  2762. -2.01403760e-06 ,-9.42299766e-02 },
  2763. { 4.90024471e-01 , 3.71047916e-02 , 1.01772677e-01, 9.51157739e-02,
  2764. -1.71601670e-01 ,9.38432195e-11 , 6.24270660e-01, -4.71965703e-01,
  2765. -3.31362503e-07 , 4.29190240e-01 },
  2766. { 7.14228024e-02 ,-5.00837424e-02 , 1.47659839e-01 , 5.17815234e-02,
  2767. 3.52253877e-01 ,4.90728233e-11 , 2.23559528e-01 ,-2.22637845e-01,
  2768. -1.25922349e-06 , 2.04710832e-01 },
  2769. { -1.36544728e-01 ,-1.49882902e-03 , 1.16554738e-01 ,-1.07862765e-01,
  2770. 9.45728675e-02 , 2.68435951e-11 ,-2.67820297e-02, 1.89775976e-01,
  2771. -2.41099251e-06, -1.34862793e-01 } };
  2772. float w1[10][10] = { { -0.23780609 ,-0.20482226 , 0.41957468, 0.40999775, 0.38139455 ,0.49962715,
  2773. 0.08417365 , 0.29976534 ,-0.08972183 ,-0.0574983 },
  2774. { 0.07750195 ,-0.75130445 ,-0.06468749 ,-0.09033038 ,-0.18993429 ,0.82029747,
  2775. -0.61669163 , 0.01430632 , 0.07597411, 0.27505365 },
  2776. { 0.49405426, 0.01242133 ,0.25904818 ,0.41172925 ,0.07578099 ,0.06032476,
  2777. 0.46222851, 0.06112816, 0.23961187, 0.11556222 },
  2778. { 1.03532351 , 0.00237677 ,-1.41761159 , 0.2577502 ,-0.29667514 ,-0.22258016,
  2779. 1.01807614, 0.47470323 , 0.64205134 , 1.17553243 },
  2780. { -0.54341226 , 0.05698822 ,-0.63832775 ,-0.3224985, -0.08834335 ,-0.20154572,
  2781. 0.33619324 , 0.53621269 ,-0.08154711 , 0.10801987 },
  2782. { -1.43359900e-10, -2.19706358e-11 ,-7.62980145e-11 , 3.80564307e-11,
  2783. 1.43483774e-10 ,1.43472783e-10 ,-7.14971534e-11 ,-4.57497718e-11,
  2784. -7.01573521e-11 , 1.53484506e-10 },
  2785. { -0.27199681, -0.06586821 , 0.06227236 ,0.50916823 ,0.12266234 , 0.25784538,
  2786. 0.21391334 ,-0.04650493 ,-0.21507827 ,-0.81284655 },
  2787. { 0.07606676, 0.13142832 ,-0.53617744 , 0.42486831 , 0.89048667 , 0.3048935,
  2788. -0.71382582, -0.61000717 ,0.2688673 , 0.47569795 },
  2789. { 1.52424197e-07 , 6.22315298e-08 - 6.52738940e-11 ,-8.68034280e-07,
  2790. -5.31746214e-07 ,-1.78368165e-07 ,-2.11015294e-08, -1.52950988e-06,
  2791. -3.42529575e-11, 2.35200781e-07 },
  2792. { -0.9658597 ,-0.68771416, -0.14909613, -0.27739111 , 0.38529542, 0.14808103,
  2793. -0.24781566 ,-0.52709518 ,0.07968214 ,-1.09388288 } };
  2794. float w2[10][4] = { { -5.53134378e-01, -5.05213046e-02, 1.17674467e+00, 1.71470806e-10 },
  2795. { 4.66713499e-01, 4.28506910e-01, -9.50886462e-02, 1.16200494e-10 },
  2796. { 2.57455827e-01, -3.01318414e-02, -4.42330819e-01, 1.27572777e-10 },
  2797. { 4.97257812e-02 ,1.68243289e-01 ,4.46669062e-01 ,2.76339875e-11 },
  2798. { -2.00255754e-01, 3.68610428e-01 , 2.01173992e-01, -6.76783395e-11 },
  2799. { -6.26138176e-02, -5.74267944e-01, 3.32356077e-01 ,-6.16432376e-12 },
  2800. { 5.33413965e-02, -5.74108504e-02 ,-7.05800150e-01 ,-1.82851013e-10 },
  2801. { -1.06458783e+00 ,-1.52935556e-01 ,-1.45886105e+00 ,-1.01166814e-10 },
  2802. { 2.78628319e-08, 2.87125783e-01 ,-1.52646659e+00 ,-1.70692624e-10 },
  2803. { -2.66851493e-01 ,-4.95637535e-02 , 7.59249111e-01 ,-6.39687400e-11 } };
  2804. float w3[4][2] = { { 0.34269875 ,0.28160403 },{ -0.2269393 , 0.46291535 },{ 0.35116205 ,0.32428314 },{ 4.44950996e-11 ,-1.64150986e-10 } };
  2805. float b0[10] = { -0.16749457 , 0.51837033 , 0.38030122 , 0.2287536 , 0.31281455 ,-0.31270079,
  2806. -0.23713096, -0.27459556 ,-0.0780897 , 0.78092204 };
  2807. float b1[10] = { -0.19847084 , 0.51427174 ,0.16520637 , 0.02815113 ,0.14546747 ,-0.33054209,
  2808. -0.22630977 ,-0.21777402 ,-0.68041806 ,-0.51254995 };
  2809. float b2[4] = { 0.10842243 , 0.55213982 ,-0.18210444, -0.43683546 };
  2810. float b3[2] = { 0.22007598, -0.15169636 };
  2811. if (nentry < 3 || nentry >(m_nWidth - 3))
  2812. return;
  2813. for (int i = nstart; i < nend; i++)
  2814. {
  2815. if (i < 2 || i >(m_nHeight - 3))
  2816. {
  2817. image[i * m_nWidth + nentry - 1] = image[i * m_nWidth + nentry - 2];
  2818. image[i * m_nWidth + nentry] = image[i * m_nWidth + nentry + 1];
  2819. }
  2820. else
  2821. {
  2822. float x0[1][20];
  2823. int count = 0;
  2824. float maxP = 0;
  2825. float minP = 1e9;
  2826. for (int k = -3; k <= 2; k++)
  2827. {
  2828. if (k == -1 || k == 0) continue;
  2829. for (int l = -2; l <= 2; l++)
  2830. {
  2831. *((float*)x0 + count) = float(image[(i + l)* m_nWidth + (nentry + k)]);
  2832. count++;
  2833. maxP = max(maxP, float(image[(i + l)* m_nWidth + (nentry + k)]));
  2834. minP = min(minP, float(image[(i + l)* m_nWidth + (nentry + k)]));
  2835. }
  2836. }
  2837. if ((maxP - minP) > 2560)
  2838. {
  2839. maxP = min(maxP + 256, 65535.0f);
  2840. minP = max(minP - 256, 0.0f);
  2841. }
  2842. if((maxP - minP) < 1)maxP = minP + 1;
  2843. for (int k = 0; k < 20; k++)
  2844. {
  2845. *((float*)x0 + k) = (*((float*)x0 + k) - minP) / (maxP - minP);
  2846. }
  2847. float z1[1][20];
  2848. float x1[1][20];
  2849. float z2[1][10];
  2850. float x2[1][10];
  2851. float z3[1][5];
  2852. float x3[1][5];
  2853. float z4[1][2];
  2854. float y[1][2];
  2855. matMul((float**)w0, 20, 10, (float**)x0, 1, 20, b0, (float**)z1);
  2856. relu((float**)z1, 1, 10, (float**)x1);
  2857. matMul((float**)w1, 10, 10, (float**)x1, 1, 10, b1, (float**)z2);
  2858. relu((float**)z2, 1, 10, (float**)x2);
  2859. matMul((float**)w2, 10, 4, (float**)x2, 1, 10, b2, (float**)z3);
  2860. relu((float**)z3, 1, 4, (float**)x3);
  2861. matMul((float**)w3, 4, 2, (float**)x3, 1, 4, b3, (float**)z4);
  2862. relu((float**)z4, 1, 2, (float**)y);
  2863. float predict1 = max(min(*((float*)y) * (maxP - minP) + minP, 65535.0f), 0.0f);
  2864. float predict2 = max(min(*((float*)y + 1) * (maxP - minP) + minP, 65535.0f), 0.0f);
  2865. float flag1line1 = float(image[i * m_nWidth + (nentry - 3)])/ 2 + float(image[i * m_nWidth + (nentry + 1)]) / 2;
  2866. float flag1line2 = float(image[i * m_nWidth + (nentry - 2)]) / 2 + float(image[i * m_nWidth + (nentry + 2)]) / 2;
  2867. float flag1score = abs(flag1line1 - predict1) +
  2868. abs(flag1line2 - predict2);
  2869. if (flag1score < 30)
  2870. {
  2871. image[i * m_nWidth + (nentry - 1)] = WORD(max(min(flag1line1, 65535.0f), 0.0f));
  2872. image[i * m_nWidth + nentry] = WORD(max(min(flag1line2, 65535.0f), 0.0f));
  2873. }
  2874. else
  2875. {
  2876. image[i * m_nWidth + (nentry - 1)] = WORD(predict1);
  2877. image[i * m_nWidth + nentry] = WORD(predict2);
  2878. }
  2879. }
  2880. }
  2881. }
  2882. bool CPixMatrix::LoadDLineMap( const char *fileName )
  2883. {
  2884. FILE *fp;
  2885. int *line_direction = new int;
  2886. int *line_entries = new int;
  2887. int *startpoint = new int;
  2888. int *endpoint = new int;
  2889. char *data_in = NULL;
  2890. data_in = new char [MAX_PIX_MAP_LINE];
  2891. LINE_MAP2 * m_DLineMapCur = NULL;
  2892. if(data_in == NULL) {
  2893. return FALSE;
  2894. }
  2895. fp = fopen(fileName, "rb");
  2896. if(fp == NULL) {
  2897. return FALSE;
  2898. }
  2899. FreeDBadLineMap();
  2900. m_doubleLineMap = NULL;
  2901. while(fgets(data_in, MAX_PIX_MAP_LINE, fp) != NULL) {
  2902. if(sscanf(data_in, "%d,%d:%d,%d", line_direction, line_entries, startpoint, endpoint) == 4) {
  2903. // line_direction = 0;line_entries=359, startpoint=45, endpoint=2700;
  2904. m_DLineMapCur = new LINE_MAP2;
  2905. m_DLineMapCur->nLineType = *line_direction;
  2906. m_DLineMapCur->startpoint = max(2, *startpoint);
  2907. if(m_DLineMapCur->nLineType == 0)
  2908. {
  2909. m_DLineMapCur->line_entries = min(m_nHeight-1, max(1,*line_entries));
  2910. m_DLineMapCur->endpoint = min(m_nWidth-2, *endpoint);
  2911. }
  2912. else
  2913. {
  2914. m_DLineMapCur->line_entries = min(m_nWidth-1, max(1,*line_entries));
  2915. m_DLineMapCur->endpoint = min(m_nHeight-2, *endpoint);
  2916. }
  2917. m_DLineMapCur->nextline = m_doubleLineMap;
  2918. m_doubleLineMap = m_DLineMapCur;
  2919. }
  2920. }
  2921. if(fp != NULL) {
  2922. fclose(fp);
  2923. }
  2924. if(data_in != NULL) {
  2925. delete [] data_in;
  2926. }
  2927. delete line_direction;
  2928. delete line_entries;
  2929. delete startpoint;
  2930. delete endpoint;
  2931. return TRUE;
  2932. }
  2933. bool CPixMatrix::MarkAdjacentLine( LINE_MAP *m_linemap)
  2934. {
  2935. LINE_MAP *temp_linemap;
  2936. WORD lineentry, llineentry, rlineentry;
  2937. while(m_linemap)
  2938. {
  2939. lineentry = m_linemap->line_entries;
  2940. llineentry = lineentry-1;
  2941. rlineentry = lineentry+1;
  2942. temp_linemap = m_linemap->nextline;
  2943. //check through the line to find out adjacent line
  2944. while(temp_linemap)
  2945. {
  2946. lineentry = temp_linemap->line_entries;
  2947. if(lineentry == llineentry)
  2948. {
  2949. m_linemap->lLine = TRUE;
  2950. temp_linemap->rLine = TRUE;
  2951. }
  2952. if(lineentry == rlineentry)
  2953. {
  2954. m_linemap->rLine = TRUE;
  2955. temp_linemap->lLine = TRUE;
  2956. }
  2957. temp_linemap = temp_linemap->nextline;
  2958. }
  2959. //next one
  2960. m_linemap = m_linemap->nextline;
  2961. }
  2962. return TRUE;
  2963. }
  2964. void CPixMatrix::CorrectLines(WORD* image)
  2965. {
  2966. int i,i1,i2,j,j1,j2;
  2967. DWORD avepoint, avesurrond;
  2968. WORD temp;
  2969. LINE_MAP *m_LineMapCur;
  2970. LINE_MAP *temp_linemap;
  2971. LINE_MAP *left_linemap = NULL;
  2972. //first cycle
  2973. m_LineMapCur = m_xLineMap;
  2974. while(m_LineMapCur)
  2975. {
  2976. if((m_LineMapCur->lLine==FALSE)&&(m_LineMapCur->rLine==FALSE))
  2977. {
  2978. //line correction
  2979. i = m_LineMapCur->line_entries;
  2980. i1 = i-1;
  2981. i2 = i+1;
  2982. temp = image[i*m_nWidth+(m_LineMapCur->startpoint)-1];
  2983. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  2984. {
  2985. j1 = j-1;
  2986. j2 = j+1;
  2987. avepoint = (2*image[i*m_nWidth+j]+temp+image[i*m_nWidth+j2])/4;
  2988. avesurrond = (2*image[i1*m_nWidth+j]+image[i1*m_nWidth+j1]+image[i1*m_nWidth+j2]+2*image[i2*m_nWidth+j]+image[i2*m_nWidth+j1]+image[i2*m_nWidth+j2])/8;
  2989. temp = image[i*m_nWidth+j];
  2990. image[i*m_nWidth+j] = WORD(image[i*m_nWidth+j]*avesurrond/avepoint);
  2991. }
  2992. }
  2993. else if((m_LineMapCur->lLine==TRUE)&&(m_LineMapCur->rLine==FALSE))
  2994. {
  2995. //line correction
  2996. i = m_LineMapCur->line_entries;
  2997. // i1 = i-1;
  2998. i2 = i+1;
  2999. temp = image[i*m_nWidth+(m_LineMapCur->startpoint)-1];
  3000. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  3001. {
  3002. j1 = j-1;
  3003. j2 = j+1;
  3004. avepoint = (2*image[i*m_nWidth+j]+temp+image[i*m_nWidth+j2])/4;
  3005. avesurrond = (2*image[i2*m_nWidth+j]+image[i2*m_nWidth+j1]+image[i2*m_nWidth+j2])/4;
  3006. temp = image[i*m_nWidth+j];
  3007. image[i*m_nWidth+j] = WORD(image[i*m_nWidth+j]*avesurrond/avepoint);
  3008. }
  3009. }
  3010. else if((m_LineMapCur->lLine==FALSE)&&(m_LineMapCur->rLine==TRUE))
  3011. {
  3012. //line correction
  3013. i = m_LineMapCur->line_entries;
  3014. i1 = i-1;
  3015. // i2 = i+1;
  3016. temp = image[i*m_nWidth+(m_LineMapCur->startpoint)-1];
  3017. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  3018. {
  3019. j1 = j-1;
  3020. j2 = j+1;
  3021. avepoint = (2*image[i*m_nWidth+j]+temp+image[i*m_nWidth+j2])/4;
  3022. avesurrond = (2*image[i1*m_nWidth+j]+image[i1*m_nWidth+j1]+image[i1*m_nWidth+j2])/4;
  3023. temp = image[i*m_nWidth+j];
  3024. image[i*m_nWidth+j] = WORD(image[i*m_nWidth+j]*avesurrond/avepoint);
  3025. }
  3026. }
  3027. else
  3028. {
  3029. //skip, next cycle to correction
  3030. temp_linemap = new LINE_MAP;
  3031. temp_linemap->nextline = left_linemap;
  3032. temp_linemap->line_entries = m_LineMapCur->line_entries;
  3033. temp_linemap->lLine = FALSE;
  3034. temp_linemap->rLine = FALSE;
  3035. temp_linemap->startpoint = m_LineMapCur->startpoint;
  3036. temp_linemap->endpoint = m_LineMapCur->endpoint;
  3037. left_linemap = temp_linemap;
  3038. }
  3039. m_LineMapCur = m_LineMapCur->nextline;
  3040. }
  3041. while(left_linemap)
  3042. {
  3043. m_LineMapCur = left_linemap;
  3044. left_linemap = NULL;
  3045. MarkAdjacentLine(m_LineMapCur);
  3046. while(m_LineMapCur)
  3047. {
  3048. if((m_LineMapCur->lLine==FALSE)&&(m_LineMapCur->rLine==FALSE))
  3049. {
  3050. i = m_LineMapCur->line_entries;
  3051. i1 = i-1;
  3052. i2 = i+1;
  3053. temp = image[i*m_nWidth+(m_LineMapCur->startpoint)-1];
  3054. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  3055. {
  3056. j1 = j-1;
  3057. j2 = j+1;
  3058. avepoint = (2*image[i*m_nWidth+j]+temp+image[i*m_nWidth+j2])/4;
  3059. avesurrond = (2*image[i1*m_nWidth+j]+image[i1*m_nWidth+j1]+image[i1*m_nWidth+j2]+2*image[i2*m_nWidth+j]+image[i2*m_nWidth+j1]+image[i2*m_nWidth+j2])/8;
  3060. temp = image[i*m_nWidth+j];
  3061. image[i*m_nWidth+j] = WORD(image[i*m_nWidth+j]*avesurrond/avepoint);
  3062. }
  3063. }
  3064. else if((m_LineMapCur->lLine==TRUE)&&(m_LineMapCur->rLine==FALSE))
  3065. {
  3066. i = m_LineMapCur->line_entries;
  3067. // i1 = i-1;
  3068. i2 = i+1;
  3069. temp = image[i*m_nWidth+(m_LineMapCur->startpoint)-1];
  3070. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  3071. {
  3072. j1 = j-1;
  3073. j2 = j+1;
  3074. avepoint = (2*image[i*m_nWidth+j]+temp+image[i*m_nWidth+j2])/4;
  3075. avesurrond = (2*image[i2*m_nWidth+j]+image[i2*m_nWidth+j1]+image[i2*m_nWidth+j2])/4;
  3076. temp = image[i*m_nWidth+j];
  3077. image[i*m_nWidth+j] = WORD(image[i*m_nWidth+j]*avesurrond/avepoint);
  3078. }
  3079. }
  3080. else if((m_LineMapCur->lLine==FALSE)&&(m_LineMapCur->rLine==TRUE))
  3081. {
  3082. i = m_LineMapCur->line_entries;
  3083. i1 = i-1;
  3084. // i2 = i+1;
  3085. temp = image[i*m_nWidth+(m_LineMapCur->startpoint)-1];
  3086. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  3087. {
  3088. j1 = j-1;
  3089. j2 = j+1;
  3090. avepoint = (2*image[i*m_nWidth+j]+temp+image[i*m_nWidth+j2])/4;
  3091. avesurrond = (2*image[i1*m_nWidth+j]+image[i1*m_nWidth+j1]+image[i1*m_nWidth+j2])/4;
  3092. temp = image[i*m_nWidth+j];
  3093. image[i*m_nWidth+j] = WORD(image[i*m_nWidth+j]*avesurrond/avepoint);
  3094. }
  3095. }
  3096. else
  3097. {
  3098. temp_linemap = new LINE_MAP;
  3099. temp_linemap->nextline = left_linemap;
  3100. temp_linemap->line_entries = m_LineMapCur->line_entries;
  3101. temp_linemap->lLine = FALSE;
  3102. temp_linemap->rLine = FALSE;
  3103. temp_linemap->startpoint = m_LineMapCur->startpoint;
  3104. temp_linemap->endpoint = m_LineMapCur->endpoint;
  3105. left_linemap = temp_linemap;
  3106. }
  3107. temp_linemap = m_LineMapCur;
  3108. m_LineMapCur = m_LineMapCur->nextline;
  3109. delete temp_linemap;
  3110. }
  3111. }
  3112. //first cycle
  3113. m_LineMapCur = m_yLineMap;
  3114. while(m_LineMapCur)
  3115. {
  3116. if((m_LineMapCur->lLine==FALSE)&&(m_LineMapCur->rLine==FALSE))
  3117. {
  3118. //line correction
  3119. i = m_LineMapCur->line_entries;
  3120. i1 = i-1;
  3121. i2 = i+1;
  3122. temp = image[((m_LineMapCur->startpoint)-1)*m_nWidth+i];
  3123. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  3124. {
  3125. j1 = j-1;
  3126. j2 = j+1;
  3127. avepoint = (2*image[j*m_nWidth+i]+temp+image[j2*m_nWidth+i])/4;
  3128. avesurrond = (2*image[j*m_nWidth+i1]+image[j1*m_nWidth+i1]+image[j2*m_nWidth+i1]+2*image[j*m_nWidth+i2]+image[j1*m_nWidth+i2]+image[j2*m_nWidth+i2])/8;
  3129. temp = image[j*m_nWidth+i];
  3130. image[j*m_nWidth+i] = WORD(image[j*m_nWidth+i]*avesurrond/avepoint);
  3131. }
  3132. }
  3133. else if((m_LineMapCur->lLine==TRUE)&&(m_LineMapCur->rLine==FALSE))
  3134. {
  3135. //line correction
  3136. i = m_LineMapCur->line_entries;
  3137. // i1 = i-1;
  3138. i2 = i+1;
  3139. temp = image[((m_LineMapCur->startpoint)-1)*m_nWidth+i];
  3140. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  3141. {
  3142. j1 = j-1;
  3143. j2 = j+1;
  3144. avepoint = (2*image[j*m_nWidth+i]+temp+image[j2*m_nWidth+i])/4;
  3145. avesurrond = (2*image[j*m_nWidth+i2]+image[j1*m_nWidth+i2]+image[j2*m_nWidth+i2])/4;
  3146. temp = image[j*m_nWidth+i];
  3147. image[j*m_nWidth+i] = WORD(image[j*m_nWidth+i]*avesurrond/avepoint);
  3148. }
  3149. }
  3150. else if((m_LineMapCur->lLine==FALSE)&&(m_LineMapCur->rLine==TRUE))
  3151. {
  3152. //line correction
  3153. i = m_LineMapCur->line_entries;
  3154. i1 = i-1;
  3155. // i2 = i+1;
  3156. temp = image[((m_LineMapCur->startpoint)-1)*m_nWidth+i];
  3157. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  3158. {
  3159. j1 = j-1;
  3160. j2 = j+1;
  3161. avepoint = (2*image[j*m_nWidth+i]+temp+image[j2*m_nWidth+i])/4;
  3162. avesurrond = (2*image[j*m_nWidth+i1]+image[j1*m_nWidth+i1]+image[j2*m_nWidth+i1])/4;
  3163. temp = image[j*m_nWidth+i];
  3164. image[j*m_nWidth+i] = WORD(image[j*m_nWidth+i]*avesurrond/avepoint);
  3165. }
  3166. }
  3167. else
  3168. {
  3169. //skip, next cycle to correction
  3170. temp_linemap = new LINE_MAP;
  3171. temp_linemap->nextline = left_linemap;
  3172. temp_linemap->line_entries = m_LineMapCur->line_entries;
  3173. temp_linemap->lLine = FALSE;
  3174. temp_linemap->rLine = FALSE;
  3175. temp_linemap->startpoint = m_LineMapCur->startpoint;
  3176. temp_linemap->endpoint = m_LineMapCur->endpoint;
  3177. left_linemap = temp_linemap;
  3178. }
  3179. m_LineMapCur = m_LineMapCur->nextline;
  3180. }
  3181. while(left_linemap)
  3182. {
  3183. m_LineMapCur = left_linemap;
  3184. left_linemap = NULL;
  3185. MarkAdjacentLine(m_LineMapCur);
  3186. while(m_LineMapCur)
  3187. {
  3188. if((m_LineMapCur->lLine==FALSE)&&(m_LineMapCur->rLine==FALSE))
  3189. {
  3190. i = m_LineMapCur->line_entries;
  3191. i1 = i-1;
  3192. i2 = i+1;
  3193. temp = image[((m_LineMapCur->startpoint)-1)*m_nWidth+i];
  3194. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  3195. {
  3196. j1 = j-1;
  3197. j2 = j+1;
  3198. avepoint = (2*image[j*m_nWidth+i]+temp+image[j2*m_nWidth+i])/4;
  3199. avesurrond = (2*image[j*m_nWidth+i1]+image[j1*m_nWidth+i1]+image[j2*m_nWidth+i1]+2*image[j*m_nWidth+i2]+image[j1*m_nWidth+i2]+image[j2*m_nWidth+i2])/8;
  3200. temp = image[j*m_nWidth+i];
  3201. image[j*m_nWidth+i] = WORD(image[j*m_nWidth+i]*avesurrond/avepoint);
  3202. }
  3203. }
  3204. else if((m_LineMapCur->lLine==TRUE)&&(m_LineMapCur->rLine==FALSE))
  3205. {
  3206. i = m_LineMapCur->line_entries;
  3207. // i1 = i-1;
  3208. i2 = i+1;
  3209. temp = image[((m_LineMapCur->startpoint)-1)*m_nWidth+i];
  3210. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  3211. {
  3212. j1 = j-1;
  3213. j2 = j+1;
  3214. avepoint = (2*image[j*m_nWidth+i]+temp+image[j2*m_nWidth+i])/4;
  3215. avesurrond = (2*image[j*m_nWidth+i2]+image[j1*m_nWidth+i2]+image[j2*m_nWidth+i2])/4;
  3216. temp = image[j*m_nWidth+i];
  3217. image[j*m_nWidth+i] = WORD(image[j*m_nWidth+i]*avesurrond/avepoint);
  3218. }
  3219. }
  3220. else if((m_LineMapCur->lLine==FALSE)&&(m_LineMapCur->rLine==TRUE))
  3221. {
  3222. i = m_LineMapCur->line_entries;
  3223. i1 = i-1;
  3224. // i2 = i+1;
  3225. temp = image[((m_LineMapCur->startpoint)-1)*m_nWidth+i];
  3226. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  3227. {
  3228. j1 = j-1;
  3229. j2 = j+1;
  3230. avepoint = (2*image[j*m_nWidth+i]+temp+image[j2*m_nWidth+i])/4;
  3231. avesurrond = (2*image[j*m_nWidth+i1]+image[j1*m_nWidth+i1]+image[j2*m_nWidth+i1])/4;
  3232. temp = image[j*m_nWidth+i];
  3233. image[j*m_nWidth+i] = WORD(image[j*m_nWidth+i]*avesurrond/avepoint);
  3234. }
  3235. }
  3236. else
  3237. {
  3238. temp_linemap = new LINE_MAP;
  3239. temp_linemap->nextline = left_linemap;
  3240. temp_linemap->line_entries = m_LineMapCur->line_entries;
  3241. temp_linemap->lLine = FALSE;
  3242. temp_linemap->rLine = FALSE;
  3243. temp_linemap->startpoint = m_LineMapCur->startpoint;
  3244. temp_linemap->endpoint = m_LineMapCur->endpoint;
  3245. left_linemap = temp_linemap;
  3246. }
  3247. temp_linemap = m_LineMapCur;
  3248. m_LineMapCur = m_LineMapCur->nextline;
  3249. delete temp_linemap;
  3250. }
  3251. }
  3252. }
  3253. bool CPixMatrix::FreeLineMap(LINE_MAP *m_linemap)
  3254. {
  3255. LINE_MAP *temp_linemap;
  3256. while(m_linemap)
  3257. {
  3258. temp_linemap = m_linemap;
  3259. m_linemap = m_linemap->nextline;
  3260. delete temp_linemap;
  3261. }
  3262. m_linemap = NULL;
  3263. return TRUE;
  3264. }
  3265. //开始自动坏点校正
  3266. void CPixMatrix::BeginAutoBadPixels()
  3267. {
  3268. //if(m_lastImg)
  3269. //{
  3270. // delete [] m_lastImg;
  3271. // m_lastImg = NULL;
  3272. //}
  3273. if(m_mapImg)
  3274. {
  3275. delete [] m_mapImg;
  3276. m_mapImg = NULL;
  3277. }
  3278. //m_lastImg = new WORD[m_nWidth*m_nHeight];
  3279. m_mapImg = new WORD[m_nWidth*m_nHeight];
  3280. ASSERT(m_mapImg);
  3281. ZeroMemory(m_mapImg, m_nWidth*m_nHeight*sizeof(WORD));
  3282. m_curAvg = -1;
  3283. m_lastAvg = -1;
  3284. }
  3285. void CPixMatrix::AddImageforBadPixels(WORD *wImage)
  3286. {
  3287. if (wImage == NULL)
  3288. {
  3289. return;
  3290. }
  3291. //m_curImg = wImage;////////////////////////chenGN 2013.01.31
  3292. int ntempHOffset = max(0, m_nHOffset);
  3293. int ntempWOffset = max(0, m_nWOffset);
  3294. bool bFlag = DetBadPxlByMF(wImage, m_nWidth, m_nHeight, ntempWOffset, ntempHOffset, m_mapImg);////////////////////////chenGN 2013.01.31
  3295. if (!bFlag)
  3296. {
  3297. delete []m_mapImg;
  3298. return;
  3299. }
  3300. //old code delete 20100906
  3301. //calculate avg pv
  3302. /*DWORD dwSum = 0;
  3303. DWORD dwIndex = 0;
  3304. int i, j;
  3305. DWORD dwCts = 0;
  3306. for(i = ntempHOffset; i < m_nHeight-ntempHOffset; i += 8) {
  3307. dwIndex = i*m_nWidth;
  3308. for(j = ntempWOffset; j < m_nWidth-ntempWOffset; j+= 8) {
  3309. dwSum += (*(m_curImg + dwIndex + j));
  3310. dwCts++;
  3311. }
  3312. }
  3313. m_curAvg = dwSum/dwCts;
  3314. //find badpixel by thresdhold;
  3315. int thresdhold1, thresdhold2;
  3316. thresdhold1 = m_curAvg*0.6;
  3317. thresdhold2 = (m_curAvg*1.4>65535?65535:m_curAvg*1.4);
  3318. for(i = ntempHOffset; i < m_nHeight-ntempHOffset; i ++)
  3319. {
  3320. dwIndex = i*m_nWidth;
  3321. for(j = ntempWOffset; j < m_nWidth-ntempWOffset; j++)
  3322. {
  3323. if(( *(m_curImg + dwIndex + j) < thresdhold1)||( *(m_curImg + dwIndex + j) > thresdhold2))
  3324. {
  3325. *(m_mapImg + dwIndex + j) = 0xFFFF;
  3326. }
  3327. }
  3328. }
  3329. AutoBadPointDec(m_curImg,m_nWidth, m_nHeight ,ntempHOffset, ntempWOffset,m_mapImg);
  3330. //check status;
  3331. if(m_lastAvg<0)
  3332. {
  3333. memcpy(m_lastImg, m_curImg, m_nHeight*m_nWidth*sizeof(WORD));
  3334. m_lastAvg = m_curAvg;
  3335. m_curAvg = -1;
  3336. //no last Image;
  3337. return;
  3338. }
  3339. else if(abs(m_curAvg - m_lastAvg)<500)
  3340. {
  3341. //not too many difference;
  3342. return;
  3343. }
  3344. else
  3345. {
  3346. double thresdhold1 = (((double) m_curAvg) / m_lastAvg)*0.9;
  3347. double thresdhold2 = (((double) m_curAvg) / m_lastAvg)*1.1;
  3348. double temp=0.0;
  3349. for(i = ntempHOffset; i < m_nHeight-ntempHOffset; i ++)
  3350. {
  3351. dwIndex = i*m_nWidth;
  3352. for(j = ntempWOffset; j < m_nWidth-ntempWOffset; j++)
  3353. {
  3354. if (*(m_lastImg + dwIndex + j)==0)
  3355. continue;
  3356. temp=((double)*(m_curImg + dwIndex + j))/(*(m_lastImg + dwIndex + j));
  3357. if((temp < thresdhold1)||(temp>thresdhold2))
  3358. {
  3359. *(m_mapImg + dwIndex + j) = 0xFFFF;
  3360. }
  3361. }
  3362. }
  3363. memcpy(m_lastImg, m_curImg, m_nHeight*m_nWidth*sizeof(WORD));
  3364. m_lastAvg = m_curAvg;
  3365. m_curAvg = -1;
  3366. }
  3367. */
  3368. }
  3369. //结束自动换点校正,-1,放弃;0,替换原有map;1,合并原有map
  3370. void CPixMatrix::EndAutoBadPixels(int nMode)
  3371. {
  3372. bool bReturn = true;
  3373. if(nMode == 0)
  3374. {
  3375. //0,替换原有map
  3376. FreeBadPixelMap();
  3377. bReturn = AutoBadPixelMap2(m_mapImg);
  3378. if (bReturn)
  3379. {
  3380. SaveBadPixelMap(m_charFilename);
  3381. }
  3382. }
  3383. else if(nMode == 1)
  3384. {
  3385. //1,合并原有map
  3386. bReturn = AutoBadPixelMap2(m_mapImg);
  3387. if (bReturn)
  3388. {
  3389. SaveBadPixelMap(m_charFilename);
  3390. }
  3391. }
  3392. else
  3393. {
  3394. //-1,放弃
  3395. }
  3396. //if(m_lastImg)////////////////////////chenGN 2013.01.31
  3397. //{
  3398. // delete [] m_lastImg;
  3399. // m_lastImg = NULL;
  3400. //}
  3401. if(m_mapImg)
  3402. {
  3403. delete [] m_mapImg;
  3404. m_mapImg = NULL;
  3405. }
  3406. m_curAvg = -1;
  3407. m_lastAvg = -1;
  3408. }
  3409. //add by wangyb 2013 for replace the previous function EndAutoBadPixels()
  3410. //the purpose is to carry on the same process with Gain Calibration;
  3411. //结束自动换点校正,-1,放弃;0,替换原有map;1,合并原有map
  3412. void CPixMatrix::StoreBadPixels(const char * charFilename,int nMode)
  3413. {
  3414. bool bReturn = true;
  3415. if(nMode == 0)
  3416. {
  3417. //0,替换原有map
  3418. FreeBadPixelMap();
  3419. bReturn = AutoBadPixelMap2(m_mapImg);
  3420. if (bReturn)
  3421. {
  3422. SaveBadPixelMap(charFilename);
  3423. }
  3424. }
  3425. else if(nMode == 1)
  3426. {
  3427. //1,合并原有map
  3428. bReturn = AutoBadPixelMap2(m_mapImg);
  3429. if (bReturn)
  3430. {
  3431. SaveBadPixelMap(charFilename);
  3432. }
  3433. }
  3434. else
  3435. {
  3436. //-1,放弃
  3437. }
  3438. //if(m_lastImg)////////////////////////chenGN 2013.01.31
  3439. //{
  3440. // delete [] m_lastImg;
  3441. // m_lastImg = NULL;
  3442. //}
  3443. if(m_mapImg)
  3444. {
  3445. delete [] m_mapImg;
  3446. m_mapImg = NULL;
  3447. }
  3448. m_curAvg = -1;
  3449. m_lastAvg = -1;
  3450. }
  3451. void CPixMatrix::CorrectButCross(WORD* image)
  3452. {
  3453. }
  3454. bool CPixMatrix::AutoBadPointDec(unsigned short* lpDetail,int lWidth, int lHeight ,int Xoffset, int Yoffset,unsigned short* pmap)
  3455. {
  3456. unsigned short* lpNewDataBits =lpDetail;
  3457. // new unsigned short[lWidth*lHeight];
  3458. // memcpy(lpNewDataBits,lpDetail,sizeof(unsigned short)*lWidth*lHeight);
  3459. int sort1[4];//big
  3460. int sort2[4];//small
  3461. int sort3[3];//big
  3462. int sort4[3];//small
  3463. double Neighbor_sum=0;
  3464. double Neighbor_avg=0;
  3465. long i,j; //for loop
  3466. int temp;
  3467. int moveoff;
  3468. int locallength=50; //区域直方图长度
  3469. int start_avg;
  3470. start_avg=GetAvg(lpNewDataBits,lWidth,lHeight,Xoffset ,Yoffset,locallength);
  3471. //
  3472. int last_avg=start_avg;
  3473. for(i = Xoffset; i<lHeight-Xoffset; i++)
  3474. {
  3475. // if (i+locallength+Xoffset>=lHeight)
  3476. // moveoff=lHeight-Xoffset-locallength;
  3477. // else
  3478. // moveoff=i;
  3479. // start_avg=GetAvg(lpNewDataBits,lWidth,lHeight,moveoff,Yoffset,locallength);
  3480. start_avg=last_avg;
  3481. for(j = Yoffset; j<lWidth-Yoffset; j++)
  3482. {
  3483. Neighbor_sum=0.0;
  3484. Neighbor_avg=0.0;
  3485. if(lpNewDataBits[(i-1)*lWidth+j-1]>lpNewDataBits[(i+1)*lWidth+j+1])
  3486. {
  3487. sort1[0] = lpNewDataBits[(i-1)*lWidth+j-1];
  3488. sort2[0] = lpNewDataBits[(i+1)*lWidth+j+1];
  3489. }
  3490. else
  3491. {
  3492. sort1[0] = lpNewDataBits[(i+1)*lWidth+j+1];
  3493. sort2[0] = lpNewDataBits[(i-1)*lWidth+j-1];
  3494. }
  3495. if(lpNewDataBits[(i-1)*lWidth+j]>lpNewDataBits[(i+1)*lWidth+j])
  3496. {
  3497. sort1[1] = lpNewDataBits[(i-1)*lWidth+j];
  3498. sort2[1] = lpNewDataBits[(i+1)*lWidth+j];
  3499. }
  3500. else
  3501. {
  3502. sort1[1] = lpNewDataBits[(i+1)*lWidth+j];
  3503. sort2[1] = lpNewDataBits[(i-1)*lWidth+j];
  3504. }
  3505. if(lpNewDataBits[(i-1)*lWidth+j+1]>lpNewDataBits[(i+1)*lWidth+j-1])
  3506. {
  3507. sort1[2] = lpNewDataBits[(i-1)*lWidth+j+1];
  3508. sort2[2] = lpNewDataBits[(i+1)*lWidth+j-1];
  3509. }
  3510. else
  3511. {
  3512. sort1[2] = lpNewDataBits[(i+1)*lWidth+j-1];
  3513. sort2[2] = lpNewDataBits[(i-1)*lWidth+j+1];
  3514. }
  3515. if(lpNewDataBits[i*lWidth+j-1]>lpNewDataBits[i*lWidth+j+1])
  3516. {
  3517. sort1[3] = lpNewDataBits[i*lWidth+j-1];
  3518. sort2[3] = lpNewDataBits[i*lWidth+j+1];
  3519. }
  3520. else
  3521. {
  3522. sort1[3] = lpNewDataBits[i*lWidth+j+1];
  3523. sort2[3] = lpNewDataBits[i*lWidth+j-1];
  3524. }
  3525. //delete the max of the sort1[4];
  3526. if (sort1[0]>sort1[2])
  3527. {
  3528. temp=sort1[2];
  3529. sort1[2]=sort1[0];
  3530. sort1[0]=temp;
  3531. }
  3532. if (sort1[1]>sort1[3])
  3533. {
  3534. temp=sort1[3];
  3535. sort1[3]=sort1[1];
  3536. sort1[1]=temp;
  3537. }
  3538. if (sort1[2]>sort1[3])
  3539. {
  3540. sort1[2]=sort1[3];
  3541. }
  3542. if (sort2[0]<sort2[2])
  3543. {
  3544. temp=sort2[2];
  3545. sort2[2]=sort2[0];
  3546. sort2[0]=temp;
  3547. }
  3548. if (sort2[1]<sort2[3])
  3549. {
  3550. temp=sort2[3];
  3551. sort2[3]=sort2[1];
  3552. sort2[1]=temp;
  3553. }
  3554. if (sort2[2]<sort2[3])
  3555. {
  3556. sort2[2]=sort2[3];
  3557. }
  3558. //在剩下的中找最值,重新生成三组值.
  3559. if (sort1[0]>sort2[0])
  3560. {
  3561. sort3[0]=sort1[0];
  3562. sort4[0]=sort2[0];
  3563. }
  3564. else
  3565. {
  3566. sort3[0]=sort2[0];
  3567. sort4[0]=sort1[0];
  3568. }
  3569. if (sort1[1]>sort2[1])
  3570. {
  3571. sort3[1]=sort1[1];
  3572. sort4[1]=sort2[1];
  3573. }
  3574. else
  3575. {
  3576. sort3[1]=sort2[1];
  3577. sort4[1]=sort1[1];
  3578. }
  3579. if (sort1[2]>sort2[2])
  3580. {
  3581. sort3[2]=sort1[2];
  3582. sort4[2]=sort2[2];
  3583. }
  3584. else
  3585. {
  3586. sort3[2]=sort2[2];
  3587. sort4[2]=sort1[2];
  3588. }
  3589. //将这六个值相加,后减去一个最大值和最小值.
  3590. Neighbor_sum=sort3[0]+sort3[1]+sort3[2]+sort4[0]+sort4[1]+sort4[2];
  3591. //剩余三个点计算均值
  3592. if (sort3[0]>sort3[1])
  3593. {
  3594. if (sort3[0]>sort3[2])
  3595. Neighbor_sum= Neighbor_sum-sort3[0];
  3596. else
  3597. Neighbor_sum= Neighbor_sum-sort3[2];
  3598. }
  3599. else
  3600. {
  3601. if (sort3[1]>sort3[2])
  3602. Neighbor_sum= Neighbor_sum-sort3[1];
  3603. else
  3604. Neighbor_sum= Neighbor_sum-sort3[2];
  3605. }
  3606. //找最小值
  3607. if (sort4[0]<sort4[1])
  3608. {
  3609. if (sort4[0]<sort4[2])
  3610. Neighbor_sum= Neighbor_sum-sort4[0];
  3611. else
  3612. Neighbor_sum= Neighbor_sum-sort4[2];
  3613. }
  3614. else
  3615. {
  3616. if (sort4[1]<sort4[2])
  3617. Neighbor_sum= Neighbor_sum-sort4[1];
  3618. else
  3619. Neighbor_sum= Neighbor_sum-sort4[2];
  3620. }
  3621. Neighbor_avg=Neighbor_sum/4.0;
  3622. if ((Neighbor_avg<start_avg*1.3)&&(Neighbor_avg>start_avg*0.8))
  3623. {
  3624. start_avg=Neighbor_avg;
  3625. }
  3626. else
  3627. {
  3628. int tempccc = 0;
  3629. }
  3630. if((*(lpNewDataBits+i*lWidth+j)>start_avg*1.2)||(*(lpNewDataBits+i*lWidth+j)<start_avg*0.8))
  3631. {
  3632. *(pmap+i*lWidth+j)=0xFFFF;
  3633. }
  3634. if (j==Yoffset)
  3635. {
  3636. last_avg=start_avg;
  3637. }
  3638. }
  3639. }
  3640. return true;
  3641. }
  3642. int CPixMatrix::GetAvg(unsigned short* pData,int Width, int Height ,int Xoffset, int Yoffset, int length)
  3643. {
  3644. int i,j;
  3645. int index;
  3646. int *hist = new int[PIXEL_MAX_VALUE];
  3647. for (i=0;i<65535;++i)
  3648. hist[i]=0;
  3649. double sum=0.0;
  3650. double sumsum=0.0;
  3651. int avg=0;
  3652. double rate;
  3653. int count=0;
  3654. for (i=Xoffset; i<Xoffset+length;++i)
  3655. {
  3656. index=i*Width;
  3657. for (j=Yoffset ; j<Yoffset+length;++j)
  3658. {
  3659. ++hist[*(pData+index+j)];
  3660. }
  3661. }
  3662. for (i=0;i<65535;++i)
  3663. {
  3664. sum+=hist[i];
  3665. rate=sum/(length*length);
  3666. if ((rate>0.2)&&(rate<0.8))
  3667. {
  3668. count+=hist[i];
  3669. sumsum+=hist[i]*i;
  3670. }
  3671. }
  3672. avg=sumsum/count;
  3673. delete []hist;
  3674. return avg;
  3675. }
  3676. //code begin 20100906
  3677. /************************************************************************
  3678. FUNCTION NAME: Mean7
  3679. DESCRIPTION: Filter raw image by 7 * 7 mean filter
  3680. RETURN VALUE:
  3681. PARA: [IN\OUT] pImgData: raw image data
  3682. [IN] nWidth: raw image width
  3683. [IN] nHeight: raw image height
  3684. [IN] nXOffset: raw image's offset in x direction
  3685. [IN] nYOffset: raw image's offset in y direction
  3686. HISTORY: Aug\24\2010 written by Alex Stocks
  3687. ************************************************************************/
  3688. /*
  3689. int CPixMatrix::Mean7(ushort_t *pImgData, int nWidth, int nHeight, int nXOffset, int nYOffset)
  3690. {
  3691. if (NULL == pImgData || nWidth <= 2 * nXOffset + 7 || nXOffset < 0
  3692. || nHeight <= 2 * nYOffset + 7 || nYOffset < 0)
  3693. {
  3694. return -1;
  3695. }
  3696. HGLOBAL hImgBuffer = ::GlobalAlloc(GHND, nHeight * nWidth * sizeof(ushort_t));
  3697. if (NULL == hImgBuffer)
  3698. {
  3699. ::GlobalUnlock(hImgBuffer);
  3700. return -1;
  3701. }
  3702. ushort_t* pImgBuffer = (ushort_t*) ::GlobalLock(hImgBuffer);
  3703. int nMinXIdx = nXOffset;
  3704. int nMaxXIdx = nWidth - nXOffset;
  3705. int nMinYIdx = nYOffset;
  3706. int nMaxYIdx = nHeight - nYOffset;
  3707. int nIdxI = 0;
  3708. int nIdxJ = 0;
  3709. int nIndex = 0;
  3710. ushort_t* pBuffer = NULL;
  3711. ushort_t* pData = NULL;
  3712. for (nIdxI = nMinYIdx; nIdxI < nMaxYIdx; ++nIdxI)
  3713. {
  3714. nIndex = nIdxI * nWidth + nMinXIdx;
  3715. pBuffer = pImgBuffer + nIndex;
  3716. pData = pImgData + nIndex;
  3717. *(pBuffer) = (*(pData) + 2 * (*(pData + 1)) + 2 * (*(pData + 2)) + 2 * (*(pData + 3))) / 7;
  3718. *(pBuffer + 1) = (*(pData) + 2 * (*(pData + 1)) + 2 * (*(pData + 2)) + *(pData + 3) + *(pData + 4)) / 7;
  3719. *(pBuffer + 2) = (*(pData) + 2 *(*(pData + 1)) + *(pData + 2) + *(pData + 3) + *(pData + 4) + *(pData + 5)) / 7;
  3720. nIndex -= nMinXIdx;
  3721. for (nIdxJ = nMinXIdx + 3; nIdxJ < nMaxXIdx - 3; ++nIdxJ)
  3722. {
  3723. *(pBuffer + nIdxJ) = (*(pData + nIdxJ - 3) + *(pData + nIdxJ - 2)
  3724. + *(pData + nIdxJ - 1) + *(pData + nIdxJ)
  3725. + *(pData + nIdxJ + 1) + *(pData + nIdxJ + 2)
  3726. + *(pData + nIdxJ + 3)) / 7;
  3727. }
  3728. nIndex += nMaxXIdx;
  3729. pBuffer = pImgBuffer + nIndex;
  3730. pData = pImgData + nIndex;
  3731. *(pBuffer - 3) = (*(pData - 6) + *(pData - 5) + *(pData - 4)
  3732. + *(pData - 3) + 2 * (*(pData - 2)) + *(pData - 1)) / 7;
  3733. *(pBuffer - 2) = (*(pData -5) + *(pData -4) + 2 * (*(pData -3))
  3734. + 2 * (*(pData -2)) + *(pData -1)) / 7;
  3735. *(pBuffer - 1) = (2 * (*(pData - 4)) + 2 * (*(pData - 3))
  3736. + 2 * (*(pData - 2)) + *(pData - 1) ) / 7;
  3737. }
  3738. int nInc1 = nWidth * 1;
  3739. int nInc2 = nWidth * 2;
  3740. int nInc3 = nWidth * 3;
  3741. int nInc4 = nWidth * 4;
  3742. int nInc5 = nWidth * 5;
  3743. int nInc6 = nWidth * 6;
  3744. int nGVSum = 0;
  3745. int nStartYPos = 0;
  3746. int nEndYPos = 0;
  3747. for (nIdxI = nMinXIdx; nIdxI < nMaxXIdx; ++nIdxI)
  3748. {
  3749. nIndex = nMinYIdx * nWidth + nIdxI;
  3750. pBuffer = pImgBuffer + nIndex;
  3751. pData = pImgData + nIndex;
  3752. *(pBuffer) = (*(pData) + 2 * (*(pData + nInc1)) + 2 * (*(pData + nInc2)) + 2 * (*(pData + nInc3))) / 7;
  3753. *(pBuffer + nInc1) = (*(pData) + 2 * (*(pData + nInc1)) + 2 * (*(pData + nInc2)) + *(pData + nInc3) + *(pData + nInc4)) / 7;
  3754. *(pBuffer + nInc2) = (*(pData) + 2 *(*(pData + nInc1)) + *(pData + nInc2) + *(pData + nInc3) + *(pData + nInc4) + *(pData + nInc5)) / 7;
  3755. nIndex -= nMinYIdx * nWidth;
  3756. nGVSum = *(pData) + *(pData + nInc1) + *(pData + nInc2) + *(pData + nInc3) + *(pData + nInc4) + *(pData + nInc5);
  3757. nStartYPos = (nMinYIdx + 3) * nWidth + nIdxI;
  3758. nEndYPos = (nMaxYIdx - 3) * nWidth + nIdxI;
  3759. pBuffer = pImgBuffer;
  3760. pData = pImgData;
  3761. for (nIdxJ = nStartYPos; nIdxJ < nEndYPos; nIdxJ += nWidth)
  3762. {
  3763. nGVSum += *(pData + nIdxJ + nInc3);
  3764. *(pBuffer + nIdxJ) = nGVSum / 7;
  3765. nGVSum -= *(pBuffer + nIdxJ - nInc3);
  3766. }
  3767. nIndex = nMaxYIdx * nWidth + nIdxI;
  3768. pBuffer = pImgBuffer + nIndex;
  3769. pData = pImgData + nIndex;
  3770. *(pBuffer - nInc3) = (*(pData - nInc6) + *(pData - nInc5) + *(pData - nInc4) + *(pData - nInc3) + 2 * (*(pData - nInc2)) + *(pData - nInc1)) / 7;
  3771. *(pBuffer - nInc2) = (*(pData - nInc5) + *(pData - nInc4) + 2 * (*(pData - nInc3)) + 2 * (*(pData - nInc2)) + *(pData - nInc1)) / 7;
  3772. *(pBuffer - nInc1) = (2 * (*(pData - nInc4)) + 2 * (*(pData - nInc3)) + 2 * (*(pData - nInc2)) + *(pData - nInc1) ) / 7;
  3773. }
  3774. memcpy(pImgData, pImgBuffer, nHeight * nWidth * sizeof(ushort_t));
  3775. ::GlobalUnlock(hImgBuffer);
  3776. ::GlobalFree(hImgBuffer);
  3777. return 1;
  3778. }
  3779. */
  3780. int CPixMatrix::Mean7(ushort_t *pImgData, int nWidth, int nHeight, int nXOffset, int nYOffset)
  3781. {
  3782. if (NULL == pImgData || nWidth <= 2 * nXOffset + 7 || nXOffset < 0
  3783. || nHeight <= 2 * nYOffset + 7 || nYOffset < 0)
  3784. {
  3785. return -1;
  3786. }
  3787. HGLOBAL hImgBuffer = ::GlobalAlloc(GHND, nHeight * nWidth * sizeof(ushort_t));
  3788. if (NULL == hImgBuffer)
  3789. {
  3790. ::GlobalUnlock(hImgBuffer);
  3791. return -1;
  3792. }
  3793. ushort_t* pImgBuffer = (ushort_t*) ::GlobalLock(hImgBuffer);
  3794. int nMinXIdx = nXOffset;
  3795. int nMaxXIdx = nWidth - nXOffset;
  3796. int nMinYIdx = nYOffset;
  3797. int nMaxYIdx = nHeight - nYOffset;
  3798. int nIdxI = 0;
  3799. int nIdxJ = 0;
  3800. int nIndex = 0;
  3801. ushort_t* pBuffer = NULL;
  3802. ushort_t* pData = NULL;
  3803. for (nIdxI = nMinYIdx; nIdxI < nMaxYIdx; ++nIdxI)
  3804. {
  3805. nIndex = nIdxI * nWidth + nMinXIdx;
  3806. pBuffer = pImgBuffer + nIndex;
  3807. pData = pImgData + nIndex;
  3808. *(pBuffer) = (*(pData) + 2 * (*(pData + 1)) + 2 * (*(pData + 2)) + 2 * (*(pData + 3))) / 7;
  3809. *(pBuffer + 1) = (*(pData) + 2 * (*(pData + 1)) + 2 * (*(pData + 2)) + *(pData + 3) + *(pData + 4)) / 7;
  3810. *(pBuffer + 2) = (*(pData) + 2 *(*(pData + 1)) + *(pData + 2) + *(pData + 3) + *(pData + 4) + *(pData + 5)) / 7;
  3811. nIndex -= nMinXIdx;
  3812. pBuffer = pImgBuffer + nIndex;
  3813. pData = pImgData + nIndex;
  3814. for (nIdxJ = nMinXIdx + 3; nIdxJ < nMaxXIdx - 3; ++nIdxJ)
  3815. {
  3816. *(pBuffer + nIdxJ) = (*(pData + nIdxJ - 3) + *(pData + nIdxJ - 2)
  3817. + *(pData + nIdxJ - 1) + *(pData + nIdxJ)
  3818. + *(pData + nIdxJ + 1) + *(pData + nIdxJ + 2)
  3819. + *(pData + nIdxJ + 3)) / 7;
  3820. }
  3821. nIndex += nMaxXIdx;
  3822. pBuffer = pImgBuffer + nIndex;
  3823. pData = pImgData + nIndex;
  3824. *(pBuffer - 3) = (*(pData - 6) + *(pData - 5) + *(pData - 4)
  3825. + *(pData - 3) + 2 * (*(pData - 2)) + *(pData - 1)) / 7;
  3826. *(pBuffer - 2) = (*(pData -5) + *(pData -4) + 2 * (*(pData -3))
  3827. + 2 * (*(pData -2)) + *(pData -1)) / 7;
  3828. *(pBuffer - 1) = (2 * (*(pData - 4)) + 2 * (*(pData - 3))
  3829. + 2 * (*(pData - 2)) + *(pData - 1) ) / 7;
  3830. }
  3831. memcpy(pImgData, pImgBuffer, nHeight * nWidth * sizeof(ushort_t));
  3832. int nInc1 = nWidth * 1;
  3833. int nInc2 = nWidth * 2;
  3834. int nInc3 = nWidth * 3;
  3835. int nInc4 = nWidth * 4;
  3836. int nInc5 = nWidth * 5;
  3837. int nInc6 = nWidth * 6;
  3838. int nGVSum = 0;
  3839. int nStartYPos = 0;
  3840. int nEndYPos = 0;
  3841. for (nIdxI = nMinXIdx; nIdxI < nMaxXIdx; ++nIdxI)
  3842. {
  3843. nIndex = nMinYIdx * nWidth + nIdxI;
  3844. pBuffer = pImgBuffer + nIndex;
  3845. pData = pImgData + nIndex;
  3846. *(pBuffer) = (*(pData) + 2 * (*(pData + nInc1)) + 2 * (*(pData + nInc2)) + 2 * (*(pData + nInc3))) / 7;
  3847. *(pBuffer + nInc1) = (*(pData) + 2 * (*(pData + nInc1)) + 2 * (*(pData + nInc2)) + *(pData + nInc3) + *(pData + nInc4)) / 7;
  3848. *(pBuffer + nInc2) = (*(pData) + 2 *(*(pData + nInc1)) + *(pData + nInc2) + *(pData + nInc3) + *(pData + nInc4) + *(pData + nInc5)) / 7;
  3849. nIndex -= nMinYIdx * nWidth;
  3850. nGVSum = *(pData) + *(pData + nInc1) + *(pData + nInc2) + *(pData + nInc3) + *(pData + nInc4) + *(pData + nInc5);
  3851. nStartYPos = (nMinYIdx + 3) * nWidth + nIdxI;
  3852. nEndYPos = (nMaxYIdx - 3) * nWidth + nIdxI;
  3853. pBuffer = pImgBuffer;
  3854. pData = pImgData;
  3855. for (nIdxJ = nStartYPos; nIdxJ < nEndYPos; nIdxJ += nWidth)
  3856. {
  3857. nGVSum += *(pData + nIdxJ + nInc3);
  3858. *(pBuffer + nIdxJ) = nGVSum / 7;
  3859. nGVSum -= *(pBuffer + nIdxJ - nInc3);
  3860. }
  3861. nIndex = nMaxYIdx * nWidth + nIdxI;
  3862. pBuffer = pImgBuffer + nIndex;
  3863. pData = pImgData + nIndex;
  3864. *(pBuffer - nInc3) = (*(pData - nInc6) + *(pData - nInc5) + *(pData - nInc4) + *(pData - nInc3) + 2 * (*(pData - nInc2)) + *(pData - nInc1)) / 7;
  3865. *(pBuffer - nInc2) = (*(pData - nInc5) + *(pData - nInc4) + 2 * (*(pData - nInc3)) + 2 * (*(pData - nInc2)) + *(pData - nInc1)) / 7;
  3866. *(pBuffer - nInc1) = (2 * (*(pData - nInc4)) + 2 * (*(pData - nInc3)) + 2 * (*(pData - nInc2)) + *(pData - nInc1) ) / 7;
  3867. }
  3868. memcpy(pImgData, pImgBuffer, nHeight * nWidth * sizeof(ushort_t));
  3869. // CFile file;
  3870. // CString strDignosisPath = _T("e:\\Varian.raw");
  3871. // file.Open(strDignosisPath, CFile::modeCreate | CFile::modeWrite );
  3872. // file.Write(pImgBuffer, m_nWidth*m_nHeight*sizeof(WORD));
  3873. // file.Close();
  3874. ::GlobalUnlock(hImgBuffer);
  3875. ::GlobalFree(hImgBuffer);
  3876. return 1;
  3877. }
  3878. /************************************************************************
  3879. FUNCTION NAME: CalcGlbAvgPxlValueBySamp
  3880. DESCREPTION: Calculate global average pixel value by sampling method
  3881. RETURN VALUE: Global average pixel value.
  3882. PARA: [IN]pImgData: raw image data array
  3883. [IN]nWidth: raw image's width
  3884. [IN]nHeight: raw image's height
  3885. [IN]nXOffset: raw image's offset in x direction
  3886. [IN]nYOffset: raw image's offset in y direction
  3887. HISTORY: August/2/2010 written by Alex Stocks
  3888. ************************************************************************/
  3889. double CPixMatrix::CalcGlbAvgPxlValueBySamp(unsigned short* pImgData, int nWidth, int nHeight, int nXOffset, int nYOffset)
  3890. {
  3891. if (NULL == pImgData || nWidth < 0 || nHeight < 0 || nXOffset < 0
  3892. || nYOffset < 0 || nWidth <= 2 * nXOffset || nHeight <= 2 * nYOffset)
  3893. {
  3894. return -1.0f;
  3895. }
  3896. int nIdxI = 0;
  3897. int nIdxJ = 0;
  3898. int nIndex = 0;
  3899. int nHist[PIXEL_MAX_VALUE] = { 0 };
  3900. int nMinYIdx = nYOffset;
  3901. int nMaxYIdx = nHeight - nYOffset;
  3902. int nMinXIdx = nXOffset;
  3903. int nMaxXIdx = nWidth - nXOffset;
  3904. unsigned short *pImage = pImgData;
  3905. int nPxlNum = 0;
  3906. for (nIdxI = nMinYIdx; nIdxI < nMaxYIdx; nIdxI += 8)
  3907. {
  3908. pImage = pImgData + nIdxI * nWidth;
  3909. for (nIdxJ = nMinXIdx; nIdxJ < nMaxXIdx; nIdxJ += 8)
  3910. {
  3911. ++nHist[*(pImage+nIdxJ)];
  3912. ++nPxlNum;
  3913. }
  3914. }
  3915. //code delete 20110125
  3916. // double fSum=0.0f;
  3917. // double fGVSum = 0.0f;
  3918. // double fGVAvg=0;
  3919. // double fRatio = 0;
  3920. // int nCount=0;
  3921. // for (nIdxI = 0; nIdxI < 65535; ++nIdxI)
  3922. // {
  3923. // fSum += nHist[nIdxI];
  3924. // fRatio = fSum / nPxlNum;
  3925. // if (0.15f < fRatio && fRatio < 0.85f)
  3926. // {
  3927. // nCount += nHist[nIdxI];
  3928. // fGVSum += nHist[nIdxI] * nIdxI;
  3929. // }
  3930. // }
  3931. // if (nCount <= 0)
  3932. // {
  3933. // return -1.0f;
  3934. // }
  3935. //code begin 20110125
  3936. double fSum=0.0f;
  3937. double fGVSum = 0.0f;
  3938. double fGVAvg=0;
  3939. double fRatio = 0;
  3940. int nCount=0;
  3941. float fTempGVSum = 0.0f;
  3942. for (nIdxI = 0; nIdxI < 65535; ++nIdxI)
  3943. {
  3944. fSum += nHist[nIdxI];
  3945. fRatio = fSum / nPxlNum;
  3946. fTempGVSum += nHist[nIdxI] * nIdxI;
  3947. if (0.15f < fRatio && fRatio < 0.85f)
  3948. {
  3949. nCount += nHist[nIdxI];
  3950. fGVSum += nHist[nIdxI] * nIdxI;
  3951. }
  3952. }
  3953. if (nCount <= 0)
  3954. {
  3955. nCount = nPxlNum;
  3956. fGVSum = fTempGVSum;
  3957. }
  3958. //code end 20110125
  3959. fGVAvg = fGVSum / nCount;
  3960. return fGVAvg;
  3961. }
  3962. ///////////////////////////////////redesigned DetBadPxlByMF by CGN 2013.01.30
  3963. bool CPixMatrix::DetBadPxlByMF(ushort_t *pImgData, int nWidth, int nHeight, int nXOffset, int nYOffset, ushort_t *pMap)
  3964. {
  3965. if (NULL == pImgData || NULL == pMap
  3966. || nWidth <= 0 || nHeight <= 0
  3967. || nXOffset < 0 || nYOffset < 0
  3968. || nWidth <= 2 * nXOffset || nHeight <= 2 * nYOffset)
  3969. {
  3970. return false;
  3971. }
  3972. ///////////////////////////全图阈值计算,挑出黑白坏点
  3973. float fGlbAvgPxlValue = CalcGlbAvgPxlValueBySamp(pImgData, nWidth, nHeight, nXOffset, nYOffset);
  3974. float fMinThreshold = 0.5f * fGlbAvgPxlValue;
  3975. float fMaxThreshold = 1.5f * fGlbAvgPxlValue;
  3976. if (65535.0f < fMaxThreshold) //可能是导致16位图像坏点过多判断的原因,16383修改为65535,by陈冠男,2012-12-20
  3977. {
  3978. fMaxThreshold = 65535.0f;
  3979. }
  3980. int nMinXIdx = nXOffset;
  3981. int nMaxXIdx = nWidth - nXOffset;
  3982. int nMinYIdx = nYOffset;
  3983. int nMaxYIdx = nHeight - nYOffset;
  3984. int bcount = 0;
  3985. for ( int i = nMinYIdx; i < nMaxYIdx; ++i )
  3986. {
  3987. for ( int j = nMinXIdx; j < nMaxXIdx; ++j )
  3988. {
  3989. if ( pImgData[ i * nWidth + j ] < fMinThreshold || fMaxThreshold < pImgData[ i * nWidth + j ] )
  3990. {
  3991. pMap[ i * nWidth + j ] = 65535;
  3992. bcount++;
  3993. }
  3994. }
  3995. }
  3996. /////////////////////////////////将图像分成4x4区域,在每个区域中用7x7像素模版进行坏点判断
  3997. /*int Heightbegin = nYOffset;
  3998. int Heightend = nYOffset + ( nHeight - 2 * nYOffset ) / 4;
  3999. int Widthbegin = nXOffset;
  4000. int Widthend = nXOffset + ( nWidth - 2 * nXOffset ) / 4;
  4001. int nsum = 0;
  4002. int ncount = 0;
  4003. int navg = 0;
  4004. int nthres = 0;
  4005. for ( int i = 0; i < 4; i++ )
  4006. {
  4007. Widthbegin = nXOffset;
  4008. Widthend = nXOffset + ( nWidth - 2 * nXOffset ) / 4;
  4009. for ( int j = 0; j < 4; j++ )
  4010. {
  4011. for ( int k = Heightbegin + 3; k < Heightend - 3 ; k++ )
  4012. {
  4013. for ( int p = Widthbegin + 3; p < Widthend - 3; p++ )
  4014. {
  4015. ncount = 0;
  4016. nsum = 0;
  4017. navg = 0;
  4018. nthres = 0;
  4019. if ( pMap[ k * nWidth + p ] != 65535 )
  4020. {
  4021. for ( int m = -3; m < 4; m++ )
  4022. {
  4023. for ( int n = -3; n < 4; n++ )
  4024. {
  4025. if ( pMap[ ( k + m ) * nWidth + p + n ] != 65535 )
  4026. {
  4027. nsum += pImgData[ ( k + m ) * nWidth + p + n ];
  4028. ncount++;
  4029. }
  4030. }
  4031. }
  4032. navg = nsum / ncount;
  4033. nthres = navg / 10;
  4034. if ( abs( pImgData[ k * nWidth + p ] - navg ) > nthres )
  4035. {
  4036. pMap[ k * nWidth + p ] = 65535;
  4037. bcount++;
  4038. }
  4039. }
  4040. }
  4041. }
  4042. Widthbegin += ( nWidth - 2 * nXOffset ) / 4;
  4043. Widthend += ( nWidth - 2 * nXOffset ) / 4;
  4044. }
  4045. Heightbegin += ( nHeight - 2 * nYOffset ) / 4;
  4046. Heightend += ( nHeight - 2 * nYOffset ) / 4;
  4047. }*/
  4048. int Heightbegin = nYOffset;
  4049. int Heightend = nHeight - nYOffset;
  4050. int Widthbegin = nXOffset;
  4051. int Widthend = nWidth - nXOffset;
  4052. int nsum = 0;
  4053. int ncount = 0;
  4054. int navg = 0;
  4055. int nthres = 0;
  4056. for ( int k = Heightbegin + 3; k < Heightend - 3 ; k++ )
  4057. {
  4058. for ( int p = Widthbegin + 3; p < Widthend - 3; p++ )
  4059. {
  4060. ncount = 0;
  4061. nsum = 0;
  4062. navg = 0;
  4063. nthres = 0;
  4064. if ( pMap[ k * nWidth + p ] != 65535 )
  4065. {
  4066. for ( int m = -3; m < 4; m++ )
  4067. {
  4068. for ( int n = -3; n < 4; n++ )
  4069. {
  4070. if ( pMap[ ( k + m ) * nWidth + p + n ] != 65535 )
  4071. {
  4072. nsum += pImgData[ ( k + m ) * nWidth + p + n ];
  4073. ncount++;
  4074. }
  4075. }
  4076. }
  4077. navg = nsum / ncount;
  4078. nthres = navg / 10;
  4079. if ( abs( pImgData[ k * nWidth + p ] - navg ) > nthres )
  4080. {
  4081. pMap[ k * nWidth + p ] = 65535;
  4082. bcount++;
  4083. }
  4084. }
  4085. }
  4086. }
  4087. return true;
  4088. }
  4089. /************************************************************************
  4090. FUNCTION NAME: DetBadPxlByMF
  4091. DESCRIPTION: detect bad pixels by 7 * 7 mean filter
  4092. RETURN VALUE:
  4093. PARA: [IN] pImageData: primary raw image data array
  4094. [IN] nWidth: raw image width
  4095. [IN] nHeight: raw image height
  4096. [IN] nXOffset: raw image's offset in x direction
  4097. [IN] nYOffset: raw image's offset in y direction
  4098. [IN\OUT] pMap: raw image's bad pixel array
  4099. HISTORY: Aug\23\2010 written by Alex Stocks
  4100. ************************************************************************/
  4101. //bool CPixMatrix::DetBadPxlByMF(ushort_t *pImgData, int nWidth, int nHeight, int nXOffset, int nYOffset, ushort_t *pMap)
  4102. //{
  4103. // if (NULL == pImgData || NULL == pMap
  4104. // || nWidth <= 0 || nHeight <= 0
  4105. // || nXOffset < 0 || nYOffset < 0
  4106. // || nWidth <= 2 * nXOffset || nHeight <= 2 * nYOffset)
  4107. // {
  4108. // return false;
  4109. // }
  4110. //
  4111. // //////////////////////////////////////////////////////////////////////////
  4112. // //step1: detect bad pixels by global threshold
  4113. // //////////////////////////////////////////////////////////////////////////
  4114. // float fGlbAvgPxlValue = CalcGlbAvgPxlValueBySamp(pImgData, nWidth, nHeight, nXOffset, nYOffset);
  4115. // float fMinThreshold = 0.5f * fGlbAvgPxlValue;
  4116. // float fMaxThreshold = 1.5f * fGlbAvgPxlValue;
  4117. // if (65535.0f < fMaxThreshold) //可能是导致16位图像坏点过多判断的原因,16383修改为65535,by陈冠男,2012-12-20
  4118. // {
  4119. // fMaxThreshold = 65535.0f;
  4120. // }
  4121. // int nMinXIdx = nXOffset;
  4122. // int nMaxXIdx = nWidth - nXOffset;
  4123. // int nMinYIdx = nYOffset;
  4124. // int nMaxYIdx = nHeight - nYOffset;
  4125. // int nIdxI = 0;
  4126. // int nIdxJ = 0;
  4127. // int nIndex = 0;
  4128. //
  4129. // HGLOBAL hMdfPic = ::GlobalAlloc(GHND, nHeight * nWidth * sizeof(ushort_t));
  4130. // if (NULL == hMdfPic)
  4131. // {
  4132. // ::GlobalUnlock((HGLOBAL)hMdfPic);
  4133. // return false;
  4134. // }
  4135. // ushort_t *pMdfPic = (ushort_t*) ::GlobalLock((HGLOBAL)hMdfPic);
  4136. // memcpy(pMdfPic, pImgData, nHeight * nWidth * sizeof(ushort_t));
  4137. //
  4138. // //use average pixel value in place of every bad pixel's pixel value
  4139. // for (nIdxI = nMinYIdx; nIdxI < nMaxYIdx; ++nIdxI)
  4140. // {
  4141. // nIndex = nIdxI * nWidth;
  4142. // for (nIdxJ = nMinXIdx; nIdxJ < nMaxXIdx; ++nIdxJ)
  4143. // {
  4144. // if (*(pMap + nIndex + nIdxJ) == 0xFFFF)
  4145. // {
  4146. // *(pMdfPic + nIndex + nIdxJ) = (int)fGlbAvgPxlValue;
  4147. // continue;
  4148. // }
  4149. // if (*(pImgData + nIndex + nIdxJ) < fMinThreshold || fMaxThreshold < *(pImgData + nIndex + nIdxJ))
  4150. // {
  4151. // *(pMap + nIndex + nIdxJ) = 0xFFFF;
  4152. // *(pMdfPic + nIndex + nIdxJ) = (int)fGlbAvgPxlValue;
  4153. // }
  4154. // }
  4155. // }
  4156. //
  4157. // //////////////////////////////////////////////////////////////////////////
  4158. // //step2: detect bad pixels by mean filter
  4159. // //////////////////////////////////////////////////////////////////////////
  4160. // HGLOBAL hImpPic = ::GlobalAlloc(GHND, nHeight * nWidth * sizeof(ushort_t));
  4161. // if (NULL == hImpPic)
  4162. // {
  4163. // ::GlobalUnlock(hImpPic);
  4164. // return false;
  4165. // }
  4166. // ushort_t* pImpPic = (ushort_t*) ::GlobalLock(hImpPic);
  4167. // memcpy(pImpPic, pMdfPic, nHeight * nWidth * sizeof(ushort_t));
  4168. // ::GlobalUnlock(hMdfPic);
  4169. // ::GlobalFree(hMdfPic);
  4170. // int nAns = Mean7(pImpPic, nWidth, nHeight, nXOffset, nYOffset);
  4171. // if (nAns < 0)
  4172. // {
  4173. // ::GlobalUnlock(hImpPic);
  4174. // ::GlobalFree(hImpPic);
  4175. // //::GlobalUnlock(hMdfPic);
  4176. // //::GlobalFree(hMdfPic);
  4177. // return false;
  4178. // }
  4179. // int nDiff = 0;
  4180. // float fAbsDiff = fGlbAvgPxlValue * 0.10f;
  4181. // for (nIdxI = nMinYIdx; nIdxI < nMaxYIdx; ++nIdxI)
  4182. // {
  4183. // nIndex = nIdxI * nWidth;
  4184. // for (nIdxJ = nMinXIdx; nIdxJ < nMaxXIdx; ++nIdxJ)
  4185. // {
  4186. // if (*(pMap + nIndex + nIdxJ) != 0xFFFF)
  4187. // {
  4188. // nDiff = *(pImgData + nIndex + nIdxJ) - *(pImpPic + nIndex + nIdxJ);
  4189. // if (fAbsDiff < abs(nDiff))
  4190. // {
  4191. // *(pMap + nIndex + nIdxJ) = 0xFFFF;
  4192. // }
  4193. // }
  4194. // }
  4195. // }
  4196. //
  4197. // ::GlobalUnlock(hImpPic);
  4198. // ::GlobalFree(hImpPic);
  4199. // //::GlobalUnlock(hMdfPic);
  4200. // //::GlobalFree(hMdfPic);
  4201. // return true;
  4202. //}
  4203. int CPixMatrix::BadGridLineCorrect( unsigned short *pImage)
  4204. {
  4205. long BadPixNum;
  4206. long BadNum;
  4207. memset( m_TempImage, 0, sizeof(unsigned short) * m_nWidth * m_nHeight );
  4208. memcpy( m_TempImage, pImage,sizeof(unsigned short) * m_nWidth * m_nHeight );
  4209. for ( int i = m_nHOffset + 2; i < m_nHeight - m_nHOffset - 2; i++ )
  4210. {
  4211. m_pBadPixNum = m_BadPixelMap[ i ].bad_pixel_num;
  4212. if ( m_BadPixelMap[ i ].num_entries > 0 )
  4213. {
  4214. for ( int j = 0; j < m_BadPixelMap[ i ].num_entries; j++ )
  4215. {
  4216. BadPixNum = *m_pBadPixNum++;
  4217. BadNum = BadPixNum & OFFSET_MASK;
  4218. if ( BadNum > 1 && BadNum < m_nWidth - 1 )
  4219. pImage[ i * m_nWidth + BadNum ] = ( m_TempImage[ ( i - 2 ) * m_nWidth + BadNum - 2 ] + m_TempImage[ ( i - 2 ) * m_nWidth + BadNum ] + m_TempImage[ ( i - 2 ) * m_nWidth + BadNum + 2 ] +
  4220. m_TempImage[ ( i ) * m_nWidth + BadNum - 2 ] + m_TempImage[ ( i ) * m_nWidth + BadNum + 2 ] +
  4221. m_TempImage[ ( i + 2 ) * m_nWidth + BadNum - 2 ] + m_TempImage[ ( i + 2 ) * m_nWidth + BadNum ] + m_TempImage[ ( i + 2 ) * m_nWidth + BadNum + 2 ] ) / 8;
  4222. }
  4223. }
  4224. }
  4225. return 1;
  4226. }