-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathimage_patches.py
More file actions
40 lines (27 loc) · 1.05 KB
/
image_patches.py
File metadata and controls
40 lines (27 loc) · 1.05 KB
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
# Based on CS294A/CS294W Programming Assignment Starter Code
from numpy import *
import scipy.io
def getPatches(numPatches, patchSize):
images = scipy.io.loadmat('IMAGES.mat')['IMAGES']
patches = zeros((numPatches, patchSize*patchSize))
numImages = images.shape[2]
imageIdxs = random.randint(numImages, size=numPatches)
sortedImageIdxs = argsort(imageIdxs)
lastImageIdx = -1
for i in range(numPatches):
imageIdx = imageIdxs[sortedImageIdxs[i]]
if lastImageIdx != imageIdx:
img = images[:,:,imageIdx]
lastImageIdx = imageIdx
x = random.randint(img.shape[0] - patchSize)
y = random.randint(img.shape[1] - patchSize)
patch = img[x:x+patchSize, y:y+patchSize]
patches[sortedImageIdxs[i], :] = patch.reshape(1, patchSize*patchSize)
# Remove DC (mean of images)
patches = patches - mean(patches)
# Truncate to +/-3 standard deviations and scale to -1 to 1
pstd = 3 * std(patches)
patches = maximum(minimum(patches, pstd), -pstd) / pstd
# Rescale from [-1,1] to [0.1,0.9]
patches = (patches + 1) * 0.4 + 0.1
return patches