3

Given a topographic raster image.

How to iterate through all pixels of this raster image ?

Solution preferably with gdal, in console. Some other libraries/script could be installed.


Related: Gdal: How to get a pixel's value from raster image?, How to conditionnally assign a new value to pixels of a raster image?.

3
  • 1
    Why? The answer will depend on what you are trying to do as it may not be necessary to loop through all pixels individually... Also, please edit your question to include what software you want to use/have access to (i.e gdal utilities, gdal python bindings etc...)
    – user2856
    Aug 20, 2013 at 2:26
  • 1. Iterate though all pixels of an image is a central question for image processing, it would be good to have an how to here. 2. My own need is How to conditionnally assign a new value to pixels of a raster image? (gis.stackexchange.com/questions/69129), in this other question, looping through all is not necessary.
    – Hugolpz
    Aug 20, 2013 at 2:32
  • 2
    Iterating through pixels (as an end user) is rarely, if ever required, especially in a console script. There is plenty of image processing software available where the iteration happens in a low level compiled language and is optimised to be fast.
    – user2856
    Aug 20, 2013 at 3:20

2 Answers 2

2

If by "using GDAL" you include writing code using the library as opposed to the more limited capabilities available only using the utilities from the command line, then the GDAL API tutorial walks you through all the steps to open a raster image, access its various properties (size, # of bands, rotation/skew, etc), and finally how to read and write a rasterband using the GDALRasterBand::RasterIO method. At this point you can do anything your programming language (and programming skills) allow.

1
  • Oh... well, I was not expecting to have to write it myself. But +1 for the trick :)
    – Hugolpz
    Aug 20, 2013 at 3:21
2

The GDAL utilities are command line tools of convenience for standard geoprocessing tasks but if what you want is not there then you will have to write code to call the underlying API yourself to build your own tool.

Here are a set of tutorials on using GDAL which will explain how to do what you want. However, I recommend that, unless you absolutely have to, you really don't want to loop through a raster pixel by pixel as this is very slow. This tutorial will explain why (with some bench marking) and show some better methods for some typical calculations.

When it comes to raster processing through GDAL, NumPy and SciPy are a crucial part of your coding mix (assuming you're doing this with the Python libraries of GDAL). SciPy especially has some very powerful tools for image processing. In particular have a look at scipy.ndimage. I often just use GDAL to open or save the raster data and have NumPy or SciPy do the heavy lifting. With SciPy, for instance, I have reduced my processing time for Zonal Statistics from hours to seconds for the exact same raster and zones.

So, unless there is a pre-made GDAL Utility, you will have to write something yourself, but using built-in functions in tools like NumPy and SciPy will vastly reduce the amount of code you need to write and your processing time in preference to pixel-by-pixel iteration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.