Dhruv Gamdhaa,
Ryan Fairc,
Adarsh Krishnamurthyab,
Enrique D. Gomezcd and
Baskar Ganapathysubramanian
*ab
aDepartment of Mechanical Engineering, Iowa State University, Ames, IA, USA. E-mail: baskarg@iastate.edu
bTranslational AI Research Center (TrAC), Iowa State University, Ames, IA, USA
cDepartment of Chemical Engineering, The Pennsylvania State University, University Park, PA, USA
dDepartment of Material Science and Engineering, The Pennsylvania State University, University Park, PA, USA
First published on 19th August 2025
Automated analysis of high-resolution transmission electron microscopy (HRTEM) images is increasingly essential for advancing research in organic electronics, where precise characterization of lamellar, one-dimensional crystalline domains in conjugated polymers governs device performance. This paper introduces an open-source computational framework—GRATEv2 (GRaph-based Analysis of TEM, Version 2)—designed for near-real-time analysis of semi-crystalline, polymeric microstructures; its capabilities are illustrated on poly[N-9′-heptadecanyl-2,7-carbazole-alt-5,5-(4′,7′-di-2-thienyl-2′,1′,3′-benzothiadiazole)] (PCDTBT), a benchmark material in organic photovoltaics. GRATEv2 employs fast, automated image processing algorithms, enabling rapid extraction of structural features like d-spacing, orientation, and crystal shape metrics. Bayesian optimization rapidly identifies the parameters (that are traditionally user-defined) in the approach, reducing the need for manual parameter tuning and thus enhancing reproducibility and usability. Additionally, GRATEv2 is compatible with high-performance computing (HPC) environments, allowing for efficient, large-scale data processing at near real-time speeds. A unique feature of GRATEv2 is a Wasserstein distance-based stopping criterion, which optimizes data collection by determining when further sampling no longer adds statistically significant information. This capability optimizes the amount of time the TEM facility is used while ensuring data adequacy for in-depth analysis. Open-source and tested on a substantial PCDTBT dataset, this tool offers a powerful, robust, and accessible solution for high-throughput material characterization in organic electronics.
High-resolution transmission electron microscopy (HRTEM) has advanced significantly in recent years, transforming nanoscale imaging and allowing researchers to capture atomic-level details of materials.4 HRTEM can now achieve sub-angstrom spatial resolution, making it possible to directly observe atomic lattices, defects, and interfaces that govern a material's behavior.5 The development of automated data acquisition systems has further broadened HRTEM's capabilities, enabling the collection of extensive datasets comprising hundreds or even thousands of high-resolution images.6 These technological advances have opened new avenues for studying complex materials, such as organic semiconductors and conjugated polymers, with applications in organic electronics and photovoltaics.7
While modern HRTEM has enhanced our ability to study complex materials, it also introduces challenges in data management and analysis due to the sheer volume and complexity of the data generated.8 The scale of high-resolution datasets can quickly overwhelm traditional analysis workflows, which are often manual and time-consuming. A particularly acute bottleneck is the creation of annotation masks that serve as “ground truth” for algorithm development: drawing them is labor-intensive, and the outcome can vary from one expert to another. This manual approach is highly dependent on the expertise and subjective judgment of the experimentalist, making it challenging to ensure consistency and reproducibility. In high-throughput applications where rapid feedback is essential, such as optimizing synthesis conditions or tracking real-time structural changes, the limitations of manual analysis become particularly pronounced.
To address these challenges, automated methods for HRTEM data analysis have emerged over the past decade. These methods aim to extract quantitative structural information from digital micrographs with minimal human intervention, improving both the efficiency and reliability of analysis.3,9–12 A motivating example is our prior work, GRATE,13 which served as a proof-of-concept for using graph-based algorithms to identify crystalline regions by first thinning fringes into skeletons and then clustering them based on proximity and orientation.13 However, this first version was limited by its implementation in MATLAB, restricting it to offline post-processing, and it relied on sensitive manual tuning of its parameters, which hindered reproducibility.13 GRATEv2 significantly advances this foundation through a complete redesign in Python for HPC compatibility and several key innovations. The image processing pipeline is refined with additional preprocessing steps and a more robust thresholding approach, and introduces an explicit step of segmenting skeletons into uniform “bones” to regularize the graph construction. Most critically, GRATEv2 replaces the manual tuning process with automated Bayesian Optimization and adds a novel Wasserstein distance-based stopping criterion to guide data collection, transforming the original concept into a reproducible, high-throughput framework.
In recent years, there has been a strong push toward developing in situ, or real-time, automated analysis methods.14–16 In situ analysis allows for data interpretation during experiments, providing immediate feedback that can guide adjustments in experimental parameters. This capability is particularly valuable in dynamic experiments, such as observing structural changes in response to external stimuli or monitoring materials during synthesis.17–23 Real-time analysis requires methods that can handle high-resolution data quickly and accurately, maintaining performance under the demanding conditions of live data acquisition.
Various automated analysis philosophies have been explored. Machine learning tools like the Trainable Weka Segmentation plugin for Fiji/ImageJ24 offer a powerful, user-friendly approach for general pixel classification by training a classifier on a small number of user-provided labels.25 However, such tools typically rely on a generic set of image filters and are designed for classifying regions or simple objects like nanoparticles rather than identifying and connecting the long, thin, and often discontinuous fringe patterns that constitute a single crystalline domain in polymers. For such specialized tasks, bespoke rule-based image processing pipelines are often more effective. A key challenge, however, is the tuning of their many interacting parameters. Recently, Bayesian Optimization has been powerfully applied to automate this tuning process. In a notable example, Barakati et al.26 engineered a “physics-based reward function,” which uses physical priors (e.g., expected atom density) to serve as the objective for the optimization, thereby avoiding the need for manual annotations.26
GRATEv2 shares the philosophy of Barakati et al.26 of using Bayesian Optimization to tune a classical pipeline but is novel in two critical areas: the algorithm being optimized and the nature of the objective function. First, while the work by Barakati et al.26 focuses on optimizing standard algorithms like Laplacian-of-Gaussian for atom-finding, GRATEv2 optimizes a novel, graph-based pipeline specifically designed to segment the unique lamellar structures of polymer films. Second, we propose a different approach for the optimization's objective. Barakati et al.26 engineer a clever “physics-based reward” that avoids manual labeling by using physical priors. For the complex and irregular morphology of polymer crystallites, defining such simple physical rules is challenging. Instead, GRATEv2 uses a small set of manually annotated images to compute the Intersection-over-Union (IoU), providing a direct and robust way to ensure the final segmentation matches expert perception.
GRATEv2 aims to bridge the gap between offline and real-time analysis in high-resolution transmission electron microscopy (HRTEM) by providing an automated, image-processing-based framework with minimal human intervention. Tailored for high-throughput settings, GRATEv2 combines rapid data extraction with a robust and user-friendly parameter optimization process. By focusing on image processing techniques augmented with Gaussian process optimization, GRATEv2 minimizes the need for manual parameter selection and tuning, enhancing reproducibility and accessibility for researchers.
A schematic overview of the GRATEv2 computational framework is shown in Fig. 1a. The framework processes raw HRTEM images of PCDTBT (Fig. 2a), applies preprocessing, and performs automated image processing with parameters optimized via Bayesian optimization (Fig. 1b). Traditionally, manual annotation of crystals in HRTEM images is time-consuming and subjective (Fig. 2). In our approach, only a dozen manually annotated images (Fig. 2b) are used as input to a Bayesian optimizer to rapidly identify material-specific image processing parameters. This minimal requirement significantly reduces the burden on researchers, facilitating rapid deployment of the algorithm on new datasets.
GRATEv2 introduces several key innovations to address the limitations of current HRTEM analysis methods:
1. GRATEv2 offers rapid processing of HRTEM micrographs that exhibit lamellar, 1-D fringe patterns typical of semi-crystalline conjugated polymers, delivering results in a few seconds per image and supporting batch multiprocessing for large datasets.
2. It brings a graph-based, image-processing pipeline to organic polymer systems such as PCDTBT, where atomic-resolution lattice models are neither required nor assumed.
3. Bayesian optimization is employed within GRATEv2 to automate the tuning of material-specific image processing parameters, allowing the framework to adapt to different datasets with minimal expert input (Fig. 1b).
4. The algorithm parameters are constructed as functions of known d-spacing values, simplifying parameter selection and making the method more accessible to users without extensive image processing expertise. This also ensures that parameter selection is interpretable and, thus, scientifically justified.
5. GRATEv2 incorporates a data sufficiency criterion based on the Wasserstein distance to guide data collection efforts. This criterion provides a quantitative stopping point, indicating when further TEM data collection no longer yields additional insights, which is important when access to imaging facilities is limited and imaging is expensive.
By optimizing data collection, GRATEv2 helps experimentalists avoid unnecessary resource expenditure while ensuring data quality. This combination of fast, automated processing, real-time adaptability, and efficient data collection positions GRATEv2 as a powerful tool for materials research, particularly in the study of organic electronic materials such as conjugated polymers.27–29
5 mg mL−1 solutions of PCDTBT and chlorobenzene (Sigma-Aldrich) were prepared in a nitrogen glovebox and mixed overnight on a hotplate at 45 °C. Silicon wafers were sonicated in acetone for 20 minutes, then isopropanol for 20 minutes. The wafers then underwent 20 minutes of ultraviolet light ozonation. A PEDOT:PSS (Clevios P and H. C. Starck) and water solution were then spin-coated onto the clean substrates at 4000 RPM for 2 minutes. The samples were then brought into the glovebox, and the heated PCDTBT solution was spin-coated on top of the PEDOT:PSS layer at 800 RPM for 2 minutes. The sample was then cut into squares and floated off in the water, and samples were collected on copper TEM grids. Samples were left to dry overnight and then annealed inside a nitrogen glovebox.
High-resolution imaging experiments were conducted on the Titan Krios microscope at the Penn State Materials Characterization Laboratory. The accelerating voltage was 300 kV, and the detector was a Falcon 3EC direct electron detector in counted mode. Regions of interest were spaced 2.5 μm apart and visually inspected on the atlas image for tears and defects before acquisition. The spot size was set to 5, and autofocus was done at 300k× magnification before being increased to 470k× for acquiring a 2.5 second exposure. The microscope produced a 650 nm beam with a dose rate of 50 e Å2 s.
We report two notions of separation as described in Table 1 and eqn (1) and (2):
![]() | (1) |
Ddirect = dcenter–center | (2) |
![]() | ||
Fig. 3 Algorithm flowchart with parameters. Blue boxes are the process parameters and brown boxes are the intermediate steps shown in Fig. 4. |
![]() | ||
Fig. 4 Intermediate and final outputs of the pipeline (see the flowchart in Fig. 3). (a) Input: initial morphology. (b) Step 1—Otsu thresholding (corresponds to orange callout 1 in Fig. 3). (c) Step 2—skeletonization and branching (callout 2). (d) Step 3—filtering short backbones and division into uniform bones (callout 3). (e) Step 4—aspect-ratio filtering of ellipses (callout 4). (f) Step 5—clustering of adjacent bones (callout 5). (g) Output: detected crystal region with boundary/principal-direction overlay. |
The algorithm parameters are classified into two categories: primary parameters and secondary parameters. The primary parameters are independent values provided by the user based on the dataset's imaging conditions and material properties, specifically the crystal d-spacing value and the image resolution. These parameters scale the key process parameters, enabling the algorithm to generalize across different imaging conditions and crystals of interest. They may vary significantly between datasets. Secondary parameters correspond to individual steps of the algorithm and are set to optimal values; they may require fine-tuning when changing datasets.
The algorithm is designed with several key objectives in mind: to generate a clear and concise representation of the essential information in the image; to enhance data quality by filtering out noise and irrelevant details; to convert the processed image data into a graph structure for advanced analysis; to use graph algorithms to identify clusters of polymer backbones forming crystallites; and to apply Fourier transform techniques to determine the d-spacing values.
Each step of the algorithm is detailed below, along with its associated parameters.
1. Initially, blurring is applied to smooth the image and reduce sharpness, which helps in minimizing noise and preparing the image for subsequent processing. This operation is performed multiple times, with the number of blurring iterations specified as a parameter. Following blurring, histogram equalization is employed to increase the image contrast, making it sharper and enhancing the distinction between polymer chains and the background. Improved contrast facilitates more accurate thresholding in the subsequent step.
2. The next step involves image thresholding using Otsu's method to create a binary representation of the image, where polymer chains appear as black regions and the background as white. The result of this step is shown in Fig. 4b. After thresholding, morphological closing and opening operations are performed to remove small black and white spots considered as noise. The kernel sizes for closing and opening are parameters that do not depend on the d-spacing and are set based on the noise characteristics of the image.
3. Subsequently, we perform skeletonization and branching. Skeletonization reduces the polymer regions to single-pixel-wide lines, representing the skeleton of the polymer chains. Branching breaks the skeleton at junctions where three or more connections occur, resulting in branched skeletonized representations called backbones. The output of this step is depicted in Fig. 4c.
4. Following skeletonisation, we remove spurious fragments by discarding any backbone whose pixel length is less than a length threshold proportional to the lattice d-spacing (in pixels). The proportionality constant is supplied by the user as a tunable parameter. This scale-aware filter suppresses noise while retaining only those backbones long enough to represent meaningful crystal fringes.
5. Each surviving backbone is traversed pixel-by-pixel from one end; after every segment length set proportional to the d-spacing in pixels—where the proportionality constant is a user-specified parameter—the backbone is severed by flipping that pixel from black to white. Because skeletons are one pixel wide, this procedure divides the polyline into uniform-length bones. Linking the segment length to d-spacing provides elasticity over the expected range of lattice spacings. The effect of filtering and segmentation is illustrated in Fig. 4d.
6. For each bone, we perform ellipse construction by fitting an ellipse to its pixel locations using the scikitimage library.33 This provides the major and minor axes of the bone, which are used for further analysis. Ellipses are preferred because they help filter out non-linear bones (curved structures not part of crystalline regions) and facilitate the creation of a graph representation where each ellipse serves as a node.
7. To identify the crystalline regions, we apply ellipse aspect ratio filtering. Crystalline regions are composed of linear polymer backbones, so we filter out curved bones by setting a threshold on the ellipse aspect ratio (major axis length divided by minor axis length). Bones with aspect ratios above this threshold are retained for further analysis. This step's output is shown in Fig. 4e.
8. A graph is then constructed where each ellipse represents a node. An edge is created between two nodes if the distance between their centers is less than the adjacency distance parameter (proportional to the d-spacing) and the angle between their major axes is less than the adjacency angle parameter (also proportional to the d-spacing). The graph is stored as an adjacency matrix.
9. Connected-component clustering. A depth-first search (DFS) identifies all connected components (CCs). CCs with fewer than a threshold number of nodes are treated as noise and discarded; the rest are considered candidate crystals (Fig. 4f).
10. Boundary extraction and overlay. For each retained CC we collect the end-points of the major axes of its ellipses, yielding a point cloud that captures the crystals footprint. We compute both a convex hull (scipy.spatial.ConvexHull) and an α-shape (alphashape) from this cloud, then overlay them on the original micrograph to visualise the detection (Fig. 4g).
11. Per-crystal metric extraction. From each crystal boundary we record area, centroid, major/minor axis lengths and orientation. A line indicating the principal lattice direction is also drawn on the micrograph; these metrics feed the statistical analysis described in Section 3.
Finally, we perform d-spacing evaluation for each detected crystal region. The largest possible square region within the crystal is selected, and a Fast Fourier Transform (FFT) is performed to transform the spatial domain into the frequency domain. Band-pass filtering is applied to remove frequencies outside the range of interest. The location of the peak frequency above a set threshold is used to calculate the exact d-spacing value and the orientation of the crystal pattern. The frequency threshold is a parameter. The evaluation of d-spacing using FFT is illustrated in Fig. 5. The algorithm outputs include visualizations such as convex hulls and alpha shapes of detected crystal regions overlaid on the original image, as well as quantitative data like area, centroid, major and minor axis lengths, orientation, and d-spacing values for each crystal. All features are saved in CSV files for further analysis. These outputs provide valuable insights into the material's microstructure and can aid in understanding material properties more effectively.
The algorithm is implemented in Python, utilizing the scikit-image library for image processing and SciPy for computational functions. A configuration file is used to input all relevant parameters, dataset paths, and result paths. The code is tested on Ubuntu Linux-based local systems and HPC servers. It supports batch processing and multiprocessing, allowing for efficient analysis of large datasets. Results are stored in a well-organized directory structure, with options to save intermediate outputs for debugging purposes. Comprehensive documentation is provided, detailing code usage and parameter settings. A summary of the primary and secondary parameters used in the algorithm is provided in Appendix A. These parameters are optimized for detecting crystals in the HRTEM dataset of PCDTBT organic photovoltaic materials.
Bayesian optimization is a sequential design strategy for global optimization of black-box functions that are expensive to evaluate especially due to large hyperparameter space.34 It builds a probabilistic model of the objective function and uses it to select the most promising hyperparameters to evaluate next, balancing exploration and exploitation.
![]() | (3) |
![]() | (4) |
![]() | (5) |
![]() | (6) |
![]() | (7) |
![]() | (8) |
![]() | (9) |
![]() | (10) |
![]() | ||
Fig. 7 Flowchart of the Bayesian optimization loop. The process iteratively updates the surrogate model and selects new hyperparameters to evaluate until convergence criteria are met. |
The Bayesian optimization algorithm begins with the initialization step, where the objective function is evaluated at an initial set of hyperparameters often chosen via a space-filling design like Latin hypercube sampling. Next, a Gaussian Process (GP) surrogate model is fitted to the observed data
capturing our current understanding of the objective function. Using this surrogate model, the algorithm maximizes the acquisition function to find the next promising set of hyperparameters:
![]() | (11) |
![]() | (12) |
Hyperparameter | Range | Type |
---|---|---|
blur_iteration | [5, 20] | Integer |
Blur_kernel_propCons | [0.1, 0.5] | Real |
closing_k_size | [1, 20] | Integer |
opening_k_size | [1, 20] | Integer |
pixThresh_propCons | [0.0, 1.0] | Real |
ellipse_len_propCons | [0.5, 5.0] | Real |
ellipse_aspect_ratio | [2.0, 7.0] | Real |
thresh_dist_propCons | [1.0, 5.0] | Real |
thresh_theta | [5.0, 15.0] | Real |
cluster_size | [1, 10] | Integer |
dspace_bandpass | [0.1, 0.5] | Real |
powSpec_peak_thresh | [1.0, 1.5] | Real |
thresh_area_factor | [1.0, 5.0] | Real |
1. Run the image processing algorithm with parameters x on the annotated HRTEM images.
2. Generate binary masks of the detected crystalline regions.
3. Compute the IoU between the detected masks and the ground truth masks:
![]() | (13) |
4. Set the objective function value as f(x) = −IoU(x).
1. Scale dependence. Several thresholds (e.g. backbone length, bone length, adjacency distance) are expressed as multiples of the lattice d-spacing and therefore change when the spacing or pixel size differs from one experiment to another.
2. Contrast variation. Grey-level statistics vary with beam current, detector settings and sample preparation, affecting Otsu thresholding and morphological noise removal.
3. Morphology differences. Polymer batches can exhibit distinct degrees of lamellar overlap, curvature and fringe density, all of which influence optimal parameter values.
From 653 HRTEM micrographs we hand-selected 13 images (≈2%) that together span the extremes of contrast, crystal size, overlap and orientation visible in the full batch. Each image was annotated with the VGG Image Annotator;37 annotating one image takes about 5 minutes, so the entire training set required ∼1 hour of manual effort.
We recommend the following protocol as a practical guide for selecting a training set and annotating images for GRATEv2:
1. Randomly draw 10–15 images, then replace or add a few so that the set covers the darkest, brightest, most densely fringed and sparsest cases in the batch.
2. Annotate only this subset; run Bayesian optimisation once.
3. Apply the resulting parameter file to the remainder of the dataset.
Linking parameter selection to just 13 annotated images therefore keeps manual effort to about one hour while enabling automated analysis of the entire dataset.
We used the gp_minimize function with the following key parameters:
• Acquisition function: expected improvement (EI).
• Number of calls: 200 total evaluations of the objective function.
• Initial points: 10 random initial evaluations to seed the GP model.
• Random state: seeded for reproducibility.
An effective data sufficiency metric should possess certain desirable features:
1. Sensitivity to distribution changes: it should accurately reflect changes in the underlying data distribution as more data is collected.
2. Scale interpretability: the metric should provide a quantitative measure that is interpretable and can be related to practical thresholds for decision-making.
3. Applicability to empirical distributions: it should be suitable for comparing empirical distributions derived from finite samples.
4. Metric properties: the measure should satisfy the properties of a mathematical metric, such as non-negativity, identity of indiscernibles, symmetry, and triangle inequality, to ensure consistent and meaningful comparisons.
In this study, we introduce a stopping criterion based on the Wasserstein distance to assess data sufficiency. The Wasserstein distance, also known as the Earth Mover's Distance, quantifies the difference between two probability distributions by measuring the minimum “cost” of transforming one distribution into the other. It is particularly well-suited for our purposes because it satisfies all the desired features of a data sufficiency metric listed above.
![]() | (14) |
![]() | (15) |
![]() | (16) |
The Wasserstein distance's ability to capture differences between distributions, even when they have overlapping support, makes it ideal for assessing the convergence of empirical data distributions as more data is collected. By monitoring the Wasserstein distance between successive data samples, we can determine when the distributions have converged, indicating that the data collection process has reached a point of diminishing returns.
Using GRATEv2, we detected crystals within each of the HRTEM images. For each identified crystal, the following features were extracted (see Section 3.2): center-of-mass coordinates, orientation angle relative to the image axis, d-spacing, and crystal lengths along both the major and minor axes. Through this process, GRATEv2 identified a total of 4350 ordered domains from the HRTEM images. In Section 3.3, we present the timing performance of GRATEv2, along with the time distribution among different components of the analysis process. In Section 3.4, we evaluate the data sufficiency—that is, how many images are sufficient to achieve statistical convergence in the extracted features.
Image filename | Manual selected parameters IoU | Bayesian parameters IoU | IoU difference, di |
---|---|---|---|
1.tif | 0.5296 | 0.6911 | +0.1615 |
2.tif | 0.3274 | 0.5156 | +0.1882 |
3.tif | 0.3178 | 0.4934 | +0.1756 |
4.tif | 0.5285 | 0.5864 | +0.0579 |
5.tif | 0.3906 | 0.5400 | +0.1494 |
6.tif | 0.5288 | 0.6438 | +0.1150 |
Average IoU | 0.4371 | 0.5784 | +0.1413 |
The average IoU score using the Bayesian-optimized parameters is 0.5784, representing an improvement of approximately 32.3% over the average IoU of 0.4371 obtained with the manually selected parameters. This significant increase demonstrates the effectiveness of Bayesian optimization in enhancing the algorithm's performance in detecting and segmenting crystalline regions in HRTEM images. The computed t-value corresponding to t-statistic is 7.074, which exceeds the critical t-value of 2.571, indicating strong statistical justification for improvement.
Several notable differences are observed between the parameter sets:
• Morphological operations: the Bayesian-optimized parameters for closing_k_size and opening_k_size are significantly smaller than the manual values (2 vs. 15 and 17, respectively). This suggests that less aggressive morphological operations preserve finer details in the images, contributing to improved segmentation accuracy.
• Edge detection and filtering: parameters like ellipse_len_propCons and dspace_bandpass are adjusted to better capture the characteristics of the crystalline structures. The increase in ellipse_len_propCons from 1.5 to 4.03 indicates a preference for detecting longer ellipses, aligning with the elongated shapes of crystals.
• Thresholding parameters: the pixThresh_propCons is slightly higher in the Bayesian-optimized parameters, which may help in differentiating crystals from the background noise more effectively.
1. Enhanced performance: the Bayesian-optimized parameters significantly improve the mean IoU score by approximately 32.3%, indicating superior segmentation performance.
2. Automated tuning: Bayesian optimization automates the hyperparameter tuning process, reducing the need for manual intervention and deep image-processing expertise, thereby saving time and resources. This is especially important when the number of hyperparameters (here, 13) makes manual exploration rather tedious.
3. Efficient exploration: the optimization process efficiently explores the hyperparameter space, converging to optimal values in fewer evaluations compared to exhaustive search methods. This is evident from the convergence plot in Fig. 10.
4. Robustness: the optimized parameters produce robust results across various images, as evidenced by consistent improvements in IoU scores.
![]() | ||
Fig. 11 (a) and (b) Are 1.tif and 2.tif images respectively corresponding to Tables 4 and 5 (a) and (b) shows the original image (left) and the segmentation output (right) from our algorithm for HRTEM of PCDTBT. The detected crystals have a d-spacing of 1.9 nm. The image on the left is the input to the algorithm, and on the right is the output of the algorithm. Each detected crystal in the output shows (1) the convex hull boundary around the crystal, (2) the shaded region representing a more exact crystal region, and (3) a straight line at the centroid of the convex hull, which shows the orientation of the crystal patterns. |
Name | Centroid (px, px) | Area (nm2) | Angle (deg) | d-Spacing (nm) | MajorAxis (nm) | MinorAxis (nm) | AxisAngle (deg) |
---|---|---|---|---|---|---|---|
1.tif | (1748, 670) | 589.7 | −164.7 | 2.1 | 21.1 | 10.2 | 23.8 |
(785, 1992) | 293.9 | −55.9 | 2.0 | 14.8 | 9.9 | −65.5 | |
2.tif | (534, 497) | 177.9 | −137.9 | 1.9 | 12.5 | 5.3 | 53.0 |
(1607, 402) | 84.3 | −136.0 | 0.8 | 7.9 | 4.3 | 35.4 | |
(3345, 546) | 71.7 | −148.8 | 1.7 | 14.4 | 4.8 | 39.6 | |
(2050, 1975) | 125.4 | −146.0 | 2.2 | 16.0 | 4.2 | 34.1 | |
(1922, 3396) | 263.8 | −141.4 | 2.0 | 14.2 | 8.0 | 46.9 |
Name | Metric distance (1) | Direct distance (nm) | Relative angle (deg) |
---|---|---|---|
1.tif | 0.89 | 20.84 | 71.24 |
2.tif | 2.91 | 35.81 | 10.95 |
1.95 | 26.97 | 8.13 | |
2.45 | 40.94 | 3.5 | |
2.21 | 24.57 | 2.81 | |
2.91 | 40.58 | 7.45 | |
1.17 | 18.18 | 4.64 |
The detection of these crystalline domains allows for a comprehensive analysis of the microstructural features of PCDTBT. The d-spacing values shown in Fig. 12b range from approximately 1.1 nm to 2.9 nm, which are consistent with the expected lattice spacings associated with PCDTBT's crystalline structures.39 Variations in d-spacing among the detected crystals may be attributed to differences in crystallite orientation, strain within the material, or inherent structural disorder due to the semi-crystalline nature of PCDTBT.
The areas of the detected crystals vary significantly as shown in Fig. 12a, with values ranging from approximately 14.89 nm2 to 2307.18 nm2. Larger crystal areas may correlate with improved charge transport properties, as larger crystalline domains can facilitate more efficient charge carrier mobility along the polymer chains.27 The aspect ratios, derived from the major and minor axis lengths, provide insights into the shapes of the crystals. Higher aspect ratios indicate elongated, rod-like crystals, while lower aspect ratios suggest more equiaxed or spherical shapes. The diversity in crystal shapes and sizes can impact the overall morphology and performance of the polymer in electronic applications.
Fig. 12 also presents additional statistical analyses of individual crystal properties across the dataset, including histograms of crystal areas, d-spacings, orientation angles, and shape descriptors like aspect ratio. These plots allow us to identify prevalent features and distributions within the material. For example, the histogram of d-spacings may show a peak around a specific value, indicating a dominant crystalline phase or preferred stacking distance within the polymer chains.
The comprehensive analysis provided by GRATEv2 enables us to establish quantitative relationships between microstructural features and potential material performance. By systematically characterizing the size, shape, orientation, and spatial distribution of crystalline domains, we can correlate these features with electronic properties measured in devices. This level of detailed microstructural understanding is essential for guiding the design and processing of conjugated polymers to achieve optimal performance in organic electronic applications.
For a more extensive and detailed exploration of this PCDTBT dataset, including insights into intercrystalline correlations and preferred crystallographic alignments, readers are referred to the work of our collaborators in ref. 40. Their study leverages automated HRTEM and the GRATEv2 image processing outputs to unravel how neighboring crystals preferentially align along certain lattice directions, likely reflecting underlying liquid crystalline order within the polymer. By combining the analysis presented here with their comprehensive assessment of orientation correlations and lattice parameters, one obtains a richer and more complete understanding of the polymers nanoscale structure and its implications for organic electronics.
![]() | ||
Fig. 13 Time taken by each step in the algorithm for the analysis of 1.9 nm d-spacing crystals in a single image using a single core. The total processing time is approximately 6.52 seconds. |
The performance enhancements of our algorithm are attributed to the use of optimized libraries and functions that efficiently handle computationally intensive tasks. Specifically, we employed:
1. The skeletonize function from skimage.morphology41 for efficient skeletonization.
2. OpenCV42 functions equalizeHist and threshold (cv2.equalizeHist, cv2.threshold) for fast histogram equalization and image thresholding.
3. Morphological operations such as closing and opening using cv2.morphologyEx from OpenCV.
4. The skeleton_to_csgraph function from the skan43 library for converting skeleton images to graph representations.
5. The label function from skimage.measure41 for rapid image segmentation.
6. Sparse graph structures and connected component analysis using csr_matrix and connected_components from scipy.sparse.csgraph44 for efficient graph construction and evaluation.
7. Fast Fourier transforms and other numerical operations using NumPy functions such as np.fft.45
8. The alphashape library to create shrink-wraps around point clouds.
By utilizing these optimized libraries, we minimized computational overhead and maximized the efficiency of each processing step. The skeletonization step, although still the most time-consuming, benefits significantly from the optimized implementation in skimage.41 Similarly, the use of sparse matrices and efficient graph algorithms from scipy.sparse.csgraph44 greatly accelerates the analysis of the skeleton's connectivity.
Our analysis of the timing statistics reveals that the skeletonization step accounts for approximately 71% of the total processing time for a single image when executed on a single core. This indicates that skeletonization is a major computational bottleneck in the algorithm. However, due to the parallel nature of image processing tasks, distributing the workload across multiple cores significantly mitigates this bottleneck. By processing images concurrently, we effectively utilize the available computational resources, resulting in a substantial reduction in total processing time.
Furthermore, the efficient handling of large data structures, such as sparse matrices in graph construction and connected component analysis, contributes to the algorithm's scalability. The use of optimized libraries ensures that even computationally intensive tasks are executed as efficiently as possible. This optimization is crucial when dealing with large datasets, as it enables rapid analysis without compromising accuracy or resolution.
The combination of algorithmic efficiency, optimized library functions, and effective parallelization allows our method to achieve high performance in processing and analyzing large volumes of HRTEM images. This capability is essential for applications requiring rapid data analysis and real-time feedback in materials science and related fields.
Preparing ground-truth masks for 13 representative images required about 5 minutes per image (≈1 h total hands-on time). Bayesian optimisation of the 13-dimensional parameter space then ran unattended for 200 iterations, taking ≈90 min on a single HPC node, after which the resulting parameter file was applied to the remaining 640 images. In practical terms, one hour of annotation replaced the ≳60 h of manual trial-and-error previously needed to tune the parameters manually. The Bayesian optimisation process was able to quickly identify the optimal parameter set that maximized the segmentation performance across the training dataset, demonstrating the effectiveness of this approach in reducing expert effort while maintaining high accuracy.
Specifically, we consider four different batch sizes at which images (rather, crystals) are added to the dataset: 10, 21, 42, and 84 crystals. For each batch size scenario, we start from 0 crystals and progressively add data in increments equal to the batch size until we reach the full dataset size (of 837 crystals). After each increment, we compute the Wasserstein distance between the new distribution (including i × batchSize crystals) and the previous distribution (with (i − 1) × batchSize crystals). To obtain a more stable and reliable statistic, we repeat this evaluation 10 times at each increment, each time randomly sampling the (i − 1) batches from i batches of crystals and then average the resulting distances.
As shown in Fig. 14 the histograms become smoother and more stable as the dataset grows, demonstrating that the distribution of crystal areas converges toward a steady form. Meanwhile, Fig. 14e shows how the averaged Wasserstein distance between consecutive increments decreases with additional data. Each curve represents a different batch size, revealing that the scale of incremental changes to the distribution depends on how many crystals are added at once.
Larger batch sizes (e.g., 84 crystals) introduce more data at each increment, leading to more pronounced changes in the distribution per step. Once enough large increments have been included, the distribution may show a sudden, relatively large drop in Wasserstein distance, then rapidly converge. In contrast, smaller batch sizes (e.g., 10 crystals) add data more gradually, producing smoother and more frequent updates. Each small increment makes a subtler change to the distribution, resulting in a more gradual and fine-grained trajectory toward convergence.
Because each batch size scenario scales the increments differently, the threshold for deciding when to stop data collection should also be scaled accordingly. For larger batch sizes, even a modest Wasserstein distance value (e.g., around 4 units for the 84-crystal batch) might indicate sufficient convergence, since one large increment can naturally shift the distribution more. For smaller batch sizes, where each increment is gentler (e.g., around 1.5 units difference for the 10-crystal batch), a lower threshold might be more appropriate, reflecting the finer control and resolution over the distributions shape. To illustrate this, Table 6 shows the Wasserstein distance between the full dataset and a dataset that is one batch smaller, for each considered batch size scenario. These values provide concrete examples of how batch size influences the scale of change in the distribution.
Batch size | Wasserstein distance (full vs. one batch less) |
---|---|
84 | 4.12 |
42 | 2.83 |
21 | 2.08 |
10 | 1.51 |
With this information, an experimentalist might set a higher threshold for larger batch sizes (e.g., around 4 units for an 84-crystal batch) and a lower threshold for smaller batch sizes (e.g., closer to 1.5 units for a 10-crystal batch). This ensures that the threshold for deciding when to cease data collection remains proportionate to the scale of changes induced by each incremental addition of data.
In practical terms, experimentalists can use insights from Fig. 14e and Table 6 to tailor their data collection strategy. By selecting an appropriate batch size and a corresponding Wasserstein distance threshold, they can decide when further data provides diminishing returns. If quick feedback and high resolution of distribution changes are desired, a smaller batch size and a lower threshold can be chosen. If time or resources are limited, larger batch sizes and a slightly higher threshold may be more suitable. In either case, once the averaged Wasserstein distance consistently falls below the chosen threshold, the experimentalist can confidently cease data collection, knowing the distribution is sufficiently representative. This approach transforms data sufficiency from a guesswork exercise into a clear, data-driven criterion that guides experimental resource allocation and ensures that the resulting dataset meets the necessary statistical rigor. An experimentalist might:
• Begin data collection in batches: they determine a batch size (e.g., 10 crystals per batch) and start collecting HRTEM images in increments of that batch size.
• Periodic assessment: after each new batch of data, they compute the Wasserstein distance between the current and previous distributions of crystal areas. This computation can be done after every increment, providing immediate feedback on the impact of newly acquired data.
• Decision point: if, after a certain number of increments, the averaged Wasserstein distance consistently falls below the established threshold (e.g., 1.5 units), the experimentalist has quantitative evidence that adding more data is unlikely to yield new insights into the crystal area distribution.
• Stopping data collection: with this statistical criterion, the experimentalist can confidently stop collecting further HRTEM images, reallocating their time and resources. This prevents unnecessary prolonged imaging campaigns.
GRATEv2's compatibility with HPC environments allows for efficient, large-scale data processing at near real-time speeds, making it suitable for high-throughput applications in materials science. By successfully applying GRATEv2 to a substantial PCDTBT dataset, we demonstrated its efficacy in rapidly extracting critical structural features such as d-spacing, orientation, and shape metrics. This capability is particularly valuable for advancing research in organic electronics, where precise nanoscale characterization is essential for optimizing material properties.
Overall, GRATEv2 addresses key limitations of existing HRTEM analysis methods by providing a fast, adaptable, and user-friendly tool that enhances the efficiency and reliability of microstructural characterization. By making GRATEv2 open-source, we aim to facilitate its adoption and further development by the research community. Future work could involve extending GRATEv2 to other material systems and incorporating additional analytical capabilities, thereby broadening its applicability and impact in the field of materials characterization.
Parameter | Description | Manually selected value | Bayesian optimized value |
---|---|---|---|
a Proportionality constant to d-spacing.dspace_nm and pix_2_nm are user inputs and not optimized. | |||
dspace_nm | d-Spacing in nm | 1.9 | 1.9 |
pix_2_nm | Pixels per nm | 78.5 | 78.5 |
blur_iteration | Blurring iterations | 15 | 20 |
Blur_kernel_propCons | Kernel size blurringa | 0.15 | 0.12 |
closing_k_size | Kernel size closing | 15 | 2 |
opening_k_size | Kernel size opening | 17 | 2 |
pixThresh_propCons | Threshold pixel lengtha | 0.63 | 0.74 |
ellipse_len_propCons | Uniform breaking lengtha | 1.50 | 4.03 |
ellipse_aspect_ratio | Threshold ellipse aspect ratio | 5.00 | 4.38 |
thresh_dist_propCons | Adjacency distancea | 2.00 | 1.36 |
thresh_theta | Adjacency angle (degrees) | 10.00 | 13.96 |
cluster_size | Threshold cluster size | 7 | 9 |
dspace_bandpass | Band pass filter size | 0.20 | 0.44 |
powSpec_peak_thresh | Power spectrum threshold | 1.15 | 1.00 |
thresh_area_factor | Area threshold factor | 4.00 | 2.79 |
![]() | (17) |
![]() | (18) |
![]() | (19) |
![]() | ||
Fig. 15 Additional comparison of ground truth, manually selected parameters, and Bayesian-optimized parameters across different images. |
1. A square window of a fixed window_size slides across the image with a given stride.
2. For each window, a 2D-FFT is computed to generate a power spectrum.
3. A score is assigned to the window based on the maximum power spectral density within an annular (ring-shaped) frequency mask. This mask is defined by the expected d-spacing range of the material.
4. These scores are assembled into a 2D “crystallinity map,” which acts as a heatmap for crystalline likelihood.
5. This map is thresholded to produce a final binary segmentation mask of the detected crystalline domains.
The Python code for this implementation is provided in the github repository https://github.com/baskargroup/GRATEv2.
The primary findings are:
• Computational cost: the SW-FFT analysis on a single 4096 × 4096 pixel image took approximately 12 min 17 s on average. In contrast, GRATEv2 processed the same image in 6.52 seconds on average, making it over 100 times faster. The high cost of the FFT method is due to the need to compute thousands of FFTs on large, overlapping windows.
• Segmentation quality: as visually demonstrated in Fig. 16, the SW-FFT method's qualitative performance is poor, failing to produce a meaningful or reliable segmentation mask. A direct comparison with the ground truth reveals several distinct and critical failure modes:
– Fragmentation and poor localization: the method struggles with spatial localization. Instead of identifying large, continuous crystals as single entities, it often detects them as a series of smaller, fragmented pieces concentrated in high-contrast areas. In contrast, GRATEv2 correctly captures the entire domain in one piece (Row 1). The SW-FFT method also completely ignored the large crystal in the top left of the image in Row 1, which is very well visible with the naked eye and precisely captured by GRATEv2.
– Noisy mapping and false detections: the generated crystallinity map (Column 3) is heavily diffused and noisy. This results in an unreliable segmentation that both completely misses major crystalline domains (false negatives) and incorrectly identifies numerous spurious regions where no crystals exist (false positives), as is evident in Rows 2 and 3.
– Poor correlation with ground truth: there is a fundamental disconnect between the high-score regions in the crystallinity map and the actual crystal locations. The algorithm often detects only a small, low-score portion of a true crystal while assigning the highest score to an entirely different, amorphous area. This poor correlation, combined with noise, leads to a fundamentally incorrect segmentation (Row 4).
This poor performance is not a matter of parameter choice but is a direct result of the method's core algorithmic flaw—the “Window Size Dilemma”.
– Need for a large window: to detect a periodic pattern via FFT, the analysis window must be large enough to contain multiple repetitions of the pattern. For our data, with a d-spacing of ≈165 pixels, a window_size of at least 400–600 pixels is required. A smaller window sees only a fraction of a fringe, failing to register a periodic signal.
– Need for a small window: accurate segmentation requires high spatial precision to delineate the irregular boundaries of crystals. However, the SW-FFT method has a spatial resolution limited by its window_size. Using a large window (e.g., 640 × 640) results in extremely poor localization, merging distinct nearby crystals and blurring boundaries with amorphous regions.
This required trade-off between pattern detection and spatial accuracy means no single window_size can produce a satisfactory result. The issue is therefore an algorithmic flaw, not a matter of parameter tuning. While GRATEv2 benefits from Bayesian Optimization to navigate its complex 13-dimensional parameter space, the SW-FFT method's failure is due to these fundamental algorithmic limitations.
In contrast, GRATEv2's spatial-domain, graph-based approach is explicitly designed to overcome these challenges. By first identifying individual fringe-like skeletons and then evaluating their connectivity, it can robustly segment complex and irregularly shaped crystalline domains. This comparison therefore validates that classical frequency-domain methods are ill-suited for this class of material analysis, reinforcing the value and necessity of the specialized GRATEv2 framework.
This journal is © The Royal Society of Chemistry 2025 |