image - PIL中的ImageFilter如何在使用Kernel或mask过滤后将0到255之间的像素值归一化

标签 image image-processing python-2.7 python-imaging-library

PIL 中的 ImageFilter 如何在使用内核或掩码过滤后将像素值(不是内核)归一化到 0 到 255 之间?(特别是零和内核,如:( -1,-1,-1,0,0, 0,1,1,1 ))

my code was like:
import Image
import ImageFilter
Horiz = ImageFilter.Kernel((3, 3), (-1,-2,-1,0,0,0,1,2,1), scale=None, offset=0) #sobel mask
im_fltd = myimage.filter(Horiz)

最佳答案

答案在 documentation 中对于 Kernel :

If the scale argument is given, the result of applying the kernel to each pixel is divided by the scale value. The default is the sum of the kernel weights.

编辑:我做了一些实验,发现当比例为零时会发生什么,因为除以零不会很好地工作。任何结果值 <= 0变成 0和任何东西> 0变成 255。

在您的情况下,我建议使用 1 的比例和 128 的偏移量相反。

附言您可以验证 scale 的计算通过查看参数 the source :

    if scale is None:
        # default scale is sum of kernel
        scale = reduce(lambda a,b: a+b, kernel)

关于image - PIL中的ImageFilter如何在使用Kernel或mask过滤后将0到255之间的像素值归一化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15906368/

相关文章:

python - 在Python中将字典列表划分为子字典

python - 当程序写入菜单/打印日志时,使用 python 在父 shell 中创建 bash 别名

javascript - 动画(淡入)img src 如果加载 react #noJQuery

c++ - C++ 读取图像文件并将其放在套接字上

C# 图像去歪斜

python - 如何在不使用磁盘存储的情况下在 python 中加载图像?

ios - swift 在 Iphone 上的屏幕截图只有白色背景

image - 如何在Unix命令行中显示多个图像?

php - 在 MongoDB GridFS 中存储图像

python - 在 python 中创建一个确认函数