X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 26 Oct 2008 03:02 PM by  anon
faster loop execution
 2 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
26 Oct 2008 03:02 PM
    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

    Deleted User



    New Member


    Posts:
    New Member


    --
    02 Dec 2008 02:02 AM
    since i don't know what pixmap and regmap are it's hard to help.. pixmap seems like a regular bitmap to me but regmap i'm not sure what it is supposed to be. i guess the regmap indicates regions in the pixmap. then it seems the basic algorithm is quite efficient already. however, you do calculate MIN(pixmap(in)) twice. also, MAX() has a second parameter in which it can return the subscript to the maximum. this should also be beneficial to the speed.   hope this helps,   Pieter van der Meer SRON - Netherlands Institute for Space Research    

    Deleted User



    New Member


    Posts:
    New Member


    --
    02 Dec 2008 10:46 AM
    I don't have time to do it for you, but i see two places  you could look to get some performance gains.  Mind you, i'm not promising you'll actually be able to do better than you're already doing, but here's where i'd look: 1) You can probably get a little better performance if you calculate your mode without using a where() call.  See here: http://www.dfanning.com/code_tips/mode.html   2)  Your 'i' loop itself is *probably* not needed, and you could potentially get some decent performance gains by removing it.  I'll warn you that this is not an easy road to go down, and doing so makes your code less intuitive to others who are not familiar with this technique, but if speed is really an issue you want to look at clever histogram() tricks.  Here's where to look: http://www.dfanning.com/t...togram_tutorial.html   Good luck! Jeff
    You are not authorized to post a reply.