Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Starting from an image I would like to apply a custom filter to an image.

I have thought and implemented this filter in HSL space and it goes like this :

  • Boost the saturation of the pixels that have a S value > .5
  • Decrease the ones where S < .5.

Would I have the HSL Values instead of the HSB the code would go like this :

img = Import["ExampleData/lena.tif"];
ImageColorSpace[img]
img = ImageResize[img, 128];
img = ColorConvert[img, "HSB"];
imgd = ImageData[img];
newimg = ({#[[1]], 1/(1 + E^(10*#[[2]]*(#[[2]] - .5))), #[[3]]} & /@ 
  imgd[[#]]) & /@ Range[Length[imgd]];
newimg = Image[newimg]

My Problem : I cannot figure out how to convert HSB (only hue based space in M9 to my knowledge) to HSL.

share|improve this question
1  
ariya.blogspot.com.ar/2008/07/… –  belisarius Jan 15 '13 at 19:11
    
@belisarius, Thank You, I don`t quite get this syntax. Especially the role of the '*'. Could you tell me how to read that symbol there ? –  500 Jan 15 '13 at 19:14
1  
*var is a pointer to var, just ignore it –  belisarius Jan 15 '13 at 19:25
    
It's got to do with pointers –  ssch Jan 15 '13 at 19:25
    
Using the formulas in wiki's "HSL and HSV" page, I think a formula from one to the other can be derived. –  Silvia Jan 15 '13 at 21:33
add comment

2 Answers

up vote 5 down vote accepted

Using the formulas in Wikipedia's "HSL and HSV" page, a function from HSB -> RGB can be defined as (where $h,s,b\in[0,1]$):

Clear[HSBtoRGB]
HSBtoRGB[{h_, s_, b_}] := Module[{c, x, R1, G1, B1},
  c = b s;
  x = c (1 - Abs[Mod[6 h, 2] - 1]);
  b - c + Piecewise[{
     {{c, x, 0}, 0 <= h < 1/6},
     {{x, c, 0}, 1/6 <= h < 2/6},
     {{0, c, x}, 2/6 <= h < 3/6},
     {{0, x, c}, 3/6 <= h < 4/6},
     {{x, 0, c}, 4/6 <= h < 5/6},
     {{c, 0, x}, 5/6 <= h < 1}
     }, {0, 0, 0}]
  ]

Also function from RGB -> HSL (where $r,g,b\in[0,1]$):

Clear[RGBtoHSL]
RGBtoHSL[{r_, g_, b_}] := Module[{m, M, c, h, s, l},
  {m, M} = Through[{Min, Max}[r, g, b]];
  c = M - m;
  h = 1/6 Piecewise[{{0, c == 0},
      {Mod[(g - b)/c, 6], M == r},
      {(b - r)/c + 2, M == g},
      {(r - g)/c + 4, M == b}}];
  l = (m + M)/2;
  s = Piecewise[{{c/(1 - Abs[2 l - 1]), c != 0}}, 0];
  {h, s, l}
  ]

So by using RGBtoHSL[HSBtoRGB[ hsb ]], a HSB color can be converted to HSL one.

This composite function evaluates to results generally the same as that does in Sjoerd C. de Vries'es answer, only differs on some boundary values:

Function[hsb, Grid[{{
       Graphics[{Hue @@ hsb, Disk[]}, ImageSize -> 50],
       {
         {hsb, SpanFromLeft, SpanFromLeft},
         RGBtoHSL[HSBtoRGB[hsb]],
         HSVtoHSL @@ hsb
         } //
        Grid[#, Frame -> All] &
       }}, Frame -> True]] /@
   Join[Tuples[{0, 1}, 3],
    RandomReal[{0, 1}, {18, 3}]] //
  Row[Riffle[#, "   "]] & // Quiet

enter image description here

share|improve this answer
add comment

A quick conversion from the page belisarius linked to:

HSVtoHSL[h_, s_, v_] :=
 Module[{ss, ll},
  ll = (2 - s) v;
  ss = s v;
  ss /= If[ll <= 1, ll, 2 - ll];
  ll /= 2;
  {h, ss, ll}
]


HSLtoHSV[hh_, ss_, ll_] :=
 Module[{s = ss, l = ll},
  l *= 2;
  s *= If[(l <= 1), l, 2 - l];
  {hh, (2*s)/(l + s), (l + s)/2}
 ]
share|improve this answer
    
Thanks god the code wasn't done is golfscript –  belisarius Jan 16 '13 at 3:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.