Esempio n. 1
0
def create_pyramid(I, layer_num=5, downscale=2):
	# create a image pyramid
	I_py_temp = tuple(pyramid_gaussian(\
		I[0,:,:,:], \
		max_layer=layer_num-1, \
		downscale=downscale
	))
	I_lap_py_temp = tuple(pyramid_laplacian(\
		I[0,:,:,:], \
		max_layer=layer_num-1, \
		downscale=downscale
	))
	I_py = []
	I_lap_py = []
	for i in range(layer_num):
		I_py.append(
			np.empty(
				(
					(I.shape[0],)+
					I_py_temp[i].shape
				)
			)
		)
		I_lap_py.append(
			np.empty(
				(
					(I.shape[0],)+
					I_lap_py_temp[i].shape
				)
			)
		)

	for i in range(I.shape[0]):
		I_py_temp = tuple(pyramid_gaussian(\
			I[i,:,:,:], \
			max_layer=layer_num-1, \
			downscale=downscale
		))
		I_lap_py_temp = tuple(pyramid_laplacian(\
			I[i,:,:,:], \
			max_layer=layer_num-1, \
			downscale=downscale
		))
		for j in range(layer_num):
			I_py[j][i,:,:,:] = I_py_temp[j]
			I_lap_py[j][i,:,:,:] = I_lap_py_temp[j]
	
	return I_py, I_lap_py
Esempio n. 2
0
def test_build_laplacian_pyramid():
    rows, cols, dim = image.shape
    pyramid = pyramid_laplacian(image, downscale=2)

    for layer, out in enumerate(pyramid):
        layer_shape = (rows / 2 ** layer, cols / 2 ** layer, dim)
        assert_array_equal(out.shape, layer_shape)
Esempio n. 3
0
def test_build_laplacian_pyramid():
    rows, cols, dim = image.shape
    pyramid = pyramid_laplacian(image, downscale=2)

    for layer, out in enumerate(pyramid):
        layer_shape = (rows / 2**layer, cols / 2**layer, dim)
        assert_array_equal(out.shape, layer_shape)
Esempio n. 4
0
def Laplacian_pyramid(I, n, s=3):  # 拉普拉斯金字塔
    gau = []
    for (i, resized) in enumerate(pyramid_laplacian(I, downscale=2, sigma=s)):
        if i == n:
            break
        gau.append(resized)
    return gau
Esempio n. 5
0
 def laplacian(self, base: np.ndarray) -> List[np.ndarray]:
     """Downsample using :func:`skimage.transform.pyramid_laplacian`."""
     return list(
         pyramid_laplacian(
             base,
             downscale=self.downscale,
             max_layer=self.max_layer,
             multichannel=False,
         ))
def laplace_pyramids(dataset, levels=4):
    pyramids = [[] for _ in range(levels)]
    for img in dataset:
        pyramid = transform.pyramid_laplacian(img,
                                              max_layer=levels - 1,
                                              multichannel=False)
        for i, img_level in enumerate(pyramid):
            pyramids[i].append(img_level)

    return pyramids
Esempio n. 7
0
def main(argv):
  filename = argv[1]
  img = io.imread(filename, as_grey=True)
  lpyra = tuple(transform.pyramid_laplacian(img))
  l = lpyra[0]
  l = exposure.equalize_hist(l)
  y, x = np.indices((l.shape[0],l.shape[1]))
  vect = np.array(zip(y.reshape(y.size),x.reshape(x.size),l.reshape(l.size)))
  io.imshow(l)
  io.show()
def laplacian_downsample(frames, pyramid_levels=4):
    nt = frames.shape[0]
    for ii, frame in enumerate(frames):
        pyr = transform.pyramid_laplacian(frame.astype(np.float))
        for jj in xrange(pyramid_levels + 1):
            ds = pyr.next()
        if ii == 0:
            out = np.empty((nt,) + ds.shape, dtype=np.float)
        out[ii] = ds
    return out
Esempio n. 9
0
def create_pyramid_tuple(I, layer_num=5, downscale=2):
	# return the pyramid as a list of tuples
	I_py = []
	I_lap_py = []
	for i in range(I.shape[0]):
		I_py.append(
			tuple(pyramid_gaussian(\
				I[i,:,:,:], \
				max_layer=layer_num-1, \
				downscale=downscale
			))
		)
		I_lap_py.append(
			tuple(pyramid_laplacian(\
				I[i,:,:,:], \
				max_layer=layer_num-1, \
				downscale=downscale
			))
		)
	return I_py, I_lap_py
Esempio n. 10
0
def loopback(image, scales=3, A=None, pyramid=None):
  """
  Generate laplacian pyramid and return reconstructred matrix from generative matrices
  """
  base_image_side = image.shape[0]
  base_image_dim = base_image_side**2

  if pyramid == None:
    pyramid = tuple(pyramid_laplacian(image, downscale=2, max_layer=scales-1, mode='constant', order=5))
  
  if A == None:
    A = laplacianGenerative(base_image_dim, scales)

  recon = np.zeros((base_image_dim,1))

  for s in range(scales):
    image_dim = int(base_image_dim/4**s)
    recon += A[s].T.dot(pyramid[s].reshape((image_dim,1)))

  return recon
    
Esempio n. 11
0
def blob_stats(im, show_images=False):
    if im.shape != (64, 64):
        im = transform.resize(im, (64, 64))
    pyramid = tuple(transform.pyramid_laplacian(im, mode='nearest'))
    num_blobs = []
    for idx in range(4):
        dist = np.ceil(8.0 / (3.0 * (idx+1)))
        maxms = feature.peak_local_max(pyramid[idx], min_distance=dist)
        if maxms == []:
            num_blobs.append(0)
            continue
        num_blobs.append(maxms.shape[0])
        if show_images:
            plt.subplot(4, 2, 2*idx + 1)
            plt.imshow(im, cmap='gray')
            plt.scatter(maxms.T[1] * (2**idx), maxms.T[0] * (2**idx),
                        s=50, c='g')
            plt.subplot(4, 2, 2*idx + 2)
            plt.imshow(pyramid[idx], cmap='gray')
            plt.scatter(maxms.T[1], maxms.T[0], s=50, c='g')
    if show_images:
        plt.show()
    return num_blobs
Esempio n. 12
0
def laplacian(image_dim=32 * 32,
              scales=3,
              normalize=False,
              plot=False,
              images='vanhateran',
              num_patches=4000):
    # Generate Laplacian pyramids from data set
    image_side = math.sqrt(image_dim)

    if images == 'vanhateran':
        IMAGES = preprocess.extract_patches(images='vanhateran',
                                            num_images=num_patches,
                                            image_dim=image_dim)

    elif images == 'berkeley':
        IMAGES = np.zeros((image_side, image_side, num_patches))
        FIMAGES = scipy.io.loadmat('../../images/IMAGES.mat')
        FIMAGES = FIMAGES['IMAGES']
        (full_image_size, full_image_size, full_num_patches) = FIMAGES.shape
        BUFF = 4

        for i in range(num_patches):
            imi = np.floor(full_num_patches * random.uniform(0, 1))
            r = BUFF + np.ceil((full_image_size - image_side - 2 * BUFF) *
                               random.uniform(0, 1))
            c = BUFF + np.ceil((full_image_size - image_side - 2 * BUFF) *
                               random.uniform(0, 1))
            IMAGES[i, :, :] = FIMAGES[r:r + image_side, c:c + image_side, imi]

    elif images == 'cifar':

        def unpickle(file):
            # Open CIFAR files
            import cPickle
            fo = open(file, 'rb')
            dict = cPickle.load(fo)
            fo.close()
            return dict

        data = 'data_batch_1'
        cifar = unpickle('./data/cifar-10/' + data)
        IMAGES = cifar['data'].T
        IMAGES = IMAGES - np.mean(IMAGES, axis=0)  # Mean subtract

    pyramids = []
    I = tuple([
        np.zeros((num_patches, image_dim / (2 * 2)**s)) for s in range(scales)
    ])
    #I = np.empty((image_dim, scales, num_patches))

    for i in range(num_patches):
        pyramids.append(
            tuple(
                pyramid_laplacian(IMAGES[i, :, :],
                                  downscale=2,
                                  max_layer=scales - 1)))

        if normalize == True:
            for s in range(scales):
                temp = pyramids[i][s].reshape((image_dim / (2 * 2)**s))
                norm = float(sqrt(np.sum(temp**2)))
                if norm != 0:
                    I[s][i, :image_dim / (2 * 2)**s] = temp / norm
                else:
                    I[s][i, :image_dim / (2 * 2)**s] = temp

        else:
            for s in range(scales):
                I[s][i, :image_dim / (2 * 2)**s] = pyramids[i][s].reshape(
                    (image_dim / (2 * 2)**s))

    if plot:
        fig, ax = plt.subplots(scales + 1)
        ax[0].imshow(IMAGES[1, :, :],
                     cmap='Greys',
                     vmin=np.min(IMAGES[1, :, :]),
                     vmax=np.max(IMAGES[1, :, :]),
                     interpolation='nearest')
        for i in range(1, 4):
            ax[i].imshow(I[1, :, i - 1].reshape(image_side, image_side),
                         vmin=np.min(I[1, :, i - 1]),
                         vmax=np.max(I[1, :, i - 1]),
                         cmap='Greys',
                         interpolation='nearest')

    return I
Esempio n. 13
0

def gauss_subsample(im, depth):
    pyramid = [im]
    for _ in range(depth - 1):
        conv = convolve2d(pyramid[-1], K_gauss,
                          mode='same')  # .astype('uint8')
        pyramid.append(conv[::2, ::2])
    return pyramid


def laplace_subsample(im, depth):
    return [(img - convolve2d(img, K_gauss, mode='same'))
            for img in gauss_subsample(im, depth)]


fix, axs = plt.subplots(4, 4)
for idx, im in enumerate(
        zip(gauss_subsample(I, 4), pyramid_gaussian(I, max_layer=3, sigma=1),
            laplace_subsample(I, 4), pyramid_laplacian(I, max_layer=3,
                                                       sigma=1))):
    axs[0, idx].imshow(im[0], cmap='gray')
    axs[1, idx].imshow(im[1], cmap='gray')
    axs[2, idx].imshow(im[2], cmap='gray')
    axs[3, idx].imshow(im[3], cmap='gray')
axs[0, 0].set_title('Own Gaussian')
axs[1, 0].set_title('skimage Gaussian')
axs[2, 0].set_title('Own Laplacian')
axs[3, 0].set_title('skimage Laplacian')
plt.show()