.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "11_demos\itom\dataProcessing\demo_LateralShift.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_11_demos_itom_dataProcessing_demo_LateralShift.py: Later shifted image =================== In this demo the lateral image shift is determined. .. GENERATED FROM PYTHON SOURCE LINES 6-35 .. code-block:: Python import numpy as np import scipy import scipy.misc import scipy.ndimage import matplotlib.pyplot as plt from numpy.typing import ArrayLike def plotImage(image: ArrayLike, cmap: str = "gray"): plt.figure() plt.gray() plt.imshow(image, cmap=cmap) plt.show() if scipy.__version__ <= "0.14.0": # removed due to licensing reasons image = scipy.misc.lena() else: image = scipy.misc.face() # Convert the image R = image[:, :, 0] G = image[:, :, 1] B = image[:, :, 2] image = R * 299.0 / 1000 + G * 587.0 / 1000 + B * 114.0 / 1000 plotImage(image) .. image-sg:: /11_demos/itom/dataProcessing/images/sphx_glr_demo_LateralShift_001.png :alt: demo LateralShift :srcset: /11_demos/itom/dataProcessing/images/sphx_glr_demo_LateralShift_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none C:\Workspace\itom_development_VS2019_Qt5.15.2_x64\itomProject\itom\demo\itom\dataProcessing\demo_LateralShift.py:26: DeprecationWarning: scipy.misc.face has been deprecated in SciPy v1.10.0; and will be completely removed in SciPy v1.12.0. Dataset methods have moved into the scipy.datasets module. Use scipy.datasets.face instead. .. GENERATED FROM PYTHON SOURCE LINES 36-37 Amount of pixel shift in x- and y-direction .. GENERATED FROM PYTHON SOURCE LINES 37-40 .. code-block:: Python xPixelShift = 16 yPixelShift = -7 .. GENERATED FROM PYTHON SOURCE LINES 41-42 Determine the ROI size: relative (centered) size of original image (relativeSize=1: original size). .. GENERATED FROM PYTHON SOURCE LINES 42-52 .. code-block:: Python row, col = image.shape relativeSize = ( np.floor(min(1 - abs(xPixelShift) / col, 1 - abs(yPixelShift) / row) * 10) / 10 ) x0 = int((col - col * relativeSize) / 2) x1 = col - x0 + 1 y0 = int((row - row * relativeSize) / 2) y1 = row - y0 + 1 .. GENERATED FROM PYTHON SOURCE LINES 53-54 not shifted ROI .. GENERATED FROM PYTHON SOURCE LINES 54-63 .. code-block:: Python image1 = image[y0:y1, x0:x1].copy() plotImage(image1) # Shifted ROI image2 = image[ y0 + yPixelShift : y1 + yPixelShift, x0 + xPixelShift : x1 + xPixelShift ].copy() plotImage(image2) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /11_demos/itom/dataProcessing/images/sphx_glr_demo_LateralShift_002.png :alt: demo LateralShift :srcset: /11_demos/itom/dataProcessing/images/sphx_glr_demo_LateralShift_002.png :class: sphx-glr-multi-img * .. image-sg:: /11_demos/itom/dataProcessing/images/sphx_glr_demo_LateralShift_003.png :alt: demo LateralShift :srcset: /11_demos/itom/dataProcessing/images/sphx_glr_demo_LateralShift_003.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 64-65 Determine the pixel shift using discrete fast fourier transformation and complex conjugation of ``image2``. .. GENERATED FROM PYTHON SOURCE LINES 65-75 .. code-block:: Python image1FFT = np.fft.fft2(image1) image2FFT = np.conjugate(np.fft.fft2(image2)) # inverse fourier transformation of product -> equal to cross correlation imageCCor = np.real(np.fft.ifft2(image1FFT * image2FFT)) # Shift the zero-frequency component to the center of the spectrum imageCCorShift = np.fft.fftshift(imageCCor) plotImage(imageCCorShift, "hot") .. image-sg:: /11_demos/itom/dataProcessing/images/sphx_glr_demo_LateralShift_004.png :alt: demo LateralShift :srcset: /11_demos/itom/dataProcessing/images/sphx_glr_demo_LateralShift_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 76-77 Determine the distance of the maximum from the center .. GENERATED FROM PYTHON SOURCE LINES 77-86 .. code-block:: Python row, col = image1.shape yShift, xShift = np.unravel_index(np.argmax(imageCCorShift), (row, col)) yShift -= int(row / 2) xShift -= int(col / 2) print("shift of image1 in x-direction [pixel]: " + str(xShift)) print("shift of image1 in y-direction [pixel]: " + str(yShift)) .. rst-class:: sphx-glr-script-out .. code-block:: none shift of image1 in x-direction [pixel]: 16 shift of image1 in y-direction [pixel]: -7 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 11.868 seconds) .. _sphx_glr_download_11_demos_itom_dataProcessing_demo_LateralShift.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_LateralShift.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_LateralShift.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_LateralShift.zip `