PixMatrix.cpp 141 KB

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