A series of tests of the SLIP package.


In [1]:
%load_ext autoreload
%autoreload 2
import os
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
fopts = {'fontsize':18}

In [2]:
from SLIP import Image, imread

In [3]:
help(Image)


Help on class Image in module SLIP.SLIP:

class Image(builtins.object)
 |  This library collects different Image Processing tools.
 |  
 |  Fork me on https://github.com/bicv/SLIP !
 |  
 |  This library is used in other projects, in particular  for use with the ``LogGabor`` and ``SparseEdges`` libraries
 |  For more information check respective pages @
 |      - http://pythonhosted.org/LogGabor and
 |      - http://pythonhosted.org/SparseEdges
 |  
 |  Collects image processing routines for one given image size:
 |   - Some classical related to pure Fourier number crunching:
 |      - creating masks
 |      - normalize,
 |      - fourier_grid : defines a useful grid for generating filters in FFT
 |      - show_FT : displays the envelope and impulse response of a filter
 |      - invert : go to the other of the fourier transform
 |  - Some usual application of Fourier filtering:
 |      - trans : translation filter in Fourier space
 |      - whitening procedures
 |   - Some related to handling experiments:
 |      - load_in_database : loads a random image in a folder and
 |      - patch : takes a random patch of the correct size
 |  
 |  Methods defined here:
 |  
 |  FTfilter(self, image, FT_filter, full=False)
 |      Using the ``FTfilter`` function, it is easy to filter an image with a filter defined in Fourier space.
 |  
 |  __init__(self, pe='https://raw.githubusercontent.com/bicv/SLIP/master/default_param.py')
 |      Initializes the Image class
 |      
 |      May take as input:
 |      
 |      - a dictionary containing parameters
 |      - a ``ndarray`` (dimensions ``N_X`` and ``N_Y`` are guessed from this array)
 |      - a string representing a file or URL pointing to an image file
 |      - a string pointing to  a file or URL containing a dictionary of parameters (or simply the name of the file)
 |      - a ``NeuroTools.parameters.ParameterSet`` object containing parameters
 |      
 |      Parameters are
 |      
 |      - N_X and N_Y which are respectively the number of pixels in the vertical and horizontal dimensions respectively (MANDATORY)
 |      - optional parameters which are used in the various functions such as N_image when handling a database or the whitening parameters.
 |  
 |  dewhitening(self, white)
 |      Returns the dewhitened image
 |  
 |  enveloppe_color(self, alpha)
 |  
 |  extract_patches_2d(self, image, patch_size, N_patches)
 |      Reshape a 2D image into a collection of patches
 |      
 |      redundant with self.patch, but similar call as
 |      https://github.com/scikit-learn/scikit-learn/blob/14031f6/sklearn/feature_extraction/image.py#L300
 |  
 |  fourier(self, image, full=True)
 |      Using the ``fourierr`` function, it is easy to retieve its Fourier transformation.
 |  
 |  fourier_grid(self)
 |      use that function to define a reference frame for envelopes in Fourier space.
 |      In general, it is more efficient to define dimensions as powers of 2.
 |  
 |  frequency_angle(self)
 |  
 |  frequency_radius(self)
 |  
 |  full_url(self, name_database)
 |  
 |  get_imagelist(self, exp, name_database='natural')
 |      returns an imagelist from a pickled database.
 |      
 |      If the stored imagelist does not exist, creates it.
 |      The ``exp`` string allows to tag the list for a particular experiment.
 |  
 |  get_pe(self, pe)
 |      Guesses the parameters from the init variable
 |      
 |      We perform a duck-typing to guess parameters from different possible sources.
 |      outputs a ParameterSet
 |  
 |  get_size(self, im)
 |  
 |  hist_radial_frequency(self, FT, N_f=20)
 |      A simple function to compute a radial histogram in different spatial frequency bands.
 |  
 |  imread(self, URL, resize=True)
 |  
 |  imshow(self, image, fig=None, ax=None, cmap=<matplotlib.colors.LinearSegmentedColormap object at 0x1113fd5f8>, axis=False, norm=True, center=True, xlabel='Y axis', ylabel='X axis', figsize=None, mask=False, vmin=-1, vmax=1)
 |      Plotting routine to show an image
 |      
 |      Place the [0,0] index of the array in the upper left  corner of the axes.
 |      Data limits for the axes. The default assigns zero-based row, column
 |      indices to the x, y centers of the pixels.
 |      Note that the convention for coordinates follows that of matrices: the
 |      origin is at the top left of the image, and coordinates are first the
 |      rows (vertical axis, going down) then the columns (horizontal axis,
 |      going right).
 |  
 |  init(self, mask_exponent=3.0)
 |      Initializes different convenient matrices for image processing.
 |      
 |      To be called when keeping the same Image object but changing the size of the image.
 |  
 |  init_logging(self, filename='debug.log', name='SLIP')
 |  
 |  invert(self, FT_image, full=False)
 |      # Fourier number crunching
 |  
 |  list_database(self, name_database)
 |      Returns a list of the files in a folder
 |  
 |  load_in_database(self, name_database, i_image=None, filename=None, verbose=True)
 |      Loads a random image from the database ``name_database``.
 |      
 |      The strategy is to pick one image in the folder using the list provided by the ``list_database``function.
 |      
 |      TODO: it would be useful to be able to load from standard databases such as http://www.cps.utexas.edu/natural_scenes/db.shtml
 |  
 |  low_pass(self, f_0, steepness)
 |      Returns the low_pass filter used by (Olshausen, 98)
 |      
 |      parameters from Atick (p.240)
 |      f_0 = 22 c/deg in primates: the full image is approx 45 deg
 |      alpha makes the aspect change (1=diamond on the vert and hor, 2 = anisotropic)
 |      
 |      from Olshausen 98 (p.11):
 |      f_0  = 200 cycles / image (512 x 512 images)
 |      in absolute coordinates : f_0 = 200 / 512 / 2
 |      
 |      steepness is to produce a "fairly sharp cutoff"
 |  
 |  make_imagelist(self, name_database, verbose=False)
 |      Makes a list of images with no repetition.
 |      
 |      Takes as an input the name of a database (the name of a folder in the ``datapath``),
 |      returns a list of the filenames along with the crop area.
 |  
 |  mkdir(self)
 |      Initializes two folders for storing intermediate matrices and images.
 |      
 |      To be called before any operation to store or retrieve a result or figure.
 |  
 |  normalize(self, image, preprocess=True, center=True, use_max=True)
 |  
 |  olshausen_whitening_filt(self)
 |      Returns the whitening filter used by (Olshausen, 98)
 |  
 |  patch(self, name_database, i_image=None, filename=None, croparea=None, threshold=0.2, verbose=True, preprocess=True, center=True, use_max=True, do_whitening=False)
 |      takes a subimage of size s (a tuple)
 |      
 |      does not accept if energy is relatively below a threshold (flat image)
 |  
 |  power_spectrum(self, image)
 |  
 |  preprocess(self, image)
 |      Returns the pre-processed image
 |      
 |      From raw pixelized images, we want to keep information that is relevent to the content of
 |      the objects in the image. In particular, we want to avoid:
 |      
 |          - information that would not be uniformly distributed when rotating the image. In
 |          particular, we discard information outside the unit disk in Fourier space, in particular
 |          above the Nyquist frequency,
 |          - information that relates to information of the order the size of the image. This
 |          involves discarding information at low-level frequencies.
 |      
 |      See https://laurentperrinet.github.io/sciblog/posts/2015-05-21-a-simple-pre-processing-filter-for-image-processing.html
 |      for more information.
 |  
 |  retina(self, df=0.07, sigma=0.5)
 |      A parametric description of the envelope of retinal processsing.
 |      See https://laurentperrinet.github.io/sciblog/posts/2015-05-21-a-simple-pre-processing-filter-for-image-processing.html
 |      for more information.
 |      
 |      In digital images, some of the energy in Fourier space is concentrated outside the
 |      disk corresponding to the Nyquist frequency. Let's design a filter with:
 |      
 |          - a sharp cut-off for radial frequencies higher than the Nyquist frequency,
 |          - times a smooth but sharp transition (implemented with a decaying exponential),
 |          - times a high-pass filter designed by one minus a gaussian blur.
 |      
 |      This filter is rotation invariant.
 |      
 |      Note that this filter is defined by two parameters:
 |          - one for scaling the smoothness of the transition in the high-frequency range,
 |          - one for the characteristic length of the high-pass filter.
 |      
 |      The first is defined relative to the Nyquist frequency (in absolute values) while the second
 |      is relative to the size of the image in pixels and is given in number of pixels.
 |  
 |  savefig(self, fig, fname, figpath='', formats=None, display=True)
 |  
 |  set_size(self, im)
 |      Re-initializes the Image class with  the size given in ``im``
 |      
 |      May take as input:
 |      
 |      - a numpy array,
 |      - a string representing a file or URL pointing to an image file
 |      - a tuple
 |      
 |      Updated parameters are
 |      
 |      - N_X and N_Y which are respectively the number of pixels in the vertical and horizontal dimensions respectively (MANDATORY)
 |  
 |  show_FT(self, FT_image, fig=None, figsize=None, a1=None, a2=None, axis=False, title=True, FT_title='Spectrum', im_title='Image', norm=True, vmin=-1.0, vmax=1.0)
 |  
 |  show_image_FT(self, image, FT_image, fig=None, figsize=None, a1=None, a2=None, axis=False, title=True, FT_title='Spectrum', im_title='Image', norm=True, vmin=-1.0, vmax=1.0)
 |  
 |  show_spectrum(self, image, fig=None, figsize=None, a1=None, a2=None, axis=False, title=True, FT_title='Spectrum', im_title='Image', norm=True, vmin=-1.0, vmax=1.0)
 |  
 |  trans(self, u, v)
 |  
 |  translate(self, image, vec, preshift=True)
 |      Translate image by vec (in pixels)
 |      
 |      Note that the convention for coordinates follows that of matrices: the origin is at the top left of the image, and coordinates are first the rows (vertical axis, going down) then the columns (horizontal axis, going right).
 |  
 |  whitening(self, image)
 |      Returns the whitened image
 |  
 |  whitening_filt(self, recompute=False)
 |      Returns the envelope of the whitening filter.
 |      
 |      if we chose one based on structural assumptions (``struct=True``)
 |          then we return a 1/f spectrum based on the assumption that the structure of images
 |          is self-similar and thus that the Fourier spectrum scales a priori in 1/f.
 |      
 |      elif we chose to learn,
 |          returns theaverage correlation filter in FT space.
 |      
 |          Computes the average power spectrum = FT of cross-correlation, the mean decorrelation
 |          is given for instance by (Attick, 92).
 |      
 |      else
 |          we return the parametrization based on Olshausen, 1996
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

