-
Notifications
You must be signed in to change notification settings - Fork 1
/
img2mat.m
91 lines (76 loc) · 2.7 KB
/
img2mat.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
90
91
function [cellIMG,filename] = img2mat(folder_in,mat_file_save,ext_in,smoothing,filt_opt,freq_img,s)
%Read images and write them out in .mat
%
% INPUTS
% -------------------------------------------------------------------------
% folder_in: folder containing orginal images
% ext_in: image formate extention
% folder_in: folder containing orginal images
% smoothing: on/off for Gaussian smoothing prefilter ([3,3],0.5)
% s: max number of images to use
% freq_img: frequency to sample the image stack
%
% OUTPUTS
% -------------------------------------------------------------------------
% IMG: image stack for all current images
% filename: regex for filename prefix
% filt_opt: filter options used if the smoothing param was set to 'yes'
%
% NOTES
% -------------------------------------------------------------------------
%
%Load all of the files directory information
files = dir(strcat(folder_in,filesep,'*',ext_in));
loc = strcat(folder_in,filesep,'*',ext_in);
if isempty(files)
fprintf('NO IMAGE FILES FOUND UNDER: %s \n',loc)
end
%Determine the number of files
if nargin<7
s = length(files);
end
if strcmp(smoothing,'yes')
filter_gauss = fspecial(filt_opt{1},filt_opt{2},filt_opt{3});
% Loop through files, reading in alpha-numeric order
cnt = 0;
for ii = 1:freq_img:s
cnt = cnt+1;
READ = imread(strcat(folder_in,filesep,files(ii).name));
%store the image, and do a small amount of gaussian blurring to
%improve contrast gradients
cellIMG{cnt} = imfilter(double(READ(:,:,1)),filter_gauss,'replicate');
% Option to plot the images
% imshow(IMG(:,:,ii))
% drawnow
end
else
cnt = 0;
filt_opt = {'none',[nan,nan],nan};
% Loop through files, reading in alpha-numeric order
for ii = 1:freq_img:s
cnt = cnt+1;
READ = imread(strcat(folder_in,filesep,files(ii).name));
%store the image, and do a small amount of gaussian blurring to
%improve contrast gradients
cellIMG{cnt} = double(READ(:,:,1));
% Option to plot the images
% imshow(IMG(:,:,ii))
% drawnow
end
end
% cellIMG = cell(1);
% only save out if requested
if strcmp(mat_file_save(1),'y')
for ii = 1:length(cellIMG)
IMG{1} = cellIMG{ii}; %Make a new variable to hold the current img
%image, needed for "save" to work properly
filename = strcat(files(ii).name(1:end-(length(ext_in)+1)),...
'_IDIC_image_',num2str(ii+9999),'.mat');
save(filename,'IMG');
end
cellIMG = 'images save to disk';
filename = '*IDIC_image*';
else
filename = files(1).name;
end
end