Moving window mode [message #50599] |
Sun, 08 October 2006 20:24 |
Luke
Messages: 4 Registered: October 2006
|
Junior Member |
|
|
I use smooth to calculate a moving window mean and have seen posts
relating to other moving window moments, but can't figure out how to do
a moving window mode (w/out nested loops).
Any ideas...?
Below is the code with nested loops I currently use...
function mode,arr ;code from JD Smith - (http://tinyurl.com/zpbxp)
compile_opt idl2
on_error, 2
;note... if array is multimodal,
;this function returns the LOWEST value
;of the equal highest frequency values
void=max(histogram(arr,omin=mn),mxpos)
mode=mn+mxpos
return, mode
end
function focalmode, arr, win
compile_opt idl2
on_error, 2
dims = size(arr,/dimensions)
cols=dims[0]
rows=dims[1]
if win mod 2.0 eq 0 then win-=1 ;win needs to be an odd number
win2=(win-1)/2 ;num pixels from centre pixel to edge of moving
window
fm_array=arr
for Y=0,ROWS-1 do begin
;watch out for the edges...
ywin_u = y < win2 ;num pixels from centre pixel to upper
edge of moving window
ywin_d = rows-1-y < win2 ;num pixels from centre pixel to lower
edge of moving window
for X=0,COLS-1 do begin
;watch out for the edges...
xwin_l = x < win2 ;num pixels from centre pixel to
left edge of moving window
xwin_r = cols-1-x < win2 ;num pixels from centre pixel to
right edge of moving window
;Calc the mode
fm_array[X,Y] =
mode(fm_array[X-xwin_l:X+xwin_r,Y-ywin_u:Y+ywin_d])
endfor
endfor
return, fm_array
end
PS: verrry newbie in IDL, be nice ;)
|
|
|