Speed up -unique-colors on a large image

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?".
Post Reply
JohnsonZA

Speed up -unique-colors on a large image

Post by JohnsonZA »

I'm using -unique-colors on fairly large (6400x25500) TIFF files that are LZW compressed and I'm struggling with both memory requirements and execution time. Each image should have around 10 unique colors.

Since the unique algorithm is O(n log n) complexity, I was wondering if it would shorten execution time by splitting the image up to calculate the unique colours for each cropped region and then combining the results.

My first attempt looks like this:

Code: Select all

convert test.tif -crop x2000 -unique-colors -append -unique-colors temp.png
This approach obviously uses the most memory because it's loading the TIFF as well as all the cropped regions into memory, but it seems to be the fastest. I've toyed around with the size of the cropped regions but can't decide where the overheads become a disadvantage.

Another approach is to use MPC files, which obviously slows down the process, but halves the memory usage.

I've noticed that only one core does all the work when -unique-colors is running. Is there no way to parallelise this function?

Does anyone have any insights as to how to get fast results, while at the same time trying to limit memory use or is this as good as it gets?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Speed up -unique-colors on a large image

Post by fmw42 »

If your images have only about 10 TRULY unique colors (and not just visually appearing that way), then you could try simply reducing the size of your input images, say by 10 using a non-interpolating reduction such as -sample and then get the unique colors.

convert image.tif -sample 10% -unique-colors ...
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Speed up -unique-colors on a large image

Post by snibgo »

I suppose you are using Q-8?
snibgo's IM pages: im.snibgo.com
JohnsonZA

Re: Speed up -unique-colors on a large image

Post by JohnsonZA »

fmw42 wrote:If your images have only about 10 TRULY unique colors (and not just visually appearing that way), then you could try simply reducing the size of your input images, say by 10 using a non-interpolating reduction such as -sample and then get the unique colors.

convert image.tif -sample 10% -unique-colors ...
That works well, but I'm worried about small regions of colour disappearing when using a nearest neighbour sampling. Thanks for the suggestion.
snibgo wrote:I suppose you are using Q-8?
Yes.
Post Reply