Re: Re: Optimization problem

```Thanks, but upon running the two functions you supplied I got pretty
much the same times as the slower original function. ~2 seconds
compared to ~.2 seconds when the If statement is directly inside of
Map[].

On Feb 21, 2009, at 7:40 PM, Daniel Lichtblau wrote:

> 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
>>  :
>>
>> //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.
>>
>
> It's a scoping issue, more or less, having to do with Compile having
> HoldAll attribute. If you check your PixelMatch[[4]] you will see it
> is
> actually making several function calls.
>
> This can be rectified using either approach below. The first forces an
> evaluation of teh body of the Compile. The second makes the values
> visible to everything inside the With[], including the body of that
> Compile.
>
> PixelMatch2 = Compile[{{H, _Real}, {S, _Real}, {B, _Real}},
>      Evaluate[If[(Hmin <= H*359 <= Hmax) &&(Smin <= S*100 <=  Smax) &&
>        		(Bmin <= B*100 <= Bmax), 1, 0]]]
>
> PixelMatch3 = With[
>  {Hmin=320, Hmax=359, Smin=65, Smax=80, Bmin=30, Bmax=75},
>   Compile[{{H, _Real}, {S, _Real}, {B, _Real}},
>      If[(Hmin <= H*359 <= Hmax) &&(Smin <= S*100 <=  Smax) &&
>       (Bmin <= B*100 <= Bmax), 1, 0]]]
>
> Daniel Lichtblau
> Wolfram Research
>
>
>
>

```

• Prev by Date: Re: Re: Optimization problem
• Next by Date: Re: Mathematica problem
• Previous by thread: Re: Re: Re: Optimization problem
• Next by thread: What is "Depth of Atomic Objects" ?