Message Boards Message Boards

2
|
7005 Views
|
2 Replies
|
7 Total Likes
View groups...
Share
Share this post:

Faster ways to hue shift an RGB? OpenCV in Python still 3x faster

testImage = ExampleData[{"TestImage", "Mandrill"}]

This method is based on example in docs and is super slow

RepeatedTiming@
 ColorConvert[
  ImageApply[Function[{h, s, b}, {Mod[h + .5, 1], s, b}] @@ # &, 
   ColorConvert[testImage, "HSB"]], "RGB"]

This method is my old school data manipulation and is twice as fast

RepeatedTiming@
 ColorConvert[
  Image[MapAt[Function[{h}, Mod[h + .5, 1]], 
    ImageData@ColorConvert[testImage, "HSB"], {All, All, 1}], 
   ColorSpace -> "HSB"], "RGB"]

And this method seems to be as fast as I can get the system to perform, which is 20x faster than the example from the docs

RepeatedTiming@
 ColorConvert[
  ImageApply[
   Function[{rgb}, MapAt[Function[{p}, Mod[p + .5, 1]], rgb, 1]], 
   ColorConvert[testImage, "HSB"]], "RGB"]

And even still OpenCV in Python is running 3x faster than that. I am running on hundreds of thousands of images so every little boost helps. I think CUDAImageApply does not help since I would need to load and unload each image onto GPU unless I am missing something?

Any hints/ideas/suggestions?

POSTED BY: Kyle Keane
2 Replies

Using ColorSeparate / ColorCombine is about 4x faster than your third solution:

In[1]:= testImage =  Image[ExampleData[{"TestImage", "Mandrill"}], "Real32"];

In[2]:= RepeatedTiming[
 r1 = ColorConvert[
    ImageApply[
     Function[{rgb}, MapAt[Function[{p}, Mod[p + .5, 1]], rgb, 1]], 
     ColorConvert[testImage, "HSB"]], "RGB"];]

Out[2]= {0.0460944, Null}

In[3]:= RepeatedTiming[r2 = (hsbList = ColorSeparate[testImage, "HSB"];
    ColorConvert[
     ColorCombine[{Mod[hsbList[[1]] + .5, 1], hsbList[[2]], 
       hsbList[[3]]}, "HSB"], "RGB"]);]

Out[3]= {0.0123619, Null}

In[4]:= ImageDifference[r1, r2] // MinMax

Out[4]= {0., 2.38419*10^-7}
POSTED BY: Piotr Wendykier

Bravo, this was really clever, thank you!

POSTED BY: Kyle Keane
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract