[SciPy-user] trying to use weave to access image data in C
prabhu at aero.iitm.ernet.in
Wed Sep 29 14:32:31 CDT 2004
>>>>> "PR" == Prabhu Ramachandran <prabhu at aero.iitm.ernet.in> writes:
AD> 2. How would I step through the img variable in the C code?
AD> For example would the green value at pixel i,j be referenced
AD> as img[i][j] or img[i + j*rows] or img[(i + j*rows)*2] ?
PR> Depends on how you weave the magic. If you use
PR> converters.blitz you can simply use img(i,j,k). If you don't
PR> use the blitz converter you will only get a flattened array
PR> (i.e. a 1D array) from Numeric and will need to do pointer
PR> arithmetic appropriately to access the elements.
I've attached yet another variant that does the array access directly
using pointer arithmetic. This does not use blitz arrays and builds
faster. I've also swapped the row and cols names to be consistent
with row-major notation and storage.
-------------- next part --------------
from scipy import imread
from weave import converters
rows, cols = 2, 3
img = Numeric.zeros((rows, cols, 3), 'b')
count = 0
for i in range(rows):
for j in range(cols):
for k in range(3):
img[i,j,k] = count
count += 1
# Notice the following:
# 1. '\\n' to escape generating a newline in the C++ code.
# 2. Access array using img(i, j, k)
# 3. The use of the type_converters=converters.blitz. This is
# what makes it possible to access img using img(i, j, k).
code = """
for (int i=0; i < rows; i++)
for (int j=0; j < cols; j++)
printf("img[%3d][%3d]=%3d %3d %3d\\n",
i, j, img[(i*cols + j)*3], img[(i*cols + j)*3 + 1],
img[(i*cols + j)*3 + 2]);
weave.inline(code, ['img', 'rows', 'cols'])
More information about the SciPy-user