Calculate the size of a grain in an SEM image

Version 1.0.0 (1.4 KB) by N/A
Code that allows you to calculate grain size in an SEM or FE-SEM image.
46 Downloads
Updated 6 Feb 2023

View License

% Load the SEM image
img = imread('sem_image_075_100K.jpg');
% Convert the image to grayscale
img_gray = rgb2gray(img);
% Apply a threshold to the image to segment the grains
img_threshold = img_gray > 128;
% Use bwlabel to identify the individual grains in the image
[labeled_img, num_grains] = bwlabel(img_threshold);
% Initialize a variable to hold the grain sizes
grain_sizes = zeros(1, num_grains);
% Loop over each grain in the image and calculate its size
for i = 1:num_grains
grain_mask = labeled_img == i;
grain_sizes(i) = sum(grain_mask(:));
end
% Calculate the average grain size
avg_grain_size = mean(grain_sizes);
% Print the average grain size to the command window
fprintf('The average grain size is %.2f pixels\n', avg_grain_size);

Cite As

N/A (2024). Calculate the size of a grain in an SEM image (https://www.mathworks.com/matlabcentral/fileexchange/124445-calculate-the-size-of-a-grain-in-an-sem-image), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2022b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0