计算机视觉课程的一次作业

  • O0_285003
    了解作者
  • 11.4MB
    文件大小
  • 文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-12 04:16
    上传日期
包含ipynb文件和edge.py功能文件
计算机视觉.rar
  • 计算机视觉
  • .ipynb_checkpoints
  • Untitled-checkpoint.ipynb
    72B
  • hw1-Copy1-checkpoint.ipynb
    1MB
  • hw1-checkpoint.ipynb
    1.6MB
  • hw2-checkpoint.ipynb
    26.9KB
  • references
  • iguana_edge_tracking.npy
    131.2KB
  • iguana_non_max_suppressed.png.npy
    1MB
  • iguana_canny.npy
    131.2KB
  • iguana_non_max_suppressed.npy
    1MB
  • images
  • gt
  • 131.pgm.gtf.pgm
    266.3KB
  • 132.pgm.gtf.pgm
    258.8KB
  • 105.pgm.gtf.pgm
    295.6KB
  • 48.pgm.gtf.pgm
    227.9KB
  • 218.pgm.gtf.pgm
    252.3KB
  • 202.pgm.gtf.pgm
    270KB
  • 106.pgm.gtf.pgm
    245.1KB
  • 217.pgm.gtf.pgm
    234KB
  • 138.pgm.gtf.pgm
    200KB
  • 104.pgm.gtf.pgm
    281.8KB
  • 133.pgm.gtf.pgm
    291.9KB
  • 125.pgm.gtf.pgm
    291.5KB
  • 144.pgm.gtf.pgm
    291.2KB
  • 126.pgm.gtf.pgm
    183.1KB
  • 134.pgm.gtf.pgm
    278.5KB
  • 207.pgm.gtf.pgm
    256KB
  • 143.pgm.gtf.pgm
    227.8KB
  • 36.pgm.gtf.pgm
    236.1KB
  • 61.pgm.gtf.pgm
    270.9KB
  • 110.pgm.gtf.pgm
    334KB
  • 214.pgm.gtf.pgm
    219KB
  • 50.pgm.gtf.pgm
    257.3KB
  • 204.pgm.gtf.pgm
    256KB
  • 130.pgm.gtf.pgm
    196.5KB
  • 221.pgm.gtf.pgm
    286KB
  • 111.pgm.gtf.pgm
    292.5KB
  • 108.pgm.gtf.pgm
    320.3KB
  • 56.pgm.gtf.pgm
    218.6KB
  • 203.pgm.gtf.pgm
    256KB
  • 47.pgm.gtf.pgm
    149.5KB
  • 101.pgm.gtf.pgm
    295.1KB
  • 223.pgm.gtf.pgm
    228KB
  • 220.pgm.gtf.pgm
    256KB
  • 215.pgm.gtf.pgm
    234KB
  • 103.pgm.gtf.pgm
    271.8KB
  • 43.pgm.gtf.pgm
    279.7KB
  • 62.pgm.gtf.pgm
    278.7KB
  • 137.pgm.gtf.pgm
    202.3KB
  • 109.pgm.gtf.pgm
    329KB
  • 146.pgm.gtf.pgm
    277.3KB
  • objects
  • 61.pgm
    270.9KB
  • 48.pgm
    227.9KB
  • 106.pgm
    245.1KB
  • 108.pgm
    320.3KB
  • 214.pgm
    219KB
  • 50.pgm
    257.3KB
  • 62.pgm
    278.7KB
  • 103.pgm
    271.8KB
  • 207.pgm
    256KB
  • 137.pgm
    202.4KB
  • 47.pgm
    149.5KB
  • 104.pgm
    281.8KB
  • 105.pgm
    295.7KB
  • 125.pgm
    291.5KB
  • 109.pgm
    329.1KB
  • 215.pgm
    234KB
  • 111.pgm
    292.5KB
  • 221.pgm
    286KB
  • 131.pgm
    266.4KB
  • 138.pgm
    200KB
  • 133.pgm
    292KB
  • 218.pgm
    252.3KB
  • 132.pgm
    258.9KB
  • 143.pgm
    227.9KB
  • 203.pgm
    256KB
  • 56.pgm
    218.6KB
  • 217.pgm
    234KB
  • 126.pgm
    183.1KB
  • 43.pgm
    279.7KB
  • 134.pgm
    278.6KB
  • 202.pgm
    270KB
  • 204.pgm
    256KB
  • 144.pgm
    291.2KB
  • 110.pgm
    334.1KB
  • 223.pgm
    228KB
  • 220.pgm
    256KB
  • 101.pgm
    295.2KB
  • 36.pgm
    236.1KB
  • 146.pgm
    277.4KB
  • 130.pgm
    196.5KB
  • .idea
  • inspectionProfiles
  • profiles_settings.xml
    174B
  • hw1_release.iml
    331B
  • modules.xml
    281B
  • __pycache__
