.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/introduction/demo_image_coordinates.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_introduction_demo_image_coordinates.py: =================== Image coordinates =================== Description of the image coordinates used to define the motion energy filters. Coordinates are specified as (vertical, horizontal). * (0,0): top-left * (1,0): bottom-left * (0,aspect_ratio): top-right * (1,aspect_ratio): bottom-right .. GENERATED FROM PYTHON SOURCE LINES 18-22 1:1 square aspect ratio ####################### .. GENERATED FROM PYTHON SOURCE LINES 22-25 .. code-block:: default import moten pyramid = moten.get_default_pyramid(vhsize=(100,100), fps=24) .. GENERATED FROM PYTHON SOURCE LINES 26-27 The vertical and horizontal positions of each filter are stored as ``centerv`` and ``centerh``, respectively, in the filter dictionary: .. GENERATED FROM PYTHON SOURCE LINES 27-31 .. code-block:: default from pprint import pprint example_filter = pyramid.filters[200].copy() pprint((example_filter['centerv'], example_filter['centerh'])) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none (0.2375, 0.2375) .. GENERATED FROM PYTHON SOURCE LINES 32-34 This is what the spatial components of this filter look like. (Note that we will only visualize the cosine component in the rest of this example). .. GENERATED FROM PYTHON SOURCE LINES 34-42 .. code-block:: default import numpy as np import matplotlib.pyplot as plt ssin, scos = pyramid.get_filter_spatial_quadrature(example_filter) fig, axes = plt.subplots(ncols=2, sharex=True, sharey=True) _ = axes[0].imshow(ssin, vmin=-1, vmax=1) _ = axes[1].imshow(scos, vmin=-1, vmax=1) .. image:: /auto_examples/introduction/images/sphx_glr_demo_image_coordinates_001.png :alt: demo image coordinates :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 43-48 We can move this filter to different positions in the image. Each filter has the parameters ``centerh`` and ``centerv``. When both are zero, the filter is located at the top left corner of the image. To move the filter to different positions, all we need to do is update the ``centerh`` and ``centerv`` parameters in the filter dictionary. The position parameters are defined in terms of percentages of height. Below, we define 4 new positions located at a 20% distance from the top, bottom, left and right. .. GENERATED FROM PYTHON SOURCE LINES 48-79 .. code-block:: default vh_positions = [(0.2, 0.2), # top left (0.2, 0.8), # top right (0.8, 0.2), # bottom left (0.8, 0.8), # bottom right ] fig, axes = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True) for idx, (vpos, hpos) in enumerate(vh_positions): axidx = np.unravel_index(idx, axes.shape) ax = axes[axidx] # Here we update the position of the filter modified_filter = example_filter.copy() modified_filter['centerv'] = vpos modified_filter['centerh'] = hpos # Show the filter ssin, scos = pyramid.get_filter_spatial_quadrature(modified_filter) ax.imshow(scos, vmin=-1, vmax=1.0) # Label its position vsize = pyramid.definition['vdim'] ax.text(hpos*vsize, vpos*vsize, '+', va='center', ha='center', fontsize=15) ax.text(hpos*vsize, vpos*vsize, (vpos, round(hpos,2)), va='bottom', ha='left', rotation=45) ax.set_xticks([20, 40, 60, 80]) ax.set_yticks([20, 40, 60, 80]) ax.grid(True) _ = fig.suptitle('1:1 square image') .. image:: /auto_examples/introduction/images/sphx_glr_demo_image_coordinates_002.png :alt: 1:1 square image :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 80-85 16:9 aspect ratio ################# In the example above, the width and height of the image is the same (100x100 pixels). For most videos, this is not the case. Videos typically have a width/height aspect ratio of 4:3 (e.g. 800x600) or 16:9 (e.g. 1920x1080). Below, we define a pyramid with a 16:9 aspect ratio. .. GENERATED FROM PYTHON SOURCE LINES 85-88 .. code-block:: default pyramid = moten.get_default_pyramid(vhsize=(576 , 1024), fps=24) vsize = pyramid.definition['vdim'] .. GENERATED FROM PYTHON SOURCE LINES 89-90 We update the aspect ratio of the example filter to match the new pyramid. .. GENERATED FROM PYTHON SOURCE LINES 90-94 .. code-block:: default aspect_ratio = pyramid.definition['aspect_ratio'] assert np.allclose(aspect_ratio, 16/9) example_filter['aspect_ratio'] = aspect_ratio .. GENERATED FROM PYTHON SOURCE LINES 95-97 This 16:9 aspect ratio means that the right-most point of the image is `(16/9)*height`. In order to position filters at the desired 20% and 80% horizontal positions, we need to scale the horizontal dimension by the aspect ratio: .. GENERATED FROM PYTHON SOURCE LINES 97-104 .. code-block:: default vh_positions = [(0.2, aspect_ratio*0.2), # top left (0.2, aspect_ratio*0.8), # top right (0.8, aspect_ratio*0.2), # bottom left (0.8, aspect_ratio*0.8), # bottom right ] .. GENERATED FROM PYTHON SOURCE LINES 105-106 Now, we can plot the filters at the desired positions using the same code: .. GENERATED FROM PYTHON SOURCE LINES 106-127 .. code-block:: default fig, axes = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True) for idx, (vpos, hpos) in enumerate(vh_positions): axidx = np.unravel_index(idx, axes.shape) ax = axes[axidx] modified_filter = example_filter.copy() modified_filter['centerv'] = vpos modified_filter['centerh'] = hpos ssin, scos = pyramid.get_filter_spatial_quadrature(modified_filter) ax.imshow(scos, vmin=-1, vmax=1.0) vsize = pyramid.definition['vdim'] ax.text(hpos*vsize, vpos*vsize, '+', va='center', ha='center', fontsize=15) ax.text(hpos*vsize, vpos*vsize, (vpos, round(hpos,2)), va='bottom', ha='left', rotation=45) _ = fig.suptitle('16:9 image') # sphinx_gallery_thumbnail_number = 2 .. image:: /auto_examples/introduction/images/sphx_glr_demo_image_coordinates_003.png :alt: 16:9 image :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.843 seconds) .. _sphx_glr_download_auto_examples_introduction_demo_image_coordinates.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_image_coordinates.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_image_coordinates.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_