Reputation: 906
I'm working on an assignment about converting a grayscale image to 1-bit binary image by dithering. I'm trying a simple 4x4 matrix that will make the image 16 times larger than original.
dithering_matrix = array([[ 0, 8, 2, 10],
[12, 4, 14, 6],
[ 3, 11, 1, 9],
[15, 7, 13, 5]], dtype=uint8)
split_num = dithering_matrix.size + 1
I read a 512x512 image to im
ndarray and did following things:
output = list()
for row in im:
row_output = list()
for pixel in row:
pixel_matrix = ((pixel / (256 / split_num)) > dithering_matrix) * 255
row_output.append(pixel_matrix)
output.append( hstack( tuple(row_output) ) )
output_matrix = vstack( tuple(output) )
I found it took 8-10s to output and I think the loop of im
above spent much time. In some software the same operation was usually done in a flash. So is it possible to improve the efficiency?
UPDATE: @Ignacio Vazquez-Abrams I'm not vert fimiliar with profiler:( I tried cProfile and the result is strange.
1852971 function calls (1852778 primitive calls) in 9.127 seconds
Ordered by: internal time
List reduced from 561 to 20 due to restriction <20>
ncalls tottime percall cumtime percall filename:lineno(function)
1 6.404 6.404 9.128 9.128 a1.1.py:10(<module>)
513 0.778 0.002 0.778 0.002 {numpy.core.multiarray.concatenate
}
262144 0.616 0.000 1.243 0.000 D:\Python27\lib\site-packages\nump
y\core\shape_base.py:6(atleast_1d)
262696 0.260 0.000 0.261 0.000 {numpy.core.multiarray.array}
262656 0.228 0.000 0.487 0.000 D:\Python27\lib\site-packages\nump
y\core\numeric.py:237(asanyarray)
515 0.174 0.000 1.419 0.003 {map}
527019 0.145 0.000 0.145 0.000 {method 'append' of 'list' objects
}
The line 10 of a1.1.py is the first line from numpy import *
(all comments before that) which really puzzles me up.
Upvotes: 2
Views: 572
Reputation: 500663
If you use the Kronecker product to turn every pixel into a 4x4 submatrix, that'll enable you to get rid of the Python loops:
im2 = np.kron(im, np.ones((4,4)))
dm2 = np.tile(dithering_matrix,(512,512))
out2 = ((im2 / (256 / split_num)) > dm2) * 255
On my machine this is roughly 20x faster than your version.
Upvotes: 8