内容介绍
""" CS131 - Computer Vision: Foundations and Applications Assignment 2 Author: Donsuk Lee (donlee90@stanford.edu) Date created: 07/2017 Last modified: 10/18/2017 Python Version: 3.5+ """ import numpy as np def conv(image, kernel): """ An implementation of convolution filter. This function uses element-wise multiplication and np.sum() to efficiently compute weighted sum of neighborhood at each pixel. Args: image: numpy array of shape (Hi, Wi). kernel: numpy array of shape (Hk, Wk). Returns: out: numpy array of shape (Hi, Wi). """ Hi, Wi = image.shape Hk, Wk = kernel.shape out = np.zeros((Hi, Wi)) # For this assignment, we will use edge values to pad the images. # Zero padding will make derivatives at the image boundary very big, # whereas we want to ignore the edges at the boundary. pad_width0 = Hk // 2 pad_width1 = Wk // 2 pad_width = ((pad_width0,pad_width0),(pad_width1,pad_width1)) padded = np.pad(image, pad_width, mode='edge') ### YOUR CODE HERE #kernel完全对称,无需反转 for i in range(Hi): for j in range(Wi): out[i][j]=(padded[i:i+Hk,j:j+Wk]*kernel).sum() pass ### END YOUR CODE return out def gaussian_kernel(size, sigma): """ Implementation of Gaussian Kernel. This function follows the gaussian kernel formula, and creates a kernel matrix. Hints: - Use np.pi and np.exp to compute pi and exp. Args: size: int of the size of output matrix. sigma: float of sigma to calculate kernel. Returns: kernel: numpy array of shape (size, size). """ kernel = np.zeros((size, size)) ### YOUR CODE HERE #计算基本参数k,除二向下取整 k=size//2 #计算矩阵的上三角部分,再使用转置和减去对角部分完成矩阵减少浮点运算次数 for i in range(size): for j in range(i,size): kernel[i][j]=np.power(i-k,2)+np.power(j-k,2) kernel += kernel.T - np.diag(kernel.diagonal()) #使用lambda函数进行简洁的矩阵处理,使其成为高斯核 param=2*np.power(sigma,2) gauss=lambda x:np.exp(-x/param)/(np.pi*param) kernel=gauss(kernel) pass ### END YOUR CODE return kernel def partial_x(img): """ Computes partial x-derivative of input img. Hints: - You may use the conv function in defined in this file. Args: img: numpy array of shape (H, W). Returns: out: x-derivative image. """ out = None ### YOUR CODE HERE x=np.array([[0, 0, 0], [-0.5, 0, 0.5], [0, 0, 0]]) out=conv(img,x) pass ### END YOUR CODE return out def partial_y(img): """ Computes partial y-derivative of input img. Hints: - You may use the conv function in defined in this file. Args: img: numpy array of shape (H, W). Returns: out: y-derivative image. """ out = None ### YOUR CODE HERE y=np.array([[0, -0.5, 0], [0, 0, 0], [0, 0.5, 0]]) out=conv(img,y) pass ### END YOUR CODE return out def gradient(img): """ Returns gradient magnitude and direction of input img. Args: img: Grayscale image. Numpy array of shape (H, W). Returns: G: Magnitude of gradient at each pixel in img. Numpy array of shape (H, W). theta: Direction(in degrees, 0 <= theta < 360) of gradient at each pixel in img. Numpy array of shape (H, W). Hints: - Use np.sqrt and np.arctan2 to calculate square root and arctan """ G = np.zeros(img.shape) theta = np.zeros(img.shape) ### YOUR CODE HERE G=np.sqrt(partial_x(img)**2+partial_y(img)**2) theta=np.arctan2(partial_y(img),partial_x(img))*180/np.pi #使用where函数将角度从[-180,180]变为[0,360] theta=np.where(theta>=0,theta,theta+360) pass ### END YOUR CODE return G, theta def non_maximum_suppression(G, theta): """ Performs non-maximum suppression. This function performs non-maximum suppression along the direction of gradient (theta) on the gradient magnitude image (G). Args: G: gradient magnitude image with shape of (H, W). theta: direction of gradients with shape of (H, W). Returns: out: non-maxima suppressed image. """ H, W = G.shape out = np.zeros((H, W)) # Round the gradient direction to the nearest 45 degrees theta = np.floor((theta + 22.5) / 45) * 45 #print(theta) ### BEGIN YOUR CODE #对于边缘的像素点,只与不超出索引范围的像素点比较 #对于可能大于索引范围的索引,使用try except进行处理 #对于可能小于0的索引,由于-1变成了当前维度的最后一个值,所以使用负索引进行纠正 sign=False for i in range(H): for j in range(W): neigh_1=0 neigh_2=0 if theta[i][j]%180==0: try: neigh_1=G[i][j-1-W] except: pass try: neigh_2=G[i][j+1] except: pass elif theta[i][j]%90==0: try: neigh_1=G[i-1-H][j] except: pass try: neigh_2=G[i+1][j] except: pass elif (theta[i][j]-45)%180==0: try: neigh_1=G[i-1-H][j-1-W] except: pass try: neigh_2=G[i+1][j+1] except: pass else: try: neigh_1=G[i+1][j-1-W] except: pass try: neigh_2=G[i-1-H][j+1] except: pass out[i][j]=G[i][j] if G[i][j]>=max(neigh_1,neigh_2) else 0 pass ### END YOUR CODE return out def double_thresholding(img, high, low): """ Args: img: numpy array of shape (H, W) representing NMS edge response. high: high threshold(float) for strong edges. low: low threshold(float) for weak edges. Returns: strong_edges: Boolean array representing strong edges. Strong edeges are the pixels with the values greater than the higher threshold. weak_edges: Boolean array representing weak edges. Weak edges are the pixels with the values smaller or equal to the higher threshold and greater than the lower threshold. """ strong_edges = np.zeros(img.shape, dtype=np.bool) weak_edges = np.zeros(img.shape, dtype=np.bool) ### YOUR CODE HERE H, W = img.shape for i in range(H): for j in range(W): if img[i][j] >= high: strong_edges[i, j] = True elif img[i][j] >= low: weak_edges[i, j] = True pass ### END YOUR CODE return strong_edges, weak_edges def get_neighbors(y, x, H, W): """ Return indices of valid neighbors of (y, x). Return indices of all the valid neighbors of (y, x) in an array of shape (H, W). An index (i, j) of a valid neighbor should satisfy the following: 1. i >= 0 and i < H 2. j >= 0 and j < W 3. (i, j) != (y, x) Args: y, x: location of the pixel. H, W: size of the image. Returns: neighbors: list of indices of neighboring pixels [(i, j)]. """ neighbors = [] for i in (y-1, y, y+1): for j in (x-1, x, x+1): if i >= 0 and i < H and j >= 0 and j < W: if (i == y and j == x): continue neighbors.append((i, j)) return neighbors def link
评论
    相关推荐