ImageMagick -clut taking forever?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
iGaret
Posts: 8
Joined: 2011-03-17T21:43:53-07:00
Authentication code: 8675308

ImageMagick -clut taking forever?

Post 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?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: ImageMagick -clut taking forever?

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
iGaret
Posts: 8
Joined: 2011-03-17T21:43:53-07:00
Authentication code: 8675308

Re: ImageMagick -clut taking forever?

Post 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...
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: ImageMagick -clut taking forever?

Post 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.
iGaret
Posts: 8
Joined: 2011-03-17T21:43:53-07:00
Authentication code: 8675308

Re: ImageMagick -clut taking forever?

Post 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.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: ImageMagick -clut taking forever?

Post 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.
iGaret
Posts: 8
Joined: 2011-03-17T21:43:53-07:00
Authentication code: 8675308

Re: ImageMagick -clut taking forever?

Post 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?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: ImageMagick -clut taking forever?

Post 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.
iGaret
Posts: 8
Joined: 2011-03-17T21:43:53-07:00
Authentication code: 8675308

Re: ImageMagick -clut taking forever?

Post by iGaret »

How would I go about doing that?
iGaret
Posts: 8
Joined: 2011-03-17T21:43:53-07:00
Authentication code: 8675308

Re: ImageMagick -clut taking forever?

Post by iGaret »

anyone?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: ImageMagick -clut taking forever?

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
iGaret
Posts: 8
Joined: 2011-03-17T21:43:53-07:00
Authentication code: 8675308

Re: ImageMagick -clut taking forever?

Post 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 :/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: ImageMagick -clut taking forever?

Post 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!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
iGaret
Posts: 8
Joined: 2011-03-17T21:43:53-07:00
Authentication code: 8675308

Re: ImageMagick -clut taking forever?

Post 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?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: ImageMagick -clut taking forever?

Post 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!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply