import numpy as np
import matplotlib.pyplot as plt
import skimage.data
#load moon image
image = skimage.data.moon()
plt.imshow(image, cmap = 'gray')
plt.show()
#get shape of image
image.shape
(512, 512)
#get infos about image values
np.mean(image)
112.16957092285156
image.mean()
112.16957092285156
image.max()
255
image.min()
0
maxval = image.max()
#create a mask of pixels higher than 0.5*(max value)
mask = image>0.5*maxval
plt.imshow(mask)
plt.show()
#crop an image region
image_crop = image[50:150,50:150]
mask_crop = mask[50:150,50:150]
#superpose image and mask
plt.imshow(image_crop,cmap = 'gray')
plt.imshow(mask_crop,cmap = 'Reds', alpha = 0.5)
plt.show()
#get only positive mask pixels
pixels = image_crop[mask_crop]
len(pixels)
1072
#find the function to generate normally distributed samples (Google is your best friend)
np.random.standard_normal?
Docstring: standard_normal(size=None) Draw samples from a standard Normal distribution (mean=0, stdev=1). Parameters ---------- size : int or tuple of ints, optional Output shape. If the given shape is, e.g., ``(m, n, k)``, then ``m * n * k`` samples are drawn. Default is None, in which case a single value is returned. Returns ------- out : float or ndarray A floating-point array of shape ``size`` of drawn samples, or a single sample if ``size`` was not specified. Notes ----- For random samples from :math:`N(\mu, \sigma^2)`, use one of:: mu + sigma * np.random.standard_normal(size=...) np.random.normal(mu, sigma, size=...) See Also -------- normal : Equivalent function with additional ``loc`` and ``scale`` arguments for setting the mean and standard deviation. Examples -------- >>> np.random.standard_normal() 2.1923875335537315 #random >>> s = np.random.standard_normal(8000) >>> s array([ 0.6888893 , 0.78096262, -0.89086505, ..., 0.49876311, # random -0.38672696, -0.4685006 ]) # random >>> s.shape (8000,) >>> s = np.random.standard_normal(size=(3, 4, 2)) >>> s.shape (3, 4, 2) Two-by-four array of samples from :math:`N(3, 6.25)`: >>> 3 + 2.5 * np.random.standard_normal(size=(2, 4)) array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], # random [ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) # random Type: builtin_function_or_method
image_crop.shape[1]
100
#generate a matrix of the right size
normal_matrix = np.random.standard_normal((image_crop.shape[0], image_crop.shape[1]))
plt.imshow(normal_matrix)
plt.show()
im1 = image_crop +100* normal_matrix
plt.imshow(im1)
plt.show()
im1 = image_crop + 10*normal_matrix
plt.imshow(im1)
plt.show()
rocket = skimage.data.rocket()
rocket.shape
(427, 640, 3)
rocket_cropped = rocket[50:150,50:150,:]
rocket_cropped.shape
(100, 100, 3)
rocket_cropped[:,:,0] = image_crop
plt.imshow(rocket_cropped)
plt.show()