Hello,
I am newbie to IDL and would very much appreciate if someon can help me modify the following code to make it execute faster. The for loop seem inefficient but I don't know how to make it go faster.
Thank you..
Michael..
; program to replace image regions with most frequently occurring (mode) pixel value in input image
PRO pixel2region, par1, par2, par3, par4, par5
pixmap=READ_BINARY(par1, data_dims=[par4,par5],data_type=1,endian='little')
regmap=READ_BINARY(par2, data_dims=[par4,par5],data_type=3,endian='little')
minreg = MIN(regmap)
maxreg = MAX(regmap)
siz = SIZE(regmap) ; zero base counting
out = INDGEN(siz(1),siz(2), /BYTE)
FOR i=LONG(minreg), maxreg DO BEGIN
in = WHERE(regmap eq i)
distfreq = HISTOGRAM(pixmap(in), MIN=MIN(pixmap(in)))
maxfreq = MAX(distfreq)
mode = WHERE(distfreq EQ maxfreq) + MIN(pixmap(in)) ; find mode of array
out(in) = byte(mode(0)) ; grab the first element of the mode array if non-unique
ENDFOR
OPENW, 1, par3
WRITEU, 1, out
CLOSE, /ALL
END
|