{ "cells": [ { "metadata": {}, "cell_type": "markdown", "source": [ "# Automatic estimate the patch size\n", "\n", "**Patch size** is an important parameter to extract local image motifs, compute rotatioanl and reflectional symmetry maps. " ], "id": "87fdf9e240b6b0a4" }, { "metadata": {}, "cell_type": "code", "execution_count": null, "source": [ "%matplotlib qt\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "#from mtflearn import ZPs" ], "id": "ff9ad3349388a903", "outputs": [] }, { "metadata": { "ExecuteTime": { "end_time": "2024-05-20T09:24:23.724787Z", "start_time": "2024-05-20T09:24:21.766098Z" } }, "cell_type": "code", "source": [ "from mtflearn.features import autocorrelation, radial_profile, get_characteristic_length, get_characteristic_length_fft\n", "from mtflearn.utils import normalize_image" ], "id": "d79a45862f3e9b25", "execution_count": 2, "outputs": [] }, { "metadata": { "ExecuteTime": { "end_time": "2024-05-20T09:24:24.171778Z", "start_time": "2024-05-20T09:24:24.146778Z" } }, "cell_type": "code", "source": "from skimage.transform import resize", "id": "891eeaa61d4ea31d", "execution_count": 3, "outputs": [] }, { "metadata": { "ExecuteTime": { "end_time": "2024-05-20T09:24:25.036208Z", "start_time": "2024-05-20T09:24:25.021204Z" } }, "cell_type": "code", "source": "", "id": "ec091987a852ab42", "execution_count": 3, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "## Load image", "id": "73114210167a2bd8" }, { "metadata": { "ExecuteTime": { "end_time": "2024-05-20T09:24:27.990143Z", "start_time": "2024-05-20T09:24:27.905144Z" } }, "cell_type": "code", "source": [ "filename = 'data\\\\monolayer_MoSe2_80K.npy'\n", "img = np.load(filename)[0:800, 0:800]\n", "img = resize(img, (512, 512))\n", "img = normalize_image(img, 0, 1)" ], "id": "1d05106aaec9fa98", "execution_count": 4, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": [ "## Method 1: Estimating the Lattice Constant from Fourier Space\n", "\n", "Given an image $I$, we follow these steps to estimate the characteristic length from the Fourier space:\n", "\n", "1. **Compute the Power Spectrum**:\n", " - Calculate the power spectrum of the image $I$, denoted as $F$.\n", "\n", "2. **Radial Averaging**:\n", " - Compute the radial average of $F$, denoted as $L$.\n", "\n", "3. **Baseline Correction**:\n", " - Perform baseline correction on $L$, resulting in a corrected curve denoted as $L'$.\n", "\n", "4. **Peak Detection**:\n", " - Identify the dominant peak in $L'$. The position of this peak is denoted as $k$.\n", "\n", "5. **Conversion to Real Space**:\n", " - Convert the peak position $k$ to a real-space length $s$.\n", "\n", "This method allows us to estimate the lattice constant by analyzing the frequency domain representation of the image and extracting the most prominent spatial frequency, which is then translated into a characteristic length in real space." ], "id": "24f52a17c98ece6c" }, { "metadata": { "ExecuteTime": { "end_time": "2024-05-20T09:25:27.506315Z", "start_time": "2024-05-20T09:25:27.426599Z" } }, "cell_type": "code", "source": [ "s = get_characteristic_length_fft(img)\n", "a1 = 2/np.sqrt(3) * s\n", "print('Lattice constant is {} pixels.'.format(a1))" ], "id": "764e2a9707c45179", "execution_count": 7, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": [ "## Method 2: Estimating the Lattice Constant from the Radial Distribution Function\n", "\n", "Given an image $I$, we follow these steps to estimate the characteristic length from the radial distribution function:\n", "\n", "1. **Compute the Autocorrelation**:\n", " - Calculate the autocorrelation of the image $I$, denoted as $A$.\n", "\n", "2. **Radial Averaging**:\n", " - Compute the radial average of $A$, denoted as $L$.\n", "\n", "3. **Peak Detection**:\n", " - Locate the first peak in $L$. The position of this peak is denoted as $s$.\n", "\n", "This method enables us to estimate the lattice constant by analyzing the spatial correlations within the image, identifying the most prominent distance over which the image exhibits periodicity." ], "id": "b309b7af62b517ea" }, { "metadata": { "ExecuteTime": { "end_time": "2024-05-20T09:30:20.655291Z", "start_time": "2024-05-20T09:30:20.544293Z" } }, "cell_type": "code", "source": [ "a2 = get_characteristic_length(img)\n", "\n", "print('Lattice constant is {} pixels.'.format(a2))" ], "id": "68ea640dfa95e51", "execution_count": 8, "outputs": [] }, { "metadata": {}, "cell_type": "code", "execution_count": null, "source": "", "id": "a8af242e8b0f38a5", "outputs": [] }, { "metadata": {}, "cell_type": "code", "execution_count": null, "source": "", "id": "9b5e564ae8bd23ac", "outputs": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }