Created
February 13, 2012 21:27
-
-
Save npinto/1820633 to your computer and use it in GitHub Desktop.
montage2d
This file contains hidden or 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 numpy as np | |
| from scipy import misc | |
| import pylab as pl | |
| from skimage.util import shape | |
| def foo(p): | |
| alpha = int(np.ceil(np.sqrt(p.shape[0]))) | |
| # -- fill missing squares | |
| cst = p.mean() | |
| n_missing = int((alpha**2.) - p.shape[0]) | |
| missing = np.ones((n_missing,) + p.shape[1:]) * cst | |
| b = np.vstack((p, missing)) | |
| # -- reshape to 2d montage | |
| c = b.reshape(alpha, alpha, b.shape[1], b.shape[2]) | |
| d = c.swapaxes(1, 2) | |
| e = d.reshape(d.shape[0] * d.shape[1], d.shape[2] * d.shape[3]) | |
| return e | |
| def main(): | |
| psize = (7, 7) | |
| n_patches = 15 | |
| l = misc.lena() / 255. | |
| p = shape.view_as_windows(l, psize) | |
| p = p.reshape((-1,) + psize) | |
| ridx = np.random.permutation(p.shape[0]) | |
| p = p[ridx] | |
| p = p[:n_patches] | |
| e = foo(p) | |
| pl.matshow(e, cmap=pl.cm.gray) | |
| pl.show() | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment