-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStep2_import_bw_slices.m
89 lines (79 loc) · 2.48 KB
/
Step2_import_bw_slices.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB CODES ACCOMPANYING QUAN ET AL. (2021) PAPER
% CODES CALCULATE POROSITY ON PROCESSED X-RAY CT IMAGES
%
% STEP2: IMPORT BLACK/WHITE IMAGES
% REFER TO README.MD FOR INSTRUCTION
%
% CITE AND CREDIT:
% SUN ET AL. (2021). POWDER TECHNOLOGY, 388:496-504.
% HTTPS://DOI.ORG/10.1016/J.POWTEC.2021.05.006
%
% TESTED ON MATLAB VERSION 2018(a) OR NEWER
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc; clear; close all;
%dbstop if error
%% Enter the ouput 3D binary .mat file name
% output_filename = 'Loose'; % for example 1
output_filename = ['Steel']; % for example 2
%%
[fileName, pathName, ~] =...
uigetfile(...
{'*.jpg;*.tif;*.tiff;*.png;*.gif;*.bmp;','All Image Files'},...
'Select stack images to be processed',...
'MultiSelect','on'...
);
%{
Determine the number of files to be processed; >= 0 and <= 2^16-1
%}
nFiles = uint16( size(fileName, 2) );
%{
Convert file name from cell to string.
%}
fileName = char(fileName);
%{
Get the pixel resolution of each image
%}
img = imread( [pathName fileName(1,:)] );
imgSize = size(img);
%{
Initiate a 3D matrix for the stack images
%}
if ( isa(img, 'double') )
BW3D = zeros( imgSize(1), imgSize(2), nFiles );
elseif ( isa(img, 'single') )
BW3D = zeros( imgSize(1), imgSize(2), nFiles, 'single' );
elseif ( isa(img, 'uint16') )
BW3D = zeros( imgSize(1), imgSize(2), nFiles, 'uint16' );
elseif ( isa(img, 'uint8') )
BW3D = zeros( imgSize(1), imgSize(2), nFiles, 'uint8' );
else
BW3D = false( imgSize(1), imgSize(2), nFiles );
end
%{
Serial-reading of images to assign values in BW3D matrix
%}
for iFile=1:nFiles
BW3D(:,:,iFile) = imread( [pathName fileName(iFile,:)] );
end
%
BW = BW3D;
%{
Binarize the image to logical format (if the image is not logical type) and crop the margin
%}
if ~isa(BW,'logical')
BW = imbinarize(BW3D);
end
stats = regionprops3(BW,'Image','BoundingBox');
BW=stats.Image{1,1};
%%
%{
save the 3D binary matrix (Select the file name!)
%}
save ([output_filename,'_packing.mat'],'BW'); %
%{
Visualize your packing by z-slices
Thanks to Maysam Shahedi for the visualization function.
(https://www.mathworks.com/matlabcentral/fileexchange/41334-imshow3d)
%}
imshow3Dfull(BW)