编辑:感谢霍华德,我已经更正了这里的代码,它现在似乎可以正常工作了。
EDIT2:我更新了代码以包含最初预期的垂直模糊。使用各种设置生成的示例输出:Blur comparison images.jpg
模糊操作的另一个引用 (Java):Blurring for Beginners
原帖:
我正在尝试学习基本图像处理并复制这个简单的 Blur method (“重用结果”下的第二个函数 BlurHorizontal)在 python 中。我知道 PIL 中已经有模糊功能,但我想自己尝试一下基本的像素操作。
此函数应获取源图像,然后根据特定半径对 RGB 像素值进行平均,并将处理后的图像写入新文件。我的问题是我得到了很多平均值完全错误的像素(例如,在某些区域,亮绿色线条而不是红色线条)。
模糊半径为 2 时,平均法将以输入像素为中心的 5 个像素的 RGB 值相加。它使用“滑动窗口”来保持总计,减去传出像素(左侧)并添加新的传入像素(窗口右侧)。 Blur method explained here
有什么我哪里出错的想法吗?我不确定为什么图像的某些部分清晰地模糊,而其他区域充满了与周围区域完全无关的颜色。
感谢您的帮助。
FIXED WORKING 代码(感谢 Howard)
import Image, numpy, ImageFilter
img = Image.open('testimage.jpg')
imgArr = numpy.asarray(img) # readonly
# blur radius in pixels
radius = 2
# blur window length in pixels
windowLen = radius*2+1
# columns (x) image width in pixels
imgWidth = imgArr.shape[1]
# rows (y) image height in pixels
imgHeight = imgArr.shape[0]
#simple box/window blur
def doblur(imgArr):
# create array for processed image based on input image dimensions
imgB = numpy.zeros((imgHeight,imgWidth,3),numpy.uint8)
imgC = numpy.zeros((imgHeight,imgWidth,3),numpy.uint8)
# blur horizontal row by row
for ro in range(imgHeight):
# RGB color values
totalR = 0
totalG = 0
totalB = 0
# calculate blurred value of first pixel in each row
for rads in range(-radius, radius+1):
if (rads) >= 0 and (rads) <= imgWidth-1:
totalR += imgArr[ro,rads][0]/windowLen
totalG += imgArr[ro,rads][1]/windowLen
totalB += imgArr[ro,rads][2]/windowLen
imgB[ro,0] = [totalR,totalG,totalB]
# calculate blurred value of the rest of the row based on
# unweighted average of surrounding pixels within blur radius
# using sliding window totals (add incoming, subtract outgoing pixels)
for co in range(1,imgWidth):
if (co-radius-1) >= 0:
totalR -= imgArr[ro,co-radius-1][0]/windowLen
totalG -= imgArr[ro,co-radius-1][1]/windowLen
totalB -= imgArr[ro,co-radius-1][2]/windowLen
if (co+radius) <= imgWidth-1:
totalR += imgArr[ro,co+radius][0]/windowLen
totalG += imgArr[ro,co+radius][1]/windowLen
totalB += imgArr[ro,co+radius][2]/windowLen
# put average color value into imgB pixel
imgB[ro,co] = [totalR,totalG,totalB]
# blur vertical
for co in range(imgWidth):
totalR = 0
totalG = 0
totalB = 0
for rads in range(-radius, radius+1):
if (rads) >= 0 and (rads) <= imgHeight-1:
totalR += imgB[rads,co][0]/windowLen
totalG += imgB[rads,co][1]/windowLen
totalB += imgB[rads,co][2]/windowLen
imgC[0,co] = [totalR,totalG,totalB]
for ro in range(1,imgHeight):
if (ro-radius-1) >= 0:
totalR -= imgB[ro-radius-1,co][0]/windowLen
totalG -= imgB[ro-radius-1,co][1]/windowLen
totalB -= imgB[ro-radius-1,co][2]/windowLen
if (ro+radius) <= imgHeight-1:
totalR += imgB[ro+radius,co][0]/windowLen
totalG += imgB[ro+radius,co][1]/windowLen
totalB += imgB[ro+radius,co][2]/windowLen
imgC[ro,co] = [totalR,totalG,totalB]
return imgC
# number of times to run blur operation
blurPasses = 3
# temporary image array for multiple passes
imgTmp = imgArr
for k in range(blurPasses):
imgTmp = doblur(imgTmp)
print "pass #",k,"done."
imgOut = Image.fromarray(numpy.uint8(imgTmp))
imgOut.save('testimage-processed.png', 'PNG')
最佳答案
我想你的线路有问题
for rads in range(-radius, radius):
仅运行到 radius-1(范围不包括最后一个)。将 1 添加到第二个范围参数。
更新:行内还有一个小问题
if (co-radius-1) > 0:
应该是
if (co-radius-1) >= 0:
关于python - 这个python图像模糊函数有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5527809/