{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Scipy\n\nOpens the ascent image from ``scipy.misc`` and shifts the image.\nFinally, the shift offsets are determined using cross-correlation.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import scipy.misc\nimport numpy\nfrom numpy.fft import fft2\nfrom numpy.fft import fftshift\nimport pylab" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get the ascent image and calculate an offset shift.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ascent = scipy.misc.ascent()\nprint(\"The ascent-image has a size of\", ascent.shape)\nprint(\"The maximum value of this image is \", ascent.max())\nprint(\"Its data type is \", ascent.dtype)\n\nF = fft2(ascent)\nF2 = fftshift(F)\n\npylab.figure()\npylab.gray()\npylab.subplot(221)\npylab.imshow(ascent)\npylab.title(\"ascent (Original)\")\n\npylab.subplot(222)\nimg = pylab.imshow(numpy.real(F))\nimg.set_clim(0, 100)\npylab.title(\"ascent (FFT)\")\n\npylab.subplot(223)\nimg = pylab.imshow(numpy.real(F2))\nimg.set_clim(0, 100)\npylab.title(\"ascent (FFT), fftshift\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate the cross-correlation. \n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pylab.figure()\npylab.subplot(231)\npylab.imshow(ascent)\n\nascent_roll = numpy.roll(ascent, 50, 1)\nascent_roll = numpy.roll(ascent_roll, -150, 0)\n\npylab.subplot(232)\npylab.imshow(ascent_roll)\n\nF = fftshift(fft2(ascent))\nF2 = fftshift(fft2(ascent_roll))\n\nF3 = numpy.multiply(F, F2.conj())\n\nF4 = fftshift(numpy.fft.ifft2(F3))\n\npylab.subplot(233)\nimg = pylab.imshow(numpy.real(F))\nimg.set_clim(0, 100)\n\npylab.subplot(234)\nimg = pylab.imshow(numpy.real(F2))\nimg.set_clim(0, 100)\n\npylab.subplot(235)\nimg = pylab.imshow(numpy.real(F3))\nimg.set_clim(0, 100)\n\npylab.subplot(236)\nF5 = numpy.real(F4)\nimg = pylab.imshow(F5, vmin=0, vmax=0.001)\n\nmax_pos = numpy.argmax(F5)\n\noffset_x = max_pos % 512\noffset_y = (max_pos - offset_x) / 512\n\nprint(\"offset_x: \", offset_x - 256)\nprint(\"offset_y: \", offset_y - 256)\n\npylab.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 0 }