12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085 |
- // PixMatrix.cpp: implementation of the CPixMatrix class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include <memory.h>
- #include <math.h>
- #include <stdio.h>
- #include "PixMatrix.h"
- #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_curImg = NULL;////////////////////////chenGN 2013.01.31
- //m_lastImg = NULL;////////////////////////chenGN 2013.01.31
- m_mapImg = NULL;
- m_curAvg = -1;
- m_lastAvg = -1;
- ZeroMemory(m_charFilename, 256);
- m_TempImage = new unsigned short [ m_nWidth * m_nHeight ];
- m_TempImage1 = new unsigned short [ m_nWidth * m_nHeight ];
- //nWidth = 0;
- //nHeight = 0;
- m_pBadPixNum = NULL;
- //m_datalen = new int [ 400 ];
- m_nLinePoint = 50;////×?D??μ??μ?êy, zhaoyiru, 2017.05.09
- m_nLineData = int( (MAXI_BADPIX_COUNT - MAX_PIX_MAP_LINE) / m_nLinePoint);//×?′ó?μ????êy, zhaoyiru, 2017.05.09
- m_datalen = new 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(WORD* 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;
- WORD *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 = (int)(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) = (WORD)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) = (WORD)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++)
- {
- 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,"%d,%d:",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,"%d,",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] = (unsigned short) (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] = (unsigned short)(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 = (int)(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 = (int)(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 = (int)(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] = (unsigned short)(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] = (unsigned short)(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] = (unsigned short)(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 = (int)(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 = (int)(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 = (int)(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] = (unsigned short)(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] = (unsigned short)(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] = (unsigned short)(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 = (int)(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 = (int)(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 = (int)(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 = (int)(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 = (int)(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 = (int)(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] = (unsigned short)(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 = (int)(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 = (int)(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 = (int)(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 = (int)(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 = (int)(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 = (int)(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] = (unsigned short)(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(WORD *wImage)
- {
- char * m_charBadPixMap;
- char * m_charLinePixMap;
- m_charLinePixMap = new char [MAX_PIX_MAP_LINE];
- m_charBadPixMap = new char [MAXI_BADPIX_COUNT];
- BYTE *wImageWB = new BYTE [m_nWidth* m_nHeight];
- int i,j; // for loop
- DWORD Hist[16384];
- DWORD count = 0;
- DWORD tempsum = 0;
- WORD lowvalue, highvalue;
- lowvalue = highvalue = 0;
- bool state = true;
- for(i = 0; i< 16384; i++)
- {
- Hist[i] = 0;
- }
- //统计直方图
- for(i = m_nHeight/3; i<m_nHeight*2/3; i++)
- {
- for(j = m_nWidth/3; j<m_nWidth*2/3; j++)
- {
- Hist[*(wImage+i*m_nWidth+j)]++;
- count ++;
- }
- }
- count = count/10;
- for(i = 0 ; i< 16384; i++)
- {
- tempsum += Hist[i];
- if (tempsum> count)
- break;
- }
- lowvalue = max(i-1500, 100);
- tempsum = 0;
- for(i = m_nPixelMax ; i>0; i--)
- {
- tempsum += Hist[i];
- if (tempsum> count)
- break;
- }
- highvalue = min(i+1500, m_nPixelMax);
- for(i = 0; i<m_nHeight; i++)
- {
- for(j = 0; j<m_nWidth; j++)
- {
- if((*(wImage+i*m_nWidth+j)>lowvalue)&&(*(wImage+i*m_nWidth+j)<highvalue))
- {
- *(wImageWB+i*m_nWidth+j) = 255;
- }
- else
- {
- *(wImageWB+i*m_nWidth+j) = 0;
- }
- }
- }
- //查找空边
- int x1,x2,y1,y2;
- x1 = m_nWOffset;
- x2 = m_nWidth-m_nWOffset;
- y1 = m_nHOffset;
- y2 = m_nHeight-m_nHOffset;
- /* for(i = 0; i<m_nHeight; i++)
- {
- if(!((*(wImageWB+m_nWidth*(m_nHeight-i-1)+m_nWidth/3)==0)&&(*(wImageWB+m_nWidth*(m_nHeight-i-1)+2*m_nWidth/3)==0)))
- {
- y1 = i;
- break;
- }
- }
- for(i = m_nHeight-1; i>=0; i--)
- {
- if(!((*(wImageWB+m_nWidth*(m_nHeight-i-1)+m_nWidth/3)==0)&&(*(wImageWB+m_nWidth*(m_nHeight-i-1)+2*m_nWidth/3)==0)))
- {
- y2 = i;
- break;
- }
- }
- for(j = 0; j<m_nWidth; j++)
- {
- if(!((*(wImageWB+m_nWidth*m_nHeight/3+j)==0)&&(*(wImageWB+2*m_nWidth*m_nHeight/3+j)==0)))
- {
- x1 = j;
- break;
- }
- }
- for(j = m_nWidth-1; j>=0; j--)
- {
- if(!((*(wImageWB+m_nWidth*m_nHeight/3+j)==0)&&(*(wImageWB+2*m_nWidth*m_nHeight/3+j)==0)))
- {
- x2 = j;
- break;
- }
- }
- */
- 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(*(wImageWB+m_nWidth*i+j)==0){
- //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,%d:",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+1; i<m_nHeight; i++)
- {
- char_count += sprintf(m_charBadPixMap+char_count,"%d,0:\n",i);
- }
- }
- delete [] wImageWB;
- if(state)
- {
- LoadBadPixelMapChar(m_charBadPixMap);
- CombineBadPixelMap();
- }
- delete [] m_charLinePixMap;
- delete [] m_charBadPixMap;
- return state;
- }
- bool CPixMatrix::AutoBadPixelMap1(WORD *wImage)
- {
- int threshold1,threshold2;
- char * m_charBadPixMap;
- char * m_charLinePixMap;
- m_charLinePixMap = new char [MAX_PIX_MAP_LINE];
- m_charBadPixMap = new char [MAXI_BADPIX_COUNT];
- BYTE *wImageWB = new BYTE [m_nWidth* m_nHeight];
- int temp;
- bool state = true;
- int i, j;
- DWORD dwMean = 0;
- DWORD dwIndex = 0;
- DWORD dwCts = 0;
- for(i = 50; i < m_nHeight-50; i += 8) {
- dwIndex = i*m_nWidth;
- for(j = 50; j < m_nWidth-15; j += 8) {
- dwMean += wImage[dwIndex+j];
- dwCts++;
- }
- }
- dwMean /= dwCts;
- threshold1 = dwMean/3;
- threshold2 = min(dwMean*2,dwMean+(m_nPixelMax-dwMean)/2);
- for(i = 0; i<m_nWidth*m_nHeight; i++)
- {
- temp = wImage[i];// - wDarkImage[i];
- if((temp<threshold1)||(temp>threshold2)) wImageWB[i] = 0;
- else wImageWB[i] = 255;
- }
- //查找空边
- 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(*(wImageWB+m_nWidth*i+j)==0){
- //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,%d:",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+1; i<m_nHeight; i++)
- {
- char_count += sprintf(m_charBadPixMap+char_count,"%d,0:\n",i);
- }
- }
- delete [] wImageWB;
- if(state)
- {
- LoadBadPixelMapChar(m_charBadPixMap,true);
- CombineBadPixelMap();
- }
- delete [] m_charLinePixMap;
- delete [] m_charBadPixMap;
- return state;
- }
- bool CPixMatrix::AutoBadPixelMap2(WORD *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,%d:",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(WORD *wImage, int threshold)
- {
- DWORD Temp,Sum;
- //指向DIB图像中的数据;
- WORD* pDataByte;
- //差值,与阈值比较
- int idelta;
- // int count;
- // WORD 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) = (WORD)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, "%d,%d:%d,%d", 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 = (WORD)*line_entries;
- m_xLineMapCur->startpoint = (WORD)*startpoint;
- m_xLineMapCur->endpoint = (WORD)*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 = (WORD)*line_entries;
- m_yLineMapCur->startpoint = (WORD)*startpoint;
- m_yLineMapCur->endpoint = (WORD)*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(WORD *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::CorrectDXLines(WORD *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(WORD *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;
- }
- }
- }
- 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 = max(2, *startpoint);
- if(m_DLineMapCur->nLineType == 0)
- {
- m_DLineMapCur->line_entries = min(m_nHeight-1, max(1,*line_entries));
- m_DLineMapCur->endpoint = min(m_nWidth-2, *endpoint);
- }
- else
- {
- m_DLineMapCur->line_entries = min(m_nWidth-1, max(1,*line_entries));
- m_DLineMapCur->endpoint = 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;
- WORD 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(WORD* image)
- {
- int i,i1,i2,j,j1,j2;
- DWORD avepoint, avesurrond;
- WORD 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] = WORD(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] = WORD(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] = WORD(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] = WORD(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] = WORD(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] = WORD(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] = WORD(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] = WORD(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] = WORD(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] = WORD(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] = WORD(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] = WORD(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 WORD[m_nWidth*m_nHeight];
- m_mapImg = new WORD[m_nWidth*m_nHeight];
- ZeroMemory(m_mapImg, m_nWidth*m_nHeight*sizeof(WORD));
- m_curAvg = -1;
- m_lastAvg = -1;
- }
- void CPixMatrix::AddImageforBadPixels(WORD *wImage)
- {
- if (wImage == NULL)
- {
- return;
- }
- //m_curImg = wImage;////////////////////////chenGN 2013.01.31
-
- int ntempHOffset = max(0, m_nHOffset);
- int ntempWOffset = 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
- /*DWORD dwSum = 0;
- DWORD dwIndex = 0;
- int i, j;
- DWORD 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(WORD));
- 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(WORD));
- 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
- bool 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;
- return bReturn;
- }
- void CPixMatrix::CorrectButCross(WORD* 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 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=(int)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=(int)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;
- }
- 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;
- 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(WORD));
- // file.Close();
- ::GlobalUnlock(hImgBuffer);
- ::GlobalFree(hImgBuffer);
- 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 = (float)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;
- }
|