1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873 |
- % VIEW_NII: Create or update a 3-View (Front, Top, Side) of the
- % brain data that is specified by nii structure
- %
- % Usage: status = view_nii([h], nii, [option]) or
- % status = view_nii(h, [option])
- %
- % Where, h is the figure on which the 3-View will be plotted;
- % nii is the brain data in NIFTI format;
- % option is a struct that configures the view plotted, can be:
- %
- % option.command = 'init'
- % option.command = 'update'
- % option.command = 'clearnii'
- % option.command = 'updatenii'
- % option.command = 'updateimg' (nii is nii.img here)
- %
- % option.usecolorbar = 0 | [1]
- % option.usepanel = 0 | [1]
- % option.usecrosshair = 0 | [1]
- % option.usestretch = 0 | [1]
- % option.useimagesc = 0 | [1]
- % option.useinterp = [0] | 1
- %
- % option.setarea = [x y w h] | [0.05 0.05 0.9 0.9]
- % option.setunit = ['vox'] | 'mm'
- % option.setviewpoint = [x y z] | [origin]
- % option.setscanid = [t] | [1]
- % option.setcrosshaircolor = [r g b] | [1 0 0]
- % option.setcolorindex = From 1 to 9 (default is 2 or 3)
- % option.setcolormap = (Mx3 matrix, 0 <= val <= 1)
- % option.setcolorlevel = No more than 256 (default 256)
- % option.sethighcolor = []
- % option.setcbarminmax = []
- % option.setvalue = []
- % option.glblocminmax = []
- % option.setbuttondown = ''
- % option.setcomplex = [0] | 1 | 2
- %
- % Options description in detail:
- % ==============================
- %
- % 1. command: A char string that can control program.
- %
- % init: If option.command='init', the program will display
- % a 3-View plot on the figure specified by figure h
- % or on a new figure. If there is already a 3-View
- % plot on the figure, please use option.command =
- % 'updatenii' (see detail below); otherwise, the
- % new 3-View plot will superimpose on the old one.
- % If there is no option provided, the program will
- % assume that this is an initial plot. If the figure
- % handle is omitted, the program knows that it is
- % an initial plot.
- %
- % update: If there is no command specified, and a figure
- % handle of the existing 3-View plot is provided,
- % the program will choose option.command='update'
- % to update the 3-View plot with some new option
- % items.
- %
- % clearnii: Clear 3-View plot on specific figure
- %
- % updatenii: If a new nii is going to be loaded on a fig
- % that has already 3-View plot on it, use this
- % command to clear existing 3-View plot, and then
- % display with new nii. So, the new nii will not
- % superimpose on the existing one. All options
- % for 'init' can be used for 'updatenii'.
- %
- % updateimg: If a new 3D matrix with the same dimension
- % is going to be loaded, option.command='updateimg'
- % can be used as a light-weighted 'updatenii, since
- % it only updates the 3 slices with new values.
- % inputing argument nii should be a 3D matrix
- % (nii.img) instead of nii struct. No other option
- % should be used together with 'updateimg' to keep
- % this command as simple as possible.
- %
- %
- % 2. usecolorbar: If specified and usecolorbar=0, the program
- % will not include the colorbar in plot area; otherwise,
- % a colorbar will be included in plot area.
- %
- % 3. usepanel: If specified and usepanel=0, the control panel
- % at lower right cornor will be invisible; otherwise,
- % it will be visible.
- %
- % 4. usecrosshair: If specified and usecrosshair=0, the crosshair
- % will be invisible; otherwise, it will be visible.
- %
- % 5. usestretch: If specified and usestretch=0, the 3 slices will
- % not be stretched, and will be displayed according to
- % the actual voxel size; otherwise, the 3 slices will be
- % stretched to the edge.
- %
- % 6. useimagesc: If specified and useimagesc=0, images data will
- % be used directly to match the colormap (like 'image'
- % command); otherwise, image data will be scaled to full
- % colormap with 'imagesc' command in Matlab.
- %
- % 7. useinterp: If specified and useinterp=1, the image will be
- % displayed using interpolation. Otherwise, it will be
- % displayed like mosaic, and each tile stands for a
- % pixel. This option does not apply to 'setvalue' option
- % is set.
- %
- %
- % 8. setarea: 3-View plot will be displayed on this specific
- % region. If it is not specified, program will set the
- % plot area to [0.05 0.05 0.9 0.9].
- %
- % 9. setunit: It can be specified to setunit='voxel' or 'mm'
- % and the view will change the axes unit of [X Y Z]
- % accordingly.
- %
- % 10. setviewpoint: If specified, [X Y Z] values will be used
- % to set the viewpoint of 3-View plot.
- %
- % 11. setscanid: If specified, [t] value will be used to display
- % the specified image scan in NIFTI data.
- %
- % 12. setcrosshaircolor: If specified, [r g b] value will be used
- % for Crosshair Color. Otherwise, red will be the default.
- %
- % 13. setcolorindex: If specified, the 3-View will choose the
- % following colormap: 2 - Bipolar; 3 - Gray; 4 - Jet;
- % 5 - Cool; 6 - Bone; 7 - Hot; 8 - Copper; 9 - Pink;
- % If not specified, it will choose 3 - Gray if all data
- % values are not less than 0; otherwise, it will choose
- % 2 - Bipolar if there is value less than 0. (Contrast
- % control can only apply to 3 - Gray colormap.
- %
- % 14. setcolormap: 3-View plot will use it as a customized colormap.
- % It is a 3-column matrix with value between 0 and 1. If
- % using MS-Windows version of Matlab, the number of rows
- % can not be more than 256, because of Matlab limitation.
- % When colormap is used, setcolorlevel option will be
- % disabled automatically.
- %
- % 15. setcolorlevel: If specified (must be no more than 256, and
- % cannot be used for customized colormap), row number of
- % colormap will be squeezed down to this level; otherwise,
- % it will assume that setcolorlevel=256.
- %
- % 16. sethighcolor: If specified, program will squeeze down the
- % colormap, and allocate sethighcolor (an Mx3 matrix)
- % to high-end portion of the colormap. The sum of M and
- % setcolorlevel should be less than 256. If setcolormap
- % option is used, sethighcolor will be inserted on top
- % of the setcolormap, and the setcolorlevel option will
- % be disabled automatically.
- %
- % 17. setcbarminmax: if specified, the [min max] will be used to
- % set the min and max of the colorbar, which does not
- % include any data for highcolor.
- %
- % 18. setvalue: If specified, setvalue.val (with the same size as
- % the source data on solution points) in the source area
- % setvalue.idx will be superimposed on the current nii
- % image. So, the size of setvalue.val should be equal to
- % the size of setvalue.idx. To use this feature, it needs
- % single or double nii structure for background image.
- %
- % 19. glblocminmax: If specified, pgm will use glblocminmax to
- % calculate the colormap, instead of minmax of image.
- %
- % 20. setbuttondown: If specified, pgm will evaluate the command
- % after a click or slide action is invoked to the new
- % view point.
- %
- % 21. setcomplex: This option will decide how complex data to be
- % displayed: 0 - Real part of complex data; 1 - Imaginary
- % part of complex data; 2 - Modulus (magnitude) of complex
- % data; If not specified, it will be set to 0 (Real part
- % of complex data as default option. This option only apply
- % when option.command is set to 'init or 'updatenii'.
- %
- %
- % Additional Options for 'update' command:
- % =======================================
- %
- % option.enablecursormove = [1] | 0
- % option.enableviewpoint = 0 | [1]
- % option.enableorigin = 0 | [1]
- % option.enableunit = 0 | [1]
- % option.enablecrosshair = 0 | [1]
- % option.enablehistogram = 0 | [1]
- % option.enablecolormap = 0 | [1]
- % option.enablecontrast = 0 | [1]
- % option.enablebrightness = 0 | [1]
- % option.enableslider = 0 | [1]
- % option.enabledirlabel = 0 | [1]
- %
- %
- % e.g.:
- % nii = load_nii('T1'); % T1.img/hdr
- % view_nii(nii);
- %
- % or
- %
- % h = figure('unit','normal','pos', [0.18 0.08 0.64 0.85]);
- % opt.setarea = [0.05 0.05 0.9 0.9];
- % view_nii(h, nii, opt);
- %
- %
- % Part of this file is copied and modified from:
- % http://www.mathworks.com/matlabcentral/fileexchange/1878-mri-analyze-tools
- %
- % NIFTI data format can be found on: http://nifti.nimh.nih.gov
- %
- % - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
- %
- function status = view_nii(varargin)
- if nargin < 1
- error('Please check inputs using ''help view_nii''');
- end;
- nii = '';
- opt = '';
- command = '';
- usecolorbar = [];
- usepanel = [];
- usecrosshair = '';
- usestretch = [];
- useimagesc = [];
- useinterp = [];
- setarea = [];
- setunit = '';
- setviewpoint = [];
- setscanid = [];
- setcrosshaircolor = [];
- setcolorindex = '';
- setcolormap = 'NA';
- setcolorlevel = [];
- sethighcolor = 'NA';
- setcbarminmax = [];
- setvalue = [];
- glblocminmax = [];
- setbuttondown = '';
- setcomplex = 0;
- status = [];
- if ishandle(varargin{1}) % plot on top of this figure
- fig = varargin{1};
- if nargin < 2
- command = 'update'; % just to get 3-View status
- end
- if nargin == 2
- if ~isstruct(varargin{2})
- error('2nd parameter should be either nii struct or option struct');
- end
- opt = varargin{2};
- if isfield(opt,'hdr') & isfield(opt,'img')
- nii = opt;
- elseif isfield(opt, 'command') & (strcmpi(opt.command,'init') ...
- | strcmpi(opt.command,'updatenii') ...
- | strcmpi(opt.command,'updateimg') )
- error('Option here cannot contain "init", "updatenii", or "updateimg" comand');
- end
- end
- if nargin == 3
- nii = varargin{2};
- opt = varargin{3};
- if ~isstruct(opt)
- error('3rd parameter should be option struct');
- end
- if ~isfield(opt,'command') | ~strcmpi(opt.command,'updateimg')
- if ~isstruct(nii) | ~isfield(nii,'hdr') | ~isfield(nii,'img')
- error('2nd parameter should be nii struct');
- end
- if isfield(nii,'untouch') & nii.untouch == 1
- error('Usage: please use ''load_nii.m'' to load the structure.');
- end
- end
- end
- set(fig, 'menubar', 'none');
- elseif ischar(varargin{1}) % call back by event
- command = lower(varargin{1});
- fig = gcbf;
- else % start nii with a new figure
- nii = varargin{1};
- if ~isstruct(nii) | ~isfield(nii,'hdr') | ~isfield(nii,'img')
- error('1st parameter should be either a figure handle or nii struct');
- end
- if isfield(nii,'untouch') & nii.untouch == 1
- error('Usage: please use ''load_nii.m'' to load the structure.');
- end
- if nargin > 1
- opt = varargin{2};
- if isfield(opt, 'command') & ~strcmpi(opt.command,'init')
- error('Option here must use "init" comand');
- end
- end
- command = 'init';
- fig = figure('unit','normal','position',[0.15 0.08 0.70 0.85]);
- view_nii_menu(fig);
- rri_file_menu(fig);
- end
- if ~isempty(opt)
- if isfield(opt,'command')
- command = lower(opt.command);
- end
- if isempty(command)
- command = 'update';
- end
- if isfield(opt,'usecolorbar')
- usecolorbar = opt.usecolorbar;
- end
- if isfield(opt,'usepanel')
- usepanel = opt.usepanel;
- end
- if isfield(opt,'usecrosshair')
- usecrosshair = opt.usecrosshair;
- end
- if isfield(opt,'usestretch')
- usestretch = opt.usestretch;
- end
- if isfield(opt,'useimagesc')
- useimagesc = opt.useimagesc;
- end
- if isfield(opt,'useinterp')
- useinterp = opt.useinterp;
- end
- if isfield(opt,'setarea')
- setarea = opt.setarea;
- end
- if isfield(opt,'setunit')
- setunit = opt.setunit;
- end
- if isfield(opt,'setviewpoint')
- setviewpoint = opt.setviewpoint;
- end
- if isfield(opt,'setscanid')
- setscanid = opt.setscanid;
- end
- if isfield(opt,'setcrosshaircolor')
- setcrosshaircolor = opt.setcrosshaircolor;
- if ~isempty(setcrosshaircolor) & (~isnumeric(setcrosshaircolor) | ~isequal(size(setcrosshaircolor),[1 3]) | min(setcrosshaircolor(:))<0 | max(setcrosshaircolor(:))>1)
- error('Crosshair Color should be a 1x3 matrix with value between 0 and 1');
- end
- end
- if isfield(opt,'setcolorindex')
- setcolorindex = round(opt.setcolorindex);
- if ~isnumeric(setcolorindex) | setcolorindex < 1 | setcolorindex > 9
- error('Colorindex should be a number between 1 and 9');
- end
- end
- if isfield(opt,'setcolormap')
- setcolormap = opt.setcolormap;
- if ~isempty(setcolormap) & (~isnumeric(setcolormap) | size(setcolormap,2) ~= 3 | min(setcolormap(:))<0 | max(setcolormap(:))>1)
- error('Colormap should be a Mx3 matrix with value between 0 and 1');
- end
- end
- if isfield(opt,'setcolorlevel')
- setcolorlevel = round(opt.setcolorlevel);
- if ~isnumeric(setcolorlevel) | setcolorlevel > 256 | setcolorlevel < 1
- error('Colorlevel should be a number between 1 and 256');
- end
- end
- if isfield(opt,'sethighcolor')
- sethighcolor = opt.sethighcolor;
- if ~isempty(sethighcolor) & (~isnumeric(sethighcolor) | size(sethighcolor,2) ~= 3 | min(sethighcolor(:))<0 | max(sethighcolor(:))>1)
- error('Highcolor should be a Mx3 matrix with value between 0 and 1');
- end
- end
- if isfield(opt,'setcbarminmax')
- setcbarminmax = opt.setcbarminmax;
- if isempty(setcbarminmax) | ~isnumeric(setcbarminmax) | length(setcbarminmax) ~= 2
- error('Colorbar MinMax should contain 2 values: [min max]');
- end
- end
- if isfield(opt,'setvalue')
- setvalue = opt.setvalue;
- if isempty(setvalue) | ~isstruct(setvalue) | ...
- ~isfield(opt.setvalue,'idx') | ~isfield(opt.setvalue,'val')
- error('setvalue should be a struct contains idx and val');
- end
- if length(opt.setvalue.idx(:)) ~= length(opt.setvalue.val(:))
- error('length of idx and val fields should be the same');
- end
- if ~strcmpi(class(opt.setvalue.idx),'single')
- opt.setvalue.idx = single(opt.setvalue.idx);
- end
- if ~strcmpi(class(opt.setvalue.val),'single')
- opt.setvalue.val = single(opt.setvalue.val);
- end
- end
- if isfield(opt,'glblocminmax')
- glblocminmax = opt.glblocminmax;
- end
- if isfield(opt,'setbuttondown')
- setbuttondown = opt.setbuttondown;
- end
- if isfield(opt,'setcomplex')
- setcomplex = opt.setcomplex;
- end
- end
- switch command
- case {'init'}
- set(fig, 'InvertHardcopy','off');
- set(fig, 'PaperPositionMode','auto');
- fig = init(nii, fig, setarea, setunit, setviewpoint, setscanid, setbuttondown, ...
- setcolorindex, setcolormap, setcolorlevel, sethighcolor, setcbarminmax, ...
- usecolorbar, usepanel, usecrosshair, usestretch, useimagesc, useinterp, ...
- setvalue, glblocminmax, setcrosshaircolor, setcomplex);
- % get status
- %
- status = get_status(fig);
- case {'update'}
- nii_view = getappdata(fig,'nii_view');
- h = fig;
- if isempty(nii_view)
- error('The figure should already contain a 3-View plot.');
- end
- if ~isempty(opt)
- % Order of the following update matters.
- %
- update_shape(h, setarea, usecolorbar, usestretch, useimagesc);
- update_useinterp(h, useinterp);
- update_useimagesc(h, useimagesc);
- update_usepanel(h, usepanel);
- update_colorindex(h, setcolorindex);
- update_colormap(h, setcolormap);
- update_highcolor(h, sethighcolor, setcolorlevel);
- update_cbarminmax(h, setcbarminmax);
- update_unit(h, setunit);
- update_viewpoint(h, setviewpoint);
- update_scanid(h, setscanid);
- update_buttondown(h, setbuttondown);
- update_crosshaircolor(h, setcrosshaircolor);
- update_usecrosshair(h, usecrosshair);
- % Enable/Disable object
- %
- update_enable(h, opt);
- end
- % get status
- %
- status = get_status(h);
- case {'updateimg'}
- if ~exist('nii','var')
- msg = sprintf('Please input a 3D matrix brain data');
- error(msg);
- end
- % Note: nii is not nii, nii should be a 3D matrix here
- %
- if ~isnumeric(nii)
- msg = sprintf('2nd parameter should be a 3D matrix, not nii struct');
- error(msg);
- end
- nii_view = getappdata(fig,'nii_view');
- if isempty(nii_view)
- error('The figure should already contain a 3-View plot.');
- end
- img = nii;
- update_img(img, fig, opt);
- % get status
- %
- status = get_status(fig);
- case {'updatenii'}
- nii_view = getappdata(fig,'nii_view');
- if isempty(nii_view)
- error('The figure should already contain a 3-View plot.');
- end
- if ~isstruct(nii) | ~isfield(nii,'hdr') | ~isfield(nii,'img')
- error('2nd parameter should be nii struct');
- end
- if isfield(nii,'untouch') & nii.untouch == 1
- error('Usage: please use ''load_nii.m'' to load the structure.');
- end
- opt.command = 'clearnii';
- view_nii(fig, opt);
- opt.command = 'init';
- view_nii(fig, nii, opt);
- % get status
- %
- status = get_status(fig);
- case {'clearnii'}
- nii_view = getappdata(fig,'nii_view');
- handles = struct2cell(nii_view.handles);
- for i=1:length(handles)
- if ishandle(handles{i}) % in case already del by parent
- delete(handles{i});
- end
- end
- rmappdata(fig,'nii_view');
- buttonmotion = get(fig,'windowbuttonmotion');
- mymotion = '; view_nii(''move_cursor'');';
- buttonmotion = strrep(buttonmotion, mymotion, '');
- set(fig, 'windowbuttonmotion', buttonmotion);
- case {'axial_image','coronal_image','sagittal_image'}
- switch command
- case 'axial_image', view = 'axi'; axi = 0; cor = 1; sag = 1;
- case 'coronal_image', view = 'cor'; axi = 1; cor = 0; sag = 1;
- case 'sagittal_image', view = 'sag'; axi = 1; cor = 1; sag = 0;
- end
- nii_view = getappdata(fig,'nii_view');
- nii_view = get_slice_position(nii_view,view);
- if isfield(nii_view, 'disp')
- img = nii_view.disp;
- else
- img = nii_view.nii.img;
- end
- % CData must be double() for Matlab 6.5 for Windows
- %
- if axi,
- if isfield(nii_view.handles,'axial_bg') & ~isempty(nii_view.handles.axial_bg) & nii_view.useinterp
- Saxi = squeeze(nii_view.bgimg(:,:,nii_view.slices.axi));
- set(nii_view.handles.axial_bg,'CData',double(Saxi)');
- end
- if isfield(nii_view.handles,'axial_image'),
- if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
- Saxi = squeeze(img(:,:,nii_view.slices.axi,:,nii_view.scanid));
- Saxi = permute(Saxi, [2 1 3]);
- else
- Saxi = squeeze(img(:,:,nii_view.slices.axi,nii_view.scanid));
- Saxi = Saxi';
- end
- set(nii_view.handles.axial_image,'CData',double(Saxi));
- end
- if isfield(nii_view.handles,'axial_slider'),
- set(nii_view.handles.axial_slider,'Value',nii_view.slices.axi);
- end;
- end
- if cor,
- if isfield(nii_view.handles,'coronal_bg') & ~isempty(nii_view.handles.coronal_bg) & nii_view.useinterp
- Scor = squeeze(nii_view.bgimg(:,nii_view.slices.cor,:));
- set(nii_view.handles.coronal_bg,'CData',double(Scor)');
- end
- if isfield(nii_view.handles,'coronal_image'),
- if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
- Scor = squeeze(img(:,nii_view.slices.cor,:,:,nii_view.scanid));
- Scor = permute(Scor, [2 1 3]);
- else
- Scor = squeeze(img(:,nii_view.slices.cor,:,nii_view.scanid));
- Scor = Scor';
- end
- set(nii_view.handles.coronal_image,'CData',double(Scor));
- end
- if isfield(nii_view.handles,'coronal_slider'),
- slider_val = nii_view.dims(2) - nii_view.slices.cor + 1;
- set(nii_view.handles.coronal_slider,'Value',slider_val);
- end;
- end;
- if sag,
- if isfield(nii_view.handles,'sagittal_bg') & ~isempty(nii_view.handles.sagittal_bg) & nii_view.useinterp
- Ssag = squeeze(nii_view.bgimg(nii_view.slices.sag,:,:));
- set(nii_view.handles.sagittal_bg,'CData',double(Ssag)');
- end
- if isfield(nii_view.handles,'sagittal_image'),
- if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
- Ssag = squeeze(img(nii_view.slices.sag,:,:,:,nii_view.scanid));
- Ssag = permute(Ssag, [2 1 3]);
- else
- Ssag = squeeze(img(nii_view.slices.sag,:,:,nii_view.scanid));
- Ssag = Ssag';
- end
- set(nii_view.handles.sagittal_image,'CData',double(Ssag));
- end
- if isfield(nii_view.handles,'sagittal_slider'),
- set(nii_view.handles.sagittal_slider,'Value',nii_view.slices.sag);
- end;
- end;
- update_nii_view(nii_view);
- if ~isempty(nii_view.buttondown)
- eval(nii_view.buttondown);
- end
-
- case {'axial_slider','coronal_slider','sagittal_slider'},
-
- switch command
- case 'axial_slider', view = 'axi'; axi = 1; cor = 0; sag = 0;
- case 'coronal_slider', view = 'cor'; axi = 0; cor = 1; sag = 0;
- case 'sagittal_slider', view = 'sag'; axi = 0; cor = 0; sag = 1;
- end
- nii_view = getappdata(fig,'nii_view');
- nii_view = get_slider_position(nii_view);
- if isfield(nii_view, 'disp')
- img = nii_view.disp;
- else
- img = nii_view.nii.img;
- end
- if axi,
- if isfield(nii_view.handles,'axial_bg') & ~isempty(nii_view.handles.axial_bg) & nii_view.useinterp
- Saxi = squeeze(nii_view.bgimg(:,:,nii_view.slices.axi));
- set(nii_view.handles.axial_bg,'CData',double(Saxi)');
- end
- if isfield(nii_view.handles,'axial_image'),
- if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
- Saxi = squeeze(img(:,:,nii_view.slices.axi,:,nii_view.scanid));
- Saxi = permute(Saxi, [2 1 3]);
- else
- Saxi = squeeze(img(:,:,nii_view.slices.axi,nii_view.scanid));
- Saxi = Saxi';
- end
- set(nii_view.handles.axial_image,'CData',double(Saxi));
- end
- if isfield(nii_view.handles,'axial_slider'),
- set(nii_view.handles.axial_slider,'Value',nii_view.slices.axi);
- end
- end
- if cor,
- if isfield(nii_view.handles,'coronal_bg') & ~isempty(nii_view.handles.coronal_bg) & nii_view.useinterp
- Scor = squeeze(nii_view.bgimg(:,nii_view.slices.cor,:));
- set(nii_view.handles.coronal_bg,'CData',double(Scor)');
- end
- if isfield(nii_view.handles,'coronal_image'),
- if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
- Scor = squeeze(img(:,nii_view.slices.cor,:,:,nii_view.scanid));
- Scor = permute(Scor, [2 1 3]);
- else
- Scor = squeeze(img(:,nii_view.slices.cor,:,nii_view.scanid));
- Scor = Scor';
- end
- set(nii_view.handles.coronal_image,'CData',double(Scor));
- end
- if isfield(nii_view.handles,'coronal_slider'),
- slider_val = nii_view.dims(2) - nii_view.slices.cor + 1;
- set(nii_view.handles.coronal_slider,'Value',slider_val);
- end
- end
- if sag,
- if isfield(nii_view.handles,'sagittal_bg') & ~isempty(nii_view.handles.sagittal_bg) & nii_view.useinterp
- Ssag = squeeze(nii_view.bgimg(nii_view.slices.sag,:,:));
- set(nii_view.handles.sagittal_bg,'CData',double(Ssag)');
- end
- if isfield(nii_view.handles,'sagittal_image'),
- if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
- Ssag = squeeze(img(nii_view.slices.sag,:,:,:,nii_view.scanid));
- Ssag = permute(Ssag, [2 1 3]);
- else
- Ssag = squeeze(img(nii_view.slices.sag,:,:,nii_view.scanid));
- Ssag = Ssag';
- end
- set(nii_view.handles.sagittal_image,'CData',double(Ssag));
- end
- if isfield(nii_view.handles,'sagittal_slider'),
- set(nii_view.handles.sagittal_slider,'Value',nii_view.slices.sag);
- end
- end
- update_nii_view(nii_view);
- if ~isempty(nii_view.buttondown)
- eval(nii_view.buttondown);
- end
- case {'impos_edit'}
- nii_view = getappdata(fig,'nii_view');
- impos = str2num(get(nii_view.handles.impos,'string'));
- if isfield(nii_view, 'disp')
- img = nii_view.disp;
- else
- img = nii_view.nii.img;
- end
- if isempty(impos) | ~all(size(impos) == [1 3])
- msg = 'Please use 3 numbers to represent X,Y and Z';
- msgbox(msg,'Error');
- return;
- end
- slices.sag = round(impos(1));
- slices.cor = round(impos(2));
- slices.axi = round(impos(3));
- nii_view = convert2voxel(nii_view,slices);
- nii_view = check_slices(nii_view);
- impos(1) = nii_view.slices.sag;
- impos(2) = nii_view.dims(2) - nii_view.slices.cor + 1;
- impos(3) = nii_view.slices.axi;
- if isfield(nii_view.handles,'sagittal_slider'),
- set(nii_view.handles.sagittal_slider,'Value',impos(1));
- end
- if isfield(nii_view.handles,'coronal_slider'),
- set(nii_view.handles.coronal_slider,'Value',impos(2));
- end
- if isfield(nii_view.handles,'axial_slider'),
- set(nii_view.handles.axial_slider,'Value',impos(3));
- end
- nii_view = get_slider_position(nii_view);
- update_nii_view(nii_view);
- if isfield(nii_view.handles,'axial_bg') & ~isempty(nii_view.handles.axial_bg) & nii_view.useinterp
- Saxi = squeeze(nii_view.bgimg(:,:,nii_view.slices.axi));
- set(nii_view.handles.axial_bg,'CData',double(Saxi)');
- end
- if isfield(nii_view.handles,'axial_image'),
- if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
- Saxi = squeeze(img(:,:,nii_view.slices.axi,:,nii_view.scanid));
- Saxi = permute(Saxi, [2 1 3]);
- else
- Saxi = squeeze(img(:,:,nii_view.slices.axi,nii_view.scanid));
- Saxi = Saxi';
- end
- set(nii_view.handles.axial_image,'CData',double(Saxi));
- end
- if isfield(nii_view.handles,'axial_slider'),
- set(nii_view.handles.axial_slider,'Value',nii_view.slices.axi);
- end
- if isfield(nii_view.handles,'coronal_bg') & ~isempty(nii_view.handles.coronal_bg) & nii_view.useinterp
- Scor = squeeze(nii_view.bgimg(:,nii_view.slices.cor,:));
- set(nii_view.handles.coronal_bg,'CData',double(Scor)');
- end
- if isfield(nii_view.handles,'coronal_image'),
- if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
- Scor = squeeze(img(:,nii_view.slices.cor,:,:,nii_view.scanid));
- Scor = permute(Scor, [2 1 3]);
- else
- Scor = squeeze(img(:,nii_view.slices.cor,:,nii_view.scanid));
- Scor = Scor';
- end
- set(nii_view.handles.coronal_image,'CData',double(Scor));
- end
- if isfield(nii_view.handles,'coronal_slider'),
- slider_val = nii_view.dims(2) - nii_view.slices.cor + 1;
- set(nii_view.handles.coronal_slider,'Value',slider_val);
- end
- if isfield(nii_view.handles,'sagittal_bg') & ~isempty(nii_view.handles.sagittal_bg) & nii_view.useinterp
- Ssag = squeeze(nii_view.bgimg(nii_view.slices.sag,:,:));
- set(nii_view.handles.sagittal_bg,'CData',double(Ssag)');
- end
- if isfield(nii_view.handles,'sagittal_image'),
- if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
- Ssag = squeeze(img(nii_view.slices.sag,:,:,:,nii_view.scanid));
- Ssag = permute(Ssag, [2 1 3]);
- else
- Ssag = squeeze(img(nii_view.slices.sag,:,:,nii_view.scanid));
- Ssag = Ssag';
- end
- set(nii_view.handles.sagittal_image,'CData',double(Ssag));
- end
- if isfield(nii_view.handles,'sagittal_slider'),
- set(nii_view.handles.sagittal_slider,'Value',nii_view.slices.sag);
- end
- axes(nii_view.handles.axial_axes);
- axes(nii_view.handles.coronal_axes);
- axes(nii_view.handles.sagittal_axes);
- if ~isempty(nii_view.buttondown)
- eval(nii_view.buttondown);
- end
- case 'coordinates',
- nii_view = getappdata(fig,'nii_view');
- set_image_value(nii_view);
- case 'crosshair',
- nii_view = getappdata(fig,'nii_view');
- if get(nii_view.handles.xhair,'value') == 2 % off
- set(nii_view.axi_xhair.lx,'visible','off');
- set(nii_view.axi_xhair.ly,'visible','off');
- set(nii_view.cor_xhair.lx,'visible','off');
- set(nii_view.cor_xhair.ly,'visible','off');
- set(nii_view.sag_xhair.lx,'visible','off');
- set(nii_view.sag_xhair.ly,'visible','off');
- else
- set(nii_view.axi_xhair.lx,'visible','on');
- set(nii_view.axi_xhair.ly,'visible','on');
- set(nii_view.cor_xhair.lx,'visible','on');
- set(nii_view.cor_xhair.ly,'visible','on');
- set(nii_view.sag_xhair.lx,'visible','on');
- set(nii_view.sag_xhair.ly,'visible','on');
- set(nii_view.handles.axial_axes,'selected','on');
- set(nii_view.handles.axial_axes,'selected','off');
- set(nii_view.handles.coronal_axes,'selected','on');
- set(nii_view.handles.coronal_axes,'selected','off');
- set(nii_view.handles.sagittal_axes,'selected','on');
- set(nii_view.handles.sagittal_axes,'selected','off');
- end
- case 'xhair_color',
- old_color = get(gcbo,'user');
- new_color = uisetcolor(old_color);
- update_crosshaircolor(fig, new_color);
- case {'color','contrast_def'}
- nii_view = getappdata(fig,'nii_view');
- if nii_view.numscan == 1
- if get(nii_view.handles.colorindex,'value') == 2
- set(nii_view.handles.contrast,'value',128);
- elseif get(nii_view.handles.colorindex,'value') == 3
- set(nii_view.handles.contrast,'value',1);
- end
- end
- [custom_color_map, custom_colorindex] = change_colormap(fig);
- if strcmpi(command, 'color')
- setcolorlevel = nii_view.colorlevel;
- if ~isempty(custom_color_map) % isfield(nii_view, 'color_map')
- setcolormap = custom_color_map; % nii_view.color_map;
- else
- setcolormap = [];
- end
- if isfield(nii_view, 'highcolor')
- sethighcolor = nii_view.highcolor;
- else
- sethighcolor = [];
- end
- redraw_cbar(fig, setcolorlevel, setcolormap, sethighcolor);
- if nii_view.numscan == 1 & ...
- (custom_colorindex < 2 | custom_colorindex > 3)
- contrastopt.enablecontrast = 0;
- else
- contrastopt.enablecontrast = 1;
- end
- update_enable(fig, contrastopt);
- end
- case {'neg_color','brightness','contrast'}
- change_colormap(fig);
- case {'brightness_def'}
- nii_view = getappdata(fig,'nii_view');
- set(nii_view.handles.brightness,'value',0);
- change_colormap(fig);
- case 'hist_plot'
- hist_plot(fig);
- case 'hist_eq'
- hist_eq(fig);
- case 'move_cursor'
- move_cursor(fig);
- case 'edit_change_scan'
- change_scan('edit_change_scan');
- case 'slider_change_scan'
- change_scan('slider_change_scan');
- end
- return; % view_nii
- %----------------------------------------------------------------
- function fig = init(nii, fig, area, setunit, setviewpoint, setscanid, buttondown, ...
- colorindex, color_map, colorlevel, highcolor, cbarminmax, ...
- usecolorbar, usepanel, usecrosshair, usestretch, useimagesc, ...
- useinterp, setvalue, glblocminmax, setcrosshaircolor, ...
- setcomplex)
- % Support data type COMPLEX64 & COMPLEX128
- %
- if nii.hdr.dime.datatype == 32 | nii.hdr.dime.datatype == 1792
- switch setcomplex,
- case 0,
- nii.img = real(nii.img);
- case 1,
- nii.img = imag(nii.img);
- case 2,
- if isa(nii.img, 'double')
- nii.img = abs(double(nii.img));
- else
- nii.img = single(abs(double(nii.img)));
- end
- end
- end
- if isempty(area)
- area = [0.05 0.05 0.9 0.9];
- end
- if isempty(setscanid)
- setscanid = 1;
- else
- setscanid = round(setscanid);
- if setscanid < 1
- setscanid = 1;
- end
- if setscanid > nii.hdr.dime.dim(5)
- setscanid = nii.hdr.dime.dim(5);
- end
- end
- if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
- usecolorbar = 0;
- elseif isempty(usecolorbar)
- usecolorbar = 1;
- end
- if isempty(usepanel)
- usepanel = 1;
- end
- if isempty(usestretch)
- usestretch = 1;
- end
- if isempty(useimagesc)
- useimagesc = 1;
- end
- if isempty(useinterp)
- useinterp = 0;
- end
- if isempty(colorindex)
- tmp = min(nii.img(:,:,:,setscanid));
- if min(tmp(:)) < 0
- colorindex = 2;
- setcrosshaircolor = [1 1 0];
- else
- colorindex = 3;
- end
- end
- if isempty(color_map) | ischar(color_map)
- color_map = [];
- else
- colorindex = 1;
- end
- bgimg = [];
- if ~isempty(glblocminmax)
- minvalue = glblocminmax(1);
- maxvalue = glblocminmax(2);
- else
- minvalue = nii.img(:,:,:,setscanid);
- minvalue = double(minvalue(:));
- minvalue = min(minvalue(~isnan(minvalue)));
- maxvalue = nii.img(:,:,:,setscanid);
- maxvalue = double(maxvalue(:));
- maxvalue = max(maxvalue(~isnan(maxvalue)));
- end
- if ~isempty(setvalue)
- if ~isempty(glblocminmax)
- minvalue = glblocminmax(1);
- maxvalue = glblocminmax(2);
- else
- minvalue = double(min(setvalue.val));
- maxvalue = double(max(setvalue.val));
- end
- bgimg = double(nii.img);
- minbg = double(min(bgimg(:)));
- maxbg = double(max(bgimg(:)));
- bgimg = scale_in(bgimg, minbg, maxbg, 55) + 200; % scale to 201~256
- % 56 level for brain structure
- %
- % highcolor = [zeros(1,3);gray(55)];
- highcolor = gray(56);
- cbarminmax = [minvalue maxvalue];
- if useinterp
- % scale signal data to 1~200
- %
- nii.img = repmat(nan, size(nii.img));
- nii.img(setvalue.idx) = setvalue.val;
- % 200 level for source image
- %
- bgimg = single(scale_out(bgimg, cbarminmax(1), cbarminmax(2), 199));
- else
- bgimg(setvalue.idx) = NaN;
- minbg = double(min(bgimg(:)));
- maxbg = double(max(bgimg(:)));
- bgimg(setvalue.idx) = minbg;
- % bgimg must be normalized to [201 256]
- %
- bgimg = 55 * (bgimg-min(bgimg(:))) / (max(bgimg(:))-min(bgimg(:))) + 201;
- bgimg(setvalue.idx) = 0;
- % scale signal data to 1~200
- %
- nii.img = zeros(size(nii.img));
- nii.img(setvalue.idx) = scale_in(setvalue.val, minvalue, maxvalue, 199);
- nii.img = nii.img + bgimg;
- bgimg = [];
- nii.img = scale_out(nii.img, cbarminmax(1), cbarminmax(2), 199);
- minvalue = double(nii.img(:));
- minvalue = min(minvalue(~isnan(minvalue)));
- maxvalue = double(nii.img(:));
- maxvalue = max(maxvalue(~isnan(maxvalue)));
- if ~isempty(glblocminmax) % maxvalue is gray
- minvalue = glblocminmax(1);
- end
- end
- colorindex = 2;
- setcrosshaircolor = [1 1 0];
- end
- if isempty(highcolor) | ischar(highcolor)
- highcolor = [];
- num_highcolor = 0;
- else
- num_highcolor = size(highcolor,1);
- end
- if isempty(colorlevel)
- colorlevel = 256 - num_highcolor;
- end
- if usecolorbar
- cbar_area = area;
- cbar_area(1) = area(1) + area(3)*0.93;
- cbar_area(3) = area(3)*0.04;
- area(3) = area(3)*0.9; % 90% used for main axes
- else
- cbar_area = [];
- end
- % init color (gray) scaling to make sure the slice clim take the
- % global clim [min(nii.img(:)) max(nii.img(:))]
- %
- if isempty(bgimg)
- clim = [minvalue maxvalue];
- else
- clim = [minvalue double(max(bgimg(:)))];
- end
- if clim(1) == clim(2)
- clim(2) = clim(1) + 0.000001;
- end
- if isempty(cbarminmax)
- cbarminmax = [minvalue maxvalue];
- end
- xdim = size(nii.img, 1);
- ydim = size(nii.img, 2);
- zdim = size(nii.img, 3);
- dims = [xdim ydim zdim];
- voxel_size = abs(nii.hdr.dime.pixdim(2:4)); % vol in mm
- if any(voxel_size <= 0)
- voxel_size(find(voxel_size <= 0)) = 1;
- end
- origin = abs(nii.hdr.hist.originator(1:3));
- if isempty(origin) | all(origin == 0) % according to SPM
- origin = (dims+1)/2;
- end;
- origin = round(origin);
- if any(origin > dims) % simulate fMRI
- origin(find(origin > dims)) = dims(find(origin > dims));
- end
- if any(origin <= 0)
- origin(find(origin <= 0)) = 1;
- end
- nii_view.dims = dims;
- nii_view.voxel_size = voxel_size;
- nii_view.origin = origin;
- nii_view.slices.sag = 1;
- nii_view.slices.cor = 1;
- nii_view.slices.axi = 1;
- if xdim > 1, nii_view.slices.sag = origin(1); end
- if ydim > 1, nii_view.slices.cor = origin(2); end
- if zdim > 1, nii_view.slices.axi = origin(3); end
- nii_view.area = area;
- nii_view.fig = fig;
- nii_view.nii = nii; % image data
- nii_view.bgimg = bgimg; % background
- nii_view.setvalue = setvalue;
- nii_view.minvalue = minvalue;
- nii_view.maxvalue = maxvalue;
- nii_view.numscan = nii.hdr.dime.dim(5);
- nii_view.scanid = setscanid;
- Font.FontUnits = 'point';
- Font.FontSize = 12;
- % create axes for colorbar
- %
- [cbar_axes cbarminmax_axes] = create_cbar_axes(fig, cbar_area);
- if isempty(cbar_area)
- nii_view.cbar_area = [];
- else
- nii_view.cbar_area = cbar_area;
- end
- % create axes for top/front/side view
- %
- vol_size = voxel_size .* dims;
- [top_ax, front_ax, side_ax] ...
- = create_ax(fig, area, vol_size, usestretch);
- top_pos = get(top_ax,'position');
- front_pos = get(front_ax,'position');
- side_pos = get(side_ax,'position');
- % Sagittal Slider
- %
- x = side_pos(1);
- y = top_pos(2) + top_pos(4);
- w = side_pos(3);
- h = (front_pos(2) - y) / 2;
- y = y + h;
- pos = [x y w h];
- if xdim > 1,
- slider_step(1) = 1/(xdim);
- slider_step(2) = 1.00001/(xdim);
- handles.sagittal_slider = uicontrol('Parent',fig, ...
- 'Style','slider','Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment','center',...
- 'BackgroundColor',[0.5 0.5 0.5],'ForegroundColor',[0 0 0],...
- 'BusyAction','queue',...
- 'TooltipString','Sagittal slice navigation',...
- 'Min',1,'Max',xdim,'SliderStep',slider_step, ...
- 'Value',nii_view.slices.sag,...
- 'Callback','view_nii(''sagittal_slider'');');
- set(handles.sagittal_slider,'position',pos); % linux66
- end
- % Coronal Slider
- %
- x = top_pos(1);
- y = top_pos(2) + top_pos(4);
- w = top_pos(3);
- h = (front_pos(2) - y) / 2;
- y = y + h;
- pos = [x y w h];
- if ydim > 1,
- slider_step(1) = 1/(ydim);
- slider_step(2) = 1.00001/(ydim);
- slider_val = nii_view.dims(2) - nii_view.slices.cor + 1;
- handles.coronal_slider = uicontrol('Parent',fig, ...
- 'Style','slider','Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment','center',...
- 'BackgroundColor',[0.5 0.5 0.5],'ForegroundColor',[0 0 0],...
- 'BusyAction','queue',...
- 'TooltipString','Coronal slice navigation',...
- 'Min',1,'Max',ydim,'SliderStep',slider_step, ...
- 'Value',slider_val,...
- 'Callback','view_nii(''coronal_slider'');');
- set(handles.coronal_slider,'position',pos); % linux66
- end
- % Axial Slider
- %
- % x = front_pos(1) + front_pos(3);
- % y = front_pos(2);
- % w = side_pos(1) - x;
- % h = front_pos(4);
- x = top_pos(1);
- y = area(2);
- w = top_pos(3);
- h = top_pos(2) - y;
- pos = [x y w h];
- if zdim > 1,
- slider_step(1) = 1/(zdim);
- slider_step(2) = 1.00001/(zdim);
- handles.axial_slider = uicontrol('Parent',fig, ...
- 'Style','slider','Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment','center',...
- 'BackgroundColor',[0.5 0.5 0.5],'ForegroundColor',[0 0 0],...
- 'BusyAction','queue',...
- 'TooltipString','Axial slice navigation',...
- 'Min',1,'Max',zdim,'SliderStep',slider_step, ...
- 'Value',nii_view.slices.axi,...
- 'Callback','view_nii(''axial_slider'');');
- set(handles.axial_slider,'position',pos); % linux66
- end
- % plot info view
- %
- % info_pos = [side_pos([1,3]); top_pos([2,4])];
- % info_pos = info_pos(:);
- gap = side_pos(1)-(top_pos(1)+top_pos(3));
- info_pos(1) = side_pos(1) + gap;
- info_pos(2) = area(2);
- info_pos(3) = side_pos(3) - gap;
- info_pos(4) = top_pos(2) + top_pos(4) - area(2) - gap;
- num_inputline = 10;
- inputline_space =info_pos(4) / num_inputline;
- % for any info_area change, update_usestretch should also be changed
- % Image Intensity Value at Cursor
- %
- x = info_pos(1);
- y = info_pos(2);
- w = info_pos(3)*0.5;
- h = inputline_space*0.6;
- pos = [x y w h];
- handles.Timvalcur = uicontrol('Parent',fig,'Style','text', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'left',...
- 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
- 'BusyAction','queue',...
- 'visible','off', ...
- 'String','Value at cursor:');
- if usepanel
- set(handles.Timvalcur, 'visible', 'on');
- end
- x = x + w;
- w = info_pos(3)*0.5;
- pos = [x y w h];
- handles.imvalcur = uicontrol('Parent',fig,'Style','text', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'right',...
- 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
- 'BusyAction','queue',...
- 'visible','off', ...
- 'String',' ');
- if usepanel
- set(handles.imvalcur, 'visible', 'on');
- end
- % Position at Cursor
- %
- x = info_pos(1);
- y = y + inputline_space;
- w = info_pos(3)*0.5;
- pos = [x y w h];
- handles.Timposcur = uicontrol('Parent',fig,'Style','text', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'left',...
- 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
- 'BusyAction','queue',...
- 'visible','off', ...
- 'String','[X Y Z] at cursor:');
- if usepanel
- set(handles.Timposcur, 'visible', 'on');
- end
- x = x + w;
- w = info_pos(3)*0.5;
- pos = [x y w h];
- handles.imposcur = uicontrol('Parent',fig,'Style','text', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'right',...
- 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
- 'BusyAction','queue',...
- 'visible','off', ...
- 'String',' ','Value',[0 0 0]);
- if usepanel
- set(handles.imposcur, 'visible', 'on');
- end
- % Image Intensity Value at Mouse Click
- %
- x = info_pos(1);
- y = y + inputline_space;
- w = info_pos(3)*0.5;
- pos = [x y w h];
- handles.Timval = uicontrol('Parent',fig,'Style','text', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'left',...
- 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
- 'BusyAction','queue',...
- 'visible','off', ...
- 'String','Value at crosshair:');
- if usepanel
- set(handles.Timval, 'visible', 'on');
- end
- x = x + w;
- w = info_pos(3)*0.5;
- pos = [x y w h];
- handles.imval = uicontrol('Parent',fig,'Style','text', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'right',...
- 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
- 'BusyAction','queue',...
- 'visible','off', ...
- 'String',' ');
- if usepanel
- set(handles.imval, 'visible', 'on');
- end
- % Viewpoint Position at Mouse Click
- %
- x = info_pos(1);
- y = y + inputline_space;
- w = info_pos(3)*0.5;
- pos = [x y w h];
- handles.Timpos = uicontrol('Parent',fig,'Style','text', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'left',...
- 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
- 'BusyAction','queue',...
- 'visible','off', ...
- 'String','[X Y Z] at crosshair:');
- if usepanel
- set(handles.Timpos, 'visible', 'on');
- end
- x = x + w + 0.005;
- y = y - 0.008;
- w = info_pos(3)*0.5;
- h = inputline_space*0.9;
- pos = [x y w h];
- handles.impos = uicontrol('Parent',fig,'Style','edit', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'right',...
- 'BackgroundColor', [1 1 1], 'ForegroundColor', [0 0 0],...
- 'BusyAction','queue',...
- 'Callback','view_nii(''impos_edit'');', ...
- 'TooltipString','Viewpoint Location in Axes Unit', ...
- 'visible','off', ...
- 'String',' ','Value',[0 0 0]);
- if usepanel
- set(handles.impos, 'visible', 'on');
- end
- % Origin Position
- %
- x = info_pos(1);
- y = y + inputline_space*1.2;
- w = info_pos(3)*0.5;
- h = inputline_space*0.6;
- pos = [x y w h];
- handles.Torigin = uicontrol('Parent',fig,'Style','text', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'left',...
- 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
- 'BusyAction','queue',...
- 'visible','off', ...
- 'String','[X Y Z] at origin:');
- if usepanel
- set(handles.Torigin, 'visible', 'on');
- end
- x = x + w;
- w = info_pos(3)*0.5;
- pos = [x y w h];
- handles.origin = uicontrol('Parent',fig,'Style','text', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'right',...
- 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
- 'BusyAction','queue',...
- 'visible','off', ...
- 'String',' ','Value',[0 0 0]);
- if usepanel
- set(handles.origin, 'visible', 'on');
- end
- if 0
- % Voxel Unit
- %
- x = info_pos(1);
- y = y + inputline_space;
- w = info_pos(3)*0.5;
- pos = [x y w h];
- handles.Tcoord = uicontrol('Parent',fig,'Style','text', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'left',...
- 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
- 'BusyAction','queue',...
- 'visible','off', ...
- 'String','Axes Unit:');
- if usepanel
- set(handles.Tcoord, 'visible', 'on');
- end
- x = x + w + 0.005;
- w = info_pos(3)*0.5 - 0.005;
- pos = [x y w h];
- Font.FontSize = 8;
- handles.coord = uicontrol('Parent',fig,'Style','popupmenu', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, ...
- 'BackgroundColor', [1 1 1], 'ForegroundColor', [0 0 0],...
- 'BusyAction','queue',...
- 'TooltipString','Choose Voxel or Millimeter',...
- 'String',{'Voxel','Millimeter'},...
- 'visible','off', ...
- 'Callback','view_nii(''coordinates'');');
- % 'TooltipString','Choose Voxel, MNI or Talairach Coordinates',...
- % 'String',{'Voxel','MNI (mm)','Talairach (mm)'},...
- Font.FontSize = 12;
- if usepanel
- set(handles.coord, 'visible', 'on');
- end
- end
- % Crosshair
- %
- x = info_pos(1);
- y = y + inputline_space;
- w = info_pos(3)*0.4;
- pos = [x y w h];
- handles.Txhair = uicontrol('Parent',fig,'Style','text', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'left',...
- 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
- 'BusyAction','queue',...
- 'visible','off', ...
- 'String','Crosshair:');
- if usepanel
- set(handles.Txhair, 'visible', 'on');
- end
- x = info_pos(1) + info_pos(3)*0.5;
- w = info_pos(3)*0.2;
- h = inputline_space*0.7;
- pos = [x y w h];
- Font.FontSize = 8;
- handles.xhair_color = uicontrol('Parent',fig,'Style','push', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'center',...
- 'TooltipString','Crosshair Color',...
- 'User',[1 0 0],...
- 'String','Color',...
- 'visible','off', ...
- 'Callback','view_nii(''xhair_color'');');
- if usepanel
- set(handles.xhair_color, 'visible', 'on');
- end
- x = info_pos(1) + info_pos(3)*0.7;
- w = info_pos(3)*0.3;
- pos = [x y w h];
- handles.xhair = uicontrol('Parent',fig,'Style','popupmenu', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, ...
- 'BackgroundColor', [1 1 1], 'ForegroundColor', [0 0 0],...
- 'BusyAction','queue',...
- 'TooltipString','Display or Hide Crosshair',...
- 'String',{'On','Off'},...
- 'visible','off', ...
- 'Callback','view_nii(''crosshair'');');
- if usepanel
- set(handles.xhair, 'visible', 'on');
- end
- % Histogram & Color
- %
- x = info_pos(1);
- w = info_pos(3)*0.45;
- h = inputline_space * 1.5;
- pos = [x, y+inputline_space*0.9, w, h];
- handles.hist_frame = uicontrol('Parent',fig, ...
- 'Units','normal', ...
- 'BackgroundColor',[0.8 0.8 0.8], ...
- 'Position',pos, ...
- 'visible','off', ...
- 'Style','frame');
- if usepanel
- % set(handles.hist_frame, 'visible', 'on');
- end
- handles.coord_frame = uicontrol('Parent',fig, ...
- 'Units','normal', ...
- 'BackgroundColor',[0.8 0.8 0.8], ...
- 'Position',pos, ...
- 'visible','off', ...
- 'Style','frame');
- if usepanel
- set(handles.coord_frame, 'visible', 'on');
- end
- x = info_pos(1) + info_pos(3)*0.475;
- w = info_pos(3)*0.525;
- h = inputline_space * 1.5;
- pos = [x, y+inputline_space*0.9, w, h];
- handles.color_frame = uicontrol('Parent',fig, ...
- 'Units','normal', ...
- 'BackgroundColor',[0.8 0.8 0.8], ...
- 'Position',pos, ...
- 'visible','off', ...
- 'Style','frame');
- if usepanel
- set(handles.color_frame, 'visible', 'on');
- end
- x = info_pos(1) + info_pos(3)*0.025;
- y = y + inputline_space*1.2;
- w = info_pos(3)*0.2;
- h = inputline_space*0.7;
- pos = [x y w h];
- Font.FontSize = 8;
- handles.hist_eq = uicontrol('Parent',fig,'Style','toggle', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'center',...
- 'TooltipString','Histogram Equalization',...
- 'String','Hist EQ',...
- 'visible','off', ...
- 'Callback','view_nii(''hist_eq'');');
- if usepanel
- % set(handles.hist_eq, 'visible', 'on');
- end
- x = x + w;
- w = info_pos(3)*0.2;
- pos = [x y w h];
- handles.hist_plot = uicontrol('Parent',fig,'Style','push', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'center',...
- 'TooltipString','Histogram Plot',...
- 'String','Hist Plot',...
- 'visible','off', ...
- 'Callback','view_nii(''hist_plot'');');
- if usepanel
- % set(handles.hist_plot, 'visible', 'on');
- end
- x = info_pos(1) + info_pos(3)*0.025;
- w = info_pos(3)*0.4;
- pos = [x y w h];
- handles.coord = uicontrol('Parent',fig,'Style','popupmenu', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, ...
- 'BackgroundColor', [1 1 1], 'ForegroundColor', [0 0 0],...
- 'BusyAction','queue',...
- 'TooltipString','Choose Voxel or Millimeter',...
- 'String',{'Voxel','Millimeter'},...
- 'visible','off', ...
- 'Callback','view_nii(''coordinates'');');
- % 'TooltipString','Choose Voxel, MNI or Talairach Coordinates',...
- % 'String',{'Voxel','MNI (mm)','Talairach (mm)'},...
- if usepanel
- set(handles.coord, 'visible', 'on');
- end
- x = info_pos(1) + info_pos(3)*0.5;
- w = info_pos(3)*0.2;
- pos = [x y w h];
- handles.neg_color = uicontrol('Parent',fig,'Style','toggle', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'center',...
- 'TooltipString','Negative Colormap',...
- 'String','Negative',...
- 'visible','off', ...
- 'Callback','view_nii(''neg_color'');');
- if usepanel
- set(handles.neg_color, 'visible', 'on');
- end
- if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
- set(handles.neg_color, 'enable', 'off');
- end
- x = info_pos(1) + info_pos(3)*0.7;
- w = info_pos(3)*0.275;
- pos = [x y w h];
- handles.colorindex = uicontrol('Parent',fig,'Style','popupmenu', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, ...
- 'BackgroundColor', [1 1 1], 'ForegroundColor', [0 0 0],...
- 'BusyAction','queue',...
- 'TooltipString','Change Colormap',...
- 'String',{'Custom','Bipolar','Gray','Jet','Cool','Bone','Hot','Copper','Pink'},...
- 'value', colorindex, ...
- 'visible','off', ...
- 'Callback','view_nii(''color'');');
- if usepanel
- set(handles.colorindex, 'visible', 'on');
- end
- if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
- set(handles.colorindex, 'enable', 'off');
- end
- x = info_pos(1) + info_pos(3)*0.1;
- y = y + inputline_space;
- w = info_pos(3)*0.28;
- h = inputline_space*0.6;
- pos = [x y w h];
- Font.FontSize = 8;
- handles.Thist = uicontrol('Parent',fig,'Style','text', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'center',...
- 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
- 'BusyAction','queue',...
- 'visible','off', ...
- 'String','Histogram');
- handles.Tcoord = uicontrol('Parent',fig,'Style','text', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'center',...
- 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
- 'BusyAction','queue',...
- 'visible','off', ...
- 'String','Axes Unit');
- if usepanel
- % set(handles.Thist, 'visible', 'on');
- set(handles.Tcoord, 'visible', 'on');
- end
- x = info_pos(1) + info_pos(3)*0.60;
- w = info_pos(3)*0.28;
- pos = [x y w h];
- handles.Tcolor = uicontrol('Parent',fig,'Style','text', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'center',...
- 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
- 'BusyAction','queue',...
- 'visible','off', ...
- 'String','Colormap');
- if usepanel
- set(handles.Tcolor, 'visible', 'on');
- end
- if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
- set(handles.Tcolor, 'enable', 'off');
- end
- % Contrast Frame
- %
- x = info_pos(1);
- w = info_pos(3)*0.45;
- h = inputline_space * 2;
- pos = [x, y+inputline_space*0.8, w, h];
- handles.contrast_frame = uicontrol('Parent',fig, ...
- 'Units','normal', ...
- 'BackgroundColor',[0.8 0.8 0.8], ...
- 'Position',pos, ...
- 'visible','off', ...
- 'Style','frame');
- if usepanel
- set(handles.contrast_frame, 'visible', 'on');
- end
- if colorindex < 2 | colorindex > 3
- set(handles.contrast_frame, 'visible', 'off');
- end
- % Brightness Frame
- %
- x = info_pos(1) + info_pos(3)*0.475;
- w = info_pos(3)*0.525;
- pos = [x, y+inputline_space*0.8, w, h];
- handles.brightness_frame = uicontrol('Parent',fig, ...
- 'Units','normal', ...
- 'BackgroundColor',[0.8 0.8 0.8], ...
- 'Position',pos, ...
- 'visible','off', ...
- 'Style','frame');
- if usepanel
- set(handles.brightness_frame, 'visible', 'on');
- end
- % Contrast
- %
- x = info_pos(1) + info_pos(3)*0.025;
- y = y + inputline_space;
- w = info_pos(3)*0.4;
- h = inputline_space*0.6;
- pos = [x y w h];
- Font.FontSize = 12;
- slider_step(1) = 5/255;
- slider_step(2) = 5.00001/255;
- handles.contrast = uicontrol('Parent',fig, ...
- 'Style','slider','Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'left',...
- 'BackgroundColor',[0.5 0.5 0.5],'ForegroundColor',[0 0 0],...
- 'BusyAction','queue',...
- 'TooltipString','Change contrast',...
- 'Min',1,'Max',256,'SliderStep',slider_step, ...
- 'Value',1, ...
- 'visible','off', ...
- 'Callback','view_nii(''contrast'');');
- if usepanel
- set(handles.contrast, 'visible', 'on');
- end
- if (nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511) & nii_view.numscan <= 1
- set(handles.contrast, 'enable', 'off');
- end
- if nii_view.numscan > 1
- set(handles.contrast, 'min', 1, 'max', nii_view.numscan, ...
- 'sliderstep',[1/(nii_view.numscan-1) 1.00001/(nii_view.numscan-1)], ...
- 'Callback', 'view_nii(''slider_change_scan'');');
- elseif colorindex < 2 | colorindex > 3
- set(handles.contrast, 'visible', 'off');
- elseif colorindex == 2
- set(handles.contrast,'value',128);
- end
- set(handles.contrast,'position',pos); % linux66
- % Brightness
- %
- x = info_pos(1) + info_pos(3)*0.5;
- w = info_pos(3)*0.475;
- pos = [x y w h];
- Font.FontSize = 12;
- slider_step(1) = 1/50;
- slider_step(2) = 1.00001/50;
- handles.brightness = uicontrol('Parent',fig, ...
- 'Style','slider','Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'left',...
- 'BackgroundColor',[0.5 0.5 0.5],'ForegroundColor',[0 0 0],...
- 'BusyAction','queue',...
- 'TooltipString','Change brightness',...
- 'Min',-1,'Max',1,'SliderStep',slider_step, ...
- 'Value',0, ...
- 'visible','off', ...
- 'Callback','view_nii(''brightness'');');
- if usepanel
- set(handles.brightness, 'visible', 'on');
- end
- if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
- set(handles.brightness, 'enable', 'off');
- end
- set(handles.brightness,'position',pos); % linux66
- % Contrast text/def
- %
- x = info_pos(1) + info_pos(3)*0.025;
- y = y + inputline_space;
- w = info_pos(3)*0.22;
- pos = [x y w h];
- handles.Tcontrast = uicontrol('Parent',fig,'Style','text', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'left',...
- 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
- 'BusyAction','queue',...
- 'visible','off', ...
- 'String','Contrast:');
- if usepanel
- set(handles.Tcontrast, 'visible', 'on');
- end
- if (nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511) & nii_view.numscan <= 1
- set(handles.Tcontrast, 'enable', 'off');
- end
- if nii_view.numscan > 1
- set(handles.Tcontrast, 'string', 'Scan ID:');
- set(handles.contrast, 'TooltipString', 'Change Scan ID');
- elseif colorindex < 2 | colorindex > 3
- set(handles.Tcontrast, 'visible', 'off');
- end
- x = x + w;
- w = info_pos(3)*0.18;
- pos = [x y w h];
- Font.FontSize = 8;
- handles.contrast_def = uicontrol('Parent',fig,'Style','push', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'center',...
- 'TooltipString','Restore initial contrast',...
- 'String','Reset',...
- 'visible','off', ...
- 'Callback','view_nii(''contrast_def'');');
- if usepanel
- set(handles.contrast_def, 'visible', 'on');
- end
- if (nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511) & nii_view.numscan <= 1
- set(handles.contrast_def, 'enable', 'off');
- end
- if nii_view.numscan > 1
- set(handles.contrast_def, 'style', 'edit', 'background', 'w', ...
- 'TooltipString','Scan (or volume) index in the time series',...
- 'string', '1', 'Callback', 'view_nii(''edit_change_scan'');');
- elseif colorindex < 2 | colorindex > 3
- set(handles.contrast_def, 'visible', 'off');
- end
- % Brightness text/def
- %
- x = info_pos(1) + info_pos(3)*0.5;
- w = info_pos(3)*0.295;
- pos = [x y w h];
- Font.FontSize = 12;
- handles.Tbrightness = uicontrol('Parent',fig,'Style','text', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'left',...
- 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
- 'BusyAction','queue',...
- 'visible','off', ...
- 'String','Brightness:');
- if usepanel
- set(handles.Tbrightness, 'visible', 'on');
- end
- if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
- set(handles.Tbrightness, 'enable', 'off');
- end
- x = x + w;
- w = info_pos(3)*0.18;
- pos = [x y w h];
- Font.FontSize = 8;
- handles.brightness_def = uicontrol('Parent',fig,'Style','push', ...
- 'Units','Normalized', Font, ...
- 'Position',pos, 'HorizontalAlignment', 'center',...
- 'TooltipString','Restore initial brightness',...
- 'String','Reset',...
- 'visible','off', ...
- 'Callback','view_nii(''brightness_def'');');
- if usepanel
- set(handles.brightness_def, 'visible', 'on');
- end
- if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
- set(handles.brightness_def, 'enable', 'off');
- end
- % init image handles
- %
- handles.axial_image = [];
- handles.coronal_image = [];
- handles.sagittal_image = [];
- % plot axial view
- %
- if ~isempty(nii_view.bgimg)
- bg_slice = squeeze(bgimg(:,:,nii_view.slices.axi));
- h1 = plot_view(fig, xdim, ydim, top_ax, bg_slice', clim, cbarminmax, ...
- handles, useimagesc, colorindex, color_map, ...
- colorlevel, highcolor, useinterp, nii_view.numscan);
- handles.axial_bg = h1;
- else
- handles.axial_bg = [];
- end
- if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
- img_slice = squeeze(nii.img(:,:,nii_view.slices.axi,:,setscanid));
- img_slice = permute(img_slice, [2 1 3]);
- else
- img_slice = squeeze(nii.img(:,:,nii_view.slices.axi,setscanid));
- img_slice = img_slice';
- end
- h1 = plot_view(fig, xdim, ydim, top_ax, img_slice, clim, cbarminmax, ...
- handles, useimagesc, colorindex, color_map, ...
- colorlevel, highcolor, useinterp, nii_view.numscan);
- set(h1,'buttondown','view_nii(''axial_image'');');
- handles.axial_image = h1;
- handles.axial_axes = top_ax;
- if size(img_slice,1) == 1 | size(img_slice,2) == 1
- set(top_ax,'visible','off');
- if isfield(handles,'sagittal_slider') & ishandle(handles.sagittal_slider)
- set(handles.sagittal_slider, 'visible', 'off');
- end
- if isfield(handles,'coronal_slider') & ishandle(handles.coronal_slider)
- set(handles.coronal_slider, 'visible', 'off');
- end
- if isfield(handles,'axial_slider') & ishandle(handles.axial_slider)
- set(handles.axial_slider, 'visible', 'off');
- end
- end
- % plot coronal view
- %
- if ~isempty(nii_view.bgimg)
- bg_slice = squeeze(bgimg(:,nii_view.slices.cor,:));
- h1 = plot_view(fig, xdim, zdim, front_ax, bg_slice', clim, cbarminmax, ...
- handles, useimagesc, colorindex, color_map, ...
- colorlevel, highcolor, useinterp, nii_view.numscan);
- handles.coronal_bg = h1;
- else
- handles.coronal_bg = [];
- end
- if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
- img_slice = squeeze(nii.img(:,nii_view.slices.cor,:,:,setscanid));
- img_slice = permute(img_slice, [2 1 3]);
- else
- img_slice = squeeze(nii.img(:,nii_view.slices.cor,:,setscanid));
- img_slice = img_slice';
- end
- h1 = plot_view(fig, xdim, zdim, front_ax, img_slice, clim, cbarminmax, ...
- handles, useimagesc, colorindex, color_map, ...
- colorlevel, highcolor, useinterp, nii_view.numscan);
- set(h1,'buttondown','view_nii(''coronal_image'');');
- handles.coronal_image = h1;
- handles.coronal_axes = front_ax;
- if size(img_slice,1) == 1 | size(img_slice,2) == 1
- set(front_ax,'visible','off');
- if isfield(handles,'sagittal_slider') & ishandle(handles.sagittal_slider)
- set(handles.sagittal_slider, 'visible', 'off');
- end
- if isfield(handles,'coronal_slider') & ishandle(handles.coronal_slider)
- set(handles.coronal_slider, 'visible', 'off');
- end
- if isfield(handles,'axial_slider') & ishandle(handles.axial_slider)
- set(handles.axial_slider, 'visible', 'off');
- end
- end
- % plot sagittal view
- %
- if ~isempty(nii_view.bgimg)
- bg_slice = squeeze(bgimg(nii_view.slices.sag,:,:));
- h1 = plot_view(fig, ydim, zdim, side_ax, bg_slice', clim, cbarminmax, ...
- handles, useimagesc, colorindex, color_map, ...
- colorlevel, highcolor, useinterp, nii_view.numscan);
- handles.sagittal_bg = h1;
- else
- handles.sagittal_bg = [];
- end
- if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
- img_slice = squeeze(nii.img(nii_view.slices.sag,:,:,:,setscanid));
- img_slice = permute(img_slice, [2 1 3]);
- else
- img_slice = squeeze(nii.img(nii_view.slices.sag,:,:,setscanid));
- img_slice = img_slice';
- end
- h1 = plot_view(fig, ydim, zdim, side_ax, img_slice, clim, cbarminmax, ...
- handles, useimagesc, colorindex, color_map, ...
- colorlevel, highcolor, useinterp, nii_view.numscan);
- set(h1,'buttondown','view_nii(''sagittal_image'');');
- set(side_ax,'Xdir', 'reverse');
- handles.sagittal_image = h1;
- handles.sagittal_axes = side_ax;
- if size(img_slice,1) == 1 | size(img_slice,2) == 1
- set(side_ax,'visible','off');
- if isfield(handles,'sagittal_slider') & ishandle(handles.sagittal_slider)
- set(handles.sagittal_slider, 'visible', 'off');
- end
- if isfield(handles,'coronal_slider') & ishandle(handles.coronal_slider)
- set(handles.coronal_slider, 'visible', 'off');
- end
- if isfield(handles,'axial_slider') & ishandle(handles.axial_slider)
- set(handles.axial_slider, 'visible', 'off');
- end
- end
- [top1_label, top2_label, side1_label, side2_label] = ...
- dir_label(fig, top_ax, front_ax, side_ax);
- % store label handles
- %
- handles.top1_label = top1_label;
- handles.top2_label = top2_label;
- handles.side1_label = side1_label;
- handles.side2_label = side2_label;
- % plot colorbar
- %
- if ~isempty(cbar_axes) & ~isempty(cbarminmax_axes)
- if 0
- if isempty(color_map)
- level = colorlevel + num_highcolor;
- else
- level = size([color_map; highcolor], 1);
- end
- end
- if isempty(color_map)
- level = colorlevel;
- else
- level = size([color_map], 1);
- end
- niiclass = class(nii.img);
- h1 = plot_cbar(fig, cbar_axes, cbarminmax_axes, cbarminmax, ...
- level, handles, useimagesc, colorindex, color_map, ...
- colorlevel, highcolor, niiclass, nii_view.numscan);
- handles.cbar_image = h1;
- handles.cbar_axes = cbar_axes;
- handles.cbarminmax_axes = cbarminmax_axes;
- end
- nii_view.handles = handles; % store handles
- nii_view.usepanel = usepanel; % whole panel at low right cornor
- nii_view.usestretch = usestretch; % stretch display of voxel_size
- nii_view.useinterp = useinterp; % use interpolation
- nii_view.colorindex = colorindex; % store colorindex variable
- nii_view.buttondown = buttondown; % command after button down click
- nii_view.cbarminmax = cbarminmax; % store min max value for colorbar
- set_coordinates(nii_view,useinterp); % coord unit
- if ~isfield(nii_view, 'axi_xhair') | ...
- ~isfield(nii_view, 'cor_xhair') | ...
- ~isfield(nii_view, 'sag_xhair')
- nii_view.axi_xhair = []; % top cross hair
- nii_view.cor_xhair = []; % front cross hair
- nii_view.sag_xhair = []; % side cross hair
- end
- if ~isempty(color_map)
- nii_view.color_map = color_map;
- end
- if ~isempty(colorlevel)
- nii_view.colorlevel = colorlevel;
- end
- if ~isempty(highcolor)
- nii_view.highcolor = highcolor;
- end
- update_nii_view(nii_view);
- if ~isempty(setunit)
- update_unit(fig, setunit);
- end
- if ~isempty(setviewpoint)
- update_viewpoint(fig, setviewpoint);
- end
- if ~isempty(setcrosshaircolor)
- update_crosshaircolor(fig, setcrosshaircolor);
- end
- if ~isempty(usecrosshair)
- update_usecrosshair(fig, usecrosshair);
- end
- nii_menu = getappdata(fig, 'nii_menu');
- if ~isempty(nii_menu)
- if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
- set(nii_menu.Minterp,'Userdata',1,'Label','Interp on','enable','off');
- elseif useinterp
- set(nii_menu.Minterp,'Userdata',0,'Label','Interp off');
- else
- set(nii_menu.Minterp,'Userdata',1,'Label','Interp on');
- end
- end
- windowbuttonmotion = get(fig, 'windowbuttonmotion');
- windowbuttonmotion = [windowbuttonmotion '; view_nii(''move_cursor'');'];
- set(fig, 'windowbuttonmotion', windowbuttonmotion);
- return; % init
- %----------------------------------------------------------------
- function fig = update_img(img, fig, opt)
- nii_menu = getappdata(fig,'nii_menu');
- if ~isempty(nii_menu)
- set(nii_menu.Mzoom,'Userdata',1,'Label','Zoom on');
- set(fig,'pointer','arrow');
- zoom off;
- end
- nii_view = getappdata(fig,'nii_view');
- change_interp = 0;
- if isfield(opt, 'useinterp') & opt.useinterp ~= nii_view.useinterp
- nii_view.useinterp = opt.useinterp;
- change_interp = 1;
- end
- setscanid = 1;
- if isfield(opt, 'setscanid')
- setscanid = round(opt.setscanid);
- if setscanid < 1
- setscanid = 1;
- end
- if setscanid > nii_view.numscan
- setscanid = nii_view.numscan;
- end
- end
- if isfield(opt, 'glblocminmax') & ~isempty(opt.glblocminmax)
- minvalue = opt.glblocminmax(1);
- maxvalue = opt.glblocminmax(2);
- else
- minvalue = img(:,:,:,setscanid);
- minvalue = double(minvalue(:));
- minvalue = min(minvalue(~isnan(minvalue)));
- maxvalue = img(:,:,:,setscanid);
- maxvalue = double(maxvalue(:));
- maxvalue = max(maxvalue(~isnan(maxvalue)));
- end
- if isfield(opt, 'setvalue')
- setvalue = opt.setvalue;
- if isfield(opt, 'glblocminmax') & ~isempty(opt.glblocminmax)
- minvalue = opt.glblocminmax(1);
- maxvalue = opt.glblocminmax(2);
- else
- minvalue = double(min(setvalue.val));
- maxvalue = double(max(setvalue.val));
- end
- bgimg = double(img);
- minbg = double(min(bgimg(:)));
- maxbg = double(max(bgimg(:)));
- bgimg = scale_in(bgimg, minbg, maxbg, 55) + 200; % scale to 201~256
- cbarminmax = [minvalue maxvalue];
- if nii_view.useinterp
- % scale signal data to 1~200
- %
- img = repmat(nan, size(img));
- img(setvalue.idx) = setvalue.val;
- % 200 level for source image
- %
- bgimg = single(scale_out(bgimg, cbarminmax(1), cbarminmax(2), 199));
- else
- bgimg(setvalue.idx) = NaN;
- minbg = double(min(bgimg(:)));
- maxbg = double(max(bgimg(:)));
- bgimg(setvalue.idx) = minbg;
- % bgimg must be normalized to [201 256]
- %
- bgimg = 55 * (bgimg-min(bgimg(:))) / (max(bgimg(:))-min(bgimg(:))) + 201;
- bgimg(setvalue.idx) = 0;
- % scale signal data to 1~200
- %
- img = zeros(size(img));
- img(setvalue.idx) = scale_in(setvalue.val, minvalue, maxvalue, 199);
- img = img + bgimg;
- bgimg = [];
- img = scale_out(img, cbarminmax(1), cbarminmax(2), 199);
- minvalue = double(min(img(:)));
- maxvalue = double(max(img(:)));
- if isfield(opt,'glblocminmax') & ~isempty(opt.glblocminmax)
- minvalue = opt.glblocminmax(1);
- end
- end
- nii_view.bgimg = bgimg;
- nii_view.setvalue = setvalue;
- else
- cbarminmax = [minvalue maxvalue];
- end
- update_cbarminmax(fig, cbarminmax);
- nii_view.cbarminmax = cbarminmax;
- nii_view.nii.img = img;
- nii_view.minvalue = minvalue;
- nii_view.maxvalue = maxvalue;
- nii_view.scanid = setscanid;
- change_colormap(fig);
- % init color (gray) scaling to make sure the slice clim take the
- % global clim [min(nii.img(:)) max(nii.img(:))]
- %
- if isempty(nii_view.bgimg)
- clim = [minvalue maxvalue];
- else
- clim = [minvalue double(max(nii_view.bgimg(:)))];
- end
- if clim(1) == clim(2)
- clim(2) = clim(1) + 0.000001;
- end
- if strcmpi(get(nii_view.handles.axial_image,'cdatamapping'), 'direct')
- useimagesc = 0;
- else
- useimagesc = 1;
- end
- if ~isempty(nii_view.bgimg) % with interpolation
- Saxi = squeeze(nii_view.bgimg(:,:,nii_view.slices.axi));
- if isfield(nii_view.handles,'axial_bg') & ~isempty(nii_view.handles.axial_bg)
- set(nii_view.handles.axial_bg,'CData',double(Saxi)');
- else
- axes(nii_view.handles.axial_axes);
- if useimagesc
- nii_view.handles.axial_bg = surface(zeros(size(Saxi')),double(Saxi'),'edgecolor','none','facecolor','interp');
- else
- nii_view.handles.axial_bg = surface(zeros(size(Saxi')),double(Saxi'),'cdatamapping','direct','edgecolor','none','facecolor','interp');
- end
- order = get(gca,'child');
- order(find(order == nii_view.handles.axial_bg)) = [];
- order = [order; nii_view.handles.axial_bg];
- set(gca, 'child', order);
- end
- end
- if isfield(nii_view.handles,'axial_image'),
- if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
- Saxi = squeeze(nii_view.nii.img(:,:,nii_view.slices.axi,:,setscanid));
- Saxi = permute(Saxi, [2 1 3]);
- else
- Saxi = squeeze(nii_view.nii.img(:,:,nii_view.slices.axi,setscanid));
- Saxi = Saxi';
- end
- set(nii_view.handles.axial_image,'CData',double(Saxi));
- end
- set(nii_view.handles.axial_axes,'CLim',clim);
- if ~isempty(nii_view.bgimg)
- Scor = squeeze(nii_view.bgimg(:,nii_view.slices.cor,:));
- if isfield(nii_view.handles,'coronal_bg') & ~isempty(nii_view.handles.coronal_bg)
- set(nii_view.handles.coronal_bg,'CData',double(Scor)');
- else
- axes(nii_view.handles.coronal_axes);
- if useimagesc
- nii_view.handles.coronal_bg = surface(zeros(size(Scor')),double(Scor'),'edgecolor','none','facecolor','interp');
- else
- nii_view.handles.coronal_bg = surface(zeros(size(Scor')),double(Scor'),'cdatamapping','direct','edgecolor','none','facecolor','interp');
- end
- order = get(gca,'child');
- order(find(order == nii_view.handles.coronal_bg)) = [];
- order = [order; nii_view.handles.coronal_bg];
- set(gca, 'child', order);
- end
- end
- if isfield(nii_view.handles,'coronal_image'),
- if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
- Scor = squeeze(nii_view.nii.img(:,nii_view.slices.cor,:,:,setscanid));
- Scor = permute(Scor, [2 1 3]);
- else
- Scor = squeeze(nii_view.nii.img(:,nii_view.slices.cor,:,setscanid));
- Scor = Scor';
- end
- set(nii_view.handles.coronal_image,'CData',double(Scor));
- end
- set(nii_view.handles.coronal_axes,'CLim',clim);
- if ~isempty(nii_view.bgimg)
- Ssag = squeeze(nii_view.bgimg(nii_view.slices.sag,:,:));
- if isfield(nii_view.handles,'sagittal_bg') & ~isempty(nii_view.handles.sagittal_bg)
- set(nii_view.handles.sagittal_bg,'CData',double(Ssag)');
- else
- axes(nii_view.handles.sagittal_axes);
- if useimagesc
- nii_view.handles.sagittal_bg = surface(zeros(size(Ssag')),double(Ssag'),'edgecolor','none','facecolor','interp');
- else
- nii_view.handles.sagittal_bg = surface(zeros(size(Ssag')),double(Ssag'),'cdatamapping','direct','edgecolor','none','facecolor','interp');
- end
- order = get(gca,'child');
- order(find(order == nii_view.handles.sagittal_bg)) = [];
- order = [order; nii_view.handles.sagittal_bg];
- set(gca, 'child', order);
- end
- end
- if isfield(nii_view.handles,'sagittal_image'),
- if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
- Ssag = squeeze(nii_view.nii.img(nii_view.slices.sag,:,:,:,setscanid));
- Ssag = permute(Ssag, [2 1 3]);
- else
- Ssag = squeeze(nii_view.nii.img(nii_view.slices.sag,:,:,setscanid));
- Ssag = Ssag';
- end
- set(nii_view.handles.sagittal_image,'CData',double(Ssag));
- end
- set(nii_view.handles.sagittal_axes,'CLim',clim);
- update_nii_view(nii_view);
- if isfield(opt, 'setvalue')
- if ~isfield(nii_view,'highcolor') | ~isequal(size(nii_view.highcolor),[56 3])
- % 55 level for brain structure (paded 0 for highcolor level 1, i.e. normal level 201, to make 56 highcolor)
- %
- update_highcolor(fig, [zeros(1,3);gray(55)], []);
- end
- if nii_view.colorindex ~= 2
- update_colorindex(fig, 2);
- end
- old_color = get(nii_view.handles.xhair_color,'user');
- if isequal(old_color, [1 0 0])
- update_crosshaircolor(fig, [1 1 0]);
- end
- % if change_interp
- % update_useinterp(fig, nii_view.useinterp);
- % end
- end
- if change_interp
- update_useinterp(fig, nii_view.useinterp);
- end
- return; % update_img
- %----------------------------------------------------------------
- function [top_pos, front_pos, side_pos] = ...
- axes_pos(fig,area,vol_size,usestretch)
- set(fig,'unit','pixel');
- fig_pos = get(fig,'position');
- gap_x = 15/fig_pos(3); % width of vertical scrollbar
- gap_y = 15/fig_pos(4); % width of horizontal scrollbar
- a = (area(3) - gap_x * 1.3) * fig_pos(3) / (vol_size(1) + vol_size(2)); % no crosshair lost in zoom
- b = (area(4) - gap_y * 3) * fig_pos(4) / (vol_size(2) + vol_size(3));
- c = min([a b]); % make sure 'ax' is inside 'area'
- top_w = vol_size(1) * c / fig_pos(3);
- side_w = vol_size(2) * c / fig_pos(3);
- top_h = vol_size(2) * c / fig_pos(4);
- side_h = vol_size(3) * c / fig_pos(4);
- side_x = area(1) + top_w + gap_x * 1.3; % no crosshair lost in zoom
- side_y = area(2) + top_h + gap_y * 3;
- if usestretch
- if a > b % top touched ceiling, use b
- d = (area(3) - gap_x * 1.3) / (top_w + side_w); % no crosshair lost in zoom
- top_w = top_w * d;
- side_w = side_w * d;
- side_x = area(1) + top_w + gap_x * 1.3; % no crosshair lost in zoom
- else
- d = (area(4) - gap_y * 3) / (top_h + side_h);
- top_h = top_h * d;
- side_h = side_h * d;
- side_y = area(2) + top_h + gap_y * 3;
- end
- end
- top_pos = [area(1) area(2)+gap_y top_w top_h];
- front_pos = [area(1) side_y top_w side_h];
- side_pos = [side_x side_y side_w side_h];
- set(fig,'unit','normal');
- return; % axes_pos
- %----------------------------------------------------------------
- function [top_ax, front_ax, side_ax] ...
- = create_ax(fig, area, vol_size, usestretch)
- cur_fig = gcf; % save h_wait fig
- figure(fig);
- [top_pos, front_pos, side_pos] = ...
- axes_pos(fig,area,vol_size,usestretch);
- nii_view = getappdata(fig, 'nii_view');
- if isempty(nii_view)
- top_ax = axes('position', top_pos);
- front_ax = axes('position', front_pos);
- side_ax = axes('position', side_pos);
- else
- top_ax = nii_view.handles.axial_axes;
- front_ax = nii_view.handles.coronal_axes;
- side_ax = nii_view.handles.sagittal_axes;
- set(top_ax, 'position', top_pos);
- set(front_ax, 'position', front_pos);
- set(side_ax, 'position', side_pos);
- end
- figure(cur_fig);
- return; % create_ax
- %----------------------------------------------------------------
- function [cbar_axes, cbarminmax_axes] = create_cbar_axes(fig, cbar_area, nii_view)
- if isempty(cbar_area) % without_cbar
- cbar_axes = [];
- cbarminmax_axes = [];
- return;
- end
- cur_fig = gcf; % save h_wait fig
- figure(fig);
- if ~exist('nii_view', 'var')
- nii_view = getappdata(fig, 'nii_view');
- end
- if isempty(nii_view) | ~isfield(nii_view.handles,'cbar_axes') | isempty(nii_view.handles.cbar_axes)
- cbarminmax_axes = axes('position', cbar_area);
- cbar_axes = axes('position', cbar_area);
- else
- cbarminmax_axes = nii_view.handles.cbarminmax_axes;
- cbar_axes = nii_view.handles.cbar_axes;
- set(cbarminmax_axes, 'position', cbar_area);
- set(cbar_axes, 'position', cbar_area);
- end
- figure(cur_fig);
- return; % create_cbar_axes
- %----------------------------------------------------------------
- function h1 = plot_view(fig, x, y, img_ax, img_slice, clim, ...
- cbarminmax, handles, useimagesc, colorindex, color_map, ...
- colorlevel, highcolor, useinterp, numscan)
- h1 = [];
- if x > 1 & y > 1,
- axes(img_ax);
- nii_view = getappdata(fig, 'nii_view');
- if isempty(nii_view)
- % set colormap first
- %
- nii.handles = handles;
- nii.handles.axial_axes = img_ax;
- nii.colorindex = colorindex;
- nii.color_map = color_map;
- nii.colorlevel = colorlevel;
- nii.highcolor = highcolor;
- nii.numscan = numscan;
- change_colormap(fig, nii, colorindex, cbarminmax);
- if useinterp
- if useimagesc
- h1 = surface(zeros(size(img_slice)),double(img_slice),'edgecolor','none','facecolor','interp');
- else
- h1 = surface(zeros(size(img_slice)),double(img_slice),'cdatamapping','direct','edgecolor','none','facecolor','interp');
- end
- set(gca,'clim',clim);
- else
- if useimagesc
- h1 = imagesc(img_slice,clim);
- else
- h1 = image(img_slice);
- end
- set(gca,'clim',clim);
- end
- else
- h1 = nii_view.handles.axial_image;
- if ~isequal(get(h1,'parent'), img_ax)
- h1 = nii_view.handles.coronal_image;
- end
- if ~isequal(get(h1,'parent'), img_ax)
- h1 = nii_view.handles.sagittal_image;
- end
- set(h1, 'cdata', double(img_slice));
- set(h1, 'xdata', 1:size(img_slice,2));
- set(h1, 'ydata', 1:size(img_slice,1));
- end
- set(img_ax,'YDir','normal','XLimMode','manual','YLimMode','manual',...
- 'ClimMode','manual','visible','off', ...
- 'xtick',[],'ytick',[], 'clim', clim);
- end
- return; % plot_view
- %----------------------------------------------------------------
- function h1 = plot_cbar(fig, cbar_axes, cbarminmax_axes, cbarminmax, ...
- level, handles, useimagesc, colorindex, color_map, ...
- colorlevel, highcolor, niiclass, numscan, nii_view)
- cbar_image = [1:level]';
- % In a uint8 or uint16 indexed image, 0 points to the first row
- % in the colormap
- %
- if 0 % strcmpi(niiclass,'uint8') | strcmpi(niiclass,'uint16')
- % we use single for display anyway
- ylim = [0, level-1];
- else
- ylim = [1, level];
- end
- axes(cbarminmax_axes);
- plot([0 0], cbarminmax, 'w');
- axis tight;
- set(cbarminmax_axes,'YDir','normal', ...
- 'XLimMode','manual','YLimMode','manual','YColor',[0 0 0], ...
- 'XColor',[0 0 0],'xtick',[],'YAxisLocation','right');
- ylimb = get(cbarminmax_axes,'ylim');
- ytickb = get(cbarminmax_axes,'ytick');
- ytick=(ylim(2)-ylim(1))*(ytickb-ylimb(1))/(ylimb(2)-ylimb(1))+ylim(1);
- axes(cbar_axes);
- if ~exist('nii_view', 'var')
- nii_view = getappdata(fig, 'nii_view');
- end
- if isempty(nii_view) | ~isfield(nii_view.handles,'cbar_image') | isempty(nii_view.handles.cbar_image)
- % set colormap first
- %
- nii.handles = handles;
- nii.colorindex = colorindex;
- nii.color_map = color_map;
- nii.colorlevel = colorlevel;
- nii.highcolor = highcolor;
- nii.numscan = numscan;
- change_colormap(fig, nii, colorindex, cbarminmax);
- h1 = image([0,1], [ylim(1),ylim(2)], cbar_image);
- else
- h1 = nii_view.handles.cbar_image;
- set(h1, 'cdata', double(cbar_image));
- end
- set(cbar_axes,'YDir','normal','XLimMode','manual', ...
- 'YLimMode','manual','YColor',[0 0 0],'XColor',[0 0 0],'xtick',[], ...
- 'YAxisLocation','right','ylim',ylim,'ytick',ytick,'yticklabel','');
- return; % plot_cbar
- %----------------------------------------------------------------
- function set_coordinates(nii_view,useinterp)
- imgPlim.vox = nii_view.dims;
- imgNlim.vox = [1 1 1];
- if useinterp
- xdata_ax = [imgNlim.vox(1) imgPlim.vox(1)];
- ydata_ax = [imgNlim.vox(2) imgPlim.vox(2)];
- zdata_ax = [imgNlim.vox(3) imgPlim.vox(3)];
- else
- xdata_ax = [imgNlim.vox(1)-0.5 imgPlim.vox(1)+0.5];
- ydata_ax = [imgNlim.vox(2)-0.5 imgPlim.vox(2)+0.5];
- zdata_ax = [imgNlim.vox(3)-0.5 imgPlim.vox(3)+0.5];
- end
- if isfield(nii_view.handles,'axial_image') & ~isempty(nii_view.handles.axial_image)
- set(nii_view.handles.axial_axes,'Xlim',xdata_ax);
- set(nii_view.handles.axial_axes,'Ylim',ydata_ax);
- end;
- if isfield(nii_view.handles,'coronal_image') & ~isempty(nii_view.handles.coronal_image)
- set(nii_view.handles.coronal_axes,'Xlim',xdata_ax);
- set(nii_view.handles.coronal_axes,'Ylim',zdata_ax);
- end;
- if isfield(nii_view.handles,'sagittal_image') & ~isempty(nii_view.handles.sagittal_image)
- set(nii_view.handles.sagittal_axes,'Xlim',ydata_ax);
- set(nii_view.handles.sagittal_axes,'Ylim',zdata_ax);
- end;
- return % set_coordinates
- %----------------------------------------------------------------
- function set_image_value(nii_view),
- % get coordinates of selected voxel and the image intensity there
- %
- sag = round(nii_view.slices.sag);
- cor = round(nii_view.slices.cor);
- axi = round(nii_view.slices.axi);
- if 0 % isfield(nii_view, 'disp')
- img = nii_view.disp;
- else
- img = nii_view.nii.img;
- end
- if nii_view.nii.hdr.dime.datatype == 128
- imgvalue = [double(img(sag,cor,axi,1,nii_view.scanid)) double(img(sag,cor,axi,2,nii_view.scanid)) double(img(sag,cor,axi,3,nii_view.scanid))];
- set(nii_view.handles.imval,'Value',imgvalue);
- set(nii_view.handles.imval,'String',sprintf('%7.4g %7.4g %7.4g',imgvalue));
- elseif nii_view.nii.hdr.dime.datatype == 511
- R = double(img(sag,cor,axi,1,nii_view.scanid)) * (nii_view.nii.hdr.dime.glmax - ...
- nii_view.nii.hdr.dime.glmin) + nii_view.nii.hdr.dime.glmin;
- G = double(img(sag,cor,axi,2,nii_view.scanid)) * (nii_view.nii.hdr.dime.glmax - ...
- nii_view.nii.hdr.dime.glmin) + nii_view.nii.hdr.dime.glmin;
- B = double(img(sag,cor,axi,3,nii_view.scanid)) * (nii_view.nii.hdr.dime.glmax - ...
- nii_view.nii.hdr.dime.glmin) + nii_view.nii.hdr.dime.glmin;
- imgvalue = [double(img(sag,cor,axi,1,nii_view.scanid)) double(img(sag,cor,axi,2,nii_view.scanid)) double(img(sag,cor,axi,3,nii_view.scanid))];
- set(nii_view.handles.imval,'Value',imgvalue);
- imgvalue = [R G B];
- set(nii_view.handles.imval,'String',sprintf('%7.4g %7.4g %7.4g',imgvalue));
- else
- imgvalue = double(img(sag,cor,axi,nii_view.scanid));
- set(nii_view.handles.imval,'Value',imgvalue);
- if isnan(imgvalue) | imgvalue > nii_view.cbarminmax(2)
- imgvalue = 0;
- end
- set(nii_view.handles.imval,'String',sprintf('%.6g',imgvalue));
- end
- % Now update the coordinates of the selected voxel
- nii_view = update_imgXYZ(nii_view);
- if get(nii_view.handles.coord,'value') == 1,
- sag = nii_view.imgXYZ.vox(1);
- cor = nii_view.imgXYZ.vox(2);
- axi = nii_view.imgXYZ.vox(3);
- org = nii_view.origin;
- elseif get(nii_view.handles.coord,'value') == 2,
- sag = nii_view.imgXYZ.mm(1);
- cor = nii_view.imgXYZ.mm(2);
- axi = nii_view.imgXYZ.mm(3);
- org = [0 0 0];
- elseif get(nii_view.handles.coord,'value') == 3,
- sag = nii_view.imgXYZ.tal(1);
- cor = nii_view.imgXYZ.tal(2);
- axi = nii_view.imgXYZ.tal(3);
- org = [0 0 0];
- end
- set(nii_view.handles.impos,'Value',[sag,cor,axi]);
- if get(nii_view.handles.coord,'value') == 1,
- string = sprintf('%7.0f %7.0f %7.0f',sag,cor,axi);
- org_str = sprintf('%7.0f %7.0f %7.0f', org(1), org(2), org(3));
- else
- string = sprintf('%7.1f %7.1f %7.1f',sag,cor,axi);
- org_str = sprintf('%7.1f %7.1f %7.1f', org(1), org(2), org(3));
- end;
-
- set(nii_view.handles.impos,'String',string);
- set(nii_view.handles.origin, 'string', org_str);
- return % set_image_value
- %----------------------------------------------------------------
- function nii_view = get_slice_position(nii_view,view),
- % obtain slices that is in correct unit, then update slices
- %
- slices = nii_view.slices;
-
- switch view,
- case 'sag',
- currentpoint = get(nii_view.handles.sagittal_axes,'CurrentPoint');
- slices.cor = currentpoint(1,1);
- slices.axi = currentpoint(1,2);
- case 'cor',
- currentpoint = get(nii_view.handles.coronal_axes,'CurrentPoint');
- slices.sag = currentpoint(1,1);
- slices.axi = currentpoint(1,2);
- case 'axi',
- currentpoint = get(nii_view.handles.axial_axes,'CurrentPoint');
- slices.sag = currentpoint(1,1);
- slices.cor = currentpoint(1,2);
- end
- % update nii_view.slices with the updated slices
- %
- nii_view.slices.axi = round(slices.axi);
- nii_view.slices.cor = round(slices.cor);
- nii_view.slices.sag = round(slices.sag);
- return % get_slice_position
- %----------------------------------------------------------------
- function nii_view = get_slider_position(nii_view),
- [nii_view.slices.sag,nii_view.slices.cor,nii_view.slices.axi] = deal(0);
-
- if isfield(nii_view.handles,'sagittal_slider'),
- if ishandle(nii_view.handles.sagittal_slider),
- nii_view.slices.sag = ...
- round(get(nii_view.handles.sagittal_slider,'Value'));
- end
- end
-
- if isfield(nii_view.handles,'coronal_slider'),
- if ishandle(nii_view.handles.coronal_slider),
- nii_view.slices.cor = ...
- round(nii_view.dims(2) - ...
- get(nii_view.handles.coronal_slider,'Value') + 1);
- end
- end
-
- if isfield(nii_view.handles,'axial_slider'),
- if ishandle(nii_view.handles.axial_slider),
- nii_view.slices.axi = ...
- round(get(nii_view.handles.axial_slider,'Value'));
- end
- end
- nii_view = check_slices(nii_view);
- return % get_slider_position
- %----------------------------------------------------------------
- function nii_view = update_imgXYZ(nii_view),
- nii_view.imgXYZ.vox = ...
- [nii_view.slices.sag,nii_view.slices.cor,nii_view.slices.axi];
- nii_view.imgXYZ.mm = ...
- (nii_view.imgXYZ.vox - nii_view.origin) .* nii_view.voxel_size;
- % nii_view.imgXYZ.tal = mni2tal(nii_view.imgXYZ.mni);
- return % update_imgXYZ
- %----------------------------------------------------------------
- function nii_view = convert2voxel(nii_view,slices),
- if get(nii_view.handles.coord,'value') == 1,
- % [slices.axi, slices.cor, slices.sag] are in vox
- %
- nii_view.slices.axi = round(slices.axi);
- nii_view.slices.cor = round(slices.cor);
- nii_view.slices.sag = round(slices.sag);
- elseif get(nii_view.handles.coord,'value') == 2,
- % [slices.axi, slices.cor, slices.sag] are in mm
- %
- xpix = nii_view.voxel_size(1);
- ypix = nii_view.voxel_size(2);
- zpix = nii_view.voxel_size(3);
- nii_view.slices.axi = round(slices.axi / zpix + nii_view.origin(3));
- nii_view.slices.cor = round(slices.cor / ypix + nii_view.origin(2));
- nii_view.slices.sag = round(slices.sag / xpix + nii_view.origin(1));
- elseif get(nii_view.handles.coord,'value') == 3,
- % [slices.axi, slices.cor, slices.sag] are in talairach
- %
- xpix = nii_view.voxel_size(1);
- ypix = nii_view.voxel_size(2);
- zpix = nii_view.voxel_size(3);
- xyz_tal = [slices.sag, slices.cor, slices.axi];
- xyz_mni = tal2mni(xyz_tal);
- nii_view.slices.axi = round(xyz_mni(3) / zpix + nii_view.origin(3));
- nii_view.slices.cor = round(xyz_mni(2) / ypix + nii_view.origin(2));
- nii_view.slices.sag = round(xyz_mni(1) / xpix + nii_view.origin(1));
- end
- return % convert2voxel
- %----------------------------------------------------------------
- function nii_view = check_slices(nii_view),
- img = nii_view.nii.img;
-
- [ SagSize, CorSize, AxiSize, TimeSize ] = size(img);
- if nii_view.slices.sag > SagSize, nii_view.slices.sag = SagSize; end;
- if nii_view.slices.sag < 1, nii_view.slices.sag = 1; end;
- if nii_view.slices.cor > CorSize, nii_view.slices.cor = CorSize; end;
- if nii_view.slices.cor < 1, nii_view.slices.cor = 1; end;
- if nii_view.slices.axi > AxiSize, nii_view.slices.axi = AxiSize; end;
- if nii_view.slices.axi < 1, nii_view.slices.axi = 1; end;
- if nii_view.scanid > TimeSize, nii_view.scanid = TimeSize; end;
- if nii_view.scanid < 1, nii_view.scanid = 1; end;
-
- return % check_slices
- %----------------------------------------------------------------
- %
- % keep this function small, since it will be called for every click
- %
- function nii_view = update_nii_view(nii_view)
- % add imgXYZ into nii_view struct
- %
- nii_view = check_slices(nii_view);
- nii_view = update_imgXYZ(nii_view);
- % update xhair
- %
- p_axi = nii_view.imgXYZ.vox([1 2]);
- p_cor = nii_view.imgXYZ.vox([1 3]);
- p_sag = nii_view.imgXYZ.vox([2 3]);
- nii_view.axi_xhair = ...
- rri_xhair(p_axi, nii_view.axi_xhair, nii_view.handles.axial_axes);
- nii_view.cor_xhair = ...
- rri_xhair(p_cor, nii_view.cor_xhair, nii_view.handles.coronal_axes);
- nii_view.sag_xhair = ...
- rri_xhair(p_sag, nii_view.sag_xhair, nii_view.handles.sagittal_axes);
- setappdata(nii_view.fig, 'nii_view', nii_view);
- set_image_value(nii_view);
- return; % update_nii_view
- %----------------------------------------------------------------
- function hist_plot(fig)
- nii_view = getappdata(fig,'nii_view');
- if isfield(nii_view, 'disp')
- img = nii_view.disp;
- else
- img = nii_view.nii.img;
- end
- img = double(img(:));
- if length(unique(round(img))) == length(unique(img))
- is_integer = 1;
- range = max(img) - min(img) + 1;
- figure; hist(img, range);
- set(gca, 'xlim', [-range/5, max(img)]);
- else
- is_integer = 0;
- figure; hist(img);
- end
- xlabel('Voxel Intensity');
- ylabel('Voxel Numbers for Each Intensity');
- set(gcf, 'NumberTitle','off','Name','Histogram Plot');
- return; % hist_plot
- %----------------------------------------------------------------
- function hist_eq(fig)
- nii_view = getappdata(fig,'nii_view');
- old_pointer = get(fig,'Pointer');
- set(fig,'Pointer','watch');
- if get(nii_view.handles.hist_eq,'value')
- max_img = double(max(nii_view.nii.img(:)));
- tmp = double(nii_view.nii.img) / max_img; % normalize for histeq
- tmp = histeq(tmp(:));
- nii_view.disp = reshape(tmp, size(nii_view.nii.img));
- min_disp = min(nii_view.disp(:));
- nii_view.disp = (nii_view.disp - min_disp); % range having eq hist
- nii_view.disp = nii_view.disp * max_img / max(nii_view.disp(:));
- nii_view.disp = single(nii_view.disp);
- else
- if isfield(nii_view, 'disp')
- nii_view.disp = nii_view.nii.img;
- else
- set(fig,'Pointer',old_pointer);
- return;
- end
- end
- % update axial view
- %
- img_slice = squeeze(double(nii_view.disp(:,:,nii_view.slices.axi)));
- h1 = nii_view.handles.axial_image;
- set(h1, 'cdata', double(img_slice)');
- % update coronal view
- %
- img_slice = squeeze(double(nii_view.disp(:,nii_view.slices.cor,:)));
- h1 = nii_view.handles.coronal_image;
- set(h1, 'cdata', double(img_slice)');
- % update sagittal view
- %
- img_slice = squeeze(double(nii_view.disp(nii_view.slices.sag,:,:)));
- h1 = nii_view.handles.sagittal_image;
- set(h1, 'cdata', double(img_slice)');
- % remove disp field if un-check 'histeq' button
- %
- if ~get(nii_view.handles.hist_eq,'value') & isfield(nii_view, 'disp')
- nii_view = rmfield(nii_view, 'disp');
- end
- update_nii_view(nii_view);
- set(fig,'Pointer',old_pointer);
- return; % hist_eq
- %----------------------------------------------------------------
- function [top1_label, top2_label, side1_label, side2_label] = ...
- dir_label(fig, top_ax, front_ax, side_ax)
- nii_view = getappdata(fig,'nii_view');
- top_pos = get(top_ax,'position');
- front_pos = get(front_ax,'position');
- side_pos = get(side_ax,'position');
- top_gap_x = (side_pos(1)-top_pos(1)-top_pos(3)) / (2*top_pos(3));
- top_gap_y = (front_pos(2)-top_pos(2)-top_pos(4)) / (2*top_pos(4));
- side_gap_x = (side_pos(1)-top_pos(1)-top_pos(3)) / (2*side_pos(3));
- side_gap_y = (front_pos(2)-top_pos(2)-top_pos(4)) / (2*side_pos(4));
- top1_label_pos = [0, 1]; % rot0
- top2_label_pos = [1, 0]; % rot90
- side1_label_pos = [1, - side_gap_y]; % rot0
- side2_label_pos = [0, 0]; % rot90
- if isempty(nii_view)
- axes(top_ax);
- top1_label = text(double(top1_label_pos(1)),double(top1_label_pos(2)), ...
- '== X =>', ...
- 'vertical', 'bottom', ...
- 'unit', 'normal', 'fontsize', 8);
- axes(top_ax);
- top2_label = text(double(top2_label_pos(1)),double(top2_label_pos(2)), ...
- '== Y =>', ...
- 'rotation', 90, 'vertical', 'top', ...
- 'unit', 'normal', 'fontsize', 8);
- axes(side_ax);
- side1_label = text(double(side1_label_pos(1)),double(side1_label_pos(2)), ...
- '<= Y ==', ...
- 'horizontal', 'right', 'vertical', 'top', ...
- 'unit', 'normal', 'fontsize', 8);
- axes(side_ax);
- side2_label = text(double(side2_label_pos(1)),double(side2_label_pos(2)), ...
- '== Z =>', ...
- 'rotation', 90, 'vertical', 'bottom', ...
- 'unit', 'normal', 'fontsize', 8);
- else
- top1_label = nii_view.handles.top1_label;
- top2_label = nii_view.handles.top2_label;
- side1_label = nii_view.handles.side1_label;
- side2_label = nii_view.handles.side2_label;
- set(top1_label, 'position', [top1_label_pos 0]);
- set(top2_label, 'position', [top2_label_pos 0]);
- set(side1_label, 'position', [side1_label_pos 0]);
- set(side2_label, 'position', [side2_label_pos 0]);
- end
- return; % dir_label
- %----------------------------------------------------------------
- function update_enable(h, opt);
- nii_view = getappdata(h,'nii_view');
- handles = nii_view.handles;
- if isfield(opt,'enablecursormove')
- if opt.enablecursormove
- v = 'on';
- else
- v = 'off';
- end
- set(handles.Timposcur, 'visible', v);
- set(handles.imposcur, 'visible', v);
- set(handles.Timvalcur, 'visible', v);
- set(handles.imvalcur, 'visible', v);
- end
- if isfield(opt,'enableviewpoint')
- if opt.enableviewpoint
- v = 'on';
- else
- v = 'off';
- end
- set(handles.Timpos, 'visible', v);
- set(handles.impos, 'visible', v);
- set(handles.Timval, 'visible', v);
- set(handles.imval, 'visible', v);
- end
- if isfield(opt,'enableorigin')
- if opt.enableorigin
- v = 'on';
- else
- v = 'off';
- end
- set(handles.Torigin, 'visible', v);
- set(handles.origin, 'visible', v);
- end
- if isfield(opt,'enableunit')
- if opt.enableunit
- v = 'on';
- else
- v = 'off';
- end
- set(handles.Tcoord, 'visible', v);
- set(handles.coord_frame, 'visible', v);
- set(handles.coord, 'visible', v);
- end
- if isfield(opt,'enablecrosshair')
- if opt.enablecrosshair
- v = 'on';
- else
- v = 'off';
- end
- set(handles.Txhair, 'visible', v);
- set(handles.xhair_color, 'visible', v);
- set(handles.xhair, 'visible', v);
- end
- if isfield(opt,'enablehistogram')
- if opt.enablehistogram
- v = 'on';
- vv = 'off';
- else
- v = 'off';
- vv = 'on';
- end
- set(handles.Tcoord, 'visible', vv);
- set(handles.coord_frame, 'visible', vv);
- set(handles.coord, 'visible', vv);
- set(handles.Thist, 'visible', v);
- set(handles.hist_frame, 'visible', v);
- set(handles.hist_eq, 'visible', v);
- set(handles.hist_plot, 'visible', v);
- end
- if isfield(opt,'enablecolormap')
- if opt.enablecolormap
- v = 'on';
- else
- v = 'off';
- end
- set(handles.Tcolor, 'visible', v);
- set(handles.color_frame, 'visible', v);
- set(handles.neg_color, 'visible', v);
- set(handles.colorindex, 'visible', v);
- end
- if isfield(opt,'enablecontrast')
- if opt.enablecontrast
- v = 'on';
- else
- v = 'off';
- end
- set(handles.Tcontrast, 'visible', v);
- set(handles.contrast_frame, 'visible', v);
- set(handles.contrast_def, 'visible', v);
- set(handles.contrast, 'visible', v);
- end
- if isfield(opt,'enablebrightness')
- if opt.enablebrightness
- v = 'on';
- else
- v = 'off';
- end
- set(handles.Tbrightness, 'visible', v);
- set(handles.brightness_frame, 'visible', v);
- set(handles.brightness_def, 'visible', v);
- set(handles.brightness, 'visible', v);
- end
- if isfield(opt,'enabledirlabel')
- if opt.enabledirlabel
- v = 'on';
- else
- v = 'off';
- end
- set(handles.top1_label, 'visible', v);
- set(handles.top2_label, 'visible', v);
- set(handles.side1_label, 'visible', v);
- set(handles.side2_label, 'visible', v);
- end
- if isfield(opt,'enableslider')
- if opt.enableslider
- v = 'on';
- else
- v = 'off';
- end
- if isfield(handles,'sagittal_slider') & ishandle(handles.sagittal_slider)
- set(handles.sagittal_slider, 'visible', v);
- end
- if isfield(handles,'coronal_slider') & ishandle(handles.coronal_slider)
- set(handles.coronal_slider, 'visible', v);
- end
- if isfield(handles,'axial_slider') & ishandle(handles.axial_slider)
- set(handles.axial_slider, 'visible', v);
- end
- end
- return; % update_enable
- %----------------------------------------------------------------
- function update_usepanel(fig, usepanel)
- if isempty(usepanel)
- return;
- end
- if usepanel
- opt.enablecursormove = 1;
- opt.enableviewpoint = 1;
- opt.enableorigin = 1;
- opt.enableunit = 1;
- opt.enablecrosshair = 1;
- % opt.enablehistogram = 1;
- opt.enablecolormap = 1;
- opt.enablecontrast = 1;
- opt.enablebrightness = 1;
- else
- opt.enablecursormove = 0;
- opt.enableviewpoint = 0;
- opt.enableorigin = 0;
- opt.enableunit = 0;
- opt.enablecrosshair = 0;
- % opt.enablehistogram = 0;
- opt.enablecolormap = 0;
- opt.enablecontrast = 0;
- opt.enablebrightness = 0;
- end
- update_enable(fig, opt);
- nii_view = getappdata(fig,'nii_view');
- nii_view.usepanel = usepanel;
- setappdata(fig,'nii_view',nii_view);
- return; % update_usepanel
- %----------------------------------------------------------------
- function update_usecrosshair(fig, usecrosshair)
- if isempty(usecrosshair)
- return;
- end
- if usecrosshair
- v=1;
- else
- v=2;
- end
- nii_view = getappdata(fig,'nii_view');
- set(nii_view.handles.xhair,'value',v);
- opt.command = 'crosshair';
- view_nii(fig, opt);
- return; % update_usecrosshair
- %----------------------------------------------------------------
- function update_usestretch(fig, usestretch)
- nii_view = getappdata(fig,'nii_view');
- handles = nii_view.handles;
- fig = nii_view.fig;
- area = nii_view.area;
- vol_size = nii_view.voxel_size .* nii_view.dims;
- % Three Axes & label
- %
- [top_ax, front_ax, side_ax] = ...
- create_ax(fig, area, vol_size, usestretch);
- dir_label(fig, top_ax, front_ax, side_ax);
- top_pos = get(top_ax,'position');
- front_pos = get(front_ax,'position');
- side_pos = get(side_ax,'position');
- % Sagittal Slider
- %
- x = side_pos(1);
- y = top_pos(2) + top_pos(4);
- w = side_pos(3);
- h = (front_pos(2) - y) / 2;
- y = y + h;
- pos = [x y w h];
- if isfield(handles,'sagittal_slider') & ishandle(handles.sagittal_slider)
- set(handles.sagittal_slider,'position',pos);
- end
- % Coronal Slider
- %
- x = top_pos(1);
- y = top_pos(2) + top_pos(4);
- w = top_pos(3);
- h = (front_pos(2) - y) / 2;
- y = y + h;
- pos = [x y w h];
- if isfield(handles,'coronal_slider') & ishandle(handles.coronal_slider)
- set(handles.coronal_slider,'position',pos);
- end
- % Axial Slider
- %
- x = top_pos(1);
- y = area(2);
- w = top_pos(3);
- h = top_pos(2) - y;
- pos = [x y w h];
- if isfield(handles,'axial_slider') & ishandle(handles.axial_slider)
- set(handles.axial_slider,'position',pos);
- end
- % plot info view
- %
- % info_pos = [side_pos([1,3]); top_pos([2,4])];
- % info_pos = info_pos(:);
- gap = side_pos(1)-(top_pos(1)+top_pos(3));
- info_pos(1) = side_pos(1) + gap;
- info_pos(2) = area(2);
- info_pos(3) = side_pos(3) - gap;
- info_pos(4) = top_pos(2) + top_pos(4) - area(2) - gap;
- num_inputline = 10;
- inputline_space =info_pos(4) / num_inputline;
- % Image Intensity Value at Cursor
- %
- x = info_pos(1);
- y = info_pos(2);
- w = info_pos(3)*0.5;
- h = inputline_space*0.6;
- pos = [x y w h];
- set(handles.Timvalcur,'position',pos);
- x = x + w;
- w = info_pos(3)*0.5;
- pos = [x y w h];
- set(handles.imvalcur,'position',pos);
- % Position at Cursor
- %
- x = info_pos(1);
- y = y + inputline_space;
- w = info_pos(3)*0.5;
- pos = [x y w h];
- set(handles.Timposcur,'position',pos);
- x = x + w;
- w = info_pos(3)*0.5;
- pos = [x y w h];
- set(handles.imposcur,'position',pos);
- % Image Intensity Value at Mouse Click
- %
- x = info_pos(1);
- y = y + inputline_space;
- w = info_pos(3)*0.5;
- pos = [x y w h];
- set(handles.Timval,'position',pos);
- x = x + w;
- w = info_pos(3)*0.5;
- pos = [x y w h];
- set(handles.imval,'position',pos);
- % Viewpoint Position at Mouse Click
- %
- x = info_pos(1);
- y = y + inputline_space;
- w = info_pos(3)*0.5;
- pos = [x y w h];
- set(handles.Timpos,'position',pos);
- x = x + w + 0.005;
- y = y - 0.008;
- w = info_pos(3)*0.5;
- h = inputline_space*0.9;
- pos = [x y w h];
- set(handles.impos,'position',pos);
- % Origin Position
- %
- x = info_pos(1);
- y = y + inputline_space*1.2;
- w = info_pos(3)*0.5;
- h = inputline_space*0.6;
- pos = [x y w h];
- set(handles.Torigin,'position',pos);
- x = x + w;
- w = info_pos(3)*0.5;
- pos = [x y w h];
- set(handles.origin,'position',pos);
- if 0
- % Axes Unit
- %
- x = info_pos(1);
- y = y + inputline_space;
- w = info_pos(3)*0.5;
- pos = [x y w h];
- set(handles.Tcoord,'position',pos);
- x = x + w + 0.005;
- w = info_pos(3)*0.5 - 0.005;
- pos = [x y w h];
- set(handles.coord,'position',pos);
- end
- % Crosshair
- %
- x = info_pos(1);
- y = y + inputline_space;
- w = info_pos(3)*0.4;
- pos = [x y w h];
- set(handles.Txhair,'position',pos);
- x = info_pos(1) + info_pos(3)*0.5;
- w = info_pos(3)*0.2;
- h = inputline_space*0.7;
- pos = [x y w h];
- set(handles.xhair_color,'position',pos);
- x = info_pos(1) + info_pos(3)*0.7;
- w = info_pos(3)*0.3;
- pos = [x y w h];
- set(handles.xhair,'position',pos);
- % Histogram & Color
- %
- x = info_pos(1);
- w = info_pos(3)*0.45;
- h = inputline_space * 1.5;
- pos = [x, y+inputline_space*0.9, w, h];
- set(handles.hist_frame,'position',pos);
- set(handles.coord_frame,'position',pos);
- x = info_pos(1) + info_pos(3)*0.475;
- w = info_pos(3)*0.525;
- h = inputline_space * 1.5;
- pos = [x, y+inputline_space*0.9, w, h];
- set(handles.color_frame,'position',pos);
- x = info_pos(1) + info_pos(3)*0.025;
- y = y + inputline_space*1.2;
- w = info_pos(3)*0.2;
- h = inputline_space*0.7;
- pos = [x y w h];
- set(handles.hist_eq,'position',pos);
- x = x + w;
- w = info_pos(3)*0.2;
- pos = [x y w h];
- set(handles.hist_plot,'position',pos);
- x = info_pos(1) + info_pos(3)*0.025;
- w = info_pos(3)*0.4;
- pos = [x y w h];
- set(handles.coord,'position',pos);
- x = info_pos(1) + info_pos(3)*0.5;
- w = info_pos(3)*0.2;
- pos = [x y w h];
- set(handles.neg_color,'position',pos);
- x = info_pos(1) + info_pos(3)*0.7;
- w = info_pos(3)*0.275;
- pos = [x y w h];
- set(handles.colorindex,'position',pos);
- x = info_pos(1) + info_pos(3)*0.1;
- y = y + inputline_space;
- w = info_pos(3)*0.28;
- h = inputline_space*0.6;
- pos = [x y w h];
- set(handles.Thist,'position',pos);
- set(handles.Tcoord,'position',pos);
- x = info_pos(1) + info_pos(3)*0.60;
- w = info_pos(3)*0.28;
- pos = [x y w h];
- set(handles.Tcolor,'position',pos);
- % Contrast Frame
- %
- x = info_pos(1);
- w = info_pos(3)*0.45;
- h = inputline_space * 2;
- pos = [x, y+inputline_space*0.8, w, h];
- set(handles.contrast_frame,'position',pos);
- % Brightness Frame
- %
- x = info_pos(1) + info_pos(3)*0.475;
- w = info_pos(3)*0.525;
- pos = [x, y+inputline_space*0.8, w, h];
- set(handles.brightness_frame,'position',pos);
- % Contrast
- %
- x = info_pos(1) + info_pos(3)*0.025;
- y = y + inputline_space;
- w = info_pos(3)*0.4;
- h = inputline_space*0.6;
- pos = [x y w h];
- set(handles.contrast,'position',pos);
- % Brightness
- %
- x = info_pos(1) + info_pos(3)*0.5;
- w = info_pos(3)*0.475;
- pos = [x y w h];
- set(handles.brightness,'position',pos);
- % Contrast text/def
- %
- x = info_pos(1) + info_pos(3)*0.025;
- y = y + inputline_space;
- w = info_pos(3)*0.22;
- pos = [x y w h];
- set(handles.Tcontrast,'position',pos);
- x = x + w;
- w = info_pos(3)*0.18;
- pos = [x y w h];
- set(handles.contrast_def,'position',pos);
- % Brightness text/def
- %
- x = info_pos(1) + info_pos(3)*0.5;
- w = info_pos(3)*0.295;
- pos = [x y w h];
- set(handles.Tbrightness,'position',pos);
- x = x + w;
- w = info_pos(3)*0.18;
- pos = [x y w h];
- set(handles.brightness_def,'position',pos);
- return; % update_usestretch
- %----------------------------------------------------------------
- function update_useinterp(fig, useinterp)
- if isempty(useinterp)
- return;
- end
- nii_menu = getappdata(fig, 'nii_menu');
- if ~isempty(nii_menu)
- if get(nii_menu.Minterp,'user')
- set(nii_menu.Minterp,'Userdata',0,'Label','Interp off');
- else
- set(nii_menu.Minterp,'Userdata',1,'Label','Interp on');
- end
- end
- nii_view = getappdata(fig, 'nii_view');
- nii_view.useinterp = useinterp;
- if ~isempty(nii_view.handles.axial_image)
- if strcmpi(get(nii_view.handles.axial_image,'cdatamapping'), 'direct')
- useimagesc = 0;
- else
- useimagesc = 1;
- end
- elseif ~isempty(nii_view.handles.coronal_image)
- if strcmpi(get(nii_view.handles.coronal_image,'cdatamapping'), 'direct')
- useimagesc = 0;
- else
- useimagesc = 1;
- end
- else
- if strcmpi(get(nii_view.handles.sagittal_image,'cdatamapping'), 'direct')
- useimagesc = 0;
- else
- useimagesc = 1;
- end
- end
- if ~isempty(nii_view.handles.axial_image)
- img_slice = get(nii_view.handles.axial_image, 'cdata');
- delete(nii_view.handles.axial_image);
- axes(nii_view.handles.axial_axes);
- clim = get(gca,'clim');
- if useinterp
- if useimagesc
- nii_view.handles.axial_image = surface(zeros(size(img_slice)),double(img_slice),'edgecolor','none','facecolor','interp');
- else
- nii_view.handles.axial_image = surface(zeros(size(img_slice)),double(img_slice),'cdatamapping','direct','edgecolor','none','facecolor','interp');
- end
- else
- if useimagesc
- nii_view.handles.axial_image = imagesc('cdata',img_slice);
- else
- nii_view.handles.axial_image = image('cdata',img_slice);
- end
- end
- set(gca,'clim',clim);
- order = get(gca,'child');
- order(find(order == nii_view.handles.axial_image)) = [];
- order = [order; nii_view.handles.axial_image];
- if isfield(nii_view.handles,'axial_bg') & ~isempty(nii_view.handles.axial_bg)
- order(find(order == nii_view.handles.axial_bg)) = [];
- order = [order; nii_view.handles.axial_bg];
- end
- set(gca, 'child', order);
- if ~useinterp
- if isfield(nii_view.handles,'axial_bg') & ~isempty(nii_view.handles.axial_bg)
- delete(nii_view.handles.axial_bg);
- nii_view.handles.axial_bg = [];
- end
- end
- set(nii_view.handles.axial_image,'buttondown','view_nii(''axial_image'');');
- end
- if ~isempty(nii_view.handles.coronal_image)
- img_slice = get(nii_view.handles.coronal_image, 'cdata');
- delete(nii_view.handles.coronal_image);
- axes(nii_view.handles.coronal_axes);
- clim = get(gca,'clim');
- if useinterp
- if useimagesc
- nii_view.handles.coronal_image = surface(zeros(size(img_slice)),double(img_slice),'edgecolor','none','facecolor','interp');
- else
- nii_view.handles.coronal_image = surface(zeros(size(img_slice)),double(img_slice),'cdatamapping','direct','edgecolor','none','facecolor','interp');
- end
- else
- if useimagesc
- nii_view.handles.coronal_image = imagesc('cdata',img_slice);
- else
- nii_view.handles.coronal_image = image('cdata',img_slice);
- end
- end
- set(gca,'clim',clim);
- order = get(gca,'child');
- order(find(order == nii_view.handles.coronal_image)) = [];
- order = [order; nii_view.handles.coronal_image];
- if isfield(nii_view.handles,'coronal_bg') & ~isempty(nii_view.handles.coronal_bg)
- order(find(order == nii_view.handles.coronal_bg)) = [];
- order = [order; nii_view.handles.coronal_bg];
- end
- set(gca, 'child', order);
- if ~useinterp
- if isfield(nii_view.handles,'coronal_bg') & ~isempty(nii_view.handles.coronal_bg)
- delete(nii_view.handles.coronal_bg);
- nii_view.handles.coronal_bg = [];
- end
- end
- set(nii_view.handles.coronal_image,'buttondown','view_nii(''coronal_image'');');
- end
- if ~isempty(nii_view.handles.sagittal_image)
- img_slice = get(nii_view.handles.sagittal_image, 'cdata');
- delete(nii_view.handles.sagittal_image);
- axes(nii_view.handles.sagittal_axes);
- clim = get(gca,'clim');
- if useinterp
- if useimagesc
- nii_view.handles.sagittal_image = surface(zeros(size(img_slice)),double(img_slice),'edgecolor','none','facecolor','interp');
- else
- nii_view.handles.sagittal_image = surface(zeros(size(img_slice)),double(img_slice),'cdatamapping','direct','edgecolor','none','facecolor','interp');
- end
- else
- if useimagesc
- nii_view.handles.sagittal_image = imagesc('cdata',img_slice);
- else
- nii_view.handles.sagittal_image = image('cdata',img_slice);
- end
- end
- set(gca,'clim',clim);
- order = get(gca,'child');
- order(find(order == nii_view.handles.sagittal_image)) = [];
- order = [order; nii_view.handles.sagittal_image];
- if isfield(nii_view.handles,'sagittal_bg') & ~isempty(nii_view.handles.sagittal_bg)
- order(find(order == nii_view.handles.sagittal_bg)) = [];
- order = [order; nii_view.handles.sagittal_bg];
- end
- set(gca, 'child', order);
- if ~useinterp
- if isfield(nii_view.handles,'sagittal_bg') & ~isempty(nii_view.handles.sagittal_bg)
- delete(nii_view.handles.sagittal_bg);
- nii_view.handles.sagittal_bg = [];
- end
- end
- set(nii_view.handles.sagittal_image,'buttondown','view_nii(''sagittal_image'');');
- end
- if ~useinterp
- nii_view.bgimg = [];
- end
- set_coordinates(nii_view,useinterp);
- setappdata(fig, 'nii_view', nii_view);
- return; % update_useinterp
- %----------------------------------------------------------------
- function update_useimagesc(fig, useimagesc)
- if isempty(useimagesc)
- return;
- end
- if useimagesc
- v='scaled';
- else
- v='direct';
- end
- nii_view = getappdata(fig,'nii_view');
- handles = nii_view.handles;
- if isfield(handles,'cbar_image') & ishandle(handles.cbar_image)
- % set(handles.cbar_image,'cdatamapping',v);
- end
- set(handles.axial_image,'cdatamapping',v);
- set(handles.coronal_image,'cdatamapping',v);
- set(handles.sagittal_image,'cdatamapping',v);
- return; % update_useimagesc
- %----------------------------------------------------------------
- function update_shape(fig, area, usecolorbar, usestretch, useimagesc)
- nii_view = getappdata(fig,'nii_view');
- if isempty(usestretch) % no change, get usestretch
- stretchchange = 0;
- usestretch = nii_view.usestretch;
- else % change, set usestretch
- stretchchange = 1;
- nii_view.usestretch = usestretch;
- end
- if isempty(area) % no change, get area
- areachange = 0;
- area = nii_view.area;
- elseif ~isempty(nii_view.cbar_area) % change, set area & cbar_area
- areachange = 1;
- cbar_area = area;
- cbar_area(1) = area(1) + area(3)*0.93;
- cbar_area(3) = area(3)*0.04;
- area(3) = area(3)*0.9; % 90% used for main axes
- [cbar_axes cbarminmax_axes] = create_cbar_axes(fig, cbar_area);
- nii_view.area = area;
- nii_view.cbar_area = cbar_area;
- else % change, set area only
- areachange = 1;
- nii_view.area = area;
- end
- % Add colorbar
- %
- if ~isempty(usecolorbar) & usecolorbar & isempty(nii_view.cbar_area)
- colorbarchange = 1;
- cbar_area = area;
- cbar_area(1) = area(1) + area(3)*0.93;
- cbar_area(3) = area(3)*0.04;
- area(3) = area(3)*0.9; % 90% used for main axes
- % create axes for colorbar
- %
- [cbar_axes cbarminmax_axes] = create_cbar_axes(fig, cbar_area);
- nii_view.area = area;
- nii_view.cbar_area = cbar_area;
- % useimagesc follows axial image
- %
- if isempty(useimagesc)
- if strcmpi(get(nii_view.handles.axial_image,'cdatamap'),'scaled')
- useimagesc = 1;
- else
- useimagesc = 0;
- end
- end
- if isfield(nii_view, 'highcolor') & ~isempty(highcolor)
- num_highcolor = size(nii_view.highcolor,1);
- else
- num_highcolor = 0;
- end
- if isfield(nii_view, 'colorlevel') & ~isempty(nii_view.colorlevel)
- colorlevel = nii_view.colorlevel;
- else
- colorlevel = 256 - num_highcolor;
- end
- if isfield(nii_view, 'color_map')
- color_map = nii_view.color_map;
- else
- color_map = [];
- end
- if isfield(nii_view, 'highcolor')
- highcolor = nii_view.highcolor;
- else
- highcolor = [];
- end
- % plot colorbar
- %
- if 0
- if isempty(color_map)
- level = colorlevel + num_highcolor;
- else
- level = size([color_map; highcolor], 1);
- end
- end
- if isempty(color_map)
- level = colorlevel;
- else
- level = size([color_map], 1);
- end
- cbar_image = [1:level]';
- niiclass = class(nii_view.nii.img);
- h1 = plot_cbar(fig, cbar_axes, cbarminmax_axes, nii_view.cbarminmax, ...
- level, nii_view.handles, useimagesc, nii_view.colorindex, ...
- color_map, colorlevel, highcolor, niiclass, nii_view.numscan);
- nii_view.handles.cbar_image = h1;
- nii_view.handles.cbar_axes = cbar_axes;
- nii_view.handles.cbarminmax_axes = cbar_axes;
- % remove colorbar
- %
- elseif ~isempty(usecolorbar) & ~usecolorbar & ~isempty(nii_view.cbar_area)
- colorbarchange = 1;
- area(3) = area(3) / 0.9;
- nii_view.area = area;
- nii_view.cbar_area = [];
- nii_view.handles = rmfield(nii_view.handles,'cbar_image');
- delete(nii_view.handles.cbarminmax_axes);
- nii_view.handles = rmfield(nii_view.handles,'cbarminmax_axes');
- delete(nii_view.handles.cbar_axes);
- nii_view.handles = rmfield(nii_view.handles,'cbar_axes');
- else
- colorbarchange = 0;
- end
- if colorbarchange | stretchchange | areachange
- setappdata(fig,'nii_view',nii_view);
- update_usestretch(fig, usestretch);
- end
- return; % update_shape
- %----------------------------------------------------------------
- function update_unit(fig, setunit)
- if isempty(setunit)
- return;
- end
- if strcmpi(setunit,'mm') | strcmpi(setunit,'millimeter') | strcmpi(setunit,'mni')
- v = 2;
- % elseif strcmpi(setunit,'tal') | strcmpi(setunit,'talairach')
- % v = 3;
- elseif strcmpi(setunit,'vox') | strcmpi(setunit,'voxel')
- v = 1;
- else
- v = 1;
- end
- nii_view = getappdata(fig,'nii_view');
- set(nii_view.handles.coord, 'value', v);
- set_image_value(nii_view);
- return; % update_unit
- %----------------------------------------------------------------
- function update_viewpoint(fig, setviewpoint)
- if isempty(setviewpoint)
- return;
- end
- nii_view = getappdata(fig,'nii_view');
- if length(setviewpoint) ~= 3
- error('Viewpoint position should contain [x y z]');
- end
- set(nii_view.handles.impos,'string',num2str(setviewpoint));
- opt.command = 'impos_edit';
- view_nii(fig, opt);
- set(nii_view.handles.axial_axes,'selected','on');
- set(nii_view.handles.axial_axes,'selected','off');
- set(nii_view.handles.coronal_axes,'selected','on');
- set(nii_view.handles.coronal_axes,'selected','off');
- set(nii_view.handles.sagittal_axes,'selected','on');
- set(nii_view.handles.sagittal_axes,'selected','off');
- return; % update_viewpoint
- %----------------------------------------------------------------
- function update_scanid(fig, setscanid)
- if isempty(setscanid)
- return;
- end
- nii_view = getappdata(fig,'nii_view');
- if setscanid < 1
- setscanid = 1;
- end
- if setscanid > nii_view.numscan
- setscanid = nii_view.numscan;
- end
- set(nii_view.handles.contrast_def,'string',num2str(setscanid));
- set(nii_view.handles.contrast,'value',setscanid);
- opt.command = 'updateimg';
- opt.setscanid = setscanid;
- view_nii(fig, nii_view.nii.img, opt);
- return; % update_scanid
- %----------------------------------------------------------------
- function update_crosshaircolor(fig, new_color)
- if isempty(new_color)
- return;
- end
- nii_view = getappdata(fig,'nii_view');
- xhair_color = nii_view.handles.xhair_color;
- set(xhair_color,'user',new_color);
- set(nii_view.axi_xhair.lx,'color',new_color);
- set(nii_view.axi_xhair.ly,'color',new_color);
- set(nii_view.cor_xhair.lx,'color',new_color);
- set(nii_view.cor_xhair.ly,'color',new_color);
- set(nii_view.sag_xhair.lx,'color',new_color);
- set(nii_view.sag_xhair.ly,'color',new_color);
- return; % update_crosshaircolor
- %----------------------------------------------------------------
- function update_colorindex(fig, colorindex)
- if isempty(colorindex)
- return;
- end
- nii_view = getappdata(fig,'nii_view');
- nii_view.colorindex = colorindex;
- setappdata(fig, 'nii_view', nii_view);
- set(nii_view.handles.colorindex,'value',colorindex);
- opt.command = 'color';
- view_nii(fig, opt);
- return; % update_colorindex
- %----------------------------------------------------------------
- function redraw_cbar(fig, colorlevel, color_map, highcolor)
- nii_view = getappdata(fig,'nii_view');
- if isempty(nii_view.cbar_area)
- return;
- end
- colorindex = nii_view.colorindex;
- if isempty(highcolor)
- num_highcolor = 0;
- else
- num_highcolor = size(highcolor,1);
- end
- if isempty(colorlevel)
- colorlevel=256;
- end
- if colorindex == 1
- colorlevel = size(color_map, 1);
- end
- % level = colorlevel + num_highcolor;
- level = colorlevel;
- cbar_image = [1:level]';
- cbar_area = nii_view.cbar_area;
- % useimagesc follows axial image
- %
- if strcmpi(get(nii_view.handles.axial_image,'cdatamap'),'scaled')
- useimagesc = 1;
- else
- useimagesc = 0;
- end
- niiclass = class(nii_view.nii.img);
- delete(nii_view.handles.cbar_image);
- delete(nii_view.handles.cbar_axes);
- delete(nii_view.handles.cbarminmax_axes);
- [nii_view.handles.cbar_axes nii_view.handles.cbarminmax_axes] = ...
- create_cbar_axes(fig, cbar_area, []);
- nii_view.handles.cbar_image = plot_cbar(fig, ...
- nii_view.handles.cbar_axes, nii_view.handles.cbarminmax_axes, ...
- nii_view.cbarminmax, level, nii_view.handles, useimagesc, ...
- colorindex, color_map, colorlevel, highcolor, niiclass, ...
- nii_view.numscan, []);
- setappdata(fig, 'nii_view', nii_view);
- return; % redraw_cbar
- %----------------------------------------------------------------
- function update_buttondown(fig, setbuttondown)
- if isempty(setbuttondown)
- return;
- end
- nii_view = getappdata(fig,'nii_view');
- nii_view.buttondown = setbuttondown;
- setappdata(fig, 'nii_view', nii_view);
- return; % update_buttondown
- %----------------------------------------------------------------
- function update_cbarminmax(fig, cbarminmax)
- if isempty(cbarminmax)
- return;
- end
- nii_view = getappdata(fig, 'nii_view');
- if ~isfield(nii_view.handles, 'cbarminmax_axes')
- return;
- end
- nii_view.cbarminmax = cbarminmax;
- setappdata(fig, 'nii_view', nii_view);
- axes(nii_view.handles.cbarminmax_axes);
- plot([0 0], cbarminmax, 'w');
- axis tight;
- set(nii_view.handles.cbarminmax_axes,'YDir','normal', ...
- 'XLimMode','manual','YLimMode','manual','YColor',[0 0 0], ...
- 'XColor',[0 0 0],'xtick',[],'YAxisLocation','right');
- ylim = get(nii_view.handles.cbar_axes,'ylim');
- ylimb = get(nii_view.handles.cbarminmax_axes,'ylim');
- ytickb = get(nii_view.handles.cbarminmax_axes,'ytick');
- ytick=(ylim(2)-ylim(1))*(ytickb-ylimb(1))/(ylimb(2)-ylimb(1))+ylim(1);
- axes(nii_view.handles.cbar_axes);
- set(nii_view.handles.cbar_axes,'YDir','normal','XLimMode','manual', ...
- 'YLimMode','manual','YColor',[0 0 0],'XColor',[0 0 0],'xtick',[], ...
- 'YAxisLocation','right','ylim',ylim,'ytick',ytick,'yticklabel','');
- return; % update_cbarminmax
- %----------------------------------------------------------------
- function update_highcolor(fig, highcolor, colorlevel)
- nii_view = getappdata(fig,'nii_view');
- if ischar(highcolor) & (isempty(colorlevel) | nii_view.colorindex == 1)
- return;
- end
- if ~ischar(highcolor)
- nii_view.highcolor = highcolor;
- if isempty(highcolor)
- nii_view = rmfield(nii_view, 'highcolor');
- end
- else
- highcolor = [];
- end
- if isempty(colorlevel) | nii_view.colorindex == 1
- nii_view.colorlevel = nii_view.colorlevel - size(highcolor,1);
- else
- nii_view.colorlevel = colorlevel;
- end
- setappdata(fig, 'nii_view', nii_view);
- if isfield(nii_view,'color_map')
- color_map = nii_view.color_map;
- else
- color_map = [];
- end
- redraw_cbar(fig, nii_view.colorlevel, color_map, highcolor);
- change_colormap(fig);
- return; % update_highcolor
- %----------------------------------------------------------------
- function update_colormap(fig, color_map)
- if ischar(color_map)
- return;
- end
- nii_view = getappdata(fig,'nii_view');
- nii = nii_view.nii;
- minvalue = nii_view.minvalue;
- if isempty(color_map)
- if minvalue < 0
- colorindex = 2;
- else
- colorindex = 3;
- end
- nii_view = rmfield(nii_view, 'color_map');
- setappdata(fig,'nii_view',nii_view);
- update_colorindex(fig, colorindex);
- return;
- else
- colorindex = 1;
- nii_view.color_map = color_map;
- nii_view.colorindex = colorindex;
- setappdata(fig,'nii_view',nii_view);
- set(nii_view.handles.colorindex,'value',colorindex);
- end
- colorlevel = nii_view.colorlevel;
- if isfield(nii_view, 'highcolor')
- highcolor = nii_view.highcolor;
- else
- highcolor = [];
- end
- redraw_cbar(fig, colorlevel, color_map, highcolor);
- change_colormap(fig);
- opt.enablecontrast = 0;
- update_enable(fig, opt);
- return; % update_colormap
- %----------------------------------------------------------------
- function status = get_status(h);
- nii_view = getappdata(h,'nii_view');
- status.fig = h;
- status.area = nii_view.area;
- if isempty(nii_view.cbar_area)
- status.usecolorbar = 0;
- else
- status.usecolorbar = 1;
- width = status.area(3) / 0.9;
- status.area(3) = width;
- end
- if strcmpi(get(nii_view.handles.imval,'visible'), 'on')
- status.usepanel = 1;
- else
- status.usepanel = 0;
- end
- if get(nii_view.handles.xhair,'value') == 1
- status.usecrosshair = 1;
- else
- status.usecrosshair = 0;
- end
- status.usestretch = nii_view.usestretch;
- if strcmpi(get(nii_view.handles.axial_image,'cdatamapping'), 'direct')
- status.useimagesc = 0;
- else
- status.useimagesc = 1;
- end
- status.useinterp = nii_view.useinterp;
- if get(nii_view.handles.coord,'value') == 1
- status.unit = 'vox';
- elseif get(nii_view.handles.coord,'value') == 2
- status.unit = 'mm';
- elseif get(nii_view.handles.coord,'value') == 3
- status.unit = 'tal';
- end
- status.viewpoint = get(nii_view.handles.impos,'value');
- status.scanid = nii_view.scanid;
- status.intensity = get(nii_view.handles.imval,'value');
- status.colorindex = get(nii_view.handles.colorindex,'value');
- if isfield(nii_view,'color_map')
- status.colormap = nii_view.color_map;
- else
- status.colormap = [];
- end
- status.colorlevel = nii_view.colorlevel;
- if isfield(nii_view,'highcolor')
- status.highcolor = nii_view.highcolor;
- else
- status.highcolor = [];
- end
- status.cbarminmax = nii_view.cbarminmax;
- status.buttondown = nii_view.buttondown;
- return; % get_status
- %----------------------------------------------------------------
- function [custom_color_map, colorindex] ...
- = change_colormap(fig, nii, colorindex, cbarminmax)
- custom_color_map = [];
- if ~exist('nii', 'var')
- nii_view = getappdata(fig,'nii_view');
- else
- nii_view = nii;
- end
- if ~exist('colorindex', 'var')
- colorindex = get(nii_view.handles.colorindex,'value');
- end
- if ~exist('cbarminmax', 'var')
- cbarminmax = nii_view.cbarminmax;
- end
- if isfield(nii_view, 'highcolor') & ~isempty(nii_view.highcolor)
- highcolor = nii_view.highcolor;
- num_highcolor = size(highcolor,1);
- else
- highcolor = [];
- num_highcolor = 0;
- end
- % if isfield(nii_view, 'colorlevel') & ~isempty(nii_view.colorlevel)
- if nii_view.colorlevel < 256
- num_color = nii_view.colorlevel;
- else
- num_color = 256 - num_highcolor;
- end
- contrast = [];
- if colorindex == 3 % for gray
- if nii_view.numscan > 1
- contrast = 1;
- else
- contrast = (num_color-1)*(get(nii_view.handles.contrast,'value')-1)/255+1;
- contrast = floor(contrast);
- end
- elseif colorindex == 2 % for bipolar
- if nii_view.numscan > 1
- contrast = 128;
- else
- contrast = get(nii_view.handles.contrast,'value');
- end
- end
- if isfield(nii_view,'color_map') & ~isempty(nii_view.color_map)
- color_map = nii_view.color_map;
- custom_color_map = color_map;
- elseif colorindex == 1
- [f p] = uigetfile('*.txt', 'Input colormap text file');
- if p==0
- colorindex = nii_view.colorindex;
- set(nii_view.handles.colorindex,'value',colorindex);
- return;
- end;
- try
- custom_color_map = load(fullfile(p,f));
- loadfail = 0;
- catch
- loadfail = 1;
- end
- if loadfail | isempty(custom_color_map) | size(custom_color_map,2)~=3 ...
- | min(custom_color_map(:)) < 0 | max(custom_color_map(:)) > 1
- msg = 'Colormap should be a Mx3 matrix with value between 0 and 1';
- msgbox(msg,'Error in colormap file');
- colorindex = nii_view.colorindex;
- set(nii_view.handles.colorindex,'value',colorindex);
- return;
- end
- color_map = custom_color_map;
- nii_view.color_map = color_map;
- end
- switch colorindex
- case {2}
- color_map = bipolar(num_color, cbarminmax(1), cbarminmax(2), contrast);
- case {3}
- color_map = gray(num_color - contrast + 1);
- case {4}
- color_map = jet(num_color);
- case {5}
- color_map = cool(num_color);
- case {6}
- color_map = bone(num_color);
- case {7}
- color_map = hot(num_color);
- case {8}
- color_map = copper(num_color);
- case {9}
- color_map = pink(num_color);
- end
- nii_view.colorindex = colorindex;
- if ~exist('nii', 'var')
- setappdata(fig,'nii_view',nii_view);
- end
- if colorindex == 3
- color_map = [zeros(contrast,3); color_map(2:end,:)];
- end
- if get(nii_view.handles.neg_color,'value') & isempty(highcolor)
- color_map = flipud(color_map);
- elseif get(nii_view.handles.neg_color,'value') & ~isempty(highcolor)
- highcolor = flipud(highcolor);
- end
- brightness = get(nii_view.handles.brightness,'value');
- color_map = brighten(color_map, brightness);
- color_map = [color_map; highcolor];
- set(fig, 'colormap', color_map);
- return; % change_colormap
- %----------------------------------------------------------------
- function move_cursor(fig)
- nii_view = getappdata(fig, 'nii_view');
- if isempty(nii_view)
- return;
- end
- axi = get(nii_view.handles.axial_axes, 'pos');
- cor = get(nii_view.handles.coronal_axes, 'pos');
- sag = get(nii_view.handles.sagittal_axes, 'pos');
- curr = get(fig, 'currentpoint');
- if curr(1) >= axi(1) & curr(1) <= axi(1)+axi(3) & ...
- curr(2) >= axi(2) & curr(2) <= axi(2)+axi(4)
- curr = get(nii_view.handles.axial_axes, 'current');
- sag = curr(1,1);
- cor = curr(1,2);
- axi = nii_view.slices.axi;
- elseif curr(1) >= cor(1) & curr(1) <= cor(1)+cor(3) & ...
- curr(2) >= cor(2) & curr(2) <= cor(2)+cor(4)
- curr = get(nii_view.handles.coronal_axes, 'current');
- sag = curr(1,1);
- cor = nii_view.slices.cor;
- axi = curr(1,2);
- elseif curr(1) >= sag(1) & curr(1) <= sag(1)+sag(3) & ...
- curr(2) >= sag(2) & curr(2) <= sag(2)+sag(4)
- curr = get(nii_view.handles.sagittal_axes, 'current');
- sag = nii_view.slices.sag;
- cor = curr(1,1);
- axi = curr(1,2);
- else
- set(nii_view.handles.imvalcur,'String',' ');
- set(nii_view.handles.imposcur,'String',' ');
- return;
- end
- sag = round(sag);
- cor = round(cor);
- axi = round(axi);
- if sag < 1
- sag = 1;
- elseif sag > nii_view.dims(1)
- sag = nii_view.dims(1);
- end
- if cor < 1
- cor = 1;
- elseif cor > nii_view.dims(2)
- cor = nii_view.dims(2);
- end
- if axi < 1
- axi = 1;
- elseif axi > nii_view.dims(3)
- axi = nii_view.dims(3);
- end
- if 0 % isfield(nii_view, 'disp')
- img = nii_view.disp;
- else
- img = nii_view.nii.img;
- end
- if nii_view.nii.hdr.dime.datatype == 128
- imgvalue = [double(img(sag,cor,axi,1,nii_view.scanid)) double(img(sag,cor,axi,2,nii_view.scanid)) double(img(sag,cor,axi,3,nii_view.scanid))];
- set(nii_view.handles.imvalcur,'String',sprintf('%7.4g %7.4g %7.4g',imgvalue));
- elseif nii_view.nii.hdr.dime.datatype == 511
- R = double(img(sag,cor,axi,1,nii_view.scanid)) * (nii_view.nii.hdr.dime.glmax - ...
- nii_view.nii.hdr.dime.glmin) + nii_view.nii.hdr.dime.glmin;
- G = double(img(sag,cor,axi,2,nii_view.scanid)) * (nii_view.nii.hdr.dime.glmax - ...
- nii_view.nii.hdr.dime.glmin) + nii_view.nii.hdr.dime.glmin;
- B = double(img(sag,cor,axi,3,nii_view.scanid)) * (nii_view.nii.hdr.dime.glmax - ...
- nii_view.nii.hdr.dime.glmin) + nii_view.nii.hdr.dime.glmin;
- imgvalue = [R G B];
- set(nii_view.handles.imvalcur,'String',sprintf('%7.4g %7.4g %7.4g',imgvalue));
- else
- imgvalue = double(img(sag,cor,axi,nii_view.scanid));
- if isnan(imgvalue) | imgvalue > nii_view.cbarminmax(2)
- imgvalue = 0;
- end
- set(nii_view.handles.imvalcur,'String',sprintf('%.6g',imgvalue));
- end
- nii_view.slices.sag = sag;
- nii_view.slices.cor = cor;
- nii_view.slices.axi = axi;
- nii_view = update_imgXYZ(nii_view);
- if get(nii_view.handles.coord,'value') == 1,
- sag = nii_view.imgXYZ.vox(1);
- cor = nii_view.imgXYZ.vox(2);
- axi = nii_view.imgXYZ.vox(3);
- elseif get(nii_view.handles.coord,'value') == 2,
- sag = nii_view.imgXYZ.mm(1);
- cor = nii_view.imgXYZ.mm(2);
- axi = nii_view.imgXYZ.mm(3);
- elseif get(nii_view.handles.coord,'value') == 3,
- sag = nii_view.imgXYZ.tal(1);
- cor = nii_view.imgXYZ.tal(2);
- axi = nii_view.imgXYZ.tal(3);
- end
- if get(nii_view.handles.coord,'value') == 1,
- string = sprintf('%7.0f %7.0f %7.0f',sag,cor,axi);
- else
- string = sprintf('%7.1f %7.1f %7.1f',sag,cor,axi);
- end;
-
- set(nii_view.handles.imposcur,'String',string);
- return; % move_cursor
- %----------------------------------------------------------------
- function change_scan(hdl_str)
- fig = gcbf;
- nii_view = getappdata(fig,'nii_view');
- if strcmpi(hdl_str, 'edit_change_scan') % edit
- hdl = nii_view.handles.contrast_def;
- setscanid = round(str2num(get(hdl, 'string')));
- else % slider
- hdl = nii_view.handles.contrast;
- setscanid = round(get(hdl, 'value'));
- end
- update_scanid(fig, setscanid);
- return; % change_scan
- %----------------------------------------------------------------
- function val = scale_in(val, minval, maxval, range)
- % scale value into range
- %
- val = range*(double(val)-double(minval))/(double(maxval)-double(minval))+1;
- return; % scale_in
- %----------------------------------------------------------------
- function val = scale_out(val, minval, maxval, range)
- % according to [minval maxval] and range of color levels (e.g. 199)
- % scale val back from any thing between 1~256 to a small number that
- % is corresonding to [minval maxval].
- %
- val = (double(val)-1)*(double(maxval)-double(minval))/range+double(minval);
- return; % scale_out
|