Differences between revisions 2 and 3
Revision 2 as of 2009-03-23 19:39:56
Size: 414
Comment:
Revision 3 as of 2009-03-23 19:48:30
Size: 1389
Comment:
Deletions are marked like this. Additions are marked like this.
Line 11: Line 11:
}}}
Line 12: Line 13:
Note that if you know the dimensionality of the image, say for example if the image is 2D, you can ignore the 3rd dimension when you use the '''get''' and '''set''' functions, for example like this:
Line 13: Line 15:
{{{#!python
a = test_image()
for j in a.get_ysize():
    for i in a.get_xsize():
        pixel_value = a.get(i,j) # get the pixel value using 2D syntax
        a.set(i,j,pixel_value*3) # set the pixel value using 2D syntax
}}}
Line 14: Line 23:
Also, there is a generic 1D approach one can take using 1D syntax and the '''get_size''' function, which returns the total number of pixels. For example:

{{{#!python
a = test_image()
for i in a.get_size(): # Note get_size, not get_xsize etc.
    pixel_value = a.get(i) # get the pixel value using 1D syntax
    a.set(i,pixel_value*3) # set the pixel value using 1D syntax

a = EMData(10,1,1) # a 1D EMData object
for i in a.get_size(): # In the 1D case this is the same as get_xsize
    # use get and set functions as above

Iterating through the pixels in the EMData object

In your Python script you can use commands like this to iterate through the pixels in an EMData object

   1 a = test_image()
   2 for k in a.get_zsize():
   3     for j in a.get_ysize():
   4         for i in a.get_xsize():
   5             pixel_value = a.get(i,j,k) # get the pixel value
   6             a.set(i,j,k,pixel_value*3) # set the pixel value

Note that if you know the dimensionality of the image, say for example if the image is 2D, you can ignore the 3rd dimension when you use the get and set functions, for example like this:

   1 a = test_image()
   2 for j in a.get_ysize():
   3     for i in a.get_xsize():
   4         pixel_value = a.get(i,j) # get the pixel value using 2D syntax
   5         a.set(i,j,pixel_value*3) # set the pixel value using 2D syntax

Also, there is a generic 1D approach one can take using 1D syntax and the get_size function, which returns the total number of pixels. For example:

   1 a = test_image()
   2 for i in a.get_size(): # Note get_size, not get_xsize etc.
   3     pixel_value = a.get(i) # get the pixel value using 1D syntax
   4     a.set(i,pixel_value*3) # set the pixel value using 1D syntax
   5 
   6 a = EMData(10,1,1) # a 1D EMData object
   7 for i in a.get_size(): # In the 1D case this is the same as get_xsize
   8     # use get and set functions as above

EMAN2/Tutorials/iter_pixels (last edited 2015-08-14 22:11:14 by SteveLudtke)