Switch to unified view

a b/combinedDeepLearningActiveContour/functions/sampleIMAGES.m
1
function patches = sampleIMAGES(IMAGES,patchsize,norm_ena)
2
% sampleIMAGES
3
% inputs
4
% numpatches: for example 1E4
5
% patchsize% for example 8*8
6
% IMAGES: images in a 3D matrix
7
% output
8
% patches % a vector of randomly chosen patches
9
if nargin==2
10
    norm_ena=1;
11
end
12
visibleSize = patchsize*patchsize;   % number of input units 
13
14
% get size and number of images
15
[xn, yn, zn]=size(IMAGES);
16
17
scale=patchsize/xn;
18
imgs = imresize(IMAGES, scale);
19
patches=(reshape(imgs,visibleSize,zn));
20
21
%% ---------------------------------------------------------------
22
% For the autoencoder to work well we need to normalize the data
23
% Specifically, since the output of the network is bounded between [0,1]
24
% (due to the sigmoid activation function), we have to make sure 
25
% the range of pixel values is also bounded between [0,1]
26
if norm_ena==1
27
patches = normalizeData(patches);
28
end
29
end
30
31
%% ---------------------------------------------------------------
32
function patches = normalizeData(patches)
33
34
% Squash data to [0.1, 0.9] since we use sigmoid as the activation
35
% function in the output layer
36
37
% Remove DC (mean of images). 
38
patches = bsxfun(@minus, patches, mean(patches));
39
40
% Truncate to +/-3 standard deviations and scale to -1 to 1
41
pstd = 3 * std(patches(:));
42
patches = max(min(patches, pstd), -pstd) / pstd;
43
44
% Rescale from [-1,1] to [0.1,0.9]
45
patches = (patches + 1) * 0.4 + 0.1;
46
47
end