python laplace过滤器返回错误的值 [英] python laplace filter returns wrong values

查看:257
本文介绍了python laplace过滤器返回错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我需要在python中实现一种图像处理程序,所以我也想实现laplace滤镜。我使用了矩阵

-1 -1 -1

-1 8 -1

-1 -1 -1



并执行以下代码:

 用于范围内的行(1,(len (1,(len(self._dataIn [row]) -  1)):
value =( - int(self._dataIn [row(self._dataIn) - 1)):
for col in range - 1] [col -1] [0])
- int(self._dataIn [row - 1] [col] [0])
- int(self._dataIn [row - 1] [ (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()

self._dataIn是图像数组。在另一种方法中,我把图片转换成了

  np.array(img)

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

但是当我启动这个程序的时候,这个程序就不能用了,它会返回一个奇怪的结果:



我已经改变了我的代码很多时间,但不能弄清楚,我做错了什么。我的执行有问题吗?或者我误解了这个过滤器?

先谢谢您!

解决方案

您不能修改数组如果你正在应用过滤器 self._dataIn ,那么你不能将结果保存在 self._dataIn

顺便说一下,使用 numpy 矩阵乘法做过滤(和使用一个组件图像):

$ $ p $ img = img。 (2)#获得一个NxM图像
imgOut = np.zeros(img.shape,dtype = uint8)
M = np.array([
[-1,-1, - 1],
[-1,8,-1],
[-1,-1,-1]
])
(1,img。 (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()))

结果:


As I need to implement a sort of image processing program in python I also wanted to implement the laplace filter. I used the matrix
-1 -1 -1
-1 8 -1
-1 -1 -1

and implemented the following code:

    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()

self._dataIn is the image array. In another method I converted the image to

np.array(img)

and after processing the filter method, I reconvert the image using

Image.fromarray(...)

But when I start the program, it returns a strange result:

I already have changed my code lots of time but can't figure out, what I'm doing wrong. Is there something wrong with my implementation? Or do I misunderstand the filter?
Thank you in advance!

解决方案

You must not modify the array in place, i.e. if you are applying the filter to self._dataIn, then you must not store the result in self._dataIn because on the next filter operation, the input will not be the correct one.

By the way, it is easier to use numpy matrix multiplication to do the filtering (and to use a one component image):

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 ()))

Result:

这篇关于python laplace过滤器返回错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