Page 1 of 2

ImageMagick -clut taking forever?

Posted: 2011-03-17T21:55:09-07:00
by iGaret
Hello! I'm currently working on an image processing application and am using the command like version of IM. I need to emulate "curves" so I've generated a 256x256 polygonal map that I'm using for a LUT. The problem is when I bring in 10 megapixel images it can take a VERY long time (I'm talking minutes..) and I have a dual core 2.8 GHz machine with 4 gigs of ram..

Here's (one) of the LUT's that gets run on an image:

Code: Select all

convert sourceimage.png \( -size 256x256 xc:black -fill white -draw "polygon 0,0 0,79 61,125 184,190 255,255 255,0" -crop 256x255+0+1 +repage -scale 256x1! -channel red \)  -clut outputimage.png
Even on low res (500x500) images it takes a few seconds to do. I'm running that through the red channel, but I'm also performing clut commands on the green/blue channels, aswell as the RGB channel as a whole.

Any idea how to speed this up? Photoshop handles curves in realtime, so do many other programs.. am I simply doing this wrong?

Re: ImageMagick -clut taking forever?

Posted: 2011-03-18T00:20:29-07:00
by anthony
You may be reaching memory limits and IM is saving the image to disk and using disk access (very very slow).

You could try compiling a 8bit version of IM that uses half the memory limits.

Re: ImageMagick -clut taking forever?

Posted: 2011-03-18T08:29:30-07:00
by iGaret
How could I possibly reaching memory limits? The images are between 1.5 megs and 10 megs, I have 4 GIGS of memory in my computer...

Re: ImageMagick -clut taking forever?

Posted: 2011-03-18T08:42:01-07:00
by magick
Add -debug cache to your command line. Post the first 30 lines or so here so we can determine if caching pixels to disk is the problem.

Re: ImageMagick -clut taking forever?

Posted: 2011-03-18T08:52:56-07:00
by iGaret
Here's what gets outputted:

Code: Select all

2011-03-18T11:52:25-04:00 0:00.000 0.000u 6.6.5 Cache convert[52955]: cache.c/DestroyPixelCache/1507/Cache
  destroy 
2011-03-18T11:52:25-04:00 0:00.000 0.000u 6.6.5 Cache convert[52955]: cache.c/OpenPixelCache/4056/Cache
  open /Users/iGaret/Desktop/DSC_3933-1.JPG[0] (heap memory, 3184x2120 51.5MiBB)
2011-03-18T11:52:25-04:00 0:00.260 0.250u 6.6.5 Cache convert[52955]: cache.c/OpenPixelCache/4056/Cache
  open black[0] (heap memory, 256x256 512KiBB)
2011-03-18T11:52:25-04:00 0:00.270 0.260u 6.6.5 Cache convert[52955]: cache.c/OpenPixelCache/4056/Cache
  open black[0] (heap memory, 256x255 510KiBB)
2011-03-18T11:52:25-04:00 0:00.270 0.260u 6.6.5 Cache convert[52955]: cache.c/DestroyPixelCache/1507/Cache
  destroy black[0]
2011-03-18T11:52:25-04:00 0:00.270 0.260u 6.6.5 Cache convert[52955]: cache.c/OpenPixelCache/4056/Cache
  open black[0] (heap memory, 256x1 2KiBB)
2011-03-18T11:52:25-04:00 0:00.270 0.260u 6.6.5 Cache convert[52955]: cache.c/DestroyPixelCache/1507/Cache
  destroy black[0]
2011-03-18T11:52:28-04:00 0:03.570 6.360u 6.6.5 Cache convert[52955]: cache.c/DestroyPixelCache/1507/Cache
  destroy black[0]
2011-03-18T11:52:29-04:00 0:04.030 6.810u 6.6.5 Cache convert[52955]: cache.c/DestroyPixelCache/1507/Cache
  destroy /Users/iGaret/Desktop/DSC_3933-1.JPG[0]
