Created
February 20, 2015 06:34
-
-
Save hasnainv/49dc4a85933de6b979f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from optparse import OptionParser | |
import cPickle | |
import skimage | |
import numpy as np | |
from scipy import misc | |
from distutils.dir_util import mkpath | |
def loadImage(imgpath): | |
try: | |
im = misc.imread(imgpath) | |
return im | |
except: | |
print "Can't open image. Check image again: " + imgpath | |
def extractPatches(im, outpath, window_shape=(13,13, 3),stride=3): | |
nr, nc, ncolor = im.shape | |
patches = skimage.util.view_as_windows(im, window_shape, stride) | |
nR, nC, t, H, W, C = patches.shape | |
nWindow = nR * nC | |
patches = np.reshape(patches, (nWindow, H, W, C)) | |
patches = patches.astype(np.float16, copy=False) | |
#pathces = np.asarray(patches, dtype=np.float16) | |
return patches | |
if __name__ == '__main__': | |
parser = OptionParser() | |
parser.add_option('-i', '--imgpath', dest='imgpath', help='path of input image') | |
parser.add_option('-o', '--outpath', dest='outpath', help="path of output") | |
(options, args) = parser.parse_args() | |
im = loadImage(options.imgpath) | |
patches = extractPatches(im, options.outpath) | |
if not os.path.isdir(options.outpath): | |
mkpath(options.outpath) | |
imname = os.path.basename(options.imgpath).split('.')[0] | |
outfile = options.outpath + imname + "_patches.dump" | |
f = open(outfile, 'wb') | |
cPickle.dump(patches, f, protocol=2) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this! You saved me a few hours of headache.