initializing the SLIP object

The SLIP object may be initialized by giving some parameters. These could be given using

  • nothing

In [4]:
im = Image()  # default is 'https://raw.githubusercontent.com/bicv/SLIP/master/default_param.py'
print(im.pe)


{'N_X': 256, 'N_Y': 256, 'do_mask': True, 'mask_exponent': 3.0, 'use_cache': True, 'verbose': 0, 'figpath': 'results', 'matpath': 'cache_dir', 'datapath': 'database', 'figsize': 14.0, 'formats': ['pdf', 'svg', 'jpg'], 'dpi': 450, 'seed': None, 'N_image': None, 'white_name_database': 'kodakdb', 'white_n_learning': 0, 'white_N': 0.07, 'white_f_0': 0.38, 'white_alpha': 1.4, 'white_steepness': 4.0, 'white_recompute': False}
  • a dictionary (dimensions N_X and N_Y and mask_exponent are mandatory)

In [5]:
im = Image({'N_X':32, 'N_Y':64})
print(im.pe)


{'N_X': 32, 'N_Y': 64, 'verbose': 30, 'mask_exponent': 3.0}
  • a ndarray (dimensions N_X and N_Y are guessed from this array)

In [6]:
im = Image(np.empty((32, 64)))
print(im.pe)


{'N_X': 32, 'N_Y': 64, 'verbose': 30, 'mask_exponent': 3.0}
  • a string indicating the path of a image file

In [7]:
im = Image('database/lena64.png')
print(im.pe)


{'N_X': 64, 'N_Y': 64, 'verbose': 30, 'mask_exponent': 3.0}
  • a string indicating the URL to a image file

In [8]:
im = Image('http://upload.wikimedia.org/wikipedia/commons/d/de/Wikipedia_Logo_1.0.png')
print(im.pe)


{'N_X': 1058, 'N_Y': 1058, 'verbose': 30, 'mask_exponent': 3.0}
  • a string indicating the path of a file containing a dictionary

In [9]:
im = Image('file://default_param.py')
im = Image('default_param.py')
print(im.pe.keys())


dict_keys(['N_X', 'N_Y', 'do_mask', 'mask_exponent', 'use_cache', 'verbose', 'figpath', 'matpath', 'datapath', 'figsize', 'formats', 'dpi', 'seed', 'N_image', 'white_name_database', 'white_n_learning', 'white_N', 'white_f_0', 'white_alpha', 'white_steepness', 'white_recompute'])
  • a string indicating the URL of a file containing a dictionary

In [10]:
im = Image('https://raw.githubusercontent.com/bicv/SLIP/master/default_param.py')
print(im.pe.keys())


