# [CS131]Homework #1 due

1、滤波

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

3、数学推导

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

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
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])
36
37     return out
38
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)
52
53     Returns:
55     """
56
57     H, W = image.shape
58     out = None
59
61
64
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
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 )
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
118     pass
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
138     # 互相关 不需要翻转 故翻转回来
139     # 这图片大小有问题 我做了一下修正
140     g = g[1:,:]
141     g_new = np.flip(np.flip(g,1),0).copy()
142     out = conv_fast(f, g_new)
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
162     g_new = g.copy() - g.mean()
163     f_new = f.copy() - f.mean()
164     out = cross_correlation(f_new, g_new)
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
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)
216
217     return out```
View Code

[CS131]Homework #1 due

(0)
(0)