Size: 44
Comment:
|
Size: 1392
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
Describe EMAN2/Tutorials/iter_pixels here. | = 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 {{{#!python a = test_image() for k in a.get_zsize(): for j in a.get_ysize(): for i in a.get_xsize(): pixel_value = a.get(i,j,k) # get the pixel value 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: {{{#!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 }}} 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 1D 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
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:
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 1D get and set functions as above