python laplace过滤器返回错误的值

use*_*641 3 python image filter edge-detection python-imaging-library

因为我需要在python中实现某种图像处理程序,所以我也想实现laplace过滤器。我使用矩阵
-1 -1 -1
-1 8 -1
-1 -1 -1

并实现了以下代码:

    for row in range(1, (len(self._dataIn) - 1)):
         for col in range(1, (len(self._dataIn[row])- 1)):
             value =  (- int(self._dataIn[row - 1][col -1][0])
                       - int(self._dataIn[row - 1][col][0])
                       - int(self._dataIn[row - 1][col + 1][0])
                       - int(self._dataIn[row][col -1][0]) + 
                       (8 * int(self._dataIn[row][col][0]))
                       - int(self._dataIn[row][col +1][0])
                       - int(self._dataIn[row + 1][col -1][0])
                       - int(self._dataIn[row + 1][col][0])
                       - int(self._dataIn[row + 1][col +1][0]))
             self._dataIn[row][col][0] = np.minimum(255, np.maximum(0, value))
             self._dataIn[row][col][1] = np.minimum(255, np.maximum(0, value))
             self._dataIn[row][col][2] = np.minimum(255, np.maximum(0, value))
    self.update()
Run Code Online (Sandbox Code Playgroud)

self._dataIn是图像数组。在另一种方法中,我将图像转换为

np.array(img)
Run Code Online (Sandbox Code Playgroud)

在处理滤镜方法后,我使用

Image.fromarray(...)
Run Code Online (Sandbox Code Playgroud)

但是当我启动程序时,它返回一个奇怪的结果: 在此处输入图片说明

我已经改变了我的代码很多时间,但不知道我在做什么错。我的实现有问题吗?还是我误会了过滤器?
先感谢您!

Hol*_*olt 6

您不得在适当的位置修改数组,即,如果将过滤器应用于self._dataIn,则不能存储结果,self._dataIn因为在下一个过滤器操作中,输入将不是正确的输入。

顺便说一句,使用numpy矩阵乘法进行过滤(并使用一个分量图像)更加容易:

img = img.mean(2) # get a NxM image
imgOut = np.zeros (img.shape, dtype = uint8)
M = np.array([
    [-1, -1, -1], 
    [-1,  8, -1], 
    [-1, -1, -1]
])
for row in range(1, img.shape[0] - 1):
    for col in range(1, img.shape[1] - 1):
        value = M * img[(row - 1):(row + 2), (col - 1):(col + 2)]
        imgOut[row, col] = min(255, max(0, value.sum ()))
Run Code Online (Sandbox Code Playgroud)

结果:

在此处输入图片说明 在此处输入图片说明