.. 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 Click :ref:`here ` 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-34 .. code-block:: default 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. / 1000 + G * 587. / 1000 + B * 114. / 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 .. GENERATED FROM PYTHON SOURCE LINES 35-36 Amount of pixel shift in x- and y-direction .. GENERATED FROM PYTHON SOURCE LINES 36-39 .. code-block:: default xPixelShift = 16 yPixelShift = -7 .. GENERATED FROM PYTHON SOURCE LINES 40-41 Determine the ROI size: relative (centered) size of original image (relativeSize=1: original size). .. GENERATED FROM PYTHON SOURCE LINES 41-49 .. code-block:: default 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 50-51 not shifted ROI .. GENERATED FROM PYTHON SOURCE LINES 51-58 .. code-block:: default 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 59-60 Determine the pixel shift using discrete fast fourier transformation and complex conjugation of ``image2``. .. GENERATED FROM PYTHON SOURCE LINES 60-70 .. code-block:: default 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 71-72 Determine the distance of the maximum from the center .. GENERATED FROM PYTHON SOURCE LINES 72-81 .. code-block:: default 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 2.547 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-python :download:`Download Python source code: demo_LateralShift.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_LateralShift.ipynb `