Re: Re: Re: Optimization problem

*To*: mathgroup at smc.vnet.net*Subject*: [mg96751] Re: [mg96721] Re: [mg96705] Re: [mg96675] Optimization problem*From*: Adam Simpson <adambsimpson at gmail.com>*Date*: Mon, 23 Feb 2009 05:05:19 -0500 (EST)*References*: <200902201046.FAA17890@smc.vnet.net> <200902220040.TAA08474@smc.vnet.net> <200902220812.DAA15123@smc.vnet.net>

Those functions seem to work great. I am analyzing high speed video which luckily is easily parallizable but it still took quite a bit of time to run using my old functions. Thanks for the help. Adam On Feb 22, 2009, at 3:12 AM, Matthias Odisio wrote: > Adam, > >> Adam Simpson wrote: >>> Hi, I'm currently working with some image files in mathematica but >>> am >>> having an optimization problem. What I am basically trying to do is >>> convert an image to HSB color space and then test each pixel to >>> see if >>> the H,S,B values fall within a certain range, making it a 1 if >>> it's in >>> the specified range else a 0. Notice I have multiplied the raw HSV >>> values to have ranges of [0,369], [0,100],[0,100] respectively. I >>> however get an order of magnitude difference between running two >>> operations that I can't rectify. >>> >>> Notice the image I am using to that I call "frame" is available for >>> download at http://www.physics.uc.edu/~simpson/page11/files/OptimizationProblem.jpg >>> : >>> >>> //test each pixel to see if the value of Hue,Saturation,Brightness >>> fall within a certain range and make the value 1 if it is, else a 0: >>> Image[Map[ >>> If[320 <= 359 #[[1]] <= 355 && 65 <= 100 #[[2]] <= 80 && >>> 30 <= 100 #[[3]] <= 75, 1, 0] &, >>> ImageData[frame], {2}]] // AbsoluteTiming >>> >>> 0.282983 Seconds >>> >>> If however I create a function as such ,even after compiling it >>> which >>> helped slightly, that is equal to the above If statement I get an >>> order of magnitude higher time. >>> >>> Hmin=320; >>> Hmax=359; >>> Smin=65; >>> Smax=80; >>> Bmin=30; >>> Bmax=75; >>> >>> PixelMatch = Compile[{{H, _Real}, {S, _Real}, {B, _Real}}, >>> If[(Hmin <= H*359 <= Hmax) &&(Smin <= S*100 <= Smax) && >>> (Bmin <= B*100 <= Bmax), 1, 0]] >>> >>> Image[Map[PixelMatch[#[[1]], #[[2]], #[[3]]] &, >>> ImageData[frame], {2}]] // AbsoluteTiming >>> >>> 2.012571 Seconds >>> >>> Any help is highly appreciated. >>> >>> Adam Simpson > > Map automatically tries to compile the function you specify. > > With your first approach, I would recommend using Binarize or > ImageApply > for the job, as they could get optimized in further versions: > > With[{hmin = 320/359., hmax = 355/359., smin = .65, smax = .8, > bmin = .3, bmax = .75}, > Binarize[frame, > hmin <= #[[1]] <= hmax && smin <= #[[2]] <= smax && > bmin <= #[[3]] <= bmax &]] // AbsoluteTiming > > 0.212292 s. > > A much faster approach is to process the whole 2d arrays at one go > using > functions which can operate on them directly: > > With[{hmin = 320/359., hmax = 355/359., smin = .65, smax = .8, > bmin = .3, bmax = .75}, > Image[Unitize[ > Times @@ > MapThread[ > Clip[#1, #2, {0, 0}] &, {ImageData[frame, > Interleaving -> False], {{hmin, hmax}, {smin, smax}, {bmin, > bmax}}}]], "Bit"]] // AbsoluteTiming > > 0.088738 s. > > > Matthias Odisio > Wolfram Research > > > >

**References**:**Optimization problem***From:*Adam Simpson <adambsimpson@gmail.com>

**Re: Optimization problem***From:*Daniel Lichtblau <danl@wolfram.com>

**Re: Re: Optimization problem***From:*Matthias Odisio <matthias@wolfram.com>