Skip to content
Snippets Groups Projects
alg.rst 5.42 KiB

:mod:`~ost.img.alg` - Image Processing Algorithms

Applying algorithms

While image properties are usually manipulated using method of the :class:'~ost.img.ImageHandle` class, their data content is manipulated using image algorithms. Image algorithms are objects. Each of them is a class, and its methods are used to handle the algorithm parameters. Applying an algorithm to an image is then conceptually a two-step process. First, an instance of an algorithm class is created, yielding an algorithm object. In a second step, the algorithm object is applied to an image. An algorithm can be applied in-place (using the :meth:`ost.img.ImageHandle.Apply` method), modifying the image. or out-of-place, (using :meth:`~ost.img.ImageHandle.ApplyIP` ), leaving the original image untouched, and returning the result as a new image.

Here is an example that uses the :class:`Randomize` algorithm. This algorithm feels the pixels of the image with random values between 0.0 and 1.0

# creates an algorithm object
rand_alg = img.alg.Randomize()
# applies algorithm object in place, overwriting the image
im.ApplyIP( rand_alg )

Now that we have some (noisy) data present, let us run another algorithm, this time a Gaussian filter with a sigma of 4 pixel.

im.ApplyIP( img.alg.GaussianFilter(4.0) ) # apply temporary algorithm object in-place As you can see, it is not always necessary to create an independent algorithm instance first, in many cases a temporary object will suffice (this applies to the randomization algorithm as well, 'im.ApplyIP(alg.Randomize())' would have been fine). However, when used this way, the algorithm class will cease to exist as soon as the algorithm is applied. This can be important if the algorithm stores some values that need to be recovered later. For example:

stat=img.alg.Stat() im.ApplyIP(stat) mean=stat.GetMean() Algorithms are stateful objects and can store values. The 'Stat' algorithm computes basic statistics about the image it is applied on (maximum and minimum values, standard deviations, etc). The data are stored within the algorithm instance and can be recovered using the algorithm's methods. It would obviously make very little sense not to create an instance of the 'Stat' algorithm. When the algorithms ceases to exist, all information would be lost.

Applying a Fourier Transform