convert: UnableToOpenConfigureFile `colors.xml' @ warning/configure.c/GetConfigureOptions/589.

Re: ImageMagick -clut taking forever?

Posted: 2011-03-18T08:58:26-07:00
by magick
For the 3184x2120 image, the pixels are in memory as expected rather than disk. We tried your convert command on a 3184x2120 image and it ran in 3 seconds. We're using ImageMagick 6.6.8-5.

Re: ImageMagick -clut taking forever?

Posted: 2011-03-18T09:06:46-07:00
by iGaret
Yes, but try this:

Create an executable shell script with this as the contents:

Code: Select all



"$3"convert "$1" \( -size 256x256 xc:black -fill white -draw "polygon 0,0 0,21 139,136 255,243 255,0" -crop 256x255+0+1 +repage -scale 256x1! -channel blue \)  -clut "$2"
"$3"convert "$2" \( -size 256x256 xc:black -fill white -draw "polygon 0,0 0,6 57,43 112,139 255,235 255,0" -crop 256x255+0+1 +repage -scale 256x1! \)  -clut "$2"
"$3"convert "$2" -modulate 100,90 "$2"
	
Then run it using:

Code: Select all

shellscript sourceimage.jpg outputimage.jpg DirectoryToTheConvertCommand 
This takes about 15 seconds on my machine. The problem is it's for an application that is going to need to process this effect and sometimes even do more LUTs than just the two I'm using here. I need the image to finish in under 10 seconds.

Is this possible?

Re: ImageMagick -clut taking forever?

Posted: 2011-03-18T09:31:54-07:00
by magick
You can combine the commands into one convert command line. That takes 3 seconds on my Linux host. Writing to the intermediate files and reading it back in across several convert commands is a performance killer.

Re: ImageMagick -clut taking forever?

Posted: 2011-03-18T09:32:55-07:00
by iGaret
How would I go about doing that?

Re: ImageMagick -clut taking forever?

Posted: 2011-03-18T20:01:08-07:00
by iGaret
anyone?

Re: ImageMagick -clut taking forever?

Posted: 2011-03-18T20:42:19-07:00
by anthony
See IM Examples, Basics, Complex Image Processing and Debugging
http://www.imagemagick.org/Usage/basics/#complex

This gives you the starting point, after that it is just a matter of refinement as you come to understand the operations.

Actually I recommend you also read the start of that whole page as it explains many things that may not be obvious at first.

Re: ImageMagick -clut taking forever?

Posted: 2011-03-21T09:49:55-07:00
by iGaret
Okay, so I'm still having speed problems :/

Here's a command with 2 channels combined:

Code: Select all

convert  /Users/iGaret/Desktop/DSC_3933-1.JPG \( -size 256x256 xc:black -fill white -draw "polygon 0,0 0,21 139,136 255,243 255,0" -crop 256x255+0+1 +repage -scale 256x1! -channel blue \)  -clut \
\( -size 256x256 xc:black -fill white -draw "polygon 0,0 0,6 57,43 112,139 255,235 255,0" -crop 256x255+0+1 +repage -scale 256x1! \)  -clut \
-modulate 100,90 /Users/iGaret/Desktop/DSC_3933-1_cp1.JPG 
It took 11 seconds to run on my computer... is it possible to run this command quicker? I need this run in 5 seconds or less :/

Re: ImageMagick -clut taking forever?

Posted: 2011-03-21T19:45:32-07:00
by anthony
generating a junk input image (as you never provided one)

Code: Select all

convert -size 3184x2120 xc: \
       \( -size 256x256 xc:black -fill white -draw "polygon 0,0 0,21 139,136 255,243 255,0" \
          -crop 256x255+0+1 +repage -scale 256x1! -channel blue \)  -clut \
       \( -size 256x256 xc:black -fill white -draw "polygon 0,0 0,6 57,43 112,139 255,235 255,0" \
          -crop 256x255+0+1 +repage -scale 256x1! \)  -clut \
       -modulate 100,90 t.jpg
that took 18 seconds on my older two-core machine.

The polygon generation however only took 0.03 seconds.
As such it looks like the clut is the thing that is being very slow.

Code: Select all

 convert -size 3184x2120 xc: \
       \( -size 256x256 xc:black -fill white -draw "polygon 0,0 0,21 139,136 255,243 255,0" \
          -crop 256x255+0+1 +repage -scale 256x1! -channel blue \)  -clut \
       null:
About 9 seconds! -clut is the cause.

It really should not take that long!

Looking at the CLUT the image appears to be a simple linear level operator to do a de-contrasting
operation. Perhaps you can use +level instead.
http://www.imagemagick.org/Usage/color_mods/#level_plus

Code: Select all

convert -size 3184x2120 xc: +level 10,5% null:
took only 1.4 seconds!

Re: ImageMagick -clut taking forever?

Posted: 2011-03-21T21:54:55-07:00
by iGaret
Hmmm aren't levels different than curves?

Can I pass a polygon to levels to make it run a lookup table on it? Because that's what I need... If I needed to adjust the levels I would've used levels, but I need curves. So I use a lookup table.


Is there something I'm missing?

Re: ImageMagick -clut taking forever?

Posted: 2011-03-21T22:33:33-07:00
by anthony
levels are linear stretching (or compression) of the color values.
You can only specify the end points.

Curves generally involve some type of function, and so are a little more complex, But IM does provide many functions that can be used. the 'polynomial' function is very versatile for this.
http://www.imagemagick.org/Usage/color_mods/#curves

The CLUT is just a more free form method, with values being actually looked up from a image (table).
But it really shouldn't be taking as long as it does!