Fax image processing (needs to be fast)

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
adam7288
Posts: 6
Joined: 2018-09-15T19:13:46-07:00
Authentication code: 1152

Fax image processing (needs to be fast)

Post by adam7288 »

Hello! Thank you for the great information. There are some posts on the non-php side of things about processing photos for faxes. However they are in CLI format and also seems to be very resource intensive. As of now I have gone down two paths:

quantizeImage(2, Imagick::COLORSPACE_GRAY, 2, TRUE, FALSE)
This had good results for photos but text especially handwriting became thin and illegible

$max = $image->getQuantumRange();
$max = $max["quantumRangeLong"];
$image->thresholdImage(0.77 * $max);
This produces great results for documents, but photos look horrible. They mostly turn out black.

Anybody have any other suggestions? A fax has 1 bit color depth. Dithering is certainly an option.

Thank you in advance!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Fax image processing (needs to be fast)

Post by fmw42 »

Try -monochrome

See https://www.imagemagick.org/Usage/quantize/#monochrome

But I don't know if it is included with Imagick. Otherwise, you may be able to achieve that with dithering of a grayscale image then threshold it.
adam7288
Posts: 6
Joined: 2018-09-15T19:13:46-07:00
Authentication code: 1152

Re: Fax image processing (needs to be fast)

Post by adam7288 »

The monochrome operation is indeed fantastic. Do you know offhand if this is the kind of operation where throwing more cores at it would result in a faster running time? Is it threaded? Thanks in advance.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Fax image processing (needs to be fast)

Post by fmw42 »

I do not know for sure, but I suspect it is multi-threaded.

If you have lots of cores, you may be better running simultaneous jobs with one thread for each core than trying to use multiple threads for a given task.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Fax image processing (needs to be fast)

Post by snibgo »

Yes, "-monochrome" is multi-threaded. Will it be much faster with lots of cores? That depends on many factors, including image size and the computer. We can test it with "-bench", for example:

Code: Select all


f:\web\im>%IMG7%magick -size 2000x2000 gradient:red-blue g.miff

f:\web\im>%IMG7%magick g.miff -bench 5 -monochrome NULL:
Performance[1]: 5i 0.232ips 1.000e 21.547u 0:21.562
Performance[2]: 5i 0.246ips 0.514e 25.453u 0:20.359
Performance[3]: 5i 0.255ips 0.523e 28.313u 0:19.641
Performance[4]: 5i 0.256ips 0.524e 31.906u 0:19.563
Performance[5]: 5i 0.251ips 0.520e 37.438u 0:19.906
Performance[6]: 5i 0.253ips 0.521e 40.469u 0:19.797
Performance[7]: 5i 0.259ips 0.527e 42.781u 0:19.328
Performance[8]: 5i 0.254ips 0.523e 44.703u 0:19.672
On my computer, which has 8 cores, this "-monochrome" runs fastest (most iterations per second) with 7 threads. But there is hardly any difference between 3 threads and 8 threads, and the best is only about 10% faster than single-threaded.

This is typical of the work I do, on my computer. Your mileage may vary.
snibgo's IM pages: im.snibgo.com
adam7288
Posts: 6
Joined: 2018-09-15T19:13:46-07:00
Authentication code: 1152

Re: Fax image processing (needs to be fast)

Post by adam7288 »

Thank you - what cpu do you have exactly?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Fax image processing (needs to be fast)

Post by snibgo »

Intel Core i7-4700MQ CPU @ 2.40 GHz, running Windows 8.1.

For me, running "-monochrome" single-threaded on that g.miff takes about 3 seconds. Running 8 processes simultaneously, each single-threaded, takes about 6 seconds. When processing many images, eg video frames, this is the fastest method. But it takes more memory as we are reading 8 images into memory at the same time, so it needs caution.
snibgo's IM pages: im.snibgo.com
adam7288
Posts: 6
Joined: 2018-09-15T19:13:46-07:00
Authentication code: 1152

Re: Fax image processing (needs to be fast)

Post by adam7288 »

What are your thoughts about true cores vs hyperthreaded cores? Your cpu only has 4 real cores. The hyperthreading I've read on average can speed up threaded applications roughly 10-15%. Is there a way to perform the benchmark with hyperthreading turned off or to account for which logical core being used is a real core?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Fax image processing (needs to be fast)

Post by snibgo »

Yes, I have four physical cores, each with two logical cores. As far as I know, IM can only see the eight logical cores, and can't switch off hyperthreading.

My laptop is five years old, and I'd need to play with more modern CPUs to judge where the sweet spot is (more physical cores versus more logical cores).

I haven't seen proper benchmarks of Windows versus different flavours of Unix, but this will also affect the efficacy of multithreading.
snibgo's IM pages: im.snibgo.com
Post Reply