码迷,mamicode.com
首页 > 其他好文 > 详细

[CS131]Homework #1 due

时间:2018-04-27 23:49:25      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:pass   home   sub   image   公式   mat   import   div   padding   

花了6个多小时,其实代码根本不花时间,主要是花在看PDF和Note上了。

感觉要是学了图像处理应该会轻松很多。

 

大概有这么几个坑吧:

1、滤波

2、卷积(convolution)和互相关(cross-correlation) 

3、数学推导

4、如何更高效地计算卷积(我没有细想,不过感觉受到后文的提示,应该利用分解做?(存疑。

 

尤其是第二点,对从未接触过图像处理的确实很难理解。

卷积是翻转的,互相关是翻转的。互相关能用在模式识别(template match)上。不过那个公式我觉得很没有道理,为什么他就能算出这个相关度呢。。

最后就直接应用上了:1、转化成灰度图:互相关。

          2、非灰度图倒是很好理解,NCC(normal cross-correlation)下利用互相关公式除以各自的模长(看成向量即可,理解的话就当成是两个向量的夹角)。

技术分享图片
  1 import numpy as np
  2 
  3 
  4 def conv_nested(image, kernel):
  5     """A naive implementation of convolution filter.
  6 
  7     This is a naive implementation of convolution using 4 nested for-loops.
  8     This function computes convolution of an image with a kernel and outputs
  9     the result that has the same shape as the input image.
 10 
 11     Args:
 12         image: numpy array of shape (Hi, Wi)
 13         kernel: numpy array of shape (Hk, Wk)
 14 
 15     Returns:
 16         out: numpy array of shape (Hi, Wi)
 17     """
 18     Hi, Wi = image.shape
 19     Hk, Wk = kernel.shape
 20     out = np.zeros((Hi, Wi))
 21     #print(image)
 22 
 23     ### YOUR CODE HERE
 24     #这里一定要转化 不然不是整型
 25     x = (int)((Hk-1)/2)
 26     y = (int)((Wk-1)/2)
 27     
 28     for a in range(x,Hi-x,1):
 29         #print(a)
 30         for b in range(y,Wi-y,1):
 31             for c in range(0,Hk,1):
 32                 for d in range(0,Wk,1):
 33                     out[a,b] += kernel[c,d] * image[a+x-c,b+y-d]
 34                     #print(out[a,b])
 35     ### END YOUR CODE
 36 
 37     return out
 38 
 39 def zero_pad(image, pad_height, pad_width):
 40     """ Zero-pad an image.
 41 
 42     Ex: a 1x1 image [[1]] with pad_height = 1, pad_width = 2 becomes:
 43 
 44         [[0, 0, 0, 0, 0],
 45          [0, 0, 1, 0, 0],
 46          [0, 0, 0, 0, 0]]         of shape (3, 5)
 47 
 48     Args:
 49         image: numpy array of shape (H, W)
 50         pad_width: width of the zero padding (left and right padding)
 51         pad_height: height of the zero padding (bottom and top padding)
 52 
 53     Returns:
 54         out: numpy array of shape (H+2*pad_height, W+2*pad_width)
 55     """
 56 
 57     H, W = image.shape
 58     out = None
 59 
 60     ### YOUR CODE HERE
 61 
 62     out = np.zeros((H+2*pad_height, W+2*pad_width))
 63     out[pad_height:(H+pad_height),pad_width:W+pad_width] = image.copy()
 64     
 65     ### END YOUR CODE
 66     return out
 67 
 68 
 69 def conv_fast(image, kernel):
 70     """ An efficient implementation of convolution filter.
 71 
 72     This function uses element-wise multiplication and np.sum()
 73     to efficiently compute weighted sum of neighborhood at each
 74     pixel.
 75 
 76     Hints:
 77         - Use the zero_pad function you implemented above
 78         - There should be two nested for-loops
 79         - You may find np.flip() and np.sum() useful
 80 
 81     Args:
 82         image: numpy array of shape (Hi, Wi)
 83         kernel: numpy array of shape (Hk, Wk)
 84 
 85     Returns:
 86         out: numpy array of shape (Hi, Wi)
 87     """
 88     Hi, Wi = image.shape
 89     Hk, Wk = kernel.shape
 90     out = np.zeros((Hi, Wi))
 91 
 92     ### YOUR CODE HERE
 93     kernel_new = np.flip(np.flip(kernel,1),0).copy()
 94     x = (int)((Hk-1)/2)
 95     y = (int)((Wk-1)/2)
 96     
 97     for i in range(x,Hi-x,1):
 98         for j in range(y,Wi-y,1):
 99             out[i,j] = np.sum( image[i-x:i+x+1,j-y:j+y+1] * kernel_new )
100     ### END YOUR CODE
101 
102     return out
103 
104 def conv_faster(image, kernel):
105     """
106     Args:
107         image: numpy array of shape (Hi, Wi)
108         kernel: numpy array of shape (Hk, Wk)
109 
110     Returns:
111         out: numpy array of shape (Hi, Wi)
112     """
113     Hi, Wi = image.shape
114     Hk, Wk = kernel.shape
115     out = np.zeros((Hi, Wi))
116 
117     ### YOUR CODE HERE
118     pass
119     ### END YOUR CODE
120 
121     return out
122 
123 def cross_correlation(f, g):
124     """ Cross-correlation of f and g
125 
126     Hint: use the conv_fast function defined above.
127 
128     Args:
129         f: numpy array of shape (Hf, Wf)
130         g: numpy array of shape (Hg, Wg)
131 
132     Returns:
133         out: numpy array of shape (Hf, Wf)
134     """
135 
136     out = None
137     ### YOUR CODE HERE
138     # 互相关 不需要翻转 故翻转回来
139     # 这图片大小有问题 我做了一下修正
140     g = g[1:,:]
141     g_new = np.flip(np.flip(g,1),0).copy()
142     out = conv_fast(f, g_new)
143     ### END YOUR CODE
144 
145     return out
146 
147 def zero_mean_cross_correlation(f, g):
148     """ Zero-mean cross-correlation of f and g
149 
150     Subtract the mean of g from g so that its mean becomes zero
151 
152     Args:
153         f: numpy array of shape (Hf, Wf)
154         g: numpy array of shape (Hg, Wg)
155 
156     Returns:
157         out: numpy array of shape (Hf, Wf)
158     """
159 
160     out = None
161     ### YOUR CODE HERE
162     g_new = g.copy() - g.mean()
163     f_new = f.copy() - f.mean()
164     out = cross_correlation(f_new, g_new)
165     ### END YOUR CODE
166 
167     return out
168 
169 def normalized_cross_correlation(f, g):
170     """ Normalized cross-correlation of f and g
171 
172     Normalize the subimage of f and the template g at each step
173     before computing the weighted sum of the two.
174 
175     Args:
176         f: numpy array of shape (Hf, Wf)
177         g: numpy array of shape (Hg, Wg)
178 
179     Returns:
180         out: numpy array of shape (Hf, Wf)
181     """
182     
183 
184     out = None
185     ### YOUR CODE HERE
186 
187     def conv_fast_new(image, kernel):
188 
189         Hi, Wi = image.shape
190         Hk, Wk = kernel.shape
191         out = np.zeros((Hi, Wi))
192 
193         kernel_new = np.flip(np.flip(kernel,1),0).copy()
194         x = (int)((Hk-1)/2)
195         y = (int)((Wk-1)/2)
196         
197         for i in range(x,Hi-x,1):
198             for j in range(y,Wi-y,1):
199                 out[i,j] = np.sum( image[i-x:i+x+1,j-y:j+y+1] * kernel_new / image[i-x:i+x+1,j-y:j+y+1].std() /kernel_new.std())
200 
201        return out
202     
203     def cross_correlation_new(f, g):
204 
205         out = None
206         g = g[1:,:]
207         g_new = np.flip(np.flip(g,1),0).copy()
208         out = conv_fast_new(f, g_new)
209 
210         return out
211     
212     g_new = g.copy() - g.mean()
213     f_new = f.copy() - f.mean()
214     out = cross_correlation_new(f_new, g_new)
215     ### END YOUR CODE
216 
217     return out
View Code

 

[CS131]Homework #1 due

标签:pass   home   sub   image   公式   mat   import   div   padding   

原文地址:https://www.cnblogs.com/AlwaysYu/p/8965089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!