I have compared the speed of some simple image processing routines between Mathematica an IDL.
1. Reading of a grey scale png image of (720,577) pixels:
The image is:
Mathematica:
t1 = AbsoluteTime[];
img = Import[filename];
Print[AbsoluteTime[] - t1]
0.029513
IDL:
t1=systime(1)
img=read_png(filename)
print,systime(1)-t1
0.013546944
IDL is giving the byte array of pixel values of the image.
To do that in addition with Mathematica:
t1 = AbsoluteTime[];
imgData = ImageData[img, "Byte"];
Print[AbsoluteTime[] - t1]
0.002133
costs another few msec.
---> IDL is about 2 times faster
2. Finding positions of pixels at which the image pixels are exceeding a threshold of 50:
Mathematica:
t1 = AbsoluteTime[];
is = Position[imgData, n_ /; n >= 50];
Print[AbsoluteTime[] - t1]
0.222408
IDL:
t1=systime(1)
is=where(img GE 50)
print,systime(1)-t1
0.00085401535
---> IDL is more than 250 times faster
3. Replacing the pixel values that exceed the threshold of 50 with 255:
Mathematica:
t1 = AbsoluteTime[];
img = ReplacePart[imgData, is -> 255];
Print[AbsoluteTime[] - t1]
0.080377
IDL:
t1=systime(1)
img(is)=255
print,systime(1)-t1
3.2901764e-05
---> IDL is nearly 2500 times faster
These differences are really disappointing.
I would be happy if somebody has a solution how to do the things faster.
SparseArray[UnitStep[imgData - 50]]["NonzeroPositions"]
$\endgroup$ImageApply[Min[{#, 255}] &, img] // AbsoluteTiming
can do the last two quickly. $\endgroup$Import
,Position
orReplacePart
are image processing routines. You should rather be asking, "how can I do this with image processing routines?" or "how can I make this code faster?" $\endgroup$