dict_keys(['N_X', 'N_Y', 'do_mask', 'mask_exponent', 'use_cache', 'verbose', 'figpath', 'matpath', 'datapath', 'figsize', 'formats', 'dpi', 'seed', 'N_image', 'white_name_database', 'white_n_learning', 'white_N', 'white_f_0', 'white_alpha', 'white_steepness', 'white_recompute'])
  • a NeuroTools object

In [11]:
from NeuroTools.parameters import ParameterSet
from SLIP import Image
im = Image(ParameterSet({'N_X':128, 'N_Y':256}))

The parameters may be handled using the properties of the ParameterSet object.

resizing the SLIP object

The SLIP object may be adapted to a new size

  • nothing

In [12]:
help(im.set_size)


Help on method set_size in module SLIP.SLIP:

set_size(im) method of SLIP.SLIP.Image instance
    Re-initializes the Image class with  the size given in ``im``
    
    May take as input:
    
    - a numpy array,
    - a string representing a file or URL pointing to an image file
    - a tuple
    
    Updated parameters are
    
    - N_X and N_Y which are respectively the number of pixels in the vertical and horizontal dimensions respectively (MANDATORY)


In [13]:
im = Image()  # default is {'N_X':128, 'N_Y':128}
print(im.pe)


{'N_X': 256, 'N_Y': 256, 'do_mask': True, 'mask_exponent': 3.0, 'use_cache': True, 'verbose': 0, 'figpath': 'results', 'matpath': 'cache_dir', 'datapath': 'database', 'figsize': 14.0, 'formats': ['pdf', 'svg', 'jpg'], 'dpi': 450, 'seed': None, 'N_image': None, 'white_name_database': 'kodakdb', 'white_n_learning': 0, 'white_N': 0.07, 'white_f_0': 0.38, 'white_alpha': 1.4, 'white_steepness': 4.0, 'white_recompute': False}

In [14]:
im.set_size((512, 234))
print(im.pe)


{'N_X': 512, 'N_Y': 234, 'do_mask': True, 'mask_exponent': 3.0, 'use_cache': True, 'verbose': 0, 'figpath': 'results', 'matpath': 'cache_dir', 'datapath': 'database', 'figsize': 14.0, 'formats': ['pdf', 'svg', 'jpg'], 'dpi': 450, 'seed': None, 'N_image': None, 'white_name_database': 'kodakdb', 'white_n_learning': 0, 'white_N': 0.07, 'white_f_0': 0.38, 'white_alpha': 1.4, 'white_steepness': 4.0, 'white_recompute': False}

In [15]:
im.set_size(np.ones((512, 234)))
print(im.pe)


{'N_X': 512, 'N_Y': 234, 'do_mask': True, 'mask_exponent': 3.0, 'use_cache': True, 'verbose': 0, 'figpath': 'results', 'matpath': 'cache_dir', 'datapath': 'database', 'figsize': 14.0, 'formats': ['pdf', 'svg', 'jpg'], 'dpi': 450, 'seed': None, 'N_image': None, 'white_name_database': 'kodakdb', 'white_n_learning': 0, 'white_N': 0.07, 'white_f_0': 0.38, 'white_alpha': 1.4, 'white_steepness': 4.0, 'white_recompute': False}

In [16]:
im.set_size('http://upload.wikimedia.org/wikipedia/commons/d/de/Wikipedia_Logo_1.0.png')
print(im.pe)


