Switch to unified view

a b/combinedDeepLearningActiveContour/functions/intersampleIMAGES.m
1
% sampleIMAGES.m
2
% sampling patches for learning
3
function patches = sampleIMAGES(numpatches)
4
% sampleIMAGES
5
% Returns 10000 patches for training
6
load IMAGES; % load images from disk 
7
patchsize = 8; % we'll use 8x8 patches 
8
%numpatches = 10000;
9
% Initialize patches with zeros. Your code will fill in this matrix--one
10
% column per patch, 10000 columns. 
11
patches = zeros(patchsize*patchsize, numpatches);
12
 
13
%% ---------- YOUR CODE HERE --------------------------------------
14
% Instructions: Fill in the variable called "patches" using data 
15
% from IMAGES. 
16
% 
17
% IMAGES is a 3D array containing 10 images
18
% For instance, IMAGES(:,:,6) is a 512x512 array containing the 6th image,
19
% and you can type "imagesc(IMAGES(:,:,6)), colormap gray;" to visualize
20
% it. (The contrast on these images look a bit off because they have
21
% been preprocessed using using "whitening." See the lecture notes for
22
% more details.) As a second example, IMAGES(21:30,21:30,1) is an image
23
% patch corresponding to the pixels in the block (21,21) to (30,30) of
24
% Image 1
25
 
26
counter = 1;
27
ranimg = ceil(rand(1, numpatches) * 10);
28
ranpix = ceil(rand(2, numpatches) * (512 - patchsize));
29
ranpixm = ranpix + patchsize - 1;
30
while(counter <= numpatches)
31
whichimg = ranimg(1, counter);
32
whichpix = ranpix(:, counter);
33
whichpixm = ranpixm(:, counter);
34
patch = IMAGES(whichpix(1):whichpixm(1), whichpix(2):whichpixm(2), whichimg);
35
repatch = reshape(patch, patchsize * patchsize, 1);
36
patches(:, counter) = repatch;
37
counter = counter + 1;
38
end
39
 
40
%% ---------------------------------------------------------------
41
% For the autoencoder to work well we need to normalize the data
42
% Specifically, since the output of the network is bounded between [0,1]
43
% (due to the sigmoid activation function), we have to make sure 
44
% the range of pixel values is also bounded between [0,1]
45
patches = normalizeData(patches);
46
 
47
end
48
49
%% ---------------------------------------------------------------
50
function patches = normalizeData(patches)
51
52
% Squash data to [0.1, 0.9] since we use sigmoid as the activation
53
% function in the output layer
54
55
% Remove DC (mean of images). 
56
patches = bsxfun(@minus, patches, mean(patches));
57
58
% Truncate to +/-3 standard deviations and scale to -1 to 1
59
pstd = 3 * std(patches(:));
60
patches = max(min(patches, pstd), -pstd) / pstd;
61
62
% Rescale from [-1,1] to [0.1,0.9]
63
patches = (patches + 1) * 0.4 + 0.1;
64
65
end