123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788 |
- // PixMatrix.cpp: implementation of the CPixMatrix class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "PixMatrix.h"
- #include <cstring>
- #include <cstdio>
- #include <algorithm> // 用于std::max和std::min
- #include <cstdint>
- #include "memory.h"
- #include "math.h"
- #include "String.Format.tlh"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- #define FULL_IMG_WIDTH 4000L
- #define FULL_IMG_HEIGHT 4000L
- #define PIXEL_MAX_VALUE 65536
- CPixMatrix* CPixMatrix::m_instance = NULL;
- CPixMatrix* CPixMatrix::Instance()
- {
- if(m_instance == NULL) {
- m_instance = new CPixMatrix(FULL_IMG_WIDTH, FULL_IMG_HEIGHT, 15, 45);
- }
- return m_instance;
- }
- CPixMatrix::CPixMatrix(int width, int height, int woffset, int hoffset, int nPixelMax)
- :m_nWidth(width), m_nHeight(height), m_nWOffset(woffset), m_nHOffset(hoffset), m_nPixelMax(nPixelMax)
- {
- m_BadPixelMap = NULL;
- m_BadNewPixelMap = NULL;
- m_BadPixArray = NULL;
- m_xLineMap = NULL;
- m_yLineMap = NULL;
- m_doubleLineMap = NULL;
- m_NewMap = new PIX_MAP[m_nHeight];
- m_mapImg = NULL;
- m_curAvg = -1;
- m_lastAvg = -1;
- memset(m_charFilename, 0, 256);
- m_TempImage = new unsigned short [ m_nWidth * m_nHeight ];
- m_TempImage1 = new unsigned short [ m_nWidth * m_nHeight ];
- m_pBadPixNum = NULL;
- m_nLinePoint = 50;
- m_nLineData = int( (MAXI_BADPIX_COUNT - MAX_PIX_MAP_LINE) / m_nLinePoint);//×?′ó?μ????êy, zhaoyiru, 2017.05.09
- m_datalen = new int[ m_nLineData ] ;
- memset(m_datalen, -1, sizeof(int)* m_nLineData );
-
- }
- CPixMatrix::~CPixMatrix()
- {
- if(m_NewMap) {
- delete [] m_NewMap;
- m_NewMap = NULL;
- }
- if(m_BadPixArray) {
- delete [] m_BadPixArray;
- m_BadPixArray = NULL;
- }
- if( m_TempImage != NULL )
- {
- delete [] m_TempImage;
- m_TempImage = NULL;
- }
- if( m_TempImage1 != NULL )
- {
- delete [] m_TempImage1;
- m_TempImage1 = NULL;
- }
- if (m_mapImg)//add by ys20180109不在析构函数中释放的话会造成内存泄漏
- {
- delete[] m_mapImg;
- m_mapImg = NULL;
- }//add end
- FreeBadPixelMap();
- //FreeBadNewPixelMap();
- FreeDBadLineMap();
- if( m_pBadPixNum != NULL )
- {
- //delete m_pBadPixNum;
- m_pBadPixNum = NULL;
- }
- if( m_datalen != NULL )
- {
- delete [] m_datalen;
- m_datalen = NULL;
- }
- }
- void CPixMatrix::CorrectBadPixels(unsigned short* image)
- {
- long line;
- long pix_num;
- long bad_pix_num;
- long bad_num;
- long *p_bad_pix_num;
- float new_value;
- long w_new_value;
- float divisor;
- unsigned short *p_line, *p_prev_line, *p_next_line;
- long unProcLineNum;
- long unProcTotalNum = 0;
- if(m_BadPixelMap == NULL){ // can't do anything without a map
- return;
- }
- if(m_BadPixArray == NULL) {
- m_BadPixArray = new long [MAXI_BADPIX_COUNT*sizeof(long)];
- if(m_BadPixArray == NULL) {
- return;
- }
- }
- MarkBadAdjacentPixels();
- for(line = 0 ; line < m_nHeight ; ++line) {
- m_NewMap[line].len = 0;
- }
- int nXOffset = m_nWOffset;
- int nYOffset = m_nHOffset;
- int nLeft = nXOffset;
- int nRight = m_nWidth - nXOffset - 1;
- int nTop = nYOffset;
- int nBottom = m_nHeight - nYOffset - 1;
- int nXStartPos = nLeft;
- int nXEndPos = nRight;
- int nYStartPos = nTop;
- int nYEndPos = nBottom;
- int nBadPxlInfo = 0;
- int nPixelPos = 0;
- int nIdxI = 0;
- float fPVSum = 0.0f;
- float fDivisor = 0.0f;
- int nAvgPxlVal = CalcGlbAvgPxlValueBySamp(image, m_nWidth, m_nHeight, nXOffset, nYOffset);
- //校正最上面一行的坏点的像素值
- if (0 < m_BadPixelMap[nTop].num_entries)
- {
- for (nIdxI = 0; nIdxI < m_BadPixelMap[nTop].num_entries; nIdxI++)
- {
- nBadPxlInfo = m_BadPixelMap[nTop].bad_pixel_num[nIdxI];
- nPixelPos = nBadPxlInfo & OFFSET_MASK;
- fPVSum = 0.0f;
- fDivisor = 0.0f;
- //左边像素[nTop, nPixelPos - 1]
- if ((nLeft <= (nPixelPos - 1)) && ((nBadPxlInfo & NEIGHBOR_3) == 0))
- {
- fPVSum += image[nTop * m_nWidth + nPixelPos - 1];
- fDivisor += 1.0f;
- }
- //左下角像素[nTop + 1, nPixelPos - 1]
- if ((nLeft <= (nPixelPos - 1)) && ((nTop + 1) <= nBottom) && ((nBadPxlInfo & NEIGHBOR_5) == 0))
- {
- fPVSum += 0.707f * image[(nTop + 1) * m_nWidth + nPixelPos - 1];
- fDivisor += 0.707f;
- }
- //右边像素[nTop, nPixelPos + 1]
- if (((nPixelPos + 1) <= nRight) && ((nBadPxlInfo & NEIGHBOR_4) == 0))
- {
- fPVSum += image[nTop * m_nWidth + nPixelPos + 1];
- fDivisor += 1.0f;
- }
- //右下角像素[nTop + 1, nPixelPos + 1]
- if (((nPixelPos + 1) <= nRight) && ((nTop + 1) <= nBottom) && ((nBadPxlInfo & NEIGHBOR_7) == 0))
- {
- fPVSum += 0.707f * image[(nTop + 1) * m_nWidth + nPixelPos + 1];
- fDivisor += 0.707f;
- }
- //下边像素[nTop + 1, nPixelPos]
- if (((nTop + 1) <= nBottom) && ((nBadPxlInfo & NEIGHBOR_6) == 0))
- {
- fPVSum += image[(nTop + 1) * m_nWidth + nPixelPos];
- fDivisor += 1.0f;
- }
- if (0.9f < fDivisor)
- {
- image[nTop * m_nWidth + nPixelPos] = (int)(fPVSum / fDivisor);
- }
- else
- {
- image[nTop * m_nWidth + nPixelPos] = nAvgPxlVal;
- }
- }
- }
- //校正最下面一行坏点的像素值
- if (0 < m_BadPixelMap[nBottom].num_entries)
- {
- for (nIdxI = 0; nIdxI < m_BadPixelMap[nBottom].num_entries; nIdxI++)
- {
- nBadPxlInfo = m_BadPixelMap[nBottom].bad_pixel_num[nIdxI];
- nPixelPos = nBadPxlInfo & OFFSET_MASK;
- fPVSum = 0.0f;
- fDivisor = 0.0f;
- //左边像素[nBottom, nPixelPos - 1]
- if ((nLeft <= (nPixelPos - 1)) && ((nBadPxlInfo & NEIGHBOR_3) == 0))
- {
- fPVSum += image[nBottom * m_nWidth + nPixelPos - 1];
- fDivisor += 1.0f;
- }
- //左上角像素[nBottom - 1, nPixelPos - 1]
- if ((nLeft <= (nPixelPos - 1)) && (nTop <= (nBottom - 1)) && ((nBadPxlInfo & NEIGHBOR_0) == 0))
- {
- fPVSum += 0.707f * image[(nBottom - 1) * m_nWidth + nPixelPos - 1];
- fDivisor += 0.707f;
- }
- //右边像素[nBottom, nPixelPos + 1]
- if (((nPixelPos + 1) <= nRight) && ((nBadPxlInfo & NEIGHBOR_4) == 0))
- {
- fPVSum += image[nBottom * m_nWidth + nPixelPos + 1];
- fDivisor += 1.0f;
- }
- //右上角像素[nBottom - 1, nPixelPos + 1]
- if (((nPixelPos + 1) <= nRight) && (nTop <= (nBottom - 1)) && ((nBadPxlInfo & NEIGHBOR_2) == 0))
- {
- fPVSum += 0.707f * image[(nBottom - 1) * m_nWidth + nPixelPos + 1];
- fDivisor += 0.707f;
- }
- //上边像素[nBottom - 1, nPixelPos]
- if ((nTop <= (nBottom - 1)) && ((nBadPxlInfo & NEIGHBOR_1) == 0))
- {
- fPVSum += image[(nBottom - 1) * m_nWidth + nPixelPos];
- fDivisor += 1.0f;
- }
- if (0.9f < fDivisor)
- {
- image[nBottom * m_nWidth + nPixelPos] = (int)(fPVSum / fDivisor);
- }
- else
- {
- image[nBottom * m_nWidth + nPixelPos] = nAvgPxlVal;
- }
- }
- }
- //校正最左边列和最右边列的坏点的像素值
- int nRow = 0;
- int nLeftBadPxlFlag = -1;
- int nRightBadPxlFlag = -1;
- for (nRow = nTop + 1; nRow < nBottom; nRow++)
- {
- nLeftBadPxlFlag = -1;
- nRightBadPxlFlag = -1;
- for (nIdxI = 0; nIdxI < m_BadPixelMap[nRow].num_entries; nIdxI++)
- {
- nBadPxlInfo = m_BadPixelMap[nRow].bad_pixel_num[nIdxI];
- nPixelPos = nBadPxlInfo & OFFSET_MASK;
- if (nPixelPos == nLeft)
- {
- nLeftBadPxlFlag = nIdxI;
- }
- else if (nPixelPos == nRight)
- {
- nRightBadPxlFlag = nIdxI;
- }
- }
- //校正最左列的坏点的像素值
- if (-1 < nLeftBadPxlFlag)
- {
- nBadPxlInfo = m_BadPixelMap[nRow].bad_pixel_num[nLeftBadPxlFlag];
- nPixelPos = nBadPxlInfo & OFFSET_MASK;
- fPVSum = 0.0f;
- fDivisor = 0.0f;
- //上边像素[nRow - 1, nPixelPos]
- if ((nTop <= (nRow - 1)) && ((nBadPxlInfo & NEIGHBOR_1) == 0))
- {
- fPVSum += image[(nRow - 1) * m_nWidth + nPixelPos];
- fDivisor += 1.0f;
- }
- //右上角像素[nRow - 1, nPixelPos + 1]
- if (((nPixelPos + 1) <= nRight) && (nTop <= (nRow - 1)) && ((nBadPxlInfo & NEIGHBOR_2) == 0))
- {
- fPVSum += 0.707f * image[(nRow - 1) * m_nWidth + nPixelPos + 1];
- fDivisor += 0.707f;
- }
- //右边像素[nRow, nPixelPos + 1]
- if (((nPixelPos + 1) <= nRight) && ((nBadPxlInfo & NEIGHBOR_4) == 0))
- {
- fPVSum += image[nRow * m_nWidth + nPixelPos + 1];
- fDivisor += 1.0f;
- }
- //右下角像素[nRow + 1, nPixelPos + 1]
- if (((nPixelPos + 1) <= nRight) && ((nRow + 1) <= nBottom) && ((nBadPxlInfo & NEIGHBOR_7) == 0))
- {
- fPVSum += 0.707f * image[(nRow + 1) * m_nWidth + nPixelPos + 1];
- fDivisor += 0.707f;
- }
- //下边像素[nRow + 1, nPixelPos]
- if (((nRow + 1) <= nBottom) && ((nBadPxlInfo & NEIGHBOR_6) == 0))
- {
- fPVSum += image[(nRow + 1) * m_nWidth + nPixelPos];
- fDivisor += 1.0f;
- }
- if (0.9f < fDivisor)
- {
- image[nRow * m_nWidth + nPixelPos] = (int)(fPVSum / fDivisor);
- }
- else
- {
- image[nRow * m_nWidth + nPixelPos] = nAvgPxlVal;
- }
- }
- //校正最右列的坏点的像素值
- if (-1 < nRightBadPxlFlag)
- {
- nBadPxlInfo = m_BadPixelMap[nRow].bad_pixel_num[nRightBadPxlFlag];
- nPixelPos = nBadPxlInfo & OFFSET_MASK;
- fPVSum = 0.0f;
- fDivisor = 0.0f;
- //上边像素[nRow - 1, nPixelPos]
- if ((nTop <= (nRow - 1)) && ((nBadPxlInfo & NEIGHBOR_1) == 0))
- {
- fPVSum += image[(nRow - 1) * m_nWidth + nPixelPos];
- fDivisor += 1.0f;
- }
- //左上角像素[nRow - 1, nPixelPos - 1]
- if ((nLeft <= (nPixelPos - 1)) && (nTop <= (nRow - 1)) && ((nBadPxlInfo & NEIGHBOR_0) == 0))
- {
- fPVSum += 0.707f * image[(nRow - 1) * m_nWidth + nPixelPos - 1];
- fDivisor += 0.707f;
- }
- //左边像素[nRow, nPixelPos - 1]
- if ((nLeft <= (nPixelPos - 1)) && ((nBadPxlInfo & NEIGHBOR_3) == 0))
- {
- fPVSum += image[nRow * m_nWidth + nPixelPos - 1];
- fDivisor += 1.0f;
- }
- //左下角像素[nRow + 1, nPixelPos - 1]
- if ((nLeft <= (nPixelPos - 1)) && ((nRow + 1) <= nBottom) && ((nBadPxlInfo & NEIGHBOR_5) == 0))
- {
- fPVSum += 0.707f * image[(nRow + 1) * m_nWidth + nPixelPos - 1];
- fDivisor += 0.707f;
- }
- //下边像素[nRow + 1, nPixelPos]
- if (((nRow + 1) <= nBottom) && ((nBadPxlInfo & NEIGHBOR_6) == 0))
- {
- fPVSum += image[(nRow + 1) * m_nWidth + nPixelPos];
- fDivisor += 1.0f;
- }
- if (0.9f < fDivisor)
- {
- image[nRow * m_nWidth + nPixelPos] = (int)(fPVSum / fDivisor);
- }
- else
- {
- image[nRow * m_nWidth + nPixelPos] = nAvgPxlVal;
- }
- }
- }
- //校正其他坏点的像素值
- for(line = nTop + 1; line < nBottom ; ++line)
- {
- /* Precalculate the column pointers */
- p_line = image + (line * m_nWidth);
- p_next_line = p_line + m_nWidth;
- p_prev_line = p_line - m_nWidth;
- p_bad_pix_num = m_BadPixelMap[line].bad_pixel_num;
- unProcLineNum = 0;
- m_NewMap[line].p_Pix = &m_BadPixArray[unProcTotalNum];
- for(pix_num = 0; pix_num < m_BadPixelMap[line].num_entries; ++pix_num)
- {
- bad_pix_num = *p_bad_pix_num++;
- bad_num = bad_pix_num & OFFSET_MASK;
- if (bad_num == nLeft || bad_num == nRight)
- {
- continue;
- }
- new_value = 0;
- divisor = 0;
- nPixelPos = bad_pix_num & NEIGHBOR_1;
- if(nPixelPos == 0)
- {
- new_value += *(p_prev_line + bad_num);
- divisor += 1.0f;
- }
- nPixelPos = bad_pix_num & NEIGHBOR_3;
- if(nPixelPos == 0 && 0 < bad_num)
- {
- new_value += *(p_line + bad_num - 1);
- divisor += 1.0f;
- }
- nPixelPos = bad_pix_num & NEIGHBOR_4;
- if(nPixelPos == 0)
- {
- new_value += *(p_line + bad_num + 1);
- divisor += 1.0f;
- }
- nPixelPos = bad_pix_num & NEIGHBOR_6;
- if(nPixelPos == 0)
- {
- new_value += *(p_next_line + bad_num);
- divisor += 1.0f;
- }
- if(divisor < 1.1f && divisor > 0.9f)
- {
- nPixelPos = bad_pix_num & NEIGHBOR_0;
- if(nPixelPos == 0 && bad_num > 0)
- {
- new_value += 0.707f * *(p_prev_line + bad_num - 1);
- divisor += 0.707f;
- }
- nPixelPos = bad_pix_num & NEIGHBOR_2;
- if(nPixelPos == 0)
- {
- new_value += 0.707f * *(p_prev_line + bad_num + 1);
- divisor += 0.707f;
- }
- nPixelPos = bad_pix_num & NEIGHBOR_7;
- if(nPixelPos == 0)
- {
- new_value += 0.707f * *(p_next_line + bad_num + 1);
- divisor += 0.707f;
- }
- nPixelPos = bad_pix_num & NEIGHBOR_5;
- if(nPixelPos == 0)
- {
- new_value += 0.707f * *(p_next_line + bad_num - 1);
- divisor += 0.707f;
- }
- }
- // At least one neighbor is good
- if(divisor > 1.9f)
- {
- w_new_value = (long)(new_value / divisor);
- //w_new_value = floor((new_value / divisor)+0.5);
- *(p_line + bad_num) = (unsigned short)w_new_value;
- }
- else
- { /* Added the pixel to the new bad pixel map */
- unProcLineNum++;
- m_BadPixArray[unProcTotalNum] = bad_pix_num & OFFSET_MASK;
- unProcTotalNum = (unProcTotalNum + 1) % MAXI_BADPIX_COUNT;
- }
- }
- m_NewMap[line].len = unProcLineNum;
- }
- //
- // Fix the remaining bad pixels until no more bad pixels are left
- //
- while(unProcTotalNum > 0)
- {
- MarkBadPixels(m_nHeight, m_NewMap);
- unProcTotalNum = 0;
- for(line = 1; line < m_nHeight - 1; ++line)
- {
- /* Precalculate the column pointers */
- p_line = image + line * m_nWidth;
- p_next_line = p_line + m_nWidth;
- p_prev_line = p_line - m_nWidth;
- p_bad_pix_num = m_NewMap[line].p_Pix;
- unProcLineNum = 0;
- m_NewMap[line].p_Pix = &m_BadPixArray[unProcTotalNum];
- for(pix_num = 0; pix_num < (int)m_NewMap[line].len; ++pix_num)
- {
- bad_pix_num = *p_bad_pix_num++;
- bad_num = bad_pix_num & OFFSET_MASK;
- new_value = 0;
- divisor = 0;
- if (nLeft == bad_num || nRight == bad_num)
- {
- continue;
- }
- nPixelPos = bad_pix_num & NEIGHBOR_1;
- if(nPixelPos == 0)
- {
- new_value += *(p_prev_line + bad_num);
- divisor += 1.0f;
- }
- nPixelPos = bad_pix_num & NEIGHBOR_3;
- if(nPixelPos == 0 && 0 < bad_num)
- {
- new_value += *(p_line + bad_num - 1);
- divisor += 1.0f;
- }
- nPixelPos = bad_pix_num & NEIGHBOR_4;
- if(nPixelPos == 0)
- {
- new_value += *(p_line + bad_num + 1);
- divisor += 1.0f;
- }
- nPixelPos = bad_pix_num & NEIGHBOR_6;
- if(nPixelPos == 0)
- {
- new_value += *(p_next_line + bad_num);
- divisor += 1.0f;
- }
- if(divisor < 1.1f && divisor > 0.9f)
- {
- nPixelPos = bad_pix_num & NEIGHBOR_0;
- if(nPixelPos == 0 && 0 < bad_num)
- {
- new_value += 0.707f * *(p_prev_line + bad_num - 1);
- divisor += 0.707f;
- }
- nPixelPos = bad_pix_num & NEIGHBOR_2;
- if(nPixelPos == 0)
- {
- new_value += 0.707f * *(p_prev_line + bad_num + 1);
- divisor += 0.707f;
- }
- nPixelPos = bad_pix_num & NEIGHBOR_7;
- if(nPixelPos == 0)
- {
- new_value += 0.707f * *(p_next_line + bad_num + 1);
- divisor += 0.707f;
- }
- nPixelPos = bad_pix_num & NEIGHBOR_5;
- if(nPixelPos == 0 && 0 < bad_num)
- {
- new_value += 0.707f * *(p_next_line + bad_num - 1);
- divisor += 0.707f;
- }
- }
- if(divisor > 1.9f)
- {
- //w_new_value = floor((new_value / divisor)+0.5);
- w_new_value = (long)((new_value / divisor)+0.5);
- *(p_line + bad_num) = (unsigned short)w_new_value;
- }
- else
- { /* Goes to the new map */
- unProcLineNum++;
- m_BadPixArray[unProcTotalNum] = bad_num;
- unProcTotalNum = (unProcTotalNum + 1) % MAXI_BADPIX_COUNT;
- }
- }
- m_NewMap[line].len = unProcLineNum;
- }
- }
- return;
- }
- #define CHECK_NEIGHBOR_NEW_U(a, b) \
- while(ju < New_Map[a].len) \
- { \
- jj = New_Map[a].p_Pix[ju] & OFFSET_MASK; \
- if(jj == (b))\
- bad_neighbor |= NEIGHBOR_1;\
- else if(jj == (b - 1)) \
- bad_neighbor |= NEIGHBOR_0; \
- else if(jj == (b + 1)) \
- bad_neighbor |= NEIGHBOR_2; \
- if(jj >= (b + 1)){ \
- ju = ju ? ju - 1 : ju;\
- break; }\
- if((ju + 1) >= New_Map[a].len) break; \
- ju++;\
- }
- #define CHECK_NEIGHBOR_U(a, b) \
- while(ju < m_BadPixelMap[a].num_entries) \
- { \
- jj = m_BadPixelMap[a].bad_pixel_num[ju] & OFFSET_MASK; \
- if(jj == (b))\
- bad_neighbor |= NEIGHBOR_1;\
- else if(jj == (b - 1)) \
- bad_neighbor |= NEIGHBOR_0; \
- else if(jj == (b + 1)) \
- bad_neighbor |= NEIGHBOR_2; \
- if(jj >= (b + 1)){ \
- ju = ju ? ju - 1 : ju;\
- break; } \
- if((ju + 1) >= m_BadPixelMap[a].num_entries) break; \
- ju++;\
- }
- #define CHECK_NEIGHBOR_NEW_D(a, b) \
- while(jd < New_Map[a].len) \
- { \
- jj = New_Map[a].p_Pix[jd] & OFFSET_MASK; \
- if(jj == (long)(b))\
- bad_neighbor |= NEIGHBOR_6;\
- else if(jj == (long)(b - 1)) \
- bad_neighbor |= NEIGHBOR_5; \
- else if(jj == (long)(b + 1)) \
- bad_neighbor |= NEIGHBOR_7; \
- if(jj >= (long)(b + 1)){ \
- jd = jd ? jd - 1 : jd;\
- break;} \
- if( ( jd + 1 ) >= New_Map[ a ].len ) break; \
- jd++;\
- }
- #define CHECK_NEIGHBOR_D(a, b) \
- while(jd < m_BadPixelMap[a].num_entries )\
- { \
- jj = m_BadPixelMap[a].bad_pixel_num[jd] & OFFSET_MASK; \
- if(jj == (b))\
- bad_neighbor |= NEIGHBOR_6;\
- else if(jj == (b - 1)) \
- bad_neighbor |= NEIGHBOR_5; \
- else if(jj == (b + 1)) \
- bad_neighbor |= NEIGHBOR_7; \
- if(jj >= (b + 1)){ \
- jd = jd ? jd - 1 : jd;\
- break;}\
- if((jd + 1) >= m_BadPixelMap[a].num_entries ) break; \
- jd++;\
- }
- void CPixMatrix::MarkBadPixels(int NumLines, PIX_MAP *New_Map)
- {
- long i, ju, jd;
- long jj;
- long pix_num;
- long *p_bad_pix;
- long bad_pix_num;
- long bad_neighbor;
- for(i = 1; i < NumLines - 1; ++i) {
- p_bad_pix = New_Map[i].p_Pix;
- for(pix_num = 0, ju = 0, jd = 0; pix_num < (int)New_Map[i].len; ++pix_num) {
- bad_pix_num = *p_bad_pix & OFFSET_MASK;
- bad_neighbor = 0;
- CHECK_NEIGHBOR_NEW_U((long)(i - 1), bad_pix_num);
- if((*(p_bad_pix - 1) & OFFSET_MASK) == bad_pix_num - 1)
- bad_neighbor |= NEIGHBOR_3;
- if((*(p_bad_pix + 1) & OFFSET_MASK) == bad_pix_num + 1)
- bad_neighbor |= NEIGHBOR_4;
- CHECK_NEIGHBOR_NEW_D(i + 1, bad_pix_num);
- *p_bad_pix++ = bad_pix_num | bad_neighbor;
- }
- }
- }
- // /****************************************************************************/
- // /* Use the upper byte of the bad pixel map offset to pre-mark bad pixels
- // that have adjacent bad pixels */
- //
- // void CPixMatrix::MarkBadAdjacentPixels()
- // {
- // long i, ju, jd;
- // long jj;
- // long pix_num;
- // long *p_bad_pix;
- // long bad_pix_num;
- // long bad_neighbor;
- //
- // /*
- // * Mark the bad pixel map for high-resolution image
- // */
- // if(m_BadPixelMap == NULL) return;
- // int
- // for(i = 1; i < (m_nHeight - 1); ++i)
- // {
- // p_bad_pix = m_BadPixelMap[i].bad_pixel_num;
- // for(pix_num = 0, ju = 0, jd = 0 ; pix_num < m_BadPixelMap[i].num_entries ; pix_num++)
- // {
- // bad_pix_num = *p_bad_pix & OFFSET_MASK;
- // bad_neighbor = 0;
- // CHECK_NEIGHBOR_U((long)(i - 1), bad_pix_num);
- // if((*(p_bad_pix - 1) & OFFSET_MASK) == bad_pix_num - 1)
- // {
- // bad_neighbor |= NEIGHBOR_3;
- // }
- // if((*(p_bad_pix + 1) & OFFSET_MASK) == bad_pix_num + 1)
- // {
- // bad_neighbor |= NEIGHBOR_4;
- // }
- // CHECK_NEIGHBOR_D((long)(i + 1), bad_pix_num);
- // *p_bad_pix++ = bad_pix_num | bad_neighbor;
- // }
- // }
- // }
- /****************************************************************************/
- /* Use the upper byte of the bad pixel map offset to pre-mark bad pixels
- that have adjacent bad pixels */
- //Modified by Alex Stocks on 2011/01/30
- void CPixMatrix::MarkBadAdjacentPixels()
- {
- if(m_BadPixelMap == NULL)
- {
- return;
- }
- long i, ju, jd;
- long jj;
- long pix_num;
- long *p_bad_pix;
- long bad_pix_num;
- long bad_neighbor;
- int a = 0;
- int b = 0;
- /*
- * Mark the bad pixel map for high-resolution image
- */
- int nXOffset = 0 < m_nWOffset ? m_nWOffset : 0;
- int nXStartPos = nXOffset;
- int nXEndPos = m_nWidth - nXOffset;
- int nYOffset = 0 < m_nHOffset ? m_nHOffset : 0;
- int nYStartPos = nYOffset;
- int nYEndPos = m_nHeight - nYOffset;
- int nLeft = nXStartPos;
- int nRight = nXEndPos - 1;
- int nTop = nYStartPos;
- int nBottom = nYEndPos - 1;
- //for(i = 1; i < (m_nHeight - 1); ++i)
- for (i = nYStartPos; i < nYEndPos; i++)
- {
- p_bad_pix = m_BadPixelMap[i].bad_pixel_num;
- for(pix_num = 0, ju = 0, jd = 0 ; pix_num < m_BadPixelMap[i].num_entries ; pix_num++)
- {
- bad_pix_num = *p_bad_pix & OFFSET_MASK;
- bad_neighbor = 0;
- //CHECK_NEIGHBOR_U((long)(i - 1), bad_pix_num);
- a = i - 1; //上一行
- b = bad_pix_num; //列
- //对每个坏点像素的上一行的三个位置进行检查
- while((nTop <= a) && (ju < m_BadPixelMap[a].num_entries))
- {
- jj = m_BadPixelMap[a].bad_pixel_num[ju] & OFFSET_MASK;
- if(jj == (b))
- {//上面
- bad_neighbor |= NEIGHBOR_1;
- }
- else if(nLeft <= (b - 1) && ((b - 1) == jj))
- {//左上
- bad_neighbor |= NEIGHBOR_0;
- }
- else if((b + 1) <= nRight && ((b + 1) == jj))
- {//右上
- bad_neighbor |= NEIGHBOR_2;
- }
- if((b + 1) <= jj)
- {
- ju = ju ? ju - 1 : ju;
- break;
- }
- if(m_BadPixelMap[a].num_entries <= (ju + 1))
- {
- break;
- }
- ju++;
- }
- //对坏点像素左边和右边进行检查
- if((nLeft <= (b - 1)) && ((*(p_bad_pix - 1) & OFFSET_MASK) == bad_pix_num - 1))
- {//左边
- bad_neighbor |= NEIGHBOR_3;
- }
- if(((b + 1) <= nRight) && ((*(p_bad_pix + 1) & OFFSET_MASK) == bad_pix_num + 1))
- {//右边
- bad_neighbor |= NEIGHBOR_4;
- }
- //CHECK_NEIGHBOR_D((long)(i + 1), bad_pix_num);
- a = i + 1; //下一行
- b = bad_pix_num; //列
- //对坏点像素的下一行进行检查
- while((a <= nBottom) && (jd < m_BadPixelMap[a].num_entries) )
- {
- jj = m_BadPixelMap[a].bad_pixel_num[jd] & OFFSET_MASK;
- if(jj == (b))
- {//下面
- bad_neighbor |= NEIGHBOR_6;
- }
- else if(nLeft <= (b - 1) && (jj == (b - 1)))
- {//左下
- bad_neighbor |= NEIGHBOR_5;
- }
- else if((b + 1) <= nRight && (jj == (b + 1)))
- {//右下
- bad_neighbor |= NEIGHBOR_7;
- }
- if(jj >= (b + 1))
- {
- jd = jd ? jd - 1 : jd;
- break;
- }
- if((jd + 1) >= m_BadPixelMap[a].num_entries )
- {
- break;
- }
- jd++;
- }
- //bad_neighbor记录了坏点位置8邻域内所有的坏点的位置
- *p_bad_pix++ = bad_pix_num | bad_neighbor;
- }
- }
- }
- bool CPixMatrix::LoadBadPixelMap(const char *fileName)
- {
- //BAD_PIXEL_ENTRY **theMap = &m_BadPixelMap;
- strcpy(m_charFilename, fileName);
- FILE *fp;
- int num_entries;
- int entry;
- int line_num;
- int file_line_num;
- long *p_bad_pixels;
- bool status = true;
- long bad_pixel_num = 0;
- char *data_in = NULL;
- char *p_token;
- long num_bad_pixels = 0;
- /* Hopefully, the largest line in a bad pix map */
- // data_in = (char *)malloc(MAX_PIX_MAP_LINE);
- data_in = new char [MAX_PIX_MAP_LINE];
- if(data_in == NULL) {
- status = false;
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: malloc of data_in failed.");
- }
- fp = fopen(fileName, "rb");
- if(fp == NULL) {
- status = false;
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Open of file : %s failed.", fileName);
- }
- else {
- if(m_BadPixelMap != NULL) {
- FreeBadPixelMap();
- }
- //m_BadPixelMap = (BAD_PIXEL_ENTRY *)calloc(m_nHeight, sizeof(BAD_PIXEL_ENTRY));
- m_BadPixelMap = new BAD_PIXEL_ENTRY [m_nHeight*sizeof(BAD_PIXEL_ENTRY)];
- if(m_BadPixelMap == NULL) {
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Malloc of m_BadPixelMap failed.");
- status = false;
- }
- else
- {
- for (int i=0;i<m_nHeight;i++)
- {
- //addbyys20190221 参考图像组意见改动
- //用于修复“从小尺寸探测器更换到大尺寸探测器,而不替换匹配的full.map文件,当调用BadLineRecognize()时偶发崩溃”的问题
- m_BadPixelMap[i].num_entries = 0;
- //addend
- m_BadPixelMap[i].bad_pixel_num = NULL;
- }
- }
- }
- /* Data structures created OK and file is open, let's roll */
- if(status == true) {
- line_num = 0;
- while((line_num < m_nHeight) && (status == true)) {
- if(fgets(data_in, MAX_PIX_MAP_LINE, fp) != NULL) {
- // Each line in bad_pixel.map begins with "linenum,num_entries: "
- if(sscanf(data_in, "%d,%d: ", &file_line_num, &num_entries) == 2) {
- // Verify the line number read matches the line num we are on
- if(line_num != file_line_num) {
- status = false;
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Pixel map read error for col %d.", line_num);
- }
- // Verify that num_entries is not larger than pixels-per-line
- if(num_entries > m_nWidth) {
- status = false;
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Pixel map read error for col %d; "
- // "num_entries (%d) > line_size (%d)", line_num, num_entries, m_nWidth);
- }
- /* Tokenize the header */
- p_token = strtok(data_in, ":");
- if(status == true) {
- /* Create the correct size bad pixel map entry */
- //(m_BadPixelMap)[line_num].bad_pixel_num = (long *)malloc((num_entries + 1) * sizeof(long));
- m_BadPixelMap[line_num].bad_pixel_num = new long [(num_entries+1)*sizeof(long)];
- if( (m_BadPixelMap)[line_num].bad_pixel_num == NULL) {
- status = false;
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Malloc of p_bad_pixels for line %d failed.", line_num);
- }
- else {
- memset( (m_BadPixelMap)[line_num].bad_pixel_num, 0, (num_entries + 1) * sizeof(long));
- p_bad_pixels = (m_BadPixelMap)[line_num].bad_pixel_num;
- (m_BadPixelMap)[line_num].num_entries = num_entries;
- /* Fill in the array */
- for(entry = 0 ; entry < num_entries ; ++entry) {
- /*
- * The remainder of each line of the Bad Pixel File consists
- * of numbers indicating the x-coordinate value of the bad
- * pixels. They should be in ascending order. None of them
- * should be greater than capture_config.line_size.
- */
- p_token = strtok(NULL, ",");
- if((p_token == NULL) ||
- (sscanf(p_token, "%ld,", &bad_pixel_num) != 1)) {
- status = false;
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Read failure on pixel entry %d.", entry);
- }
- else {
- if(bad_pixel_num > m_nWidth) {
- status = false;
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Bad value (%ld) on pixel entry %d "
- // "exceeds line_size (%d)",
- // bad_pixel_num, entry, m_nWidth);
- }
- else {
- p_bad_pixels[entry] = bad_pixel_num;
- num_bad_pixels++;
- }
- }
- }
- }
- }
- line_num++;
- }
- /* Not a bad pixel entry; This is a bad line in the file */
- else {
- if(strstr(data_in, "synthseam"))
- continue;
- else
- status = false;
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Read of entry for line_num %d failed", line_num );
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, " Line_num %d is: <%s>", line_num, data_in );
- }
- }
- else {
- // file read error here or end of file.
- break;
- }
- }
- }
- if(status == true) {
- if(num_bad_pixels > MAX_BAD_PIXELS) {
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Bad pixel map exceeds limit (%ld) : %ld pixels",
- // MAX_BAD_PIXELS, num_bad_pixels);
- }
- }
- if(fp != NULL) {
- fclose(fp);
- }
- if(data_in != NULL) {
- delete [] data_in;
- }
- if(status != true)
- {
- FreeBadPixelMap();
- }
- else
- {
-
- /////////////////////////新方法-基于二值图坏线识别
- BadLineRecognize();
-
- }
- return status;
- }
- bool CPixMatrix::LoadBadPixelMapChar(char* data_char, bool bNew)
- {
- //BAD_PIXEL_ENTRY **theMap = &m_BadPixelMap;
- // FILE *fp;
- int num_entries;
- int entry;
- int line_num;
- int file_line_num;
- long *p_bad_pixels;
- bool status = true;
- long bad_pixel_num = 0;
- char *data_in = NULL;
- char *p_token;
- long num_bad_pixels = 0;
- // char *data_temp;
- long char_readcount = 0;
- /* Hopefully, the largest line in a bad pix map */
- // data_in = (char *)malloc(MAX_PIX_MAP_LINE);
- // data_in = new char [MAX_PIX_MAP_LINE];
- // if(data_in == NULL) {
- // status = false;
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: malloc of data_in failed.");
- // }
- // fp = fopen(fileName, "rb");
- if(data_char == NULL) {
- status = false;
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Open of file : %s failed.", fileName);
- }
- else {
- if(m_BadNewPixelMap != NULL) {
- FreeBadPixelMap();
- }
- //m_BadPixelMap = (BAD_PIXEL_ENTRY *)calloc(m_nHeight, sizeof(BAD_PIXEL_ENTRY));
- m_BadNewPixelMap = new BAD_PIXEL_ENTRY [m_nHeight*sizeof(BAD_PIXEL_ENTRY)];
- if(m_BadNewPixelMap == NULL) {
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Malloc of m_BadPixelMap failed.");
- status = false;
- }
- }
- /* Data structures created OK and file is open, let's roll */
- if(status == true) {
- line_num = 0;
- while((line_num < m_nHeight) && (status == true)) {
- data_in = strtok(data_char+char_readcount, "\n");
- char_readcount += long(strlen( data_in )+1);
- if(data_in!= NULL) {
- // Each line in bad_pixel.map begins with "linenum,num_entries: "
-
- if(sscanf(data_in, "%d,%d: ", &file_line_num, &num_entries) == 2) {
- // Verify the line number read matches the line num we are on
- if(line_num != file_line_num) {
- status = false;
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Pixel map read error for col %d.", line_num);
- }
- // Verify that num_entries is not larger than pixels-per-line
- if(num_entries > m_nWidth) {
- status = false;
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Pixel map read error for col %d; "
- // "num_entries (%d) > line_size (%d)", line_num, num_entries, m_nWidth);
- }
- /* Tokenize the header */
- p_token = strtok(data_in, ":");
- if(status == true) {
- /* Create the correct size bad pixel map entry */
- //(m_BadPixelMap)[line_num].bad_pixel_num = (long *)malloc((num_entries + 1) * sizeof(long));
- m_BadNewPixelMap[line_num].bad_pixel_num = new long [(num_entries+1)*sizeof(long)];
- if( (m_BadNewPixelMap)[line_num].bad_pixel_num == NULL) {
- status = false;
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Malloc of p_bad_pixels for line %d failed.", line_num);
- }
- else {
- memset( (m_BadNewPixelMap)[line_num].bad_pixel_num, 0, (num_entries + 1) * sizeof(long));
- p_bad_pixels = (m_BadNewPixelMap)[line_num].bad_pixel_num;
- (m_BadNewPixelMap)[line_num].num_entries = num_entries;
- /* Fill in the array */
- for(entry = 0 ; entry < num_entries ; ++entry) {
- /*
- * The remainder of each line of the Bad Pixel File consists
- * of numbers indicating the x-coordinate value of the bad
- * pixels. They should be in ascending order. None of them
- * should be greater than capture_config.line_size.
- */
- p_token = strtok(NULL, ",");
- if((p_token == NULL) ||
- (sscanf(p_token, "%ld,", &bad_pixel_num) != 1)) {
- status = false;
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Read failure on pixel entry %d.", entry);
- }
- else {
- if(bad_pixel_num > m_nWidth) {
- status = false;
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Bad value (%ld) on pixel entry %d "
- // "exceeds line_size (%d)",
- // bad_pixel_num, entry, m_nWidth);
- }
- else {
- p_bad_pixels[entry] = bad_pixel_num;
- num_bad_pixels++;
- }
- }
- }
- }
- }
- line_num++;
- }
- /* Not a bad pixel entry; This is a bad line in the file */
- else {
- if(strstr(data_in, "synthseam"))
- continue;
- else
- status = false;
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Read of entry for line_num %d failed", line_num );
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, " Line_num %d is: <%s>", line_num, data_in );
- }
- }
- else {
- // file read error here or end of file.
- break;
- }
- }
- }
- if(status == true) {
- if(num_bad_pixels > MAX_BAD_PIXELS) {
- // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Bad pixel map exceeds limit (%ld) : %ld pixels",
- // MAX_BAD_PIXELS, num_bad_pixels);
- }
- }
- if(status != true) {
- FreeBadNewPixelMap();
- }
- if(!bNew)
- {
- if(m_BadPixelMap != NULL) {
- FreeBadPixelMap();
- }
- m_BadPixelMap = m_BadNewPixelMap;
- m_BadNewPixelMap = NULL;
- }
- return status;
- }
- bool CPixMatrix::SaveBadPixelMap(const char *fileName)
- {
- FILE *fp;
- char *data_out = NULL;
- long char_count = 0;
- bool status = true;
- long line;
- long bad_pix_num;
- long bad_num;
- long pix_num;
- long *p_bad_pix_num;
- long char_length;
- data_out = new char [MAXI_BADPIX_COUNT*2];
- for(line = 0 ; line < m_nHeight; ++line) {
- char_length = sprintf(data_out+char_count,"%ld,%ld:",line,m_BadPixelMap[line].num_entries);
- char_count += char_length;
- p_bad_pix_num = m_BadPixelMap[line].bad_pixel_num;//
- for(pix_num = 0; pix_num < m_BadPixelMap[line].num_entries; ++pix_num) {
- bad_pix_num = *p_bad_pix_num++;
- bad_num = bad_pix_num & OFFSET_MASK;
- char_length = sprintf(data_out+char_count,"%ld,",bad_num);
- char_count += char_length;
- }
- char_length = sprintf(data_out+char_count,"\n");
- char_count += char_length;
- }
- fp = fopen(fileName, "wb");
- if (fwrite(data_out,1,char_count,fp)!=char_count){
- status = false;
- }
- fclose(fp);
- if(m_BadPixelMap == NULL)
- {
- status = false;
- }
- else
- {
- //////////////////////////////////////////坏线自动识别
- BadLineRecognize();
- }
- if(data_out != NULL) {
- delete [] data_out;
- }
- return status;
- }
- int CPixMatrix::BadGridLineCorrect1( unsigned short *pImage, int nLineDirection, int nEntries, int nStartPoint, int nEndPoint, int nWidth, int nHeight)
- {
- if( !pImage || nEntries < m_nHOffset || nEntries > nHeight - m_nHOffset )
- return -1;
- memcpy( m_TempImage, pImage,sizeof(unsigned short) * m_nWidth * m_nHeight );
- if( nEntries -2 < m_nHOffset)
- {
- for( int i = nStartPoint; i < nEndPoint; i++ )
- {
- pImage[ (nEntries) * nWidth + i] = m_TempImage[( nEntries +2 ) * nWidth + i] ;
- }
- }
- else if( nEntries + 2 > nHeight - m_nHOffset )
- {
- for( int i = nStartPoint; i < nEndPoint; i++ )
- {
- pImage[ (nEntries) * nWidth + i] = m_TempImage[( nEntries -2 ) * nWidth + i] ;
- }
- }
- else
- {
- for( int i = nStartPoint; i < nEndPoint; i++ )
- {
- pImage[ (nEntries) * nWidth + i] = 0.5*( m_TempImage[( nEntries +2 ) * nWidth + i] + m_TempImage[( nEntries -2 ) * nWidth + i] );
- }
- }
- return 0;
- }
- //¨¨£¤??è¨???????ê??|ì??3????D?ê?y?ê?¨o¨2??à?|ì???ê?zhaoyiru,2017.05.09
- int CPixMatrix::BadGridLineCorrect2( unsigned short *pImage, int nLineDirection, int nEntries, int nStartPoint, int nEndPoint, int nWidth, int nHeight)//¨o¨2??à
- {
- if( !pImage || nEntries < m_nWOffset || nEntries > nWidth - m_nWOffset )
- return -1;
- memcpy( m_TempImage, pImage,sizeof(unsigned short) * m_nWidth * m_nHeight );
- if( nEntries -2 <= m_nWOffset)
- {
- for( int i = nStartPoint; i < nEndPoint; i++ )
- {
- pImage[i * nWidth + nEntries] =m_TempImage[( i ) * nWidth + nEntries + 2];
- }
- }
- else if( nEntries + 2 > nHeight - m_nWOffset )
- {
- for( int i = nStartPoint; i < nEndPoint; i++ )
- {
- pImage[i * nWidth + nEntries] =m_TempImage[( i ) * nWidth + nEntries - 2];
- }
- }
- else
- {
- for( int i = nStartPoint; i < nEndPoint; i++ )
- {
- pImage[i * nWidth + nEntries] = 0.5*( m_TempImage[( i ) * nWidth + nEntries - 2] + m_TempImage[( i ) * nWidth + nEntries + 2 ] );
- }
- }
- return 0;
- }
- //¨¨£¤??è¨???o¨??ê??|ì???t???D?ê?y?ê??????|ì???ê? zhaoyiru,2017.05.09
- int CPixMatrix::BadGridLineCorrect3( unsigned short *pImage, int nLineDirection, int nEntries, int nStartPoint, int nEndPoint, int nWidth, int nHeight)
- {
- if( !pImage || nEntries < m_nHOffset+2 || nEntries > nHeight -m_nHOffset-3 )
- return -1;
- if ( nStartPoint < m_nWOffset + 2 )
- nStartPoint = m_nWOffset + 2;
- if ( nEndPoint > m_nWOffset -3)
- nEndPoint = nWidth- m_nWOffset -3;
- memcpy( m_TempImage, pImage,sizeof(unsigned short) * m_nWidth * m_nHeight );
- const float coef1=0.25f;
- const float coef2=0.5f;//0.67f;
- const float coef3=0.5f;//0.33f;
- const float coef4=1.0f;
- const float coef5=2.0f;
- const float coef6=1.0f;
- for( int i = nStartPoint; i < nEndPoint; i++ )
- {
- int nA,nB,nC,nA1, nB1,nC1;
- 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 -
- 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);
- 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-
- 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);
- 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 -
- 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);
- nA = nA1*nA1;
- nB = nB1*nB1;
- nC = nC1*nC1;
- if( nA <= nB && nA<= nC )
- {
- 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)+
- 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);
- }
- else if( nB<= nC )
- {
- 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)+
- 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);
- }
- else
- {
- 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)+
- 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);
- }
- }
- return 0;
- }
- //¨¨£¤??è¨???o¨??ê??|ì???t???D?ê?y?ê?¨o¨2??à?|ì???ê?zhaoyiru,2017.05.09
- int CPixMatrix::BadGridLineCorrect4( unsigned short *pImage, int nLineDirection, int nEntries, int nStartPoint, int nEndPoint, int nWidth, int nHeight)
- {
- if( !pImage || nEntries < m_nWOffset+3 || nEntries > nHeight -m_nWOffset-4 )
- return -1;
- if ( nStartPoint < m_nHOffset + 3 )
- nStartPoint = m_nHOffset + 3;
- if ( nEndPoint > m_nHOffset -4)
- nEndPoint = nHeight-m_nHOffset -4;
- memcpy( m_TempImage, pImage,sizeof(unsigned short) * m_nWidth * m_nHeight );
- const float coef1=0.25f;
- const float coef2=0.5f;//0.67f;
- const float coef3=0.5f;//0.33f;
- const float coef4=1.0f;
- const float coef5=2.0f;
- const float coef6=1.0f;
- for( int i = nStartPoint; i < nEndPoint; i++ )
- {
- int nA,nB,nC,nA1, nB1,nC1;
- 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 -
- 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);
- 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-
- 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);
- 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 -
- 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);
- nA = nA1*nA1;
- nB = nB1*nB1;
- nC = nC1*nC1;
- if( nA <= nB && nA<= nC )
- {
- 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)+
- 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);
- }
- else if( nB<= nC )
- {
- 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)+
- 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 );
- }
- else
- {
- 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)+
- 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);
- }
- }
- return 0;
- }
- void CPixMatrix::BadLineRecognize()
- {
- //////////////////////////////////////////坏线自动识别
- long BadPixNum;
- long BadNum;
- memset( m_TempImage, 0, sizeof(unsigned short) * m_nWidth * m_nHeight );
- memset( m_TempImage1, 0, sizeof(unsigned short) * m_nWidth * m_nHeight );
- memset( m_datalen, -1, sizeof(int) * (int( (MAXI_BADPIX_COUNT - MAX_PIX_MAP_LINE) / m_nLinePoint)) );
- for ( int i = m_nHOffset + 2; i < m_nHeight - m_nHOffset - 2; i++ )
- {
- m_pBadPixNum = m_BadPixelMap[ i ].bad_pixel_num;
- if ( m_BadPixelMap[ i ].num_entries > 0 )
- {
- for ( int j = 0; j < m_BadPixelMap[ i ].num_entries; j++ )
- {
- BadPixNum = *m_pBadPixNum++;
- BadNum = BadPixNum & OFFSET_MASK;
- if ( BadNum > 1 && BadNum < m_nWidth - 1 )
- m_TempImage[ i * m_nWidth + BadNum ] = 65535;
- }
- }
- }
- /////////////////////按行标记坏线,水平坏线
- //char *data_out = NULL;改为m_datalen;
- int linedirection;
- //m_datalen = new char [ 40000 ];
- int count = 0;
- for ( int i = m_nHOffset; i < m_nHeight - m_nHOffset; i++ )
- {
- for ( int j = m_nWOffset + 2; j < m_nWidth - m_nWOffset - 2; j++ )
- {
- 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;
- if ( m_TempImage1[ i * m_nWidth + j ] < 40000 )
- m_TempImage1[ i * m_nWidth + j ] = 0;
- else
- m_TempImage1[ i * m_nWidth + j ] = 65535;
- }
- int tempcount = 0;
- for ( int p = m_nWOffset; p < m_nWidth - m_nWOffset; p++ )
- {
- if ( m_TempImage1[ i * m_nWidth + p ] == 65535 )
- tempcount++;
- }
- int minX = 0;
- int maxX = 0;
- if ( tempcount > m_nLinePoint )//add by chen 2015-1-13 judge condition change from 300 to 50
- {
- for ( int k = m_nWOffset; k < m_nWidth - m_nWOffset; k++ )
- {
- if ( m_TempImage1[ i * m_nWidth + k ] == 65535 )
- {
- minX = k;
- break;
- }
- }
- for ( int k1 = m_nWidth - m_nWOffset - 1; k1 > m_nWOffset; k1-- )
- {
- if ( m_TempImage1[ i * m_nWidth + k1 ] == 65535 )
- {
- maxX = k1;
- break;
- }
- }
- linedirection = 0;
- //char_length = sprintf( m_datalen + char_count, "%d,%d:%d,%d", linedirection, i, minX - 2, maxX + 2 );
- //char_count += char_length;
- //char_length = sprintf( m_datalen + char_count, "\n" );
- //char_count += char_length;
-
-
- m_datalen[ count ] = linedirection;
- m_datalen[ count + 1 ] = i;
- m_datalen[ count + 2 ] = minX - 2;
- m_datalen[ count + 3 ] = maxX + 2;
- count += 4;
- }
- }
- ////////////////////////////按列标记坏线--竖直坏线
- memset( m_TempImage1, 0, sizeof(unsigned short) * m_nWidth * m_nHeight );
- for ( int i = m_nWOffset; i < m_nWidth - m_nWOffset; i++ )
- {
- for ( int j = m_nHOffset + 2; j < m_nHeight - m_nHOffset - 2; j++ )
- {
- 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;
- if ( m_TempImage1[ j * m_nWidth + i ] < 40000 )
- m_TempImage1[ j * m_nWidth + i ] = 0;
- else
- m_TempImage1[ j * m_nWidth + i ] = 65535;
- }
- int tempcount = 0;
- for ( int p = m_nHOffset; p < m_nHeight - m_nHOffset; p++ )
- {
- if ( m_TempImage1[ p * m_nWidth + i ] == 65535 )
- tempcount++;
- }
- int minY = 0;
- int maxY = 0;
- if ( tempcount > m_nLinePoint )//add by chen 2015-1-13 judge condition change from 300 to 50
- {
- for ( int k = m_nHOffset; k < m_nHeight - m_nHOffset; k++ )
- {
- if ( m_TempImage1[ k * m_nWidth + i ] == 65535 )
- {
- minY = k;
- break;
- }
- }
- for ( int k1 = m_nHeight - m_nHOffset - 1; k1 > m_nHOffset; k1-- )
- {
- if ( m_TempImage1[ k1 * m_nWidth + i ] == 65535 )
- {
- maxY = k1;
- break;
- }
- }
- linedirection = 1;
- //char_length = sprintf( m_datalen + char_count, "%d,%d:%d,%d", linedirection, i, minY - 2, maxY + 2 );
- //char_count += char_length;
- //char_length = sprintf( m_datalen + char_count, "\n" );
- //char_count += char_length;
-
- m_datalen[ count ] = linedirection;
- m_datalen[ count + 1 ] = i;
- m_datalen[ count + 2 ] = minY - 2;
- m_datalen[ count + 3 ] = maxY + 2;
- count += 4;
- }
- }
-
- }
- ///////////////////生成坏线标记文件,by chen G N 2013 - 01 - 16
- int CPixMatrix::SaveBadLineMap(const char *fileName)
- {
- ////////////////////////////新方法-基于二值图,返回值: -2 坏点文件未载入;-1 坏线写失败;0 无坏线;1有坏线
- if(m_BadPixelMap == NULL)
- {
- return -2;
- }
-
- long BadPixNum;
- long BadNum;
- //////////////////////////标记坏点
- //if ( nWidth != m_nWidth && nHeight != m_nHeight )
- //{
- // unsigned short *m_TempImage = new unsigned short [ m_nWidth * m_nHeight ];
- // unsigned short *m_TempImage1 = new unsigned short [ m_nWidth * m_nHeight ];
- // nWidth = m_nWidth;
- // nHeight = m_nHeight;
- //}
-
- memset( m_TempImage, 0, sizeof(unsigned short) * m_nWidth * m_nHeight );
- memset( m_TempImage1, 0, sizeof(unsigned short) * m_nWidth * m_nHeight );
- for ( int i = 2; i < m_nHeight - 2; i++ )
- {
- m_pBadPixNum = m_BadPixelMap[ i ].bad_pixel_num;
- if ( m_BadPixelMap[ i ].num_entries > 0 )
- {
- for ( int j = 0; j < m_BadPixelMap[ i ].num_entries; j++ )
- {
- BadPixNum = *m_pBadPixNum++;
- BadNum = BadPixNum & OFFSET_MASK;
- if ( BadNum > 1 && BadNum < m_nWidth - 1 )
- m_TempImage[ i * m_nWidth + BadNum ] = 65535;
- }
- }
- }
- /////////////////////按行标记坏线,水平坏线
- char *data_out = NULL;
- long char_count = 0;
- long char_length;
- int linedirection;
- data_out = new char [ 40000 ];
- for ( int i = 0; i < m_nHeight; i++ )
- {
- for ( int j = 2; j < m_nWidth - 2; j++ )
- {
- 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;
- if ( m_TempImage1[ i * m_nWidth + j ] < 40000 )
- m_TempImage1[ i * m_nWidth + j ] = 0;
- else
- m_TempImage1[ j * m_nWidth + i ] = 65535;
- }
- int tempcount = 0;
- for ( int p = 0; p < m_nWidth; p++ )
- {
- if ( m_TempImage1[ i * m_nWidth + p ] == 65535 )
- tempcount++;
- }
- int minX = 0;
- int maxX = 0;
- if ( tempcount > 500 )
- {
- for ( int k = 0; k < m_nWidth; k++ )
- {
- if ( m_TempImage1[ i * m_nWidth + k ] == 65535 )
- {
- minX = k;
- break;
- }
-
- }
- for ( int k1 = m_nWidth - 1; k1 > 0; k1-- )
- {
- if ( m_TempImage1[ i * m_nWidth + k1 ] == 65535 )
- {
- maxX = k1;
- break;
- }
-
- }
- linedirection = 0;
- char_length = sprintf( data_out + char_count, "%d,%d:%d,%d", linedirection, i, minX - 2, maxX + 2 );
- char_count += char_length;
- char_length = sprintf( data_out + char_count, "\n" );
- char_count += char_length;
- }
- }
- ////////////////////////////按列标记坏线--竖直坏线
- memset( m_TempImage1, 0, sizeof(unsigned short) * m_nWidth * m_nHeight );
- for ( int i = 0; i < m_nWidth; i++ )
- {
- for ( int j = 2; j < m_nHeight - 2; j++ )
- {
- 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;
- if ( m_TempImage1[ j * m_nWidth + i ] < 40000 )
- m_TempImage1[ j * m_nWidth + i ] = 0;
- else
- m_TempImage1[ j * m_nWidth + i ] = 65535;
- }
- int tempcount = 0;
- for ( int p = 0; p < m_nHeight; p++ )
- {
- if ( m_TempImage1[ p * m_nWidth + i ] == 65535 )
- tempcount++;
- }
- int minY = 0;
- int maxY = 0;
- if ( tempcount > 500 )
- {
- for ( int k = 0; k < m_nHeight; k++ )
- {
- if ( m_TempImage1[ k * m_nWidth + i ] == 65535 )
- {
- minY = k;
- break;
- }
-
- }
- for ( int k1 = m_nHeight - 1; k1 > 0; k1-- )
- {
- if ( m_TempImage1[ k1 * m_nWidth + i ] == 65535 )
- {
- maxY = k1;
- break;
- }
-
- }
- linedirection = 1;
- char_length = sprintf( data_out + char_count, "%d,%d:%d,%d", linedirection, i, minY - 2, maxY + 2 );
- char_count += char_length;
- char_length = sprintf( data_out + char_count, "\n" );
- char_count += char_length;
- }
- }
-
- ///////////////////////写坏线坐标/////////////////////////////
- if ( char_count == 0 )
- {
- return 0;
- }
- else
- {
- FILE *fp;
- fp = fopen(fileName, "wb");
- if(m_BadPixelMap == NULL)
- {
- return -2;
- }
- if (fwrite(data_out,1,char_count,fp)!=char_count)
- {
- return -1;
- }
- fclose(fp);
- }
- if( data_out != NULL )
- {
- delete [] data_out;
- data_out = NULL;
- }
- //if( m_TempImage != NULL )
- //{
- // delete [] m_TempImage;
- // m_TempImage = NULL;
- //}
- //if( m_TempImage1 != NULL )
- //{
- // delete [] m_TempImage1;
- // m_TempImage1 = NULL;
- //}
- return 1;
- }
- //////////////解决去栅影后的坏线振铃效应,返回-2为异常,返回1为正常去除坏线/////////////////////////////////////////
- int CPixMatrix::LoadandCorrectBadLine( unsigned short *pImage, int GridDirection )
- {
- //FILE *fp;
- int line_direction;
- int line_entries;
- int startpoint;
- int endpoint;
- //int char_count = 0;
- //int char_length = 0;
- int count = 0;
- //char *data_in = NULL;
- //data_in = new char [MAX_PIX_MAP_LINE];
- if( m_datalen == NULL)
- {
- return -2;
- }
- //fp = fopen(fileName, "rt");
- //if(fp == NULL)
- //{
- // return -2;
- //}
- // while( (m_datalen + char_count)!= NULL)
- // {
- //while( sscanf( m_datalen + char_count, "%d,%d:%d,%d", &line_direction, &line_entries, &startpoint, &endpoint ) == 4 )
- //while( (m_datalen[ count ] != -1) && (count < 400) )
- while( (m_datalen[ count ] != -1) && (count < m_nLineData) ) //???acoutD?óú×?′ó?μ????êy,zhaoyiru, 2017.05.09
- {
- line_direction = m_datalen[ count ] ;
- line_entries = m_datalen[ count + 1 ];
- startpoint = m_datalen[ count + 2 ];
- endpoint = m_datalen[ count + 3 ];
- count = count + 4;
- if( line_direction == 0 && line_entries > 2 && line_entries < m_nHeight - 2 && GridDirection == 2 )//////////////////////去除水平坏线
- {
- int nTGray = 0, nBGray = 0, nGray = 0;
- int nTDetails = 0, nBDetails = 0, nTAvg = 0, nBAvg = 0;
- if ( startpoint < m_nWOffset + 2 )
- startpoint = m_nWOffset + 2;
- if ( endpoint > m_nWidth - m_nWOffset - 2 )
- endpoint = m_nWidth - m_nWOffset - 2;
- for ( int i = startpoint; i < endpoint; i++ )
- {
- nTAvg = 0;
- for ( int j = -2; j < 3; j++ )
- {
- nTAvg += pImage[ ( line_entries - 1 ) * m_nWidth + i + j ];
- }
- nTAvg /= 5;
- nTDetails = pImage[ ( line_entries - 1 ) * m_nWidth + i ] - nTAvg;
- 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 ];
- 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 ];
- nGray = 0.75 * nTGray + 0.25 * nBGray;
- //pImage[ ( line_entries - 1 ) * m_nWidth + i ] = nGray + nTDetails;
- if ( (nGray + nTDetails) < 0 )
- pImage[ ( line_entries - 1 ) * m_nWidth + i ] = 0;
- else if ( (nGray + nTDetails) > 65535 )
- pImage[ ( line_entries - 1 ) * m_nWidth + i ] = 65535;
- else
- pImage[ ( line_entries - 1 ) * m_nWidth + i ] = nGray + nTDetails;
- }
- for ( int i = startpoint; i < endpoint; i++ )
- {
- nBAvg = 0;
- for ( int j = -2; j < 3; j++ )
- {
- nBAvg += pImage[ ( line_entries + 1 ) * m_nWidth + i + j ];
- }
- nBAvg /= 5;
- nBDetails = pImage[ ( line_entries + 1 ) * m_nWidth + i ] - nBAvg;
- 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 ];
- 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 ];
- nGray = 0.25 * nTGray + 0.75 * nBGray;
- //pImage[ ( line_entries - 1 ) * m_nWidth + i ] = nGray + nBDetails;
- if ( (nGray + nBDetails) < 0 )
- pImage[ ( line_entries + 1 ) * m_nWidth + i ] = 0;
- else if ( (nGray + nBDetails) > 65535 )
- pImage[ ( line_entries + 1 ) * m_nWidth + i ] = 65535;
- else
- pImage[ ( line_entries + 1 ) * m_nWidth + i ] = nGray + nBDetails;
- }
- for ( int i = startpoint; i < endpoint; i++ )
- {
- 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 ];
- }
- }
- if( line_direction == 1 && line_entries > 2 && line_entries < m_nWidth - 2 && GridDirection == 1 )//////////////////////去除竖直坏线
- {
- int nLGray = 0, nRGray = 0, nGray = 0;
- int nLDetails = 0, nRDetails = 0, nLAvg = 0, nRAvg = 0;
- if ( startpoint < m_nHOffset + 2 )
- startpoint = m_nHOffset + 2;
- if ( endpoint > m_nHeight - m_nHOffset - 2 )
- endpoint = m_nHeight - m_nHOffset - 2;
- for ( int i = startpoint; i < endpoint; i++ )
- {
- nLAvg = 0;
- for ( int j = -2; j < 3; j++ )
- {
- nLAvg += pImage[ ( i + j ) * m_nWidth + line_entries - 1 ];
- }
- nLAvg /= 5;
- nLDetails = pImage[ i * m_nWidth + line_entries - 1 ] - nLAvg;
- 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 ];
- 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 ];
- nGray = 0.75 * nLGray + 0.25 * nRGray;
- //pImage[ i * m_nWidth + line_entries - 1 ] = nGray + nLDetails;
- if ( (nGray + nLDetails) < 0 )
- pImage[ i * m_nWidth + line_entries - 1 ] = 0;
- else if ( (nGray + nLDetails) > 65535 )
- pImage[ i * m_nWidth + line_entries - 1 ] = 65535;
- else
- pImage[ i * m_nWidth + line_entries - 1 ] = nGray + nLDetails;
- }
- for ( int i = startpoint; i < endpoint; i++ )
- {
- nRAvg = 0;
- for ( int j = -2; j < 3; j++ )
- {
- nRAvg += pImage[ ( i + j ) * m_nWidth + line_entries + 1 ];
- }
- nRAvg /= 5;
- nRDetails = pImage[ i * m_nWidth + line_entries + 1 ] - nRAvg;
- 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 ];
- 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 ];
- nGray = 0.75 * nRGray + 0.25 * nLGray;
- //pImage[ i * m_nWidth + line_entries - 1 ] = nGray + nRDetails;
- if ( (nGray + nRDetails) < 0 )
- pImage[ i * m_nWidth + line_entries + 1 ] = 0;
- else if ( (nGray + nRDetails) > 65535 )
- pImage[ i * m_nWidth + line_entries + 1 ] = 65535;
- else
- pImage[ i * m_nWidth + line_entries + 1 ] = nGray + nRDetails;
- }
- for ( int i = startpoint; i < endpoint; i++ )
- {
- 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 ];
- }
- }
- //char_count += 4;
- //char_length = sprintf( m_datalen + char_count, "\n" );
- //char_count += char_length;
- }
- // }
-
-
- //if(fp != NULL)
- //{
- // fclose(fp);
- //}
- //if(data_in != NULL)
- //{
- // delete [] data_in;
- //}
- return 1;
- }
- bool CPixMatrix::AutoBadPixelMap(unsigned short* wImage)
- {
- char* m_charBadPixMap;
- char* m_charLinePixMap;
- uint8_t* wImageWB = nullptr;
- bool state;
- try {
- m_charLinePixMap = new char[MAX_PIX_MAP_LINE];
- m_charBadPixMap = new char[MAXI_BADPIX_COUNT];
- wImageWB = new uint8_t[m_nWidth * m_nHeight];
- int i, j; // for loop
- unsigned short Hist[16384];
- unsigned short count = 0;
- unsigned short tempsum = 0;
- unsigned short lowvalue, highvalue;
- lowvalue = highvalue = 0;
- bool state = true;
- // 统计直方图(只处理中间区域)
- for (int i = m_nHeight / 3; i < m_nHeight * 2 / 3; ++i)
- {
- for (int j = m_nWidth / 3; j < m_nWidth * 2 / 3; ++j)
- {
- unsigned short pixel = wImage[i * m_nWidth + j];
- if (pixel < 16384) // 防止数组越界
- {
- Hist[pixel]++;
- count++;
- }
- }
- }
- count = count / 10;
- for (i = 0; i < 16384; i++)
- {
- tempsum += Hist[i];
- if (tempsum > count)
- break;
- }
- lowvalue = std::max(i - 1500, 100);
- tempsum = 0;
- for (i = m_nPixelMax; i > 0; i--)
- {
- if (i < 16384) // 防止数组越界
- tempsum += Hist[i];
- if (tempsum > count)
- break;
- }
- highvalue = std::min(i + 1500, m_nPixelMax);
- for (i = 0; i < m_nHeight; i++)
- {
- for (j = 0; j < m_nWidth; j++)
- {
- unsigned short pixel = wImage[i * m_nWidth + j];
- wImageWB[i * m_nWidth + j] = (pixel > lowvalue && pixel < highvalue) ? 255 : 0;
- }
- }
- //查找空边
- int x1, x2, y1, y2;
- x1 = m_nWOffset;
- x2 = m_nWidth - m_nWOffset;
- y1 = m_nHOffset;
- y2 = m_nHeight - m_nHOffset;
- long char_count;
- long char_length;
- long nPixcount;
- nPixcount = char_count = char_length = 0;
- // 处理顶部区域
- for (int i = 0; i < y1; ++i)
- {
- int written = snprintf(m_charBadPixMap + char_count,
- MAXI_BADPIX_COUNT - char_count,
- "%d,0:\n", i);
- if (written < 0) {
- state = false;
- break;
- }
- char_count += written;
- }
- if (!state) throw std::runtime_error("Buffer overflow in top region");
- for (i = y1; i <= y2; i++)
- {
- // 每行
- nPixcount = char_length = 0;
- memset(m_charLinePixMap, 0, MAX_PIX_MAP_LINE); // 清空行缓存
- for (j = x1; j <= x2; ++j)
- {
- if (wImageWB[m_nWidth * i + j] == 0)
- {
- // 使用snprintf防止缓冲区溢出
- int written = snprintf(m_charLinePixMap + char_length,
- MAX_PIX_MAP_LINE - char_length,
- "%d,", j);
- if (written < 0) {
- state = false;
- break;
- }
- char_length += written;
- nPixcount++;
- }
- }
- if (!state) break;
- // 检查缓冲区是否足够
- if (char_count + MAX_PIX_MAP_LINE >= MAXI_BADPIX_COUNT) {
- state = false;
- break;
- }
- // 写入行数据
- int written = snprintf(m_charBadPixMap + char_count,
- MAXI_BADPIX_COUNT - char_count,
- "%d,%ld:", i, nPixcount);
- if (written < 0) {
- state = false;
- break;
- }
- char_count += written;
- // 复制行数据
- size_t copy_len = std::min((size_t)char_length, (size_t)(MAXI_BADPIX_COUNT - char_count));
- memcpy(m_charBadPixMap + char_count, m_charLinePixMap, copy_len);
- char_count += copy_len;
- // 添加换行符
- if (char_count < MAXI_BADPIX_COUNT - 1) {
- m_charBadPixMap[char_count++] = '\n';
- }
- else {
- state = false;
- break;
- }
- }
- if (state)
- {
- for (int i = y2 + 1; i < m_nHeight; ++i)
- {
- int written = snprintf(m_charBadPixMap + char_count,
- MAXI_BADPIX_COUNT - char_count,
- "%d,0:\n", i);
- if (written < 0) {
- state = false;
- break;
- }
- char_count += written;
- }
- }
- if (state)
- {
- LoadBadPixelMapChar(m_charBadPixMap);
- CombineBadPixelMap();
- }
- else
- {
- // Linux环境下使用标准错误输出
- fprintf(stderr, "Too many bad points, maybe incorrect exposure method!\n");
- }
- }
- catch (...) {
- state = false;
- }
- delete[] wImageWB;
- delete[] m_charLinePixMap;
- delete[] m_charBadPixMap;
- return state;
- }
- bool CPixMatrix::AutoBadPixelMap1(unsigned short *wImage)
- {
- int threshold1 = 0, threshold2 = 0;
- char* m_charBadPixMap = nullptr;
- char* m_charLinePixMap = nullptr;
- uint8_t* wImageWB = nullptr; // 用标准uint8_t替代Windows的BYTE
- bool state = true;
- int i, j;
- unsigned short dwMean = 0; // 替换Dunsigned short为标准unsigned short
- unsigned short dwIndex = 0;
- unsigned short dwCts = 0;
- try {
- // 内存分配并初始化,增加安全性
- m_charLinePixMap = new char[MAX_PIX_MAP_LINE]();
- m_charBadPixMap = new char[MAXI_BADPIX_COUNT]();
- wImageWB = new uint8_t[m_nWidth * m_nHeight]();
- // 计算均值(采样部分区域)
- for (i = 50; i < m_nHeight - 50; i += 8) {
- dwIndex = static_cast<unsigned short>(i * m_nWidth);
- // 确保不越界访问
- const int max_j = std::min(m_nWidth - 15, m_nWidth - 1);
- for (j = 50; j < max_j; j += 8) {
- dwMean += wImage[dwIndex + j];
- dwCts++;
- }
- }
- // 避免除零错误
- if (dwCts > 0) {
- dwMean /= dwCts;
- }
- else {
- dwMean = 0;
- state = false;
- fprintf(stderr, "No valid pixels for mean calculation\n");
- }
- if (state) {
- threshold1 = dwMean / 3;
- // 使用std::min确保跨平台兼容性
- threshold2 = std::min(dwMean * 2, dwMean + (m_nPixelMax - dwMean) / 2);
- // 生成二值化图像
- for (i = 0; i < m_nWidth * m_nHeight; ++i) {
- int temp = wImage[i]; // 注释掉了未定义的wDarkImage引用
- wImageWB[i] = ((temp < threshold1) || (temp > threshold2)) ? 0 : 255;
- }
- // 查找空边
- int x1 = m_nWOffset;
- int x2 = m_nWidth - m_nWOffset;
- int y1 = m_nHOffset;
- int y2 = m_nHeight - m_nHOffset;
- long char_count = 0;
- long char_length = 0;
- long nPixcount = 0;
- // 处理顶部区域
- for (i = 0; i < y1; ++i) {
- // 使用snprintf防止缓冲区溢出
- int written = snprintf(m_charBadPixMap + char_count,
- MAXI_BADPIX_COUNT - char_count,
- "%d,0:\n", i);
- if (written < 0) {
- state = false;
- break;
- }
- char_count += written;
- }
- if (!state) throw std::runtime_error("Buffer overflow in top region");
- // 处理中间区域
- for (i = y1; i <= y2 && state; ++i) {
- nPixcount = 0;
- char_length = 0;
- // 清空行缓存
- memset(m_charLinePixMap, 0, MAX_PIX_MAP_LINE);
- // 检测坏点并记录
- for (j = x1; j <= x2; ++j) {
- if (wImageWB[m_nWidth * i + j] == 0) {
- int written = snprintf(m_charLinePixMap + char_length,
- MAX_PIX_MAP_LINE - char_length,
- "%d,", j);
- if (written < 0) {
- state = false;
- break;
- }
- char_length += written;
- nPixcount++;
- }
- }
- if (!state) break;
- // 检查缓冲区剩余空间
- if (char_count + MAX_PIX_MAP_LINE >= MAXI_BADPIX_COUNT) {
- state = false;
- break;
- }
- // 写入行信息
- int written = snprintf(m_charBadPixMap + char_count,
- MAXI_BADPIX_COUNT - char_count,
- "%d,%ld:", i, nPixcount);
- if (written < 0) {
- state = false;
- break;
- }
- char_count += written;
- // 复制行数据(带边界检查)
- size_t copy_len = std::min(static_cast<size_t>(char_length),
- static_cast<size_t>(MAXI_BADPIX_COUNT - char_count));
- memcpy(m_charBadPixMap + char_count, m_charLinePixMap, copy_len);
- char_count += copy_len;
- // 添加换行符
- if (char_count < MAXI_BADPIX_COUNT - 1) {
- m_charBadPixMap[char_count++] = '\n';
- }
- else {
- state = false;
- break;
- }
- }
- // 处理底部区域
- if (state) {
- for (i = y2 + 1; i < m_nHeight; ++i) {
- int written = snprintf(m_charBadPixMap + char_count,
- MAXI_BADPIX_COUNT - char_count,
- "%d,0:\n", i);
- if (written < 0) {
- state = false;
- break;
- }
- char_count += written;
- }
- }
- }
- // 处理结果
- if (state) {
- LoadBadPixelMapChar(m_charBadPixMap, true);
- CombineBadPixelMap();
- }
- else {
- // Linux环境下使用标准错误输出
- fprintf(stderr, "Too many bad points, maybe incorrect exposure method!\n");
- }
- }
- catch (...) {
- state = false;
- fprintf(stderr, "Exception occurred during bad pixel detection\n");
- }
- // 确保内存释放,防止泄漏
- delete[] wImageWB;
- delete[] m_charLinePixMap;
- delete[] m_charBadPixMap;
- return state;
- }
- bool CPixMatrix::AutoBadPixelMap2(unsigned short *wImage)
- {
- char * m_charBadPixMap;
- char * m_charLinePixMap;
- m_charLinePixMap = new char [MAX_PIX_MAP_LINE];
- m_charBadPixMap = new char [MAXI_BADPIX_COUNT];
- bool state = true;
- int i, j;
- //查找空边
- int x1,x2,y1,y2;
- x1 = m_nWOffset;
- x2 = m_nWidth-m_nWOffset;
- y1 = m_nHOffset;
- y2 = m_nHeight-m_nHOffset;
- long char_count;
- long char_length;
- long nPixcount;
- nPixcount=char_count = char_length = 0;
- // 每列
- for(i = 0; i<y1; i++)
- {
- char_count += sprintf(m_charBadPixMap+char_count,"%d,0:\n",i);
- }
- for(i = y1; i<y2; i++)
- {
- // 每行
- nPixcount=char_length = 0;
- for(j = x1; j <x2; j++)
- {
- if(*(wImage+m_nWidth*i+j)==0xffff){
- //bad pix, add to charbuffer
- char_length += sprintf(m_charLinePixMap+char_length,"%d,",j);
- nPixcount++;
- }
- }
-
- //写入buffer
- if (char_count>(MAXI_BADPIX_COUNT-MAX_PIX_MAP_LINE)){
- state = false;
- // AfxMessageBox("Too Many bad point, maybe bad method to expose!");
- //gfun_LogError("Too Many bad point, maybe bad method to expose!");
- break;
- }
- char_count += sprintf(m_charBadPixMap+char_count,"%d,%ld:",i,nPixcount);
- memcpy(m_charBadPixMap+char_count,m_charLinePixMap,char_length);
- char_count += char_length;
- char_count += sprintf(m_charBadPixMap+char_count,"\n");
- }
- if(state)
- {
- for(i = y2; i<m_nHeight; i++)
- {
- char_count += sprintf(m_charBadPixMap+char_count,"%d,0:\n",i);
- }
- }
- if(state)
- {
- LoadBadPixelMapChar(m_charBadPixMap,true);
- CombineBadPixelMap();
- }
- if (m_charLinePixMap)
- {
- delete m_charLinePixMap;
- m_charLinePixMap = NULL;
- }
- if (m_charBadPixMap)
- {
- delete m_charBadPixMap;
- m_charBadPixMap = NULL;
- }
- return state;
- }
- void CPixMatrix::FreeBadPixelMap()
- {
- if(m_BadPixelMap != NULL) {
- for(int i = 0 ; i < m_nHeight ; ++i) {
- if(m_BadPixelMap[i].bad_pixel_num != NULL) {
- try
- {
- delete [] m_BadPixelMap[i].bad_pixel_num;
- }
- catch(...)
- {
- }
- m_BadPixelMap[i].bad_pixel_num = NULL;
- }
- }
- delete m_BadPixelMap;
- m_BadPixelMap = NULL;
- }
- }
- void CPixMatrix::FreeDBadLineMap()
- {
- LINE_MAP2 *ptemp = NULL;
- while(m_doubleLineMap)
- {
- ptemp = m_doubleLineMap;
- m_doubleLineMap = m_doubleLineMap->nextline;
- delete ptemp;
- }
- }
- void CPixMatrix::FreeBadNewPixelMap()
- {
- if(m_BadNewPixelMap != NULL) {
- for(int i = 0 ; i < m_nHeight ; ++i) {
- if(m_BadNewPixelMap[i].bad_pixel_num != NULL) {
- delete [] m_BadNewPixelMap[i].bad_pixel_num;
- m_BadNewPixelMap[i].bad_pixel_num = NULL;
- }
- }
- delete [] m_BadNewPixelMap;
- m_BadNewPixelMap = NULL;
- }
- }
- bool CPixMatrix::CombineBadPixelMap()
- {
- long line;
- long bad_pix_num;
- long bad_pix_numcount;
- long bad_new_pix_num;
- long bad_new_pix_numcount;
- long bad_temp_pix_num;
- // long bad_pix;
- // long bad_new_pix;
- long *p_bad_pix_num;
- long *p_bad_new_pix_num;
- long *p_bad_temp_pix;
- if (m_BadNewPixelMap == NULL)
- return true;
- if (m_BadPixelMap == NULL)
- {
- m_BadPixelMap = m_BadNewPixelMap;
- m_BadNewPixelMap = NULL;
- return true;
- }
- for(line = 0 ; line < m_nHeight ; ++line)
- {
- //逐行合并
- bad_pix_num = m_BadPixelMap[line].num_entries;
- bad_new_pix_num = m_BadNewPixelMap[line].num_entries;
- p_bad_pix_num = m_BadPixelMap[line].bad_pixel_num;
- p_bad_new_pix_num = m_BadNewPixelMap[line].bad_pixel_num;
- bad_temp_pix_num = bad_pix_numcount = bad_new_pix_numcount = 0;
-
- if(bad_new_pix_num==0)
- continue;
- p_bad_temp_pix = new long [(bad_pix_num+bad_new_pix_num+1)*sizeof(long)];
- memset( p_bad_temp_pix, 0, (bad_pix_num+bad_new_pix_num+1) * sizeof(long));
- while (!((bad_pix_numcount>=bad_pix_num)&&(bad_new_pix_numcount>=bad_new_pix_num)))
- {
- if(bad_pix_numcount==bad_pix_num)
- {
- for(;bad_new_pix_numcount<bad_new_pix_num;bad_new_pix_numcount++)
- {
- *(p_bad_temp_pix+bad_temp_pix_num) = *(p_bad_new_pix_num+bad_new_pix_numcount);
- bad_temp_pix_num++;
- }
- }
- else if(bad_new_pix_numcount==bad_new_pix_num)
- {
- for(;bad_pix_numcount<bad_pix_num;bad_pix_numcount++)
- {
- *(p_bad_temp_pix+bad_temp_pix_num) = *(p_bad_pix_num+bad_pix_numcount);
- bad_temp_pix_num++;
- }
- }
- else if((*(p_bad_new_pix_num+bad_new_pix_numcount)& OFFSET_MASK)> (*(p_bad_pix_num+bad_pix_numcount)& OFFSET_MASK))
- {
- *(p_bad_temp_pix+bad_temp_pix_num) = *(p_bad_pix_num+bad_pix_numcount);
- bad_temp_pix_num++;
- bad_pix_numcount++;
- }
- else if((*(p_bad_new_pix_num+bad_new_pix_numcount)& OFFSET_MASK)< (*(p_bad_pix_num+bad_pix_numcount)& OFFSET_MASK))
- {
- *(p_bad_temp_pix+bad_temp_pix_num) = *(p_bad_new_pix_num+bad_new_pix_numcount);
- bad_temp_pix_num++;
- bad_new_pix_numcount++;
- }
- else
- {
- *(p_bad_temp_pix+bad_temp_pix_num) = *(p_bad_pix_num+bad_pix_numcount);
- bad_temp_pix_num++;
- bad_pix_numcount++;
- bad_new_pix_numcount++;
- }
- }
- delete [] m_BadPixelMap[line].bad_pixel_num;
- m_BadPixelMap[line].bad_pixel_num = new long [(bad_temp_pix_num+1)*sizeof(long)];
- m_BadPixelMap[line].num_entries = bad_temp_pix_num;
- memcpy(m_BadPixelMap[line].bad_pixel_num,p_bad_temp_pix,(bad_temp_pix_num+1)*sizeof(long));
- delete [] p_bad_temp_pix;
- }
- FreeBadNewPixelMap();
- return true;
- }
- bool CPixMatrix::AutoBadPixelFixed(unsigned short *wImage, int threshold)
- {
- unsigned short Temp,Sum;
- //指向DIB图像中的数据;
- unsigned short* pDataByte;
- //差值,与阈值比较
- int idelta;
- // int count;
- // unsigned short sourcetemp[9];
- int i,j,x,y,k;//for loop
- for (j = m_nHOffset+2; j < m_nHeight-m_nHOffset; j++)
- {
- 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;
- for (i = m_nWOffset+2; i < m_nWidth-m_nHOffset ; i++)
- {
- //如果差值小于阈值,则不改变此点
- pDataByte = wImage + j * m_nWidth + i;
- idelta = *pDataByte - Temp;
- if (idelta<0) idelta = -1*idelta;
- if (idelta > threshold)
- {
- //如果差值大于阈值,则在邻近区域3x3找到所有满足阈值条件的点,取均值
- Sum = 0;
- k = 0;
- for (y = j - 1; y<= j+1; y++)
- {
- pDataByte = (unsigned short*)(wImage + y * m_nWidth + i - 1);
- for(x = i - 1; x <= i+1; x++)
- {
- idelta = *pDataByte - Temp;
- if (idelta<0) idelta = -1*idelta;
- if (idelta > threshold) break;
- Sum += *pDataByte;
- pDataByte++;
- k++;
- }
- }
- if (k>3) *(wImage + j * m_nWidth + i) = (unsigned short)Sum/k;
- }
- Temp = *(wImage + j * m_nWidth + i);
- }
- }
- return true;
- }
- bool CPixMatrix::LoadLineMap( const char *fileName )
- {
- FILE *fp;
- long *line_direction = new long;
- long *line_entries = new long;
- long *startpoint = new long;
- long *endpoint = new long;
- char *data_in = NULL;
- data_in = new char [MAX_PIX_MAP_LINE];
- LINE_MAP * m_xLineMapCur = NULL;
- LINE_MAP * m_yLineMapCur = NULL;
- if(data_in == NULL) {
- return false;
- }
- fp = fopen(fileName, "rb");
- if(fp == NULL) {
- return false;
- }
- FreeLineMap(m_xLineMap);
- m_xLineMap = NULL;
- FreeLineMap(m_yLineMap);
- m_yLineMap = NULL;
- while(fgets(data_in, MAX_PIX_MAP_LINE, fp) != NULL) {
- if(sscanf(data_in, "%ld,%ld:%ld,%ld", line_direction, line_entries, startpoint, endpoint) == 4) {
- // line_direction = 0;line_entries=359, startpoint=45, endpoint=2700;
- if(*line_direction==0)
- {
- m_xLineMapCur = new LINE_MAP;
- m_xLineMapCur->nextline = m_xLineMap;
- m_xLineMapCur->lLine = false;
- m_xLineMapCur->rLine = false;
- m_xLineMapCur->line_entries = (unsigned short)*line_entries;
- m_xLineMapCur->startpoint = (unsigned short)*startpoint;
- m_xLineMapCur->endpoint = (unsigned short)*endpoint;
- m_xLineMap = m_xLineMapCur;
-
- }
- else
- {
- m_yLineMapCur = new LINE_MAP;
- m_yLineMapCur->nextline = m_yLineMap;
- m_yLineMapCur->lLine = false;
- m_yLineMapCur->rLine = false;
- m_yLineMapCur->line_entries = (unsigned short)*line_entries;
- m_yLineMapCur->startpoint = (unsigned short)*startpoint;
- m_yLineMapCur->endpoint = (unsigned short)*endpoint;
- m_yLineMap = m_yLineMapCur;
- }
- }
- }
- MarkAdjacentLine(m_xLineMap);
- MarkAdjacentLine(m_yLineMap);
- if(fp != NULL) {
- fclose(fp);
- }
- if(data_in != NULL) {
- delete [] data_in;
- }
- if(m_xLineMapCur){
- FreeLineMap(m_xLineMapCur);
- }
- delete line_direction;
- delete line_entries;
- delete startpoint;
- delete endpoint;
- return true;
- }
- void CPixMatrix::CorrectDBadLines(unsigned short *image)
- {
- LINE_MAP2 * m_DLineMapCur = m_doubleLineMap;
- while(m_DLineMapCur)
- {
- if(m_DLineMapCur->nLineType == 0)
- {
- CorrectDXLines(image, m_DLineMapCur->line_entries, m_DLineMapCur->startpoint, m_DLineMapCur->endpoint);
- }
- else
- {
- CorrectDYLines(image, m_DLineMapCur->line_entries, m_DLineMapCur->startpoint, m_DLineMapCur->endpoint);
- }
- m_DLineMapCur = m_DLineMapCur->nextline;
- }
- }
- void CPixMatrix::SwitchDBadLines(unsigned short *image)
- {
- LINE_MAP2 * m_DLineMapCur = m_doubleLineMap;
- int specX = CalSpecX(image);
- int specY = CalSpecY(image);
- while (m_DLineMapCur)
- {
- if (m_DLineMapCur->nLineType == 0)
- {
- SwitchDXLines(image, m_DLineMapCur->line_entries, m_DLineMapCur->startpoint, m_DLineMapCur->endpoint, specX);
- }
- else
- {
- SwitchDYLines(image, m_DLineMapCur->line_entries, m_DLineMapCur->startpoint, m_DLineMapCur->endpoint, specY);
- }
- m_DLineMapCur = m_DLineMapCur->nextline;
- }
- }
- // modified by shihaozhe on 2019-4-18
- //below is old version
- /*void CPixMatrix::CorrectDXLines(unsigned short *image, int nentry, int nstart, int nend)
- {
- int nA,nB,nC, nA1, nA2, nB1, nB2, nC1, nC2;
- int nMIN;//, MID, MAX;
- for(int i = nstart; i<nend; i++)
- {
- nA1 = image[(nentry-2)*m_nWidth + i-2] - image[(nentry-1)*m_nWidth + i-1];
- nB1 = image[(nentry-2)*m_nWidth + i] - image[(nentry-1)*m_nWidth + i];
- nC1 = image[(nentry-2)*m_nWidth + i+2] - image[(nentry-1)*m_nWidth + i+1];
- nA2 = image[(nentry-1)*m_nWidth + i-1] - image[(nentry+2)*m_nWidth + i+2];
- nB2 = image[(nentry-1)*m_nWidth + i] - image[(nentry+2)*m_nWidth + i];
- nC2 = image[(nentry-1)*m_nWidth + i+1] - image[(nentry+2)*m_nWidth + i-2];
- //frame1[i*2880 + 1434] = (A + B + C)/3;
- nA = nA1*nA1 + nA2*nA2;
- nB = nB1*nB1 + nB2*nB2;
- nC = nC1*nC1 + nC2*nC2;
- nMIN = nA;
- if(nB<nMIN)
- {
- nMIN = nB;
- }
- if(nC<nMIN)
- {
- nMIN = nC;
- }
- if(nMIN == nA)
- {
- image[nentry*m_nWidth + i] = (image[(nentry-1)*m_nWidth + i-1]*2 + image[(nentry+2)*2880 + i+2])/3;
- }
- else if(nMIN == nB)
- {
- image[nentry*m_nWidth + i] = (image[(nentry-1)*m_nWidth + i]*2 + image[(nentry+2)*2880 + i])/3;
- }
- else //nMIN == nC
- {
- image[nentry*m_nWidth + i] = (image[(nentry-1)*m_nWidth + i+1]*2 + image[(nentry+2)*2880 + i-2])/3;
- }
- nA1 = image[(nentry+2)*m_nWidth + i+1] - image[(nentry+3)*m_nWidth + i+2];
- nB1 = image[(nentry+2)*m_nWidth + i] - image[(nentry+3)*m_nWidth + i];
- nC1 = image[(nentry+2)*m_nWidth + i-1] - image[(nentry+3)*m_nWidth + i-2];
- nA = nA1*nA1 + nA2*nA2;
- nB = nB1*nB1 + nB2*nB2;
- nC = nC1*nC1 + nC2*nC2;
- //frame1[i*2880 + 1434] = (A + B + C)/3;
- nMIN = nA;
- if(nB<nMIN)
- {
- nMIN = nB;
- }
- if(nC<nMIN)
- {
- nMIN = nC;
- }
- if(nMIN == nA)
- {
- image[(nentry+1)*m_nWidth + i] = (image[(nentry-1)*m_nWidth + i-2] + image[(nentry+2)*m_nWidth + i+1]*2)/3;
- }
- else if(nMIN == nB)
- {
- image[(nentry+1)*m_nWidth + i] = (image[(nentry-1)*m_nWidth + i] + image[(nentry+2)*m_nWidth + i]*2)/3;
- }
- else //nMIN == nC
- {
- image[(nentry+1)*m_nWidth + i] = (image[(nentry-1)*m_nWidth + i+2] + image[(nentry+2)*m_nWidth + i-1]*2)/3;
- }
- }
- }
- void CPixMatrix::CorrectDYLines(unsigned short *image, int nentry, int nstart, int nend)
- {
- int nA,nB,nC, nA1, nA2, nB1, nB2, nC1, nC2;
- int nMIN;//, MID, MAX;
- for(int i = nstart; i<nend; i++)
- {
- nA1 = image[(i-2)*m_nWidth + nentry-2] - image[(i-1)*m_nWidth + nentry-1];
- nB1 = image[i*m_nWidth + nentry-2] - image[i*m_nWidth + nentry-1];
- nC1 = image[(i+2)*m_nWidth + nentry-2] - image[(i+1)*m_nWidth + nentry-1];
- nA2 = image[(i-1)*m_nWidth + nentry-1] - image[(i+2)*m_nWidth + nentry+2];
- nB2 = image[i*m_nWidth + nentry-1] - image[i*m_nWidth + nentry+2];
- nC2 = image[(i+1)*m_nWidth + nentry-1] - image[(i-2)*m_nWidth + nentry+2];
- //frame1[i*2880 + 1434] = (A + B + C)/3;
- nA = nA1*nA1 + nA2*nA2;
- nB = nB1*nB1 + nB2*nB2;
- nC = nC1*nC1 + nC2*nC2;
- nMIN = nA;
- if(nB<nMIN)
- {
- nMIN = nB;
- }
- if(nC<nMIN)
- {
- nMIN = nC;
- }
- if(nMIN == nA)
- {
- image[i*m_nWidth + nentry] = (image[(i-1)*m_nWidth + nentry-1]*2 + image[(i+2)*2880 + nentry+2])/3;
- }
- else if(nMIN == nB)
- {
- image[i*m_nWidth + nentry] = (image[i*m_nWidth + nentry-1]*2 + image[i*2880 + nentry+2])/3;
- }
- else //nMIN == nC
- {
- image[i*m_nWidth + nentry] = (image[(i+1)*m_nWidth + nentry-1]*2 + image[(i-2)*2880 + nentry+2])/3;
- }
- nA1 = image[(i+1)*m_nWidth + nentry+2] - image[(i+2)*m_nWidth + nentry+3];
- nB1 = image[i*m_nWidth + nentry+2] - image[i*m_nWidth + nentry+3];
- nC1 = image[(i-1)*m_nWidth + nentry+2] - image[(i-2)*m_nWidth + nentry+3];
- nA = nA1*nA1 + nA2*nA2;
- nB = nB1*nB1 + nB2*nB2;
- nC = nC1*nC1 + nC2*nC2;
- //frame1[i*2880 + 1434] = (A + B + C)/3;
- nMIN = nA;
- if(nB<nMIN)
- {
- nMIN = nB;
- }
- if(nC<nMIN)
- {
- nMIN = nC;
- }
- if(nMIN == nA)
- {
- image[i*m_nWidth + nentry+1] = (image[(i-2)*m_nWidth + nentry-1] + image[(i+1)*m_nWidth + nentry+2]*2)/3;
- }
- else if(nMIN == nB)
- {
- image[i*m_nWidth + nentry+1] = (image[i*m_nWidth + nentry-1] + image[i*m_nWidth + nentry+2]*2)/3;
- }
- else //nMIN == nC
- {
- image[i*m_nWidth + nentry+1] = (image[(i+2)*m_nWidth + nentry-1] + image[(i-1)*m_nWidth + nentry+2]*2)/3;
- }
- }
- }*/
- void matMul(float** A, int Arow, int Acol, float** x, int xrow, int xcol, float* bias, float** b)
- {
- for (int i = 0; i < xrow; i++)
- {
- for (int j = 0; j < Acol; j++)
- {
- *((float*)b + i * Acol + j) = 0;
- for (int k = 0; k < Arow; k++)
- {
- *((float*)b + i * Acol + j) += (*((float*)A + k * Acol + j) * (*((float*)x + i * xcol + k)));
- }
- *((float*)b + i * Acol + j) += bias[j];
- }
- }
- }
- void relu(float **x, int xrow, int xcol, float** xnew)
- {
- for (int i = 0; i < xrow; i++)
- {
- for (int j = 0; j < xcol; j++)
- {
- *((float*)xnew + i * xcol + j) = std::max(*((float*)x + i * xcol + j), 0.0f);
- }
- }
- }
- int CPixMatrix::CalSpecX(unsigned short* image)
- {
- int spec = 2;
- int Flag1Votes = 0;
- int Flag2Votes = 0;
- for (int i = 3; i < (m_nHeight - 4); i = i + 100)
- {
- for (int j = 0; j < m_nWidth; j++)
- {
- float Flag1ResLine1 = (float(image[(i - 2)* m_nWidth + j]) / 2 + float(image[(i + 2)* m_nWidth + j]) / 2);
- float Flag1ResLine2 = (float(image[(i - 1)* m_nWidth + j]) / 2 + float(image[(i + 3)* m_nWidth + j]) / 2);
- float Flag2ResLine1 = (float(image[(i - 3)* m_nWidth + j]) / 2 + float(image[(i + 3)* m_nWidth + j]) / 2);
- float Flag2ResLine2 = (float(image[(i - 2)* m_nWidth + j]) / 2 + float(image[(i + 4)* m_nWidth + j]) / 2);
- float Flag1Score = abs(Flag1ResLine1 - float(image[i * m_nWidth + j])) + abs(Flag1ResLine2 - float(image[(i + 1) * m_nWidth + j]));
- float Flag2Score = abs(Flag2ResLine1 - float(image[i * m_nWidth + j])) + abs(Flag2ResLine2 - float(image[(i + 1) * m_nWidth + j]));
- if (Flag1Score < Flag2Score)
- Flag1Votes++;
- else
- Flag2Votes++;
- }
- }
- if (Flag1Votes >= Flag2Votes)
- spec = 2;
- else
- spec = 3;
- return spec;
- }
- int CPixMatrix::CalSpecY(unsigned short* image)
- {
- int spec = 2;
- int Flag1Votes = 0;
- int Flag2Votes = 0;
- for (int i = 3; i < (m_nWidth - 4); i = i + 100)
- {
- for (int j = 0; j < m_nHeight; j++)
- {
- float Flag1ResLine1 = (float(image[j * m_nWidth + (i - 2)]) / 2 + float(image[j* m_nWidth + (i + 2)]) / 2);
- float Flag1ResLine2 = (float(image[j * m_nWidth + (i - 1)]) / 2 + float(image[j * m_nWidth + (i + 3)]) / 2);
- float Flag2ResLine1 = (float(image[j * m_nWidth + (i - 3)]) / 2 + float(image[j * m_nWidth + (i + 3)]) / 2);
- float Flag2ResLine2 = (float(image[j * m_nWidth + (i - 2)]) / 2 + float(image[j * m_nWidth + (i + 4)]) / 2);
- float Flag1Score = abs(Flag1ResLine1 - float(image[j * m_nWidth + i])) + abs(Flag1ResLine2 - float(image[j * m_nWidth + (i + 1)]));
- float Flag2Score = abs(Flag2ResLine1 - float(image[j * m_nWidth + i])) + abs(Flag2ResLine2 - float(image[j * m_nWidth + (i + 1)]));
- if (Flag1Score < Flag2Score)
- Flag1Votes++;
- else
- Flag2Votes++;
- }
- }
- if (Flag1Votes >= Flag2Votes)
- spec = 2;
- else
- spec = 3;
- return spec;
- }
- void CPixMatrix::SwitchDXLines(unsigned short* image, int nentry, int nstart, int nend, int spec)
- {
- // 增加空指针检查,避免访问无效内存
- if (!image) return;
- // 边界有效性检查
- if (nentry < 1 || nentry >= m_nHeight) return;
- if (nstart >= nend) return; // 避免无效循环
- if (nstart < 0 || nend > m_nWidth) return; // 列范围检查
- // 计算有效行范围,防止越界
- const int upperBound = m_nHeight - 1;
- if ((nentry - 1 - spec) >= 0 && (nentry + spec) <= upperBound)
- {
- // 中间区域:使用上下行平均值替换
- for (int i = nstart; i < nend; ++i)
- {
- // 使用static_cast进行类型转换,增强C++类型安全性
- const unsigned short upperVal = image[(nentry - 1 - spec) * m_nWidth + i];
- const unsigned short lowerVal = image[(nentry - 1 + spec) * m_nWidth + i];
- image[(nentry - 1) * m_nWidth + i] = static_cast<unsigned short>(
- (static_cast<float>(upperVal) + static_cast<float>(lowerVal)) / 2.0f
- );
- const unsigned short upperVal2 = image[(nentry - spec) * m_nWidth + i];
- const unsigned short lowerVal2 = image[(nentry + spec) * m_nWidth + i];
- image[nentry * m_nWidth + i] = static_cast<unsigned short>(
- (static_cast<float>(upperVal2) + static_cast<float>(lowerVal2)) / 2.0f
- );
- }
- }
- else if ((nentry - 1 - spec) < 0)
- {
- // 顶部区域:使用下方行替换
- for (int i = nstart; i < nend; ++i)
- {
- image[(nentry - 1) * m_nWidth + i] = image[(nentry - 1 + spec) * m_nWidth + i];
- image[nentry * m_nWidth + i] = image[(nentry + spec) * m_nWidth + i];
- }
- }
- else
- {
- // 底部区域:使用上方行替换
- for (int i = nstart; i < nend; ++i)
- {
- image[(nentry - 1) * m_nWidth + i] = image[(nentry - 1 - spec) * m_nWidth + i];
- image[nentry * m_nWidth + i] = image[(nentry - spec) * m_nWidth + i];
- }
- }
- }
- void CPixMatrix::SwitchDYLines(unsigned short* image, int nentry, int nstart, int nend, int spec)
- {
- // 空指针检查:防止访问无效内存
- if (!image) return;
- // 基础边界有效性检查
- if (nentry < 1 || nentry >= m_nWidth) return; // 列索引越界
- if (nstart >= nend) return; // 无效的行范围(开始 >= 结束)
- if (nstart < 0 || nend > m_nHeight) return; // 行索引超出图像高度范围
- // 列索引最大有效值(0-based)
- const int max_col = m_nWidth - 1;
- // 检查列范围是否安全(避免spec导致的越界)
- if ((nentry - 1 - spec) >= 0 && (nentry + spec) <= max_col)
- {
- // 中间区域:用左右列平均值替换当前列
- for (int i = nstart; i < nend; ++i)
- {
- // 计算当前行的基地址(提升可读性,减少重复计算)
- const int row_base = i * m_nWidth;
- // 左侧列和右侧列像素值
- const unsigned short left1 = image[row_base + (nentry - 1 - spec)];
- const unsigned short right1 = image[row_base + (nentry - 1 + spec)];
- // 平均值计算,使用static_cast确保类型安全
- image[row_base + (nentry - 1)] = static_cast<unsigned short>(
- (static_cast<float>(left1) + static_cast<float>(right1)) / 2.0f
- );
- // 处理下一列
- const unsigned short left2 = image[row_base + (nentry - spec)];
- const unsigned short right2 = image[row_base + (nentry + spec)];
- image[row_base + nentry] = static_cast<unsigned short>(
- (static_cast<float>(left2) + static_cast<float>(right2)) / 2.0f
- );
- }
- }
- else if ((nentry - 1 - spec) < 0)
- {
- // 左侧边界区域:用右侧列替换当前列(无左侧有效列)
- for (int i = nstart; i < nend; ++i)
- {
- const int row_base = i * m_nWidth;
- image[row_base + (nentry - 1)] = image[row_base + (nentry - 1 + spec)];
- image[row_base + nentry] = image[row_base + (nentry + spec)];
- }
- }
- else
- {
- // 右侧边界区域:用左侧列替换当前列(无右侧有效列)
- for (int i = nstart; i < nend; ++i)
- {
- const int row_base = i * m_nWidth;
- image[row_base + (nentry - 1)] = image[row_base + (nentry - 1 - spec)];
- image[row_base + nentry] = image[row_base + (nentry - spec)];
- }
- }
- }
- void CPixMatrix::CorrectDXLines(unsigned short *image, int nentry, int nstart, int nend)
- {
- float w0[20][10] = { { -1.80551376e-01 , 1.48339527e-01 , 1.93434153e-01 ,-1.69102470e-01,
- -8.30293637e-02 , 4.68743068e-11 ,1.07092565e-01 , 4.90719458e-02,
- -1.76974159e-06 ,-2.34796443e-01 },
- { 2.26791936e-01 ,-4.42808635e-02 ,-1.96939813e-03 , 1.14001209e-01,
- 6.47560641e-01 , 5.78895742e-11 ,-4.89402386e-02 ,-4.71011428e-02,
- 1.41082019e-06 ,4.77452935e-01 },
- { 7.13725661e-01, -4.36174689e-01 ,1.00634693e-01 ,-2.00562230e-02,
- 4.35445448e-01 , 4.63354817e-12 , 2.24216928e-01, -6.99454931e-02,
- -2.63495911e-06 , 3.18166017e-01 },
- { -1.64505637e-01, 6.80188531e-03 , 2.86472226e-01 ,-4.37072848e-01,
- 1.32676203e-03 ,-1.86652083e-11 , 5.94614663e-02 ,-2.56715695e-01,
- 3.52631615e-08 , 1.19374415e-02 },
- { 1.92242685e-01 , 1.81255810e-02 ,-3.13366263e-01 , 1.65171353e-01,
- 6.37306352e-02 ,-8.55124219e-11 , 2.06302921e-03 , 7.13230627e-02,
- -8.22837481e-08 , 4.43748465e-04 },
- { 2.56683926e-02 ,-1.32064094e-01 ,5.92931576e-01 ,-3.93658658e-01,
- 1.89241730e-01 ,-1.02160955e-10 ,-2.60349051e-01, 6.00632605e-02,
- -1.08522973e-06 , 1.38723903e-02 },
- { 2.01019043e-01 , 4.60498596e-01 , 5.17829841e-01 ,-7.58273701e-01,
- -1.86647317e-02 ,-1.76434531e-11, -6.46477206e-01, 4.13885018e-01,
- -1.16803458e-06, -4.68910295e-01 },
- { 8.62645998e-01 , 9.68335169e-01 , 8.94262963e-02 , 1.74861574e-01,
- -1.85454720e-01 ,-3.19849587e-11 , 5.96511029e-01 ,6.05654463e-01,
- -7.56176036e-07, -5.07831177e-01 },
- { -8.71116572e-02, 3.66096894e-01 ,-6.28023440e-01 , 6.91073259e-01,
- 3.01648578e-01 ,-1.15338717e-10 ,3.58207881e-01 , 6.94645261e-01,
- -5.48317706e-06 ,-5.13752419e-01 },
- { 2.82701627e-01 ,-1.52676216e-01 ,-1.74173102e-01 , 1.68326554e-01,
- -2.20833287e-01, -1.17141445e-10 ,-2.30629014e-01 , 5.09502973e-02,
- 1.03628056e-05 , 5.17466666e-02 },
- { -2.93915459e-01, 2.59599255e-01, -1.57063692e-01 ,2.47658474e-01,
- 2.44114791e-01 ,-4.60543803e-11 , 1.60940275e-01, 2.99402289e-01,
- -5.21160106e-07 ,-6.60931206e-02 },
- { -2.18481332e-01, -3.65660965e-02 ,-2.76270937e-01 , 6.37817927e-01,
- -2.25210248e-01 ,-2.57545543e-11 , 5.04407490e-01 , 3.25644418e-01,
- -1.67397865e-06 ,-3.38918731e-01 },
- { 4.30656416e-01 ,-6.05850820e-01 , 3.56411868e-01 , 1.39871104e-01,
- 1.61133433e-02, 1.95278541e-11, 4.25782008e-01 ,9.79111337e-01,
- -9.56787789e-07, -3.11803801e-01 },
- { -2.36239573e-01 ,-1.73015706e-01 , 3.71253420e-01 ,-4.43126520e-01,
- -6.16987099e-01, -3.47112370e-11 ,-9.48308722e-02 , 2.35392910e-01,
- 3.90978755e-07 ,-5.30585275e-01 },
- { -9.21914745e-03 , 2.10569685e-01 , 5.86744516e-01, -3.29272382e-01,
- 6.87882591e-02 ,-5.60479430e-11, -1.31813225e-01 ,-9.01748595e-02,
- 1.48535772e-06 ,7.75831897e-02 },
- { -3.87850229e-02 , 2.12077616e-02 , 3.81875744e-02 ,1.12848455e-01,
- 1.83825444e-01, -1.18965572e-10 ,1.27163810e-01, 9.96867991e-02,
- 1.84582238e-06 ,7.19917185e-02 },
- { -5.18885615e-02 ,-1.74264405e-01 ,2.27000308e-01, -4.29314728e-01,
- -1.17003563e-01, -2.81658523e-11 , 1.17201003e-01 ,-3.07566778e-01,
- -2.01403760e-06 ,-9.42299766e-02 },
- { 4.90024471e-01 , 3.71047916e-02 , 1.01772677e-01, 9.51157739e-02,
- -1.71601670e-01 ,9.38432195e-11 , 6.24270660e-01, -4.71965703e-01,
- -3.31362503e-07 , 4.29190240e-01 },
- { 7.14228024e-02 ,-5.00837424e-02 , 1.47659839e-01 , 5.17815234e-02,
- 3.52253877e-01 ,4.90728233e-11 , 2.23559528e-01 ,-2.22637845e-01,
- -1.25922349e-06 , 2.04710832e-01 },
- { -1.36544728e-01 ,-1.49882902e-03 , 1.16554738e-01 ,-1.07862765e-01,
- 9.45728675e-02 , 2.68435951e-11 ,-2.67820297e-02, 1.89775976e-01,
- -2.41099251e-06, -1.34862793e-01 } };
- float w1[10][10] = { { -0.23780609 ,-0.20482226 , 0.41957468, 0.40999775, 0.38139455 ,0.49962715,
- 0.08417365 , 0.29976534 ,-0.08972183 ,-0.0574983 },
- { 0.07750195 ,-0.75130445 ,-0.06468749 ,-0.09033038 ,-0.18993429 ,0.82029747,
- -0.61669163 , 0.01430632 , 0.07597411, 0.27505365 },
- { 0.49405426, 0.01242133 ,0.25904818 ,0.41172925 ,0.07578099 ,0.06032476,
- 0.46222851, 0.06112816, 0.23961187, 0.11556222 },
- { 1.03532351 , 0.00237677 ,-1.41761159 , 0.2577502 ,-0.29667514 ,-0.22258016,
- 1.01807614, 0.47470323 , 0.64205134 , 1.17553243 },
- { -0.54341226 , 0.05698822 ,-0.63832775 ,-0.3224985, -0.08834335 ,-0.20154572,
- 0.33619324 , 0.53621269 ,-0.08154711 , 0.10801987 },
- { -1.43359900e-10, -2.19706358e-11 ,-7.62980145e-11 , 3.80564307e-11,
- 1.43483774e-10 ,1.43472783e-10 ,-7.14971534e-11 ,-4.57497718e-11,
- -7.01573521e-11 , 1.53484506e-10 },
- { -0.27199681, -0.06586821 , 0.06227236 ,0.50916823 ,0.12266234 , 0.25784538,
- 0.21391334 ,-0.04650493 ,-0.21507827 ,-0.81284655 },
- { 0.07606676, 0.13142832 ,-0.53617744 , 0.42486831 , 0.89048667 , 0.3048935,
- -0.71382582, -0.61000717 ,0.2688673 , 0.47569795 },
- { 1.52424197e-07 , 6.22315298e-08 - 6.52738940e-11 ,-8.68034280e-07,
- -5.31746214e-07 ,-1.78368165e-07 ,-2.11015294e-08, -1.52950988e-06,
- -3.42529575e-11, 2.35200781e-07 },
- { -0.9658597 ,-0.68771416, -0.14909613, -0.27739111 , 0.38529542, 0.14808103,
- -0.24781566 ,-0.52709518 ,0.07968214 ,-1.09388288 } };
- float w2[10][4] = { { -5.53134378e-01, -5.05213046e-02, 1.17674467e+00, 1.71470806e-10 },
- { 4.66713499e-01, 4.28506910e-01, -9.50886462e-02, 1.16200494e-10 },
- { 2.57455827e-01, -3.01318414e-02, -4.42330819e-01, 1.27572777e-10 },
- { 4.97257812e-02 ,1.68243289e-01 ,4.46669062e-01 ,2.76339875e-11 },
- { -2.00255754e-01, 3.68610428e-01 , 2.01173992e-01, -6.76783395e-11 },
- { -6.26138176e-02, -5.74267944e-01, 3.32356077e-01 ,-6.16432376e-12 },
- { 5.33413965e-02, -5.74108504e-02 ,-7.05800150e-01 ,-1.82851013e-10 },
- { -1.06458783e+00 ,-1.52935556e-01 ,-1.45886105e+00 ,-1.01166814e-10 },
- { 2.78628319e-08, 2.87125783e-01 ,-1.52646659e+00 ,-1.70692624e-10 },
- { -2.66851493e-01 ,-4.95637535e-02 , 7.59249111e-01 ,-6.39687400e-11 } };
- float w3[4][2] = { { 0.34269875 ,0.28160403 },{ -0.2269393 , 0.46291535 },{ 0.35116205 ,0.32428314 },{ 4.44950996e-11 ,-1.64150986e-10 } };
- float b0[10] = { -0.16749457 , 0.51837033 , 0.38030122 , 0.2287536 , 0.31281455 ,-0.31270079,
- -0.23713096, -0.27459556 ,-0.0780897 , 0.78092204 };
- float b1[10] = { -0.19847084 , 0.51427174 ,0.16520637 , 0.02815113 ,0.14546747 ,-0.33054209,
- -0.22630977 ,-0.21777402 ,-0.68041806 ,-0.51254995 };
- float b2[4] = { 0.10842243 , 0.55213982 ,-0.18210444, -0.43683546 };
- float b3[2] = { 0.22007598, -0.15169636 };
- if (nentry < 3 || nentry >(m_nHeight - 3))
- return;
- for (int i = nstart; i<nend; i++)
- {
- if (i < 2 || i >(m_nWidth - 3))
- {
- image[nentry * m_nWidth + i] = image[(nentry + 1) * m_nWidth + i];
- image[(nentry - 1) * m_nWidth + i] = image[(nentry - 2) * m_nWidth + i];
- }
- else
- {
- float x0[1][20];
- int count = 0;
- float maxP = 0;
- float minP = 1e9;
- for (int k = -3; k <= 2; k++)
- {
- if (k == -1 || k == 0) continue;
- for (int l = -2; l <= 2; l++)
- {
- *((float*)x0 + count) = float(image[(nentry + k) * m_nWidth + (i + l)]);
- count++;
- maxP = std::max(maxP, float(image[(nentry + k) * m_nWidth + (i + l)]));
- minP = std::min(minP, float(image[(nentry + k) * m_nWidth + (i + l)]));
- }
- }
- if ((maxP - minP) > 2560)
- {
- maxP = std::min(maxP + 256, 65535.0f);
- minP = std::max(minP - 256, 0.0f);
- }
- if((maxP - minP) < 1)maxP = minP + 1;
- for (int k = 0; k < 20; k++)
- {
- *((float*)x0 + k) = (*((float*)x0 + k) - minP) / (maxP - minP);
- }
- float z1[1][20];
- float x1[1][20];
- float z2[1][10];
- float x2[1][10];
- float z3[1][5];
- float x3[1][5];
- float z4[1][2];
- float y[1][2];
- matMul((float**)w0, 20, 10, (float**)x0, 1, 20, b0, (float**)z1);
- relu((float**)z1, 1, 10, (float**)x1);
- matMul((float**)w1, 10, 10, (float**)x1, 1, 10, b1, (float**)z2);
- relu((float**)z2, 1, 10, (float**)x2);
- matMul((float**)w2, 10, 4, (float**)x2, 1, 10, b2, (float**)z3);
- relu((float**)z3, 1, 4, (float**)x3);
- matMul((float**)w3, 4, 2, (float**)x3, 1, 4, b3, (float**)z4);
- relu((float**)z4, 1, 2, (float**)y);
- float predict1 = std::max(std::min(*((float*)y) * (maxP - minP) + minP, 65535.0f), 0.0f);
- float predict2 = std::max(std::min(*((float*)y + 1) * (maxP - minP) + minP, 65535.0f), 0.0f);
- float flag1line1 = float(image[(nentry - 3) * m_nWidth + i]) / 2 + float(image[(nentry + 1) * m_nWidth + i]) / 2;
- float flag1line2 = float(image[(nentry - 2) * m_nWidth + i]) / 2 + float(image[(nentry + 2) * m_nWidth + i]) / 2;
- float flag1score = abs(flag1line1 - predict1) +
- abs(flag1line2 - predict2);
- if (flag1score < 30)
- {
- image[(nentry - 1) * m_nWidth + i] = static_cast<unsigned short>(
- std::max(std::min(flag1line1, 65535.0f), 0.0f)
- );
- image[nentry * m_nWidth + i] = static_cast<unsigned short>(
- std::max(std::min(flag1line2, 65535.0f), 0.0f)
- );
- }
- else
- {
- // 同样使用static_cast进行类型转换,明确转换意图
- image[(nentry - 1) * m_nWidth + i] = static_cast<unsigned short>(predict1);
- image[nentry * m_nWidth + i] = static_cast<unsigned short>(predict2);
- }
- }
- }
- }
- void CPixMatrix::CorrectDYLines(unsigned short *image, int nentry, int nstart, int nend)
- {
- float w0[20][10] = { { -1.80551376e-01 , 1.48339527e-01 , 1.93434153e-01 ,-1.69102470e-01,
- -8.30293637e-02 , 4.68743068e-11 ,1.07092565e-01 , 4.90719458e-02,
- -1.76974159e-06 ,-2.34796443e-01 },
- { 2.26791936e-01 ,-4.42808635e-02 ,-1.96939813e-03 , 1.14001209e-01,
- 6.47560641e-01 , 5.78895742e-11 ,-4.89402386e-02 ,-4.71011428e-02,
- 1.41082019e-06 ,4.77452935e-01 },
- { 7.13725661e-01, -4.36174689e-01 ,1.00634693e-01 ,-2.00562230e-02,
- 4.35445448e-01 , 4.63354817e-12 , 2.24216928e-01, -6.99454931e-02,
- -2.63495911e-06 , 3.18166017e-01 },
- { -1.64505637e-01, 6.80188531e-03 , 2.86472226e-01 ,-4.37072848e-01,
- 1.32676203e-03 ,-1.86652083e-11 , 5.94614663e-02 ,-2.56715695e-01,
- 3.52631615e-08 , 1.19374415e-02 },
- { 1.92242685e-01 , 1.81255810e-02 ,-3.13366263e-01 , 1.65171353e-01,
- 6.37306352e-02 ,-8.55124219e-11 , 2.06302921e-03 , 7.13230627e-02,
- -8.22837481e-08 , 4.43748465e-04 },
- { 2.56683926e-02 ,-1.32064094e-01 ,5.92931576e-01 ,-3.93658658e-01,
- 1.89241730e-01 ,-1.02160955e-10 ,-2.60349051e-01, 6.00632605e-02,
- -1.08522973e-06 , 1.38723903e-02 },
- { 2.01019043e-01 , 4.60498596e-01 , 5.17829841e-01 ,-7.58273701e-01,
- -1.86647317e-02 ,-1.76434531e-11, -6.46477206e-01, 4.13885018e-01,
- -1.16803458e-06, -4.68910295e-01 },
- { 8.62645998e-01 , 9.68335169e-01 , 8.94262963e-02 , 1.74861574e-01,
- -1.85454720e-01 ,-3.19849587e-11 , 5.96511029e-01 ,6.05654463e-01,
- -7.56176036e-07, -5.07831177e-01 },
- { -8.71116572e-02, 3.66096894e-01 ,-6.28023440e-01 , 6.91073259e-01,
- 3.01648578e-01 ,-1.15338717e-10 ,3.58207881e-01 , 6.94645261e-01,
- -5.48317706e-06 ,-5.13752419e-01 },
- { 2.82701627e-01 ,-1.52676216e-01 ,-1.74173102e-01 , 1.68326554e-01,
- -2.20833287e-01, -1.17141445e-10 ,-2.30629014e-01 , 5.09502973e-02,
- 1.03628056e-05 , 5.17466666e-02 },
- { -2.93915459e-01, 2.59599255e-01, -1.57063692e-01 ,2.47658474e-01,
- 2.44114791e-01 ,-4.60543803e-11 , 1.60940275e-01, 2.99402289e-01,
- -5.21160106e-07 ,-6.60931206e-02 },
- { -2.18481332e-01, -3.65660965e-02 ,-2.76270937e-01 , 6.37817927e-01,
- -2.25210248e-01 ,-2.57545543e-11 , 5.04407490e-01 , 3.25644418e-01,
- -1.67397865e-06 ,-3.38918731e-01 },
- { 4.30656416e-01 ,-6.05850820e-01 , 3.56411868e-01 , 1.39871104e-01,
- 1.61133433e-02, 1.95278541e-11, 4.25782008e-01 ,9.79111337e-01,
- -9.56787789e-07, -3.11803801e-01 },
- { -2.36239573e-01 ,-1.73015706e-01 , 3.71253420e-01 ,-4.43126520e-01,
- -6.16987099e-01, -3.47112370e-11 ,-9.48308722e-02 , 2.35392910e-01,
- 3.90978755e-07 ,-5.30585275e-01 },
- { -9.21914745e-03 , 2.10569685e-01 , 5.86744516e-01, -3.29272382e-01,
- 6.87882591e-02 ,-5.60479430e-11, -1.31813225e-01 ,-9.01748595e-02,
- 1.48535772e-06 ,7.75831897e-02 },
- { -3.87850229e-02 , 2.12077616e-02 , 3.81875744e-02 ,1.12848455e-01,
- 1.83825444e-01, -1.18965572e-10 ,1.27163810e-01, 9.96867991e-02,
- 1.84582238e-06 ,7.19917185e-02 },
- { -5.18885615e-02 ,-1.74264405e-01 ,2.27000308e-01, -4.29314728e-01,
- -1.17003563e-01, -2.81658523e-11 , 1.17201003e-01 ,-3.07566778e-01,
- -2.01403760e-06 ,-9.42299766e-02 },
- { 4.90024471e-01 , 3.71047916e-02 , 1.01772677e-01, 9.51157739e-02,
- -1.71601670e-01 ,9.38432195e-11 , 6.24270660e-01, -4.71965703e-01,
- -3.31362503e-07 , 4.29190240e-01 },
- { 7.14228024e-02 ,-5.00837424e-02 , 1.47659839e-01 , 5.17815234e-02,
- 3.52253877e-01 ,4.90728233e-11 , 2.23559528e-01 ,-2.22637845e-01,
- -1.25922349e-06 , 2.04710832e-01 },
- { -1.36544728e-01 ,-1.49882902e-03 , 1.16554738e-01 ,-1.07862765e-01,
- 9.45728675e-02 , 2.68435951e-11 ,-2.67820297e-02, 1.89775976e-01,
- -2.41099251e-06, -1.34862793e-01 } };
- float w1[10][10] = { { -0.23780609 ,-0.20482226 , 0.41957468, 0.40999775, 0.38139455 ,0.49962715,
- 0.08417365 , 0.29976534 ,-0.08972183 ,-0.0574983 },
- { 0.07750195 ,-0.75130445 ,-0.06468749 ,-0.09033038 ,-0.18993429 ,0.82029747,
- -0.61669163 , 0.01430632 , 0.07597411, 0.27505365 },
- { 0.49405426, 0.01242133 ,0.25904818 ,0.41172925 ,0.07578099 ,0.06032476,
- 0.46222851, 0.06112816, 0.23961187, 0.11556222 },
- { 1.03532351 , 0.00237677 ,-1.41761159 , 0.2577502 ,-0.29667514 ,-0.22258016,
- 1.01807614, 0.47470323 , 0.64205134 , 1.17553243 },
- { -0.54341226 , 0.05698822 ,-0.63832775 ,-0.3224985, -0.08834335 ,-0.20154572,
- 0.33619324 , 0.53621269 ,-0.08154711 , 0.10801987 },
- { -1.43359900e-10, -2.19706358e-11 ,-7.62980145e-11 , 3.80564307e-11,
- 1.43483774e-10 ,1.43472783e-10 ,-7.14971534e-11 ,-4.57497718e-11,
- -7.01573521e-11 , 1.53484506e-10 },
- { -0.27199681, -0.06586821 , 0.06227236 ,0.50916823 ,0.12266234 , 0.25784538,
- 0.21391334 ,-0.04650493 ,-0.21507827 ,-0.81284655 },
- { 0.07606676, 0.13142832 ,-0.53617744 , 0.42486831 , 0.89048667 , 0.3048935,
- -0.71382582, -0.61000717 ,0.2688673 , 0.47569795 },
- { 1.52424197e-07 , 6.22315298e-08 - 6.52738940e-11 ,-8.68034280e-07,
- -5.31746214e-07 ,-1.78368165e-07 ,-2.11015294e-08, -1.52950988e-06,
- -3.42529575e-11, 2.35200781e-07 },
- { -0.9658597 ,-0.68771416, -0.14909613, -0.27739111 , 0.38529542, 0.14808103,
- -0.24781566 ,-0.52709518 ,0.07968214 ,-1.09388288 } };
- float w2[10][4] = { { -5.53134378e-01, -5.05213046e-02, 1.17674467e+00, 1.71470806e-10 },
- { 4.66713499e-01, 4.28506910e-01, -9.50886462e-02, 1.16200494e-10 },
- { 2.57455827e-01, -3.01318414e-02, -4.42330819e-01, 1.27572777e-10 },
- { 4.97257812e-02 ,1.68243289e-01 ,4.46669062e-01 ,2.76339875e-11 },
- { -2.00255754e-01, 3.68610428e-01 , 2.01173992e-01, -6.76783395e-11 },
- { -6.26138176e-02, -5.74267944e-01, 3.32356077e-01 ,-6.16432376e-12 },
- { 5.33413965e-02, -5.74108504e-02 ,-7.05800150e-01 ,-1.82851013e-10 },
- { -1.06458783e+00 ,-1.52935556e-01 ,-1.45886105e+00 ,-1.01166814e-10 },
- { 2.78628319e-08, 2.87125783e-01 ,-1.52646659e+00 ,-1.70692624e-10 },
- { -2.66851493e-01 ,-4.95637535e-02 , 7.59249111e-01 ,-6.39687400e-11 } };
- float w3[4][2] = { { 0.34269875 ,0.28160403 },{ -0.2269393 , 0.46291535 },{ 0.35116205 ,0.32428314 },{ 4.44950996e-11 ,-1.64150986e-10 } };
- float b0[10] = { -0.16749457 , 0.51837033 , 0.38030122 , 0.2287536 , 0.31281455 ,-0.31270079,
- -0.23713096, -0.27459556 ,-0.0780897 , 0.78092204 };
- float b1[10] = { -0.19847084 , 0.51427174 ,0.16520637 , 0.02815113 ,0.14546747 ,-0.33054209,
- -0.22630977 ,-0.21777402 ,-0.68041806 ,-0.51254995 };
- float b2[4] = { 0.10842243 , 0.55213982 ,-0.18210444, -0.43683546 };
- float b3[2] = { 0.22007598, -0.15169636 };
- if (nentry < 3 || nentry >(m_nWidth - 3))
- return;
- for (int i = nstart; i < nend; i++)
- {
- if (i < 2 || i >(m_nHeight - 3))
- {
- image[i * m_nWidth + nentry - 1] = image[i * m_nWidth + nentry - 2];
- image[i * m_nWidth + nentry] = image[i * m_nWidth + nentry + 1];
- }
- else
- {
- float x0[1][20];
- int count = 0;
- float maxP = 0;
- float minP = 1e9;
- for (int k = -3; k <= 2; k++)
- {
- if (k == -1 || k == 0) continue;
- for (int l = -2; l <= 2; l++)
- {
- *((float*)x0 + count) = float(image[(i + l)* m_nWidth + (nentry + k)]);
- count++;
- maxP = std::max(maxP, float(image[(i + l)* m_nWidth + (nentry + k)]));
- minP = std::min(minP, float(image[(i + l)* m_nWidth + (nentry + k)]));
- }
- }
- if ((maxP - minP) > 2560)
- {
- maxP = std::min(maxP + 256, 65535.0f);
- minP = std::max(minP - 256, 0.0f);
- }
- if((maxP - minP) < 1)maxP = minP + 1;
- for (int k = 0; k < 20; k++)
- {
- *((float*)x0 + k) = (*((float*)x0 + k) - minP) / (maxP - minP);
- }
- float z1[1][20];
- float x1[1][20];
- float z2[1][10];
- float x2[1][10];
- float z3[1][5];
- float x3[1][5];
- float z4[1][2];
- float y[1][2];
- matMul((float**)w0, 20, 10, (float**)x0, 1, 20, b0, (float**)z1);
- relu((float**)z1, 1, 10, (float**)x1);
- matMul((float**)w1, 10, 10, (float**)x1, 1, 10, b1, (float**)z2);
- relu((float**)z2, 1, 10, (float**)x2);
- matMul((float**)w2, 10, 4, (float**)x2, 1, 10, b2, (float**)z3);
- relu((float**)z3, 1, 4, (float**)x3);
- matMul((float**)w3, 4, 2, (float**)x3, 1, 4, b3, (float**)z4);
- relu((float**)z4, 1, 2, (float**)y);
- float predict1 = std::max(std::min(*((float*)y) * (maxP - minP) + minP, 65535.0f), 0.0f);
- float predict2 = std::max(std::min(*((float*)y + 1) * (maxP - minP) + minP, 65535.0f), 0.0f);
- float flag1line1 = float(image[i * m_nWidth + (nentry - 3)])/ 2 + float(image[i * m_nWidth + (nentry + 1)]) / 2;
- float flag1line2 = float(image[i * m_nWidth + (nentry - 2)]) / 2 + float(image[i * m_nWidth + (nentry + 2)]) / 2;
- float flag1score = abs(flag1line1 - predict1) +
- abs(flag1line2 - predict2);
- if (flag1score < 30)
- {
- image[(nentry - 1) * m_nWidth + i] = static_cast<unsigned short>(
- std::max(std::min(flag1line1, 65535.0f), 0.0f)
- );
- image[nentry * m_nWidth + i] = static_cast<unsigned short>(
- std::max(std::min(flag1line2, 65535.0f), 0.0f)
- );
- }
- else
- {
- image[(nentry - 1) * m_nWidth + i] = static_cast<unsigned short>(predict1);
- image[nentry * m_nWidth + i] = static_cast<unsigned short>(predict2);
- }
- }
- }
- }
- bool CPixMatrix::LoadDLineMap( const char *fileName )
- {
- FILE *fp;
- int *line_direction = new int;
- int *line_entries = new int;
- int *startpoint = new int;
- int *endpoint = new int;
- char *data_in = NULL;
- data_in = new char [MAX_PIX_MAP_LINE];
- LINE_MAP2 * m_DLineMapCur = NULL;
- if(data_in == NULL) {
- return false;
- }
- fp = fopen(fileName, "rb");
- if(fp == NULL) {
- return false;
- }
- FreeDBadLineMap();
- m_doubleLineMap = NULL;
- while(fgets(data_in, MAX_PIX_MAP_LINE, fp) != NULL) {
- if(sscanf(data_in, "%d,%d:%d,%d", line_direction, line_entries, startpoint, endpoint) == 4) {
- // line_direction = 0;line_entries=359, startpoint=45, endpoint=2700;
- m_DLineMapCur = new LINE_MAP2;
- m_DLineMapCur->nLineType = *line_direction;
- m_DLineMapCur->startpoint = std::max(2, *startpoint);
- if(m_DLineMapCur->nLineType == 0)
- {
- m_DLineMapCur->line_entries = std::min(m_nHeight-1, std::max(1,*line_entries));
- m_DLineMapCur->endpoint = std::min(m_nWidth-2, *endpoint);
- }
- else
- {
- m_DLineMapCur->line_entries = std::min(m_nWidth-1, std::max(1,*line_entries));
- m_DLineMapCur->endpoint = std::min(m_nHeight-2, *endpoint);
- }
- m_DLineMapCur->nextline = m_doubleLineMap;
- m_doubleLineMap = m_DLineMapCur;
- }
- }
- if(fp != NULL) {
- fclose(fp);
- }
- if(data_in != NULL) {
- delete [] data_in;
- }
- delete line_direction;
- delete line_entries;
- delete startpoint;
- delete endpoint;
- return true;
- }
- bool CPixMatrix::MarkAdjacentLine( LINE_MAP *m_linemap)
- {
- LINE_MAP *temp_linemap;
- unsigned short lineentry, llineentry, rlineentry;
- while(m_linemap)
- {
- lineentry = m_linemap->line_entries;
- llineentry = lineentry-1;
- rlineentry = lineentry+1;
- temp_linemap = m_linemap->nextline;
- //check through the line to find out adjacent line
- while(temp_linemap)
- {
- lineentry = temp_linemap->line_entries;
- if(lineentry == llineentry)
- {
- m_linemap->lLine = true;
- temp_linemap->rLine = true;
- }
- if(lineentry == rlineentry)
- {
- m_linemap->rLine = true;
- temp_linemap->lLine = true;
- }
- temp_linemap = temp_linemap->nextline;
- }
- //next one
- m_linemap = m_linemap->nextline;
- }
- return true;
- }
- void CPixMatrix::CorrectLines(unsigned short* image)
- {
- int i,i1,i2,j,j1,j2;
- unsigned short avepoint, avesurrond;
- unsigned short temp;
- LINE_MAP *m_LineMapCur;
- LINE_MAP *temp_linemap;
- LINE_MAP *left_linemap = NULL;
- //first cycle
- m_LineMapCur = m_xLineMap;
- while(m_LineMapCur)
- {
- if((m_LineMapCur->lLine==false)&&(m_LineMapCur->rLine==false))
- {
- //line correction
- i = m_LineMapCur->line_entries;
- i1 = i-1;
- i2 = i+1;
- temp = image[i*m_nWidth+(m_LineMapCur->startpoint)-1];
- for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
- {
- j1 = j-1;
- j2 = j+1;
- avepoint = (2*image[i*m_nWidth+j]+temp+image[i*m_nWidth+j2])/4;
- 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;
- temp = image[i*m_nWidth+j];
- image[i*m_nWidth+j] = static_cast<unsigned short>(image[i*m_nWidth+j]*avesurrond/avepoint);
- }
- }
- else if((m_LineMapCur->lLine==true)&&(m_LineMapCur->rLine==false))
- {
- //line correction
- i = m_LineMapCur->line_entries;
- // i1 = i-1;
- i2 = i+1;
- temp = image[i*m_nWidth+(m_LineMapCur->startpoint)-1];
- for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
- {
- j1 = j-1;
- j2 = j+1;
- avepoint = (2*image[i*m_nWidth+j]+temp+image[i*m_nWidth+j2])/4;
- avesurrond = (2*image[i2*m_nWidth+j]+image[i2*m_nWidth+j1]+image[i2*m_nWidth+j2])/4;
- temp = image[i*m_nWidth+j];
- image[i*m_nWidth+j] = static_cast<unsigned short>(image[i*m_nWidth+j]*avesurrond/avepoint);
- }
- }
- else if((m_LineMapCur->lLine==false)&&(m_LineMapCur->rLine==true))
- {
- //line correction
- i = m_LineMapCur->line_entries;
- i1 = i-1;
- // i2 = i+1;
- temp = image[i*m_nWidth+(m_LineMapCur->startpoint)-1];
- for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
- {
- j1 = j-1;
- j2 = j+1;
- avepoint = (2*image[i*m_nWidth+j]+temp+image[i*m_nWidth+j2])/4;
- avesurrond = (2*image[i1*m_nWidth+j]+image[i1*m_nWidth+j1]+image[i1*m_nWidth+j2])/4;
- temp = image[i*m_nWidth+j];
- image[i*m_nWidth+j] = static_cast<unsigned short>(image[i*m_nWidth+j]*avesurrond/avepoint);
- }
- }
- else
- {
- //skip, next cycle to correction
- temp_linemap = new LINE_MAP;
- temp_linemap->nextline = left_linemap;
- temp_linemap->line_entries = m_LineMapCur->line_entries;
- temp_linemap->lLine = false;
- temp_linemap->rLine = false;
- temp_linemap->startpoint = m_LineMapCur->startpoint;
- temp_linemap->endpoint = m_LineMapCur->endpoint;
- left_linemap = temp_linemap;
- }
- m_LineMapCur = m_LineMapCur->nextline;
- }
- while(left_linemap)
- {
- m_LineMapCur = left_linemap;
- left_linemap = NULL;
- MarkAdjacentLine(m_LineMapCur);
- while(m_LineMapCur)
- {
- if((m_LineMapCur->lLine==false)&&(m_LineMapCur->rLine==false))
- {
- i = m_LineMapCur->line_entries;
- i1 = i-1;
- i2 = i+1;
- temp = image[i*m_nWidth+(m_LineMapCur->startpoint)-1];
- for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
- {
- j1 = j-1;
- j2 = j+1;
- avepoint = (2*image[i*m_nWidth+j]+temp+image[i*m_nWidth+j2])/4;
- 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;
- temp = image[i*m_nWidth+j];
- image[i*m_nWidth+j] = static_cast<unsigned short>(image[i*m_nWidth+j]*avesurrond/avepoint);
- }
- }
- else if((m_LineMapCur->lLine==true)&&(m_LineMapCur->rLine==false))
- {
- i = m_LineMapCur->line_entries;
- // i1 = i-1;
- i2 = i+1;
- temp = image[i*m_nWidth+(m_LineMapCur->startpoint)-1];
- for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
- {
- j1 = j-1;
- j2 = j+1;
- avepoint = (2*image[i*m_nWidth+j]+temp+image[i*m_nWidth+j2])/4;
- avesurrond = (2*image[i2*m_nWidth+j]+image[i2*m_nWidth+j1]+image[i2*m_nWidth+j2])/4;
- temp = image[i*m_nWidth+j];
- image[i*m_nWidth+j] = static_cast<unsigned short>(image[i*m_nWidth+j]*avesurrond/avepoint);
- }
- }
- else if((m_LineMapCur->lLine==false)&&(m_LineMapCur->rLine==true))
- {
- i = m_LineMapCur->line_entries;
- i1 = i-1;
- // i2 = i+1;
- temp = image[i*m_nWidth+(m_LineMapCur->startpoint)-1];
- for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
- {
- j1 = j-1;
- j2 = j+1;
- avepoint = (2*image[i*m_nWidth+j]+temp+image[i*m_nWidth+j2])/4;
- avesurrond = (2*image[i1*m_nWidth+j]+image[i1*m_nWidth+j1]+image[i1*m_nWidth+j2])/4;
- temp = image[i*m_nWidth+j];
- image[i*m_nWidth+j] = static_cast<unsigned short>(image[i*m_nWidth+j]*avesurrond/avepoint);
- }
- }
- else
- {
- temp_linemap = new LINE_MAP;
- temp_linemap->nextline = left_linemap;
- temp_linemap->line_entries = m_LineMapCur->line_entries;
- temp_linemap->lLine = false;
- temp_linemap->rLine = false;
- temp_linemap->startpoint = m_LineMapCur->startpoint;
- temp_linemap->endpoint = m_LineMapCur->endpoint;
- left_linemap = temp_linemap;
- }
- temp_linemap = m_LineMapCur;
- m_LineMapCur = m_LineMapCur->nextline;
- delete temp_linemap;
- }
- }
- //first cycle
- m_LineMapCur = m_yLineMap;
- while(m_LineMapCur)
- {
- if((m_LineMapCur->lLine==false)&&(m_LineMapCur->rLine==false))
- {
- //line correction
- i = m_LineMapCur->line_entries;
- i1 = i-1;
- i2 = i+1;
- temp = image[((m_LineMapCur->startpoint)-1)*m_nWidth+i];
- for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
- {
- j1 = j-1;
- j2 = j+1;
- avepoint = (2*image[j*m_nWidth+i]+temp+image[j2*m_nWidth+i])/4;
- 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;
- temp = image[j*m_nWidth+i];
- image[j*m_nWidth+i] = static_cast<unsigned short>(image[j*m_nWidth+i]*avesurrond/avepoint);
- }
- }
- else if((m_LineMapCur->lLine==true)&&(m_LineMapCur->rLine==false))
- {
- //line correction
- i = m_LineMapCur->line_entries;
- // i1 = i-1;
- i2 = i+1;
- temp = image[((m_LineMapCur->startpoint)-1)*m_nWidth+i];
- for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
- {
- j1 = j-1;
- j2 = j+1;
- avepoint = (2*image[j*m_nWidth+i]+temp+image[j2*m_nWidth+i])/4;
- avesurrond = (2*image[j*m_nWidth+i2]+image[j1*m_nWidth+i2]+image[j2*m_nWidth+i2])/4;
- temp = image[j*m_nWidth+i];
- image[j*m_nWidth+i] = static_cast<unsigned short>(image[j*m_nWidth+i]*avesurrond/avepoint);
- }
- }
- else if((m_LineMapCur->lLine==false)&&(m_LineMapCur->rLine==true))
- {
- //line correction
- i = m_LineMapCur->line_entries;
- i1 = i-1;
- // i2 = i+1;
- temp = image[((m_LineMapCur->startpoint)-1)*m_nWidth+i];
- for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
- {
- j1 = j-1;
- j2 = j+1;
- avepoint = (2*image[j*m_nWidth+i]+temp+image[j2*m_nWidth+i])/4;
- avesurrond = (2*image[j*m_nWidth+i1]+image[j1*m_nWidth+i1]+image[j2*m_nWidth+i1])/4;
- temp = image[j*m_nWidth+i];
- image[j*m_nWidth+i] = static_cast<unsigned short>(image[j*m_nWidth+i]*avesurrond/avepoint);
- }
- }
- else
- {
- //skip, next cycle to correction
- temp_linemap = new LINE_MAP;
- temp_linemap->nextline = left_linemap;
- temp_linemap->line_entries = m_LineMapCur->line_entries;
- temp_linemap->lLine = false;
- temp_linemap->rLine = false;
- temp_linemap->startpoint = m_LineMapCur->startpoint;
- temp_linemap->endpoint = m_LineMapCur->endpoint;
- left_linemap = temp_linemap;
- }
- m_LineMapCur = m_LineMapCur->nextline;
- }
- while(left_linemap)
- {
- m_LineMapCur = left_linemap;
- left_linemap = NULL;
- MarkAdjacentLine(m_LineMapCur);
- while(m_LineMapCur)
- {
- if((m_LineMapCur->lLine==false)&&(m_LineMapCur->rLine==false))
- {
- i = m_LineMapCur->line_entries;
- i1 = i-1;
- i2 = i+1;
- temp = image[((m_LineMapCur->startpoint)-1)*m_nWidth+i];
- for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
- {
- j1 = j-1;
- j2 = j+1;
- avepoint = (2*image[j*m_nWidth+i]+temp+image[j2*m_nWidth+i])/4;
- 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;
- temp = image[j*m_nWidth+i];
- image[j*m_nWidth+i] = static_cast<unsigned short>(image[j*m_nWidth+i]*avesurrond/avepoint);
- }
- }
- else if((m_LineMapCur->lLine==true)&&(m_LineMapCur->rLine==false))
- {
- i = m_LineMapCur->line_entries;
- // i1 = i-1;
- i2 = i+1;
- temp = image[((m_LineMapCur->startpoint)-1)*m_nWidth+i];
- for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
- {
- j1 = j-1;
- j2 = j+1;
- avepoint = (2*image[j*m_nWidth+i]+temp+image[j2*m_nWidth+i])/4;
- avesurrond = (2*image[j*m_nWidth+i2]+image[j1*m_nWidth+i2]+image[j2*m_nWidth+i2])/4;
- temp = image[j*m_nWidth+i];
- image[j*m_nWidth+i] = static_cast<unsigned short>(image[j*m_nWidth+i]*avesurrond/avepoint);
- }
- }
- else if((m_LineMapCur->lLine==false)&&(m_LineMapCur->rLine==true))
- {
- i = m_LineMapCur->line_entries;
- i1 = i-1;
- // i2 = i+1;
- temp = image[((m_LineMapCur->startpoint)-1)*m_nWidth+i];
- for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
- {
- j1 = j-1;
- j2 = j+1;
- avepoint = (2*image[j*m_nWidth+i]+temp+image[j2*m_nWidth+i])/4;
- avesurrond = (2*image[j*m_nWidth+i1]+image[j1*m_nWidth+i1]+image[j2*m_nWidth+i1])/4;
- temp = image[j*m_nWidth+i];
- image[j*m_nWidth+i] = static_cast<unsigned short>(image[j*m_nWidth+i]*avesurrond/avepoint);
- }
- }
- else
- {
- temp_linemap = new LINE_MAP;
- temp_linemap->nextline = left_linemap;
- temp_linemap->line_entries = m_LineMapCur->line_entries;
- temp_linemap->lLine = false;
- temp_linemap->rLine = false;
- temp_linemap->startpoint = m_LineMapCur->startpoint;
- temp_linemap->endpoint = m_LineMapCur->endpoint;
- left_linemap = temp_linemap;
- }
- temp_linemap = m_LineMapCur;
- m_LineMapCur = m_LineMapCur->nextline;
- delete temp_linemap;
- }
- }
- }
- bool CPixMatrix::FreeLineMap(LINE_MAP *m_linemap)
- {
- LINE_MAP *temp_linemap;
- while(m_linemap)
- {
- temp_linemap = m_linemap;
- m_linemap = m_linemap->nextline;
- delete temp_linemap;
- }
- m_linemap = NULL;
- return true;
- }
- //开始自动坏点校正
- void CPixMatrix::BeginAutoBadPixels()
- {
- //if(m_lastImg)
- //{
- // delete [] m_lastImg;
- // m_lastImg = NULL;
- //}
- if(m_mapImg)
- {
- delete [] m_mapImg;
- m_mapImg = NULL;
- }
- //m_lastImg = new unsigned short[m_nWidth*m_nHeight];
- m_mapImg = new unsigned short[m_nWidth*m_nHeight];
- ASSERT(m_mapImg);
- std::memset(m_mapImg, 0, m_nWidth * m_nHeight * sizeof(unsigned short));
- m_curAvg = -1;
- m_lastAvg = -1;
- }
- void CPixMatrix::AddImageforBadPixels(unsigned short *wImage)
- {
- if (wImage == NULL)
- {
- return;
- }
- //m_curImg = wImage;////////////////////////chenGN 2013.01.31
-
- int ntempHOffset = std::max(0, m_nHOffset);
- int ntempWOffset = std::max(0, m_nWOffset);
- bool bFlag = DetBadPxlByMF(wImage, m_nWidth, m_nHeight, ntempWOffset, ntempHOffset, m_mapImg);////////////////////////chenGN 2013.01.31
- if (!bFlag)
- {
- delete []m_mapImg;
- return;
- }
- //old code delete 20100906
- //calculate avg pv
- /*Dunsigned short dwSum = 0;
- Dunsigned short dwIndex = 0;
- int i, j;
- Dunsigned short dwCts = 0;
- for(i = ntempHOffset; i < m_nHeight-ntempHOffset; i += 8) {
- dwIndex = i*m_nWidth;
- for(j = ntempWOffset; j < m_nWidth-ntempWOffset; j+= 8) {
- dwSum += (*(m_curImg + dwIndex + j));
- dwCts++;
- }
- }
- m_curAvg = dwSum/dwCts;
- //find badpixel by thresdhold;
- int thresdhold1, thresdhold2;
- thresdhold1 = m_curAvg*0.6;
- thresdhold2 = (m_curAvg*1.4>65535?65535:m_curAvg*1.4);
- for(i = ntempHOffset; i < m_nHeight-ntempHOffset; i ++)
- {
- dwIndex = i*m_nWidth;
- for(j = ntempWOffset; j < m_nWidth-ntempWOffset; j++)
- {
- if(( *(m_curImg + dwIndex + j) < thresdhold1)||( *(m_curImg + dwIndex + j) > thresdhold2))
- {
- *(m_mapImg + dwIndex + j) = 0xFFFF;
- }
- }
- }
- AutoBadPointDec(m_curImg,m_nWidth, m_nHeight ,ntempHOffset, ntempWOffset,m_mapImg);
- //check status;
- if(m_lastAvg<0)
- {
- memcpy(m_lastImg, m_curImg, m_nHeight*m_nWidth*sizeof(unsigned short));
- m_lastAvg = m_curAvg;
- m_curAvg = -1;
- //no last Image;
- return;
- }
- else if(abs(m_curAvg - m_lastAvg)<500)
- {
- //not too many difference;
- return;
- }
- else
- {
- double thresdhold1 = (((double) m_curAvg) / m_lastAvg)*0.9;
- double thresdhold2 = (((double) m_curAvg) / m_lastAvg)*1.1;
- double temp=0.0;
- for(i = ntempHOffset; i < m_nHeight-ntempHOffset; i ++)
- {
- dwIndex = i*m_nWidth;
- for(j = ntempWOffset; j < m_nWidth-ntempWOffset; j++)
- {
- if (*(m_lastImg + dwIndex + j)==0)
- continue;
- temp=((double)*(m_curImg + dwIndex + j))/(*(m_lastImg + dwIndex + j));
- if((temp < thresdhold1)||(temp>thresdhold2))
- {
- *(m_mapImg + dwIndex + j) = 0xFFFF;
- }
- }
- }
- memcpy(m_lastImg, m_curImg, m_nHeight*m_nWidth*sizeof(unsigned short));
- m_lastAvg = m_curAvg;
- m_curAvg = -1;
- }
- */
- }
- //结束自动换点校正,-1,放弃;0,替换原有map;1,合并原有map
- void CPixMatrix::EndAutoBadPixels(int nMode)
- {
- bool bReturn = true;
- if(nMode == 0)
- {
- //0,替换原有map
- FreeBadPixelMap();
- bReturn = AutoBadPixelMap2(m_mapImg);
- if (bReturn)
- {
- SaveBadPixelMap(m_charFilename);
- }
-
- }
- else if(nMode == 1)
- {
- //1,合并原有map
- bReturn = AutoBadPixelMap2(m_mapImg);
- if (bReturn)
- {
- SaveBadPixelMap(m_charFilename);
- }
- }
- else
- {
- //-1,放弃
- }
- //if(m_lastImg)////////////////////////chenGN 2013.01.31
- //{
- // delete [] m_lastImg;
- // m_lastImg = NULL;
- //}
- if(m_mapImg)
- {
- delete [] m_mapImg;
- m_mapImg = NULL;
- }
- m_curAvg = -1;
- m_lastAvg = -1;
- }
- //add by wangyb 2013 for replace the previous function EndAutoBadPixels()
- //the purpose is to carry on the same process with Gain Calibration;
- //结束自动换点校正,-1,放弃;0,替换原有map;1,合并原有map
- void CPixMatrix::StoreBadPixels(const char * charFilename,int nMode)
- {
- bool bReturn = true;
- if(nMode == 0)
- {
- //0,替换原有map
- FreeBadPixelMap();
- bReturn = AutoBadPixelMap2(m_mapImg);
- if (bReturn)
- {
- SaveBadPixelMap(charFilename);
- }
- }
- else if(nMode == 1)
- {
- //1,合并原有map
- bReturn = AutoBadPixelMap2(m_mapImg);
- if (bReturn)
- {
- SaveBadPixelMap(charFilename);
- }
- }
- else
- {
- //-1,放弃
- }
- //if(m_lastImg)////////////////////////chenGN 2013.01.31
- //{
- // delete [] m_lastImg;
- // m_lastImg = NULL;
- //}
- if(m_mapImg)
- {
- delete [] m_mapImg;
- m_mapImg = NULL;
- }
- m_curAvg = -1;
- m_lastAvg = -1;
- }
- void CPixMatrix::CorrectButCross(unsigned short* image)
- {
- }
- bool CPixMatrix::AutoBadPointDec(unsigned short* lpDetail,int lWidth, int lHeight ,int Xoffset, int Yoffset,unsigned short* pmap)
- {
- unsigned short* lpNewDataBits =lpDetail;
- // new unsigned short[lWidth*lHeight];
- // memcpy(lpNewDataBits,lpDetail,sizeof(unsigned short)*lWidth*lHeight);
- int sort1[4];//big
- int sort2[4];//small
- int sort3[3];//big
- int sort4[3];//small
- double Neighbor_sum=0;
- double Neighbor_avg=0;
-
- long i,j; //for loop
- int temp;
- int moveoff;
- int locallength=50; //区域直方图长度
- int start_avg;
- start_avg=GetAvg(lpNewDataBits,lWidth,lHeight,Xoffset ,Yoffset,locallength);
- //
- int last_avg=start_avg;
- for(i = Xoffset; i<lHeight-Xoffset; i++)
- {
- // if (i+locallength+Xoffset>=lHeight)
- // moveoff=lHeight-Xoffset-locallength;
- // else
- // moveoff=i;
-
- // start_avg=GetAvg(lpNewDataBits,lWidth,lHeight,moveoff,Yoffset,locallength);
-
- start_avg=last_avg;
- for(j = Yoffset; j<lWidth-Yoffset; j++)
- {
- Neighbor_sum=0.0;
- Neighbor_avg=0.0;
- if(lpNewDataBits[(i-1)*lWidth+j-1]>lpNewDataBits[(i+1)*lWidth+j+1])
- {
- sort1[0] = lpNewDataBits[(i-1)*lWidth+j-1];
- sort2[0] = lpNewDataBits[(i+1)*lWidth+j+1];
- }
- else
- {
- sort1[0] = lpNewDataBits[(i+1)*lWidth+j+1];
- sort2[0] = lpNewDataBits[(i-1)*lWidth+j-1];
- }
- if(lpNewDataBits[(i-1)*lWidth+j]>lpNewDataBits[(i+1)*lWidth+j])
- {
- sort1[1] = lpNewDataBits[(i-1)*lWidth+j];
- sort2[1] = lpNewDataBits[(i+1)*lWidth+j];
- }
- else
- {
- sort1[1] = lpNewDataBits[(i+1)*lWidth+j];
- sort2[1] = lpNewDataBits[(i-1)*lWidth+j];
- }
- if(lpNewDataBits[(i-1)*lWidth+j+1]>lpNewDataBits[(i+1)*lWidth+j-1])
- {
- sort1[2] = lpNewDataBits[(i-1)*lWidth+j+1];
- sort2[2] = lpNewDataBits[(i+1)*lWidth+j-1];
- }
- else
- {
- sort1[2] = lpNewDataBits[(i+1)*lWidth+j-1];
- sort2[2] = lpNewDataBits[(i-1)*lWidth+j+1];
- }
- if(lpNewDataBits[i*lWidth+j-1]>lpNewDataBits[i*lWidth+j+1])
- {
- sort1[3] = lpNewDataBits[i*lWidth+j-1];
- sort2[3] = lpNewDataBits[i*lWidth+j+1];
- }
- else
- {
- sort1[3] = lpNewDataBits[i*lWidth+j+1];
- sort2[3] = lpNewDataBits[i*lWidth+j-1];
- }
- //delete the max of the sort1[4];
- if (sort1[0]>sort1[2])
- {
- temp=sort1[2];
- sort1[2]=sort1[0];
- sort1[0]=temp;
- }
- if (sort1[1]>sort1[3])
- {
- temp=sort1[3];
- sort1[3]=sort1[1];
- sort1[1]=temp;
- }
- if (sort1[2]>sort1[3])
- {
- sort1[2]=sort1[3];
- }
- if (sort2[0]<sort2[2])
- {
- temp=sort2[2];
- sort2[2]=sort2[0];
- sort2[0]=temp;
- }
- if (sort2[1]<sort2[3])
- {
- temp=sort2[3];
- sort2[3]=sort2[1];
- sort2[1]=temp;
- }
- if (sort2[2]<sort2[3])
- {
- sort2[2]=sort2[3];
- }
-
- //在剩下的中找最值,重新生成三组值.
- if (sort1[0]>sort2[0])
- {
- sort3[0]=sort1[0];
- sort4[0]=sort2[0];
- }
- else
- {
- sort3[0]=sort2[0];
- sort4[0]=sort1[0];
- }
- if (sort1[1]>sort2[1])
- {
- sort3[1]=sort1[1];
- sort4[1]=sort2[1];
- }
- else
- {
- sort3[1]=sort2[1];
- sort4[1]=sort1[1];
- }
- if (sort1[2]>sort2[2])
- {
- sort3[2]=sort1[2];
- sort4[2]=sort2[2];
- }
- else
- {
- sort3[2]=sort2[2];
- sort4[2]=sort1[2];
- }
-
- //将这六个值相加,后减去一个最大值和最小值.
-
- Neighbor_sum=sort3[0]+sort3[1]+sort3[2]+sort4[0]+sort4[1]+sort4[2];
- //剩余三个点计算均值
- if (sort3[0]>sort3[1])
- {
- if (sort3[0]>sort3[2])
- Neighbor_sum= Neighbor_sum-sort3[0];
- else
- Neighbor_sum= Neighbor_sum-sort3[2];
- }
- else
- {
- if (sort3[1]>sort3[2])
- Neighbor_sum= Neighbor_sum-sort3[1];
- else
- Neighbor_sum= Neighbor_sum-sort3[2];
- }
- //找最小值
- if (sort4[0]<sort4[1])
- {
- if (sort4[0]<sort4[2])
- Neighbor_sum= Neighbor_sum-sort4[0];
- else
- Neighbor_sum= Neighbor_sum-sort4[2];
- }
- else
- {
- if (sort4[1]<sort4[2])
- Neighbor_sum= Neighbor_sum-sort4[1];
- else
- Neighbor_sum= Neighbor_sum-sort4[2];
- }
- Neighbor_avg=Neighbor_sum/4.0;
- if ((Neighbor_avg<start_avg*1.3)&&(Neighbor_avg>start_avg*0.8))
- {
- start_avg=Neighbor_avg;
- }
- else
- {
- int tempccc = 0;
- }
- if((*(lpNewDataBits+i*lWidth+j)>start_avg*1.2)||(*(lpNewDataBits+i*lWidth+j)<start_avg*0.8))
- {
- *(pmap+i*lWidth+j)=0xFFFF;
- }
- if (j==Yoffset)
- {
- last_avg=start_avg;
- }
-
- }
- }
- return true;
- }
- int CPixMatrix::GetAvg(unsigned short* pData,int Width, int Height ,int Xoffset, int Yoffset, int length)
- {
- int i,j;
- int index;
- int *hist = new int[PIXEL_MAX_VALUE];
- for (i=0;i<65535;++i)
- hist[i]=0;
- double sum=0.0;
- double sumsum=0.0;
- int avg=0;
- double rate;
- int count=0;
- for (i=Xoffset; i<Xoffset+length;++i)
- {
- index=i*Width;
- for (j=Yoffset ; j<Yoffset+length;++j)
- {
- ++hist[*(pData+index+j)];
- }
- }
- for (i=0;i<65535;++i)
- {
- sum+=hist[i];
- rate=sum/(length*length);
- if ((rate>0.2)&&(rate<0.8))
- {
- count+=hist[i];
- sumsum+=hist[i]*i;
- }
- }
- avg=sumsum/count;
- delete []hist;
- return avg;
- }
- //code begin 20100906
- /************************************************************************
- FUNCTION NAME: Mean7
- DESCRIPTION: Filter raw image by 7 * 7 mean filter
- RETURN VALUE:
- PARA: [IN\OUT] pImgData: raw image data
- [IN] nWidth: raw image width
- [IN] nHeight: raw image height
- [IN] nXOffset: raw image's offset in x direction
- [IN] nYOffset: raw image's offset in y direction
- HISTORY: Aug\24\2010 written by Alex Stocks
- ************************************************************************/
- /*
- int CPixMatrix::Mean7(ushort_t *pImgData, int nWidth, int nHeight, int nXOffset, int nYOffset)
- {
- if (NULL == pImgData || nWidth <= 2 * nXOffset + 7 || nXOffset < 0
- || nHeight <= 2 * nYOffset + 7 || nYOffset < 0)
- {
- return -1;
- }
- HGLOBAL hImgBuffer = ::GlobalAlloc(GHND, nHeight * nWidth * sizeof(ushort_t));
- if (NULL == hImgBuffer)
- {
- ::GlobalUnlock(hImgBuffer);
- return -1;
- }
- ushort_t* pImgBuffer = (ushort_t*) ::GlobalLock(hImgBuffer);
- int nMinXIdx = nXOffset;
- int nMaxXIdx = nWidth - nXOffset;
- int nMinYIdx = nYOffset;
- int nMaxYIdx = nHeight - nYOffset;
- int nIdxI = 0;
- int nIdxJ = 0;
- int nIndex = 0;
- ushort_t* pBuffer = NULL;
- ushort_t* pData = NULL;
- for (nIdxI = nMinYIdx; nIdxI < nMaxYIdx; ++nIdxI)
- {
- nIndex = nIdxI * nWidth + nMinXIdx;
- pBuffer = pImgBuffer + nIndex;
- pData = pImgData + nIndex;
- *(pBuffer) = (*(pData) + 2 * (*(pData + 1)) + 2 * (*(pData + 2)) + 2 * (*(pData + 3))) / 7;
- *(pBuffer + 1) = (*(pData) + 2 * (*(pData + 1)) + 2 * (*(pData + 2)) + *(pData + 3) + *(pData + 4)) / 7;
- *(pBuffer + 2) = (*(pData) + 2 *(*(pData + 1)) + *(pData + 2) + *(pData + 3) + *(pData + 4) + *(pData + 5)) / 7;
- nIndex -= nMinXIdx;
- for (nIdxJ = nMinXIdx + 3; nIdxJ < nMaxXIdx - 3; ++nIdxJ)
- {
- *(pBuffer + nIdxJ) = (*(pData + nIdxJ - 3) + *(pData + nIdxJ - 2)
- + *(pData + nIdxJ - 1) + *(pData + nIdxJ)
- + *(pData + nIdxJ + 1) + *(pData + nIdxJ + 2)
- + *(pData + nIdxJ + 3)) / 7;
- }
- nIndex += nMaxXIdx;
- pBuffer = pImgBuffer + nIndex;
- pData = pImgData + nIndex;
- *(pBuffer - 3) = (*(pData - 6) + *(pData - 5) + *(pData - 4)
- + *(pData - 3) + 2 * (*(pData - 2)) + *(pData - 1)) / 7;
- *(pBuffer - 2) = (*(pData -5) + *(pData -4) + 2 * (*(pData -3))
- + 2 * (*(pData -2)) + *(pData -1)) / 7;
- *(pBuffer - 1) = (2 * (*(pData - 4)) + 2 * (*(pData - 3))
- + 2 * (*(pData - 2)) + *(pData - 1) ) / 7;
- }
- int nInc1 = nWidth * 1;
- int nInc2 = nWidth * 2;
- int nInc3 = nWidth * 3;
- int nInc4 = nWidth * 4;
- int nInc5 = nWidth * 5;
- int nInc6 = nWidth * 6;
- int nGVSum = 0;
- int nStartYPos = 0;
- int nEndYPos = 0;
- for (nIdxI = nMinXIdx; nIdxI < nMaxXIdx; ++nIdxI)
- {
- nIndex = nMinYIdx * nWidth + nIdxI;
- pBuffer = pImgBuffer + nIndex;
- pData = pImgData + nIndex;
- *(pBuffer) = (*(pData) + 2 * (*(pData + nInc1)) + 2 * (*(pData + nInc2)) + 2 * (*(pData + nInc3))) / 7;
- *(pBuffer + nInc1) = (*(pData) + 2 * (*(pData + nInc1)) + 2 * (*(pData + nInc2)) + *(pData + nInc3) + *(pData + nInc4)) / 7;
- *(pBuffer + nInc2) = (*(pData) + 2 *(*(pData + nInc1)) + *(pData + nInc2) + *(pData + nInc3) + *(pData + nInc4) + *(pData + nInc5)) / 7;
- nIndex -= nMinYIdx * nWidth;
- nGVSum = *(pData) + *(pData + nInc1) + *(pData + nInc2) + *(pData + nInc3) + *(pData + nInc4) + *(pData + nInc5);
- nStartYPos = (nMinYIdx + 3) * nWidth + nIdxI;
- nEndYPos = (nMaxYIdx - 3) * nWidth + nIdxI;
- pBuffer = pImgBuffer;
- pData = pImgData;
- for (nIdxJ = nStartYPos; nIdxJ < nEndYPos; nIdxJ += nWidth)
- {
- nGVSum += *(pData + nIdxJ + nInc3);
- *(pBuffer + nIdxJ) = nGVSum / 7;
- nGVSum -= *(pBuffer + nIdxJ - nInc3);
- }
- nIndex = nMaxYIdx * nWidth + nIdxI;
- pBuffer = pImgBuffer + nIndex;
- pData = pImgData + nIndex;
- *(pBuffer - nInc3) = (*(pData - nInc6) + *(pData - nInc5) + *(pData - nInc4) + *(pData - nInc3) + 2 * (*(pData - nInc2)) + *(pData - nInc1)) / 7;
- *(pBuffer - nInc2) = (*(pData - nInc5) + *(pData - nInc4) + 2 * (*(pData - nInc3)) + 2 * (*(pData - nInc2)) + *(pData - nInc1)) / 7;
- *(pBuffer - nInc1) = (2 * (*(pData - nInc4)) + 2 * (*(pData - nInc3)) + 2 * (*(pData - nInc2)) + *(pData - nInc1) ) / 7;
- }
- memcpy(pImgData, pImgBuffer, nHeight * nWidth * sizeof(ushort_t));
- ::GlobalUnlock(hImgBuffer);
- ::GlobalFree(hImgBuffer);
- return 1;
- }
- */
- int CPixMatrix::Mean7(ushort_t *pImgData, int nWidth, int nHeight, int nXOffset, int nYOffset)
- {
- if (NULL == pImgData || nWidth <= 2 * nXOffset + 7 || nXOffset < 0
- || nHeight <= 2 * nYOffset + 7 || nYOffset < 0)
- {
- return -1;
- }
- ushort_t* pImgBuffer = nullptr;
- try {
- pImgBuffer = new ushort_t[nHeight * nWidth];
- }
- catch (const std::bad_alloc& e) {
- // 内存分配失败处理
- return -1;
- }
- int nMinXIdx = nXOffset;
- int nMaxXIdx = nWidth - nXOffset;
- int nMinYIdx = nYOffset;
- int nMaxYIdx = nHeight - nYOffset;
- int nIdxI = 0;
- int nIdxJ = 0;
- int nIndex = 0;
- ushort_t* pBuffer = NULL;
- ushort_t* pData = NULL;
- for (nIdxI = nMinYIdx; nIdxI < nMaxYIdx; ++nIdxI)
- {
- nIndex = nIdxI * nWidth + nMinXIdx;
- pBuffer = pImgBuffer + nIndex;
- pData = pImgData + nIndex;
- *(pBuffer) = (*(pData) + 2 * (*(pData + 1)) + 2 * (*(pData + 2)) + 2 * (*(pData + 3))) / 7;
- *(pBuffer + 1) = (*(pData) + 2 * (*(pData + 1)) + 2 * (*(pData + 2)) + *(pData + 3) + *(pData + 4)) / 7;
- *(pBuffer + 2) = (*(pData) + 2 *(*(pData + 1)) + *(pData + 2) + *(pData + 3) + *(pData + 4) + *(pData + 5)) / 7;
- nIndex -= nMinXIdx;
- pBuffer = pImgBuffer + nIndex;
- pData = pImgData + nIndex;
- for (nIdxJ = nMinXIdx + 3; nIdxJ < nMaxXIdx - 3; ++nIdxJ)
- {
- *(pBuffer + nIdxJ) = (*(pData + nIdxJ - 3) + *(pData + nIdxJ - 2)
- + *(pData + nIdxJ - 1) + *(pData + nIdxJ)
- + *(pData + nIdxJ + 1) + *(pData + nIdxJ + 2)
- + *(pData + nIdxJ + 3)) / 7;
- }
- nIndex += nMaxXIdx;
- pBuffer = pImgBuffer + nIndex;
- pData = pImgData + nIndex;
- *(pBuffer - 3) = (*(pData - 6) + *(pData - 5) + *(pData - 4)
- + *(pData - 3) + 2 * (*(pData - 2)) + *(pData - 1)) / 7;
- *(pBuffer - 2) = (*(pData -5) + *(pData -4) + 2 * (*(pData -3))
- + 2 * (*(pData -2)) + *(pData -1)) / 7;
- *(pBuffer - 1) = (2 * (*(pData - 4)) + 2 * (*(pData - 3))
- + 2 * (*(pData - 2)) + *(pData - 1) ) / 7;
- }
- memcpy(pImgData, pImgBuffer, nHeight * nWidth * sizeof(ushort_t));
- int nInc1 = nWidth * 1;
- int nInc2 = nWidth * 2;
- int nInc3 = nWidth * 3;
- int nInc4 = nWidth * 4;
- int nInc5 = nWidth * 5;
- int nInc6 = nWidth * 6;
- int nGVSum = 0;
- int nStartYPos = 0;
- int nEndYPos = 0;
- for (nIdxI = nMinXIdx; nIdxI < nMaxXIdx; ++nIdxI)
- {
- nIndex = nMinYIdx * nWidth + nIdxI;
- pBuffer = pImgBuffer + nIndex;
- pData = pImgData + nIndex;
- *(pBuffer) = (*(pData) + 2 * (*(pData + nInc1)) + 2 * (*(pData + nInc2)) + 2 * (*(pData + nInc3))) / 7;
- *(pBuffer + nInc1) = (*(pData) + 2 * (*(pData + nInc1)) + 2 * (*(pData + nInc2)) + *(pData + nInc3) + *(pData + nInc4)) / 7;
- *(pBuffer + nInc2) = (*(pData) + 2 *(*(pData + nInc1)) + *(pData + nInc2) + *(pData + nInc3) + *(pData + nInc4) + *(pData + nInc5)) / 7;
- nIndex -= nMinYIdx * nWidth;
- nGVSum = *(pData) + *(pData + nInc1) + *(pData + nInc2) + *(pData + nInc3) + *(pData + nInc4) + *(pData + nInc5);
- nStartYPos = (nMinYIdx + 3) * nWidth + nIdxI;
- nEndYPos = (nMaxYIdx - 3) * nWidth + nIdxI;
- pBuffer = pImgBuffer;
- pData = pImgData;
- for (nIdxJ = nStartYPos; nIdxJ < nEndYPos; nIdxJ += nWidth)
- {
- nGVSum += *(pData + nIdxJ + nInc3);
- *(pBuffer + nIdxJ) = nGVSum / 7;
- nGVSum -= *(pBuffer + nIdxJ - nInc3);
- }
- nIndex = nMaxYIdx * nWidth + nIdxI;
- pBuffer = pImgBuffer + nIndex;
- pData = pImgData + nIndex;
- *(pBuffer - nInc3) = (*(pData - nInc6) + *(pData - nInc5) + *(pData - nInc4) + *(pData - nInc3) + 2 * (*(pData - nInc2)) + *(pData - nInc1)) / 7;
- *(pBuffer - nInc2) = (*(pData - nInc5) + *(pData - nInc4) + 2 * (*(pData - nInc3)) + 2 * (*(pData - nInc2)) + *(pData - nInc1)) / 7;
- *(pBuffer - nInc1) = (2 * (*(pData - nInc4)) + 2 * (*(pData - nInc3)) + 2 * (*(pData - nInc2)) + *(pData - nInc1) ) / 7;
- }
- memcpy(pImgData, pImgBuffer, nHeight * nWidth * sizeof(ushort_t));
- // CFile file;
- // CString strDignosisPath = _T("e:\\Varian.raw");
- // file.Open(strDignosisPath, CFile::modeCreate | CFile::modeWrite );
- // file.Write(pImgBuffer, m_nWidth*m_nHeight*sizeof(unsigned short));
- // file.Close();
- delete[] pImgBuffer;
- return 1;
- }
- /************************************************************************
- FUNCTION NAME: CalcGlbAvgPxlValueBySamp
- DESCREPTION: Calculate global average pixel value by sampling method
- RETURN VALUE: Global average pixel value.
- PARA: [IN]pImgData: raw image data array
- [IN]nWidth: raw image's width
- [IN]nHeight: raw image's height
- [IN]nXOffset: raw image's offset in x direction
- [IN]nYOffset: raw image's offset in y direction
- HISTORY: August/2/2010 written by Alex Stocks
- ************************************************************************/
- double CPixMatrix::CalcGlbAvgPxlValueBySamp(unsigned short* pImgData, int nWidth, int nHeight, int nXOffset, int nYOffset)
- {
- if (NULL == pImgData || nWidth < 0 || nHeight < 0 || nXOffset < 0
- || nYOffset < 0 || nWidth <= 2 * nXOffset || nHeight <= 2 * nYOffset)
- {
- return -1.0f;
- }
- int nIdxI = 0;
- int nIdxJ = 0;
- int nIndex = 0;
- int nHist[PIXEL_MAX_VALUE] = { 0 };
- int nMinYIdx = nYOffset;
- int nMaxYIdx = nHeight - nYOffset;
- int nMinXIdx = nXOffset;
- int nMaxXIdx = nWidth - nXOffset;
- unsigned short *pImage = pImgData;
- int nPxlNum = 0;
- for (nIdxI = nMinYIdx; nIdxI < nMaxYIdx; nIdxI += 8)
- {
- pImage = pImgData + nIdxI * nWidth;
- for (nIdxJ = nMinXIdx; nIdxJ < nMaxXIdx; nIdxJ += 8)
- {
- ++nHist[*(pImage+nIdxJ)];
- ++nPxlNum;
- }
- }
- //code delete 20110125
- // double fSum=0.0f;
- // double fGVSum = 0.0f;
- // double fGVAvg=0;
- // double fRatio = 0;
- // int nCount=0;
- // for (nIdxI = 0; nIdxI < 65535; ++nIdxI)
- // {
- // fSum += nHist[nIdxI];
- // fRatio = fSum / nPxlNum;
- // if (0.15f < fRatio && fRatio < 0.85f)
- // {
- // nCount += nHist[nIdxI];
- // fGVSum += nHist[nIdxI] * nIdxI;
- // }
- // }
- // if (nCount <= 0)
- // {
- // return -1.0f;
- // }
- //code begin 20110125
- double fSum=0.0f;
- double fGVSum = 0.0f;
- double fGVAvg=0;
- double fRatio = 0;
- int nCount=0;
- float fTempGVSum = 0.0f;
- for (nIdxI = 0; nIdxI < 65535; ++nIdxI)
- {
- fSum += nHist[nIdxI];
- fRatio = fSum / nPxlNum;
- fTempGVSum += nHist[nIdxI] * nIdxI;
- if (0.15f < fRatio && fRatio < 0.85f)
- {
- nCount += nHist[nIdxI];
- fGVSum += nHist[nIdxI] * nIdxI;
- }
- }
- if (nCount <= 0)
- {
- nCount = nPxlNum;
- fGVSum = fTempGVSum;
- }
- //code end 20110125
- fGVAvg = fGVSum / nCount;
- return fGVAvg;
- }
- ///////////////////////////////////redesigned DetBadPxlByMF by CGN 2013.01.30
- bool CPixMatrix::DetBadPxlByMF(ushort_t *pImgData, int nWidth, int nHeight, int nXOffset, int nYOffset, ushort_t *pMap)
- {
- if (NULL == pImgData || NULL == pMap
- || nWidth <= 0 || nHeight <= 0
- || nXOffset < 0 || nYOffset < 0
- || nWidth <= 2 * nXOffset || nHeight <= 2 * nYOffset)
- {
- return false;
- }
- ///////////////////////////全图阈值计算,挑出黑白坏点
- float fGlbAvgPxlValue = CalcGlbAvgPxlValueBySamp(pImgData, nWidth, nHeight, nXOffset, nYOffset);
- float fMinThreshold = 0.5f * fGlbAvgPxlValue;
- float fMaxThreshold = 1.5f * fGlbAvgPxlValue;
- if (65535.0f < fMaxThreshold) //可能是导致16位图像坏点过多判断的原因,16383修改为65535,by陈冠男,2012-12-20
- {
- fMaxThreshold = 65535.0f;
- }
- int nMinXIdx = nXOffset;
- int nMaxXIdx = nWidth - nXOffset;
- int nMinYIdx = nYOffset;
- int nMaxYIdx = nHeight - nYOffset;
- int bcount = 0;
- for ( int i = nMinYIdx; i < nMaxYIdx; ++i )
- {
- for ( int j = nMinXIdx; j < nMaxXIdx; ++j )
- {
- if ( pImgData[ i * nWidth + j ] < fMinThreshold || fMaxThreshold < pImgData[ i * nWidth + j ] )
- {
- pMap[ i * nWidth + j ] = 65535;
- bcount++;
- }
- }
- }
- /////////////////////////////////将图像分成4x4区域,在每个区域中用7x7像素模版进行坏点判断
- /*int Heightbegin = nYOffset;
- int Heightend = nYOffset + ( nHeight - 2 * nYOffset ) / 4;
- int Widthbegin = nXOffset;
- int Widthend = nXOffset + ( nWidth - 2 * nXOffset ) / 4;
- int nsum = 0;
- int ncount = 0;
- int navg = 0;
- int nthres = 0;
- for ( int i = 0; i < 4; i++ )
- {
- Widthbegin = nXOffset;
- Widthend = nXOffset + ( nWidth - 2 * nXOffset ) / 4;
- for ( int j = 0; j < 4; j++ )
- {
- for ( int k = Heightbegin + 3; k < Heightend - 3 ; k++ )
- {
- for ( int p = Widthbegin + 3; p < Widthend - 3; p++ )
- {
- ncount = 0;
- nsum = 0;
- navg = 0;
- nthres = 0;
- if ( pMap[ k * nWidth + p ] != 65535 )
- {
- for ( int m = -3; m < 4; m++ )
- {
- for ( int n = -3; n < 4; n++ )
- {
- if ( pMap[ ( k + m ) * nWidth + p + n ] != 65535 )
- {
- nsum += pImgData[ ( k + m ) * nWidth + p + n ];
- ncount++;
- }
- }
- }
- navg = nsum / ncount;
- nthres = navg / 10;
- if ( abs( pImgData[ k * nWidth + p ] - navg ) > nthres )
- {
- pMap[ k * nWidth + p ] = 65535;
- bcount++;
- }
-
- }
- }
- }
- Widthbegin += ( nWidth - 2 * nXOffset ) / 4;
- Widthend += ( nWidth - 2 * nXOffset ) / 4;
- }
- Heightbegin += ( nHeight - 2 * nYOffset ) / 4;
- Heightend += ( nHeight - 2 * nYOffset ) / 4;
- }*/
- int Heightbegin = nYOffset;
- int Heightend = nHeight - nYOffset;
- int Widthbegin = nXOffset;
- int Widthend = nWidth - nXOffset;
- int nsum = 0;
- int ncount = 0;
- int navg = 0;
- int nthres = 0;
- for ( int k = Heightbegin + 3; k < Heightend - 3 ; k++ )
- {
- for ( int p = Widthbegin + 3; p < Widthend - 3; p++ )
- {
- ncount = 0;
- nsum = 0;
- navg = 0;
- nthres = 0;
- if ( pMap[ k * nWidth + p ] != 65535 )
- {
- for ( int m = -3; m < 4; m++ )
- {
- for ( int n = -3; n < 4; n++ )
- {
- if ( pMap[ ( k + m ) * nWidth + p + n ] != 65535 )
- {
- nsum += pImgData[ ( k + m ) * nWidth + p + n ];
- ncount++;
- }
- }
- }
- navg = nsum / ncount;
- nthres = navg / 10;
- if ( abs( pImgData[ k * nWidth + p ] - navg ) > nthres )
- {
- pMap[ k * nWidth + p ] = 65535;
- bcount++;
- }
- }
- }
- }
- //FILE *f1;
- //f1=fopen("D:\\my study\\program\\grid suppression image\\careray1500p test\\badmap.raw","wb");
- //
- // fwrite( pMap, sizeof(unsigned short), nHeight * nWidth, f1);
- // fclose( f1 );
- return true;
- }
- /************************************************************************
- FUNCTION NAME: DetBadPxlByMF
- DESCRIPTION: detect bad pixels by 7 * 7 mean filter
- RETURN VALUE:
- PARA: [IN] pImageData: primary raw image data array
- [IN] nWidth: raw image width
- [IN] nHeight: raw image height
- [IN] nXOffset: raw image's offset in x direction
- [IN] nYOffset: raw image's offset in y direction
- [IN\OUT] pMap: raw image's bad pixel array
- HISTORY: Aug\23\2010 written by Alex Stocks
- ************************************************************************/
- //bool CPixMatrix::DetBadPxlByMF(ushort_t *pImgData, int nWidth, int nHeight, int nXOffset, int nYOffset, ushort_t *pMap)
- //{
- // if (NULL == pImgData || NULL == pMap
- // || nWidth <= 0 || nHeight <= 0
- // || nXOffset < 0 || nYOffset < 0
- // || nWidth <= 2 * nXOffset || nHeight <= 2 * nYOffset)
- // {
- // return false;
- // }
- //
- // //////////////////////////////////////////////////////////////////////////
- // //step1: detect bad pixels by global threshold
- // //////////////////////////////////////////////////////////////////////////
- // float fGlbAvgPxlValue = CalcGlbAvgPxlValueBySamp(pImgData, nWidth, nHeight, nXOffset, nYOffset);
- // float fMinThreshold = 0.5f * fGlbAvgPxlValue;
- // float fMaxThreshold = 1.5f * fGlbAvgPxlValue;
- // if (65535.0f < fMaxThreshold) //可能是导致16位图像坏点过多判断的原因,16383修改为65535,by陈冠男,2012-12-20
- // {
- // fMaxThreshold = 65535.0f;
- // }
- // int nMinXIdx = nXOffset;
- // int nMaxXIdx = nWidth - nXOffset;
- // int nMinYIdx = nYOffset;
- // int nMaxYIdx = nHeight - nYOffset;
- // int nIdxI = 0;
- // int nIdxJ = 0;
- // int nIndex = 0;
- //
- // HGLOBAL hMdfPic = ::GlobalAlloc(GHND, nHeight * nWidth * sizeof(ushort_t));
- // if (NULL == hMdfPic)
- // {
- // ::GlobalUnlock((HGLOBAL)hMdfPic);
- // return false;
- // }
- // ushort_t *pMdfPic = (ushort_t*) ::GlobalLock((HGLOBAL)hMdfPic);
- // memcpy(pMdfPic, pImgData, nHeight * nWidth * sizeof(ushort_t));
- //
- // //use average pixel value in place of every bad pixel's pixel value
- // for (nIdxI = nMinYIdx; nIdxI < nMaxYIdx; ++nIdxI)
- // {
- // nIndex = nIdxI * nWidth;
- // for (nIdxJ = nMinXIdx; nIdxJ < nMaxXIdx; ++nIdxJ)
- // {
- // if (*(pMap + nIndex + nIdxJ) == 0xFFFF)
- // {
- // *(pMdfPic + nIndex + nIdxJ) = (int)fGlbAvgPxlValue;
- // continue;
- // }
- // if (*(pImgData + nIndex + nIdxJ) < fMinThreshold || fMaxThreshold < *(pImgData + nIndex + nIdxJ))
- // {
- // *(pMap + nIndex + nIdxJ) = 0xFFFF;
- // *(pMdfPic + nIndex + nIdxJ) = (int)fGlbAvgPxlValue;
- // }
- // }
- // }
- //
- // //////////////////////////////////////////////////////////////////////////
- // //step2: detect bad pixels by mean filter
- // //////////////////////////////////////////////////////////////////////////
- // HGLOBAL hImpPic = ::GlobalAlloc(GHND, nHeight * nWidth * sizeof(ushort_t));
- // if (NULL == hImpPic)
- // {
- // ::GlobalUnlock(hImpPic);
- // return false;
- // }
- // ushort_t* pImpPic = (ushort_t*) ::GlobalLock(hImpPic);
- // memcpy(pImpPic, pMdfPic, nHeight * nWidth * sizeof(ushort_t));
- // ::GlobalUnlock(hMdfPic);
- // ::GlobalFree(hMdfPic);
- // int nAns = Mean7(pImpPic, nWidth, nHeight, nXOffset, nYOffset);
- // if (nAns < 0)
- // {
- // ::GlobalUnlock(hImpPic);
- // ::GlobalFree(hImpPic);
- // //::GlobalUnlock(hMdfPic);
- // //::GlobalFree(hMdfPic);
- // return false;
- // }
- // int nDiff = 0;
- // float fAbsDiff = fGlbAvgPxlValue * 0.10f;
- // for (nIdxI = nMinYIdx; nIdxI < nMaxYIdx; ++nIdxI)
- // {
- // nIndex = nIdxI * nWidth;
- // for (nIdxJ = nMinXIdx; nIdxJ < nMaxXIdx; ++nIdxJ)
- // {
- // if (*(pMap + nIndex + nIdxJ) != 0xFFFF)
- // {
- // nDiff = *(pImgData + nIndex + nIdxJ) - *(pImpPic + nIndex + nIdxJ);
- // if (fAbsDiff < abs(nDiff))
- // {
- // *(pMap + nIndex + nIdxJ) = 0xFFFF;
- // }
- // }
- // }
- // }
- //
- // ::GlobalUnlock(hImpPic);
- // ::GlobalFree(hImpPic);
- // //::GlobalUnlock(hMdfPic);
- // //::GlobalFree(hMdfPic);
- // return true;
- //}
- int CPixMatrix::BadGridLineCorrect( unsigned short *pImage)
- {
- long BadPixNum;
- long BadNum;
- memset( m_TempImage, 0, sizeof(unsigned short) * m_nWidth * m_nHeight );
- memcpy( m_TempImage, pImage,sizeof(unsigned short) * m_nWidth * m_nHeight );
- for ( int i = m_nHOffset + 2; i < m_nHeight - m_nHOffset - 2; i++ )
- {
- m_pBadPixNum = m_BadPixelMap[ i ].bad_pixel_num;
- if ( m_BadPixelMap[ i ].num_entries > 0 )
- {
- for ( int j = 0; j < m_BadPixelMap[ i ].num_entries; j++ )
- {
- BadPixNum = *m_pBadPixNum++;
- BadNum = BadPixNum & OFFSET_MASK;
- if ( BadNum > 1 && BadNum < m_nWidth - 1 )
- 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 ] +
- m_TempImage[ ( i ) * m_nWidth + BadNum - 2 ] + m_TempImage[ ( i ) * m_nWidth + BadNum + 2 ] +
- m_TempImage[ ( i + 2 ) * m_nWidth + BadNum - 2 ] + m_TempImage[ ( i + 2 ) * m_nWidth + BadNum ] + m_TempImage[ ( i + 2 ) * m_nWidth + BadNum + 2 ] ) / 8;
- }
- }
- }
- return 1;
- }
|