{'N_X': 1058, 'N_Y': 1058, 'do_mask': True, 'mask_exponent': 3.0, 'use_cache': True, 'verbose': 0, 'figpath': 'results', 'matpath': 'cache_dir', 'datapath': 'database', 'figsize': 14.0, 'formats': ['pdf', 'svg', 'jpg'], 'dpi': 450, 'seed': None, 'N_image': None, 'white_name_database': 'kodakdb', 'white_n_learning': 0, 'white_N': 0.07, 'white_f_0': 0.38, 'white_alpha': 1.4, 'white_steepness': 4.0, 'white_recompute': False}

testing logging

we use https://docs.python.org/3.4/library/logging.html to handle logging of events


In [17]:
im = Image()
print('Verbosity level=', im.pe.verbose)


Verbosity level= 0

In [18]:
im.log.debug(' > this should not appear')
im.log.info(' > this should not appear')
im.log.error(' > this *should* appear')
Logging Levels -------------- The numeric values of logging levels are given in the following table. These are primarily of interest if you want to define your own levels, and need them to have specific values relative to the predefined levels. If you define a level with the same numeric value, it overwrites the predefined value; the predefined name is lost. +--------------+---------------+ | Level | Numeric value | +==============+===============+ | ``CRITICAL`` | 50 | +--------------+---------------+ | ``ERROR`` | 40 | +--------------+---------------+ | ``WARNING`` | 30 | +--------------+---------------+ | ``INFO`` | 20 | +--------------+---------------+ | ``DEBUG`` | 10 | +--------------+---------------+ | ``NOTSET`` | 0 | +--------------+---------------+

In [19]:
im.pe.verbose = 15
im.init_logging()
im.log.debug(' > this should not appear')
im.log.info(' > this *should* appear')
im.log.error(' > this *should* appear')

basic parameter check

TODO


In [20]:
try:
    Image({'N_X':128, 'N_Y':-12, 'mask_exponent':3})
except Exception as e:
    print(e)


negative dimensions are not allowed

testing reading an image file

Note that if the image is of a different size, the size of the Image object is adapted:


In [21]:
im = Image()
print(im.pe)
image = im.imread('database/lena64.png')
print(im.pe)


{'N_X': 256, 'N_Y': 256, 'do_mask': True, 'mask_exponent': 3.0, 'use_cache': True, 'verbose': 0, 'figpath': 'results', 'matpath': 'cache_dir', 'datapath': 'database', 'figsize': 14.0, 'formats': ['pdf', 'svg', 'jpg'], 'dpi': 450, 'seed': None, 'N_image': None, 'white_name_database': 'kodakdb', 'white_n_learning': 0, 'white_N': 0.07, 'white_f_0': 0.38, 'white_alpha': 1.4, 'white_steepness': 4.0, 'white_recompute': False}
{'N_X': 64, 'N_Y': 64, 'do_mask': True, 'mask_exponent': 3.0, 'use_cache': True, 'verbose': 0, 'figpath': 'results', 'matpath': 'cache_dir', 'datapath': 'database', 'figsize': 14.0, 'formats': ['pdf', 'svg', 'jpg'], 'dpi': 450, 'seed': None, 'N_image': None, 'white_name_database': 'kodakdb', 'white_n_learning': 0, 'white_N': 0.07, 'white_f_0': 0.38, 'white_alpha': 1.4, 'white_steepness': 4.0, 'white_recompute': False}

In [22]:
image = im.imread('http://upload.wikimedia.org/wikipedia/commons/d/de/Wikipedia_Logo_1.0.png')
print(im.pe)


{'N_X': 1058, 'N_Y': 1058, 'do_mask': True, 'mask_exponent': 3.0, 'use_cache': True, 'verbose': 0, 'figpath': 'results', 'matpath': 'cache_dir', 'datapath': 'database', 'figsize': 14.0, 'formats': ['pdf', 'svg', 'jpg'], 'dpi': 450, 'seed': None, 'N_image': None, 'white_name_database': 'kodakdb', 'white_n_learning': 0, 'white_N': 0.07, 'white_f_0': 0.38, 'white_alpha': 1.4, 'white_steepness': 4.0, 'white_recompute': False}

testing showing an image file


In [23]:
'N_X' in im.pe.keys()


Out[23]:
True

In [24]:
im = Image()
print(im.pe)
image = im.imread('database/lena64.png')
_ = im.imshow(image)


{'N_X': 256, 'N_Y': 256, 'do_mask': True, 'mask_exponent': 3.0, 'use_cache': True, 'verbose': 0, 'figpath': 'results', 'matpath': 'cache_dir', 'datapath': 'database', 'figsize': 14.0, 'formats': ['pdf', 'svg', 'jpg'], 'dpi': 450, 'seed': None, 'N_image': None, 'white_name_database': 'kodakdb', 'white_n_learning': 0, 'white_N': 0.07, 'white_f_0': 0.38, 'white_alpha': 1.4, 'white_steepness': 4.0, 'white_recompute': False}

In [25]:
image = im.imread('http://upload.wikimedia.org/wikipedia/commons/d/de/Wikipedia_Logo_1.0.png')
_ = im.imshow(image)



In [26]:
_ = im.show_spectrum(image)


testing import and cropping


In [27]:
axs = []
im = Image({'N_X':32, 'N_Y':128, 'seed':None, 'do_mask':False})
im.pe.datapath = 'database/'
for name_database in ['serre07_targets', 'serre07_distractors']:
    fig = plt.figure(figsize=(14, 3.5))
    for _ in range(4):
        image, filename, croparea = im.patch(name_database, )
        ax = fig.add_subplot(1, 4, _+1)
        im.imshow(image, fig=fig, ax=ax)
    fig.show()


Using image  M_N136054.jpg
Cropping @ [top, bottom, left, right]:  [170, 202, 44, 172]
Using image  H_N816021.jpg
Cropping @ [top, bottom, left, right]:  [92, 124, 3, 131]
Using image  F_N408020.jpg
Cropping @ [top, bottom, left, right]:  [87, 119, 26, 154]
Using image  B_N326074.jpg
Cropping @ [top, bottom, left, right]:  [94, 126, 75, 203]
Using image  Hda_obj71.jpg
Cropping @ [top, bottom, left, right]:  [130, 162, 65, 193]
Using image  Bda_art1158.jpg
Cropping @ [top, bottom, left, right]:  [67, 99, 22, 150]
Using image  Fda_urb342.jpg
Cropping @ [top, bottom, left, right]:  [140, 172, 64, 192]
Using image  Bdn_natu977.jpg
Cropping @ [top, bottom, left, right]:  [39, 71, 56, 184]

Handling a database


In [28]:
im = Image({'N_X':64, 'N_Y':128, 'seed':None, 'do_mask':False, 'N_image':10})
im.pe.datapath = 'database/'
help(im.make_imagelist)
# extract one image list from a database
imagelist = im.make_imagelist('serre07_targets')
print('Number of images:', len(imagelist))
print('First image:', imagelist[0])


Help on method make_imagelist in module SLIP.SLIP:

make_imagelist(name_database, verbose=False) method of SLIP.SLIP.Image instance
    Makes a list of images with no repetition.
    
    Takes as an input the name of a database (the name of a folder in the ``datapath``),
    returns a list of the filenames along with the crop area.

Number of images: 10
First image: ['B_N826057.jpg', [57, 121, 35, 163]]

In [29]:
help(im.get_imagelist)
im.pe.figpath, im.pe.matpath = '/tmp/fig', '/tmp/mat'
# store one image list from a database
imagelist = im.get_imagelist('classifier', 'serre07_targets')
!ls -l /tmp/mat
print('Number of images:', len(imagelist))
print('First image:', imagelist[0])


Help on method get_imagelist in module SLIP.SLIP:

get_imagelist(exp, name_database='natural') method of SLIP.SLIP.Image instance
    returns an imagelist from a pickled database.
    
    If the stored imagelist does not exist, creates it.
    The ``exp`` string allows to tag the list for a particular experiment.

total 8
-rw-r--r--  1 laurentperrinet  wheel  384 Jun 14 17:33 classifier_serre07_targets_images.pickle
Number of images: 10
First image: ['M_N415038.jpg', [165, 229, 52, 180]]

In [30]:
im = Image({'N_X':128, 'N_Y':128, 'seed':None, 'do_mask':False, 'N_image':10})
im.pe.figpath, im.pe.matpath = '/tmp/fig', '/tmp/mat'
# now we can access again this stored list
!ls -l /tmp/mat
imagelist = im.get_imagelist('classifier', 'serre07_targets')
print('Number of images:', len(imagelist))
print('First image:', imagelist[0])


total 8
-rw-r--r--  1 laurentperrinet  wheel  384 Jun 14 17:33 classifier_serre07_targets_images.pickle
Number of images: 10
First image: ['M_N415038.jpg', [165, 229, 52, 180]]

checking basic translation mechanism

defining a reference test image; check the axis labels for a (x,y) translation


In [31]:
im = Image({'N_X':128, 'N_Y':256})
image = np.zeros((im.pe.N_X, im.pe.N_Y))
image[im.pe.N_X//2:im.pe.N_X//2+im.pe.N_X//4, im.pe.N_X//2:im.pe.N_X//2+im.pe.N_X//4] = 1 # white square on the right
image[im.pe.N_X//2:im.pe.N_X//2+im.pe.N_X//4, im.pe.N_X//4:im.pe.N_X//2] = -1  # black square on its left
_ = im.imshow(image, axis=True)


translating the image by an integer by rolling indices


In [32]:
print(im.pe.N_X//8., im.pe.N_Y//4.)
_ = im.imshow(np.roll(np.roll(image, np.int(im.pe.N_X//8.), axis=0), np.int(im.pe.N_X//4.), axis=1))


16.0 64.0

remember axis conventions are: x going down, y going right with the origin on top

translating the image down


In [33]:
_ = im.imshow(im.translate(image, [im.pe.N_X//8., 0.]))


translating the image right


In [34]:
_ = im.imshow(im.translate(image, [0., im.pe.N_Y//4.]))


translating the image on both axis


In [35]:
_ = im.imshow(im.translate(image, [im.pe.N_X//8., im.pe.N_Y//4.]))


translating the image over the torus


In [36]:
_ = im.imshow(im.translate(image, [im.pe.N_X//2., im.pe.N_Y//4.]))


some checks that should produce gray images (= null error)

null translation gives the same image


In [37]:
_ = im.imshow(image - im.translate(image, [0., 0.]), norm=False)


the lg.translate function is invertible


In [38]:
_ = im.imshow(image - im.translate(im.translate(image, [1.64, -2.233]), [-1.64, 2.233]), norm=False)


also true for bigger translations (we separate integer part from fractional part in the translation)


In [39]:
_ = im.imshow(image - im.translate(im.translate(image, [182.64, -286.233]), [-182.64, 286.233], preshift=True), norm=False)


but not always true when we don't separate


In [40]:
_ = im.imshow(image - im.translate(im.translate(image, [182.64, -286.233]), [-182.64, 286.233], preshift=False), norm=False)


the lg.translate function is periodic


In [41]:
_ = im.imshow(image - im.translate(image, [-45*im.pe.N_X, 5*im.pe.N_Y]), norm=False)


lg.translate function is associative


In [42]:
_ = im.imshow(im.translate(image, [1., .0]) - im.translate(im.translate(image, [.5, .0]), [.5, .0]), norm=False)



In [43]:
_ = im.imshow(im.translate(image, [2., .0]) - im.translate(im.translate(image, [1.5, -1.0]), [.5, 1.0]), norm=False)


whitening

Here, we test the whitening on different images.


In [44]:
axs = []
im = Image('default_param.py')
im.pe.datapath = 'database/'
for name_database in ['serre07_targets', 'serre07_distractors']:
    for _ in range(4):
        fig = plt.figure(figsize=(14, 7))
        image, filename, croparea = im.patch(name_database, do_whitening=False)
        ax = fig.add_subplot(1, 2, 1)
        fig , ax = im.imshow(image, fig=fig, ax=ax)
        ax = fig.add_subplot(1, 2, 2)
        image, filename, croparea = im.patch(name_database, filename=filename, croparea=croparea, do_whitening=True)
        fig , ax = im.imshow(image, fig=fig, ax=ax)
        plt.show()


Using image  F_N310039.jpg
Using image  B_N707011.jpg
Using image  F_N408052.jpg
Using image  B_N109016.jpg
Using image  Fda_art358.jpg
Using image  Hdn_text6.jpg
Using image  Fdn_nat1004.jpg
Using image  Mdn_nat1253.jpg

more book keeping


In [45]:
%load_ext watermark
%watermark


2018-06-14T17:33:43+02:00

CPython 3.6.5
IPython 6.4.0

compiler   : GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)
system     : Darwin
release    : 17.6.0
machine    : x86_64
processor  : i386
CPU cores  : 36
interpreter: 64bit