时间紧张,再加上样式编辑也学得不好,就先不编辑了。
最近在学CS131,因为网上没有视频资源,所以就PPT和Note一起看,下面记录一下所做的Homewoek.
作业全部可以在官网上找到,这里就不妨连接了。
import numpy as np from PIL import Image import matplotlib.pyplot as plt import math from skimage import color from skimage import io def load(image_path): """ Loads an image from a file path Args: image_path: file path to the image Returns: out: numpy array of shape(image_height, image_width, 3) """ out = None ### YOUR CODE HERE # Use skimage io.imread out = io.imread(image_path) ### END YOUR CODE return out def change_value(image): """ Change the value of every pixel by following x_n = 0.5*x_p^2 where x_n is the new value and x_p is the original value Args: image: numpy array of shape(image_height, image_width, 3) Returns: out: numpy array of shape(image_height, image_width, 3) """ out = None ### YOUR CODE HERE out = 0.5 * image**2 ### END YOUR CODE return out def convert_to_grey_scale(image): """ Change image to gray scale Args: image: numpy array of shape(image_height, image_width, 3) Returns: out: numpy array of shape(image_height, image_width, 3) """ out = None ### YOUR CODE HERE out = color.rgb2gray(image) ### END YOUR CODE return out def rgb_decomposition(image, channel): """ Return image **excluding** the rgb channel specified Args: image: numpy array of shape(image_height, image_width, 3) channel: str specifying the channel Returns: out: numpy array of shape(image_height, image_width, 3) """ out = None ### YOUR CODE HERE ### 有点奇怪 第三个参数 我应该提前知道是吗 out = image.copy() #print(image) #这个是 变量 = 标签 ! if channel == 'R': out[:,:,0] = 0 elif channel == 'G': out[:,:,1] = 0 elif channel == 'B': out[:,:,2] = 0 ### END YOUR CODE return out def lab_decomposition(image, channel): """ Return image decomposed to just the lab channel specified Args: image: numpy array of shape(image_height, image_width, 3) channel: str specifying the channel Returns: out: numpy array of shape(image_height, image_width, 3) """ lab = color.rgb2lab(image) out = lab.copy() ### YOUR CODE HERE if channel == 'L': out = out[:,:,0] elif channel == 'A': out = out[:,:,1] elif channel == 'B': out = out[:,:,2] ### END YOUR CODE return out def hsv_decomposition(image, channel='H'): """ Return image decomposed to just the hsv channel specified Args: image: numpy array of shape(image_height, image_width, 3) channel: str specifying the channel Returns: out: numpy array of shape(image_height, image_width, 3) """ hsv = color.rgb2hsv(image) out = hsv.copy() ### YOUR CODE HERE if channel == 'H': out = out[:,:,0] elif channel == 'S': out = out[:,:,1] elif channel == 'V': out = out[:,:,2] ### END YOUR CODE return out def mix_images(image1, image2, channel1, channel2): """ Return image which is the left of image1 and right of image 2 excluding the specified channels for each image Args: image1: numpy array of shape(image_height, image_width, 3) image2: numpy array of shape(image_height, image_width, 3) channel1: str specifying channel used for image1 channel2: str specifying channel used for image2 Returns: out: numpy array of shape(image_height, image_width, 3) """ out = None ### YOUR CODE HERE def rgb_decomposition_new(image,channel): out = image.copy() if channel == 'R': out = out[:,:,0] elif channel == 'G': out = out[:,:,1] elif channel == 'B': out = out[:,:,2] return out image1_new = rgb_decomposition_new(image1,channel1) image2_new = rgb_decomposition_new(image2,channel2) out = image2_new.copy() a,b,c = image2.shape b = int(b/2) #这行有点问题,out[0:a,0:b,:]就会报错 out[0:a,0:b] = image1_new[0:a,0:b] ### END YOUR CODE return out
import numpy as np def dot_product(vector1, vector2): """ Implement dot product of the two vectors. Args: vector1: numpy array of shape (x, n) vector2: numpy array of shape (n, x) Returns: out: numpy array of shape (x,x) (scalar if x = 1) """ out = None ### YOUR CODE HERE out = np.dot(vector1,vector2) ### END YOUR CODE return out def matrix_mult(M, vector1, vector2): """ Implement (vector1.T * vector2) * (M * vector1) Args: M: numpy matrix of shape (x, n) vector1: numpy array of shape (1, n) vector2: numpy array of shape (n, 1) Returns: out: numpy matrix of shape (1, x) """ out = None ### YOUR CODE HERE out = np.dot(dot_product(vector1,vector2) , np.dot(M,vector1)) ### END YOUR CODE return out def svd(matrix): """ Implement Singular Value Decomposition Args: matrix: numpy matrix of shape (m, n) Returns: u: numpy array of shape (m, m) s: numpy array of shape (k) v: numpy array of shape (n, n) """ u = None s = None v = None ### YOUR CODE HERE u,s,v = np.linalg.svd(matrix) ### END YOUR CODE return u, s, v def get_singular_values(matrix, n): """ Return top n singular values of matrix Args: matrix: numpy matrix of shape (m, w) n: number of singular values to output Returns: singular_values: array of shape (n) """ singular_values = None u, s, v = svd(matrix) ### YOUR CODE HERE s = np.argsort(s) singular_values = s[0:n] ### END YOUR CODE return singular_values def eigen_decomp(matrix): """ Implement Eigen Value Decomposition Args: matrix: numpy matrix of shape (m, ) Returns: w: numpy array of shape (m, m) such that the column v[:,i] is the eigenvector corresponding to the eigenvalue w[i]. """ w = None v = None ### YOUR CODE HERE w,v = np.linalg.eig(matrix) ### END YOUR CODE return w, v def get_eigen_values_and_vectors(matrix, num_values): """ Return top n eigen values and corresponding vectors of matrix Args: matrix: numpy matrix of shape (m, m) num_values: number of eigen values and respective vectors to return Returns: eigen_values: array of shape (n) eigen_vectors: array of shape (m, n) """ w, v = eigen_decomp(matrix) eigen_values = [] eigen_vectors = [] ### YOUR CODE HERE w = np.argsort(w) v = np.argsort(v) eigen_values = w[0:num_values] eigen_vectors = v[0:num_values] ### END YOUR CODE return eigen_values, eigen_vectors