python图像模糊_这个python图像模糊功能有什么问题?

importImage,numpy,ImageFilterimg=Image.open('testimage.jpg')imgArr=numpy.asarray(img)# readonly# blur radius in pixelsradius=2# blur window length in pixelswindowLen=radius*2+1# columns (x) image width in pixelsimgWidth=imgArr.shape[1]# rows (y) image height in pixelsimgHeight=imgArr.shape[0]#simple box/window blurdefdoblur(imgArr):# create array for processed image based on input image dimensionsimgB=numpy.zeros((imgHeight,imgWidth,3),numpy.uint8)imgC=numpy.zeros((imgHeight,imgWidth,3),numpy.uint8)# blur horizontal row by rowforroinrange(imgHeight):# RGB color valuestotalR=0totalG=0totalB=0# calculate blurred value of first pixel in each rowforradsinrange(-radius,radius+1):if(rads)>=0and(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)forcoinrange(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]/windowLenif(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 pixelimgB[ro,co]=[totalR,totalG,totalB]# blur verticalforcoinrange(imgWidth):totalR=0totalG=0totalB=0forradsinrange(-radius,radius+1):if(rads)>=0and(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]forroinrange(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]/windowLenif(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]returnimgC# number of times to run blur operationblurPasses=3# temporary image array for multiple passesimgTmp=imgArrforkinrange(blurPasses):imgTmp=doblur(imgTmp)print"pass #",k,"done."imgOut=Image.fromarray(numpy.uint8(imgTmp))imgOut.save('testimage-processed.png','PNG')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值