Source code for ipcv.ip.pixels

"""
Iterators to access images

Thusfar only the rasterscan iterator for an $n$ dimensional array is
implemented.

In some image operators (that are 'recursive') the scanning order if
of great importance. Think of the morphological reconstructions and
the like. Or the error diffusion dithering.

We would like have to have the *reverse* rasterscan too. But also the
rasterscans with an arbitrary order of the axes. Any permutation of
the axes including reverse direction for all individual axes are just
matter of shuffling the start, end and step arrays.  What would be a
logical way to do so?

More elaborate scanning orders like a space filling Hilbert curve
would be nice too.
"""

from itertools import product
from ipcv.utils.errors import IPCVError
from ipcv.utils.timing import Timer
from numpy import array, ndarray, arange, rint, int

class pixel(tuple):  # is this really what it takes to allow use of pixel in the pixel class def?
    pass

class pixel(tuple):
    def __lt__(self, p):
        return all( [a < b for a,b in zip(self, p)] )
    
    def __le__(self, p):
        return all( [a <= b for a,b in zip(self, p)] )
    
    def isin(self, end, start=pixel((0,0))):
        return all( [a<x and x<b for a,b,x in zip(start,end,self)] )

    def rint(self):
        return pixel([rint(a).astype(int) for a in self])

[docs]def domainIterator(end, start=None, step=None): """ Returns an iterator that yields all multi-indices in the range (start)-(end) with stepsizes (step) in rasterscan order """ if type(end) != ndarray: end = array(end); if start == None: start = 0*end; elif type(start) != ndarray: start = array(start) if len(start) != len(end): raise IPCVError("start and end should have same length") if step != None and type(step) != ndarray: step = array(step) if step == None: return product(*[ arange(*p) for p in zip(start, end) ]) else: return product(*[ arange(*p) for p in zip(start, end, step) ])
if __name__=='__main__': import numpy as np import matplotlib.pyplot as plt from numba import jit print( pixel((2,2,2)) > pixel((1,0,1)) ) p = pixel((1.2, 2.2)).rint() print(p) a = arange(12).reshape(3,4) print(a[p]) a = plt.imread('/home/rein/Dropbox/UvA/LocalFeatures/images/trui.png') plt.imshow(a); b = np.zeros_like(a) with Timer("DomainIterator"): for i in range(10): for p in domainIterator(a.shape[:2]): b[p] = 1 - a[p] @jit def inverse(f): M,N = f.shape[:2] g = np.zeros_like(f) for i in range(M): for j in range(N): g[i,j] = 1 - f[i,j] return g with Timer("jit"): for i in range(10): b = inverse(a) plt.imshow(b) plt.show()