【发布时间】:2017-05-07 04:41:05
【问题描述】:
在我的一个个人项目中,我尝试在灰度图像上应用以下水平边缘蒙版。通过应用水平边缘蒙版,我试图检测图像中的水平边缘。
[1 2 1
0 0 0
-1 -2 -1]
当我尝试用上面给出的掩码对图像矩阵进行卷积时,输出图像旋转了 180 度。我不确定这是预期的行为还是我做错了什么?
这里是卷积的代码sn-p。
def convolution(self):
result = np.zeros((self.mat_width, self.mat_height))
print(self.mat_width)
print(self.mat_height)
for i in range(0, self.mat_width-self.window_width):
for j in range(0, self.mat_height-self.window_height):
# deflate both mat and mask
# if j+self.window_height >= self.mat_height:
# row_index = j+self.window_height + 1
# else:
row_index = j+self.window_height
col_index = i+self.window_width
mat_masked = self.mat[j:row_index, i:col_index]
# pixel position
index_i = i + int(self.window_width / 2)
index_j = j + int(self.window_height / 2)
prod = np.sum(mat_masked*self.mask)
if prod >= 255:
result[index_i, index_j] = 255
else:
result[index_i, index_j] = 0
return result
这是正在生成的输出。
【问题讨论】:
-
这不是预期的输出。这可能会有所帮助:songho.ca/dsp/convolution/convolution.html#convolution_2d
标签: python image image-processing edge-detection sobel