PixMatrix.cpp 146 KB

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