编辑:感谢霍华德,我在这里纠正代码,它似乎是现在的工作。这个python图像模糊函数有什么问题?
EDIT2:我已经更新了代码以包含垂直模糊。使用不同的设置产生的样本输出:Blur comparison images.jpg
的模糊操作的另一个参考(JAVA):Blurring for Beginners
原帖:
我想了解基本的图像处理和重复这个简单Blur method在python(在“重用结果”第二功能BlurHorizontal)。我知道PIL中已经有模糊函数,但我想自己尝试基本的像素操作。
该函数应该获取源图像,然后基于某个半径平均RGB像素值并将处理后的图像写入新文件。我的问题是,我得到了很多的像素与完全错误的平均值(例如,亮绿色的线,而不是红色在某些领域)。
模糊半径为2时,平均方法将以输入像素为中心的5个像素的RGB值相加。它使用“滑动窗口”,以保持一个运行总计,减去传出像素(左侧)以及将所述新的输入像素(窗口的右侧)。 Blur method explained here
在那里我已经出了错任何想法?我不知道为什么图像的某些部分干净模糊而其他地区都充满了色彩完全无关的周边地区。
感谢您的帮助。
固定的工作守则(感谢霍华德)
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')
你可以张贴一些样品输入/输出? – Blender 2011-04-03 05:46:21
当我看到'a-b-c'时,我总是很担心。我从来没有记得任何语言的运算符的关联性,以便知道它是否会被解释为'a-(bc)'或'(ab)-c' – sarnold 2011-04-03 06:05:33
在我使用的每种语言中,加法,减法,乘法,和师从左到右,如数学。幂指数是唯一经常将右向左联系起来的常见现象。 – 2011-04-03 06:08:49