Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to improve efficiency in this numpy iterating?
    text
    copied!<p>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.</p> <pre><code>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 </code></pre> <p>I read a 512x512 image to <code>im</code> ndarray and did following things:</p> <pre><code>output = list() for row in im: row_output = list() for pixel in row: pixel_matrix = ((pixel / (256 / split_num)) &gt; dithering_matrix) * 255 row_output.append(pixel_matrix) output.append( hstack( tuple(row_output) ) ) output_matrix = vstack( tuple(output) ) </code></pre> <p>I found it took 8-10s to output and I think the loop of <code>im</code> above spent much time. In some software the same operation was usually done in a flash. So is it possible to improve the efficiency? </p> <hr> <p>UPDATE: @Ignacio Vazquez-Abrams I'm not vert fimiliar with profiler:( I tried cProfile and the result is strange.</p> <pre><code> 1852971 function calls (1852778 primitive calls) in 9.127 seconds Ordered by: internal time List reduced from 561 to 20 due to restriction &lt;20&gt; ncalls tottime percall cumtime percall filename:lineno(function) 1 6.404 6.404 9.128 9.128 a1.1.py:10(&lt;module&gt;) 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 } </code></pre> <p>The line 10 of a1.1.py is the first line <code>from numpy import *</code> (all comments before that) which really puzzles me up.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload