从图像中去除噪声 Python PIL
Removing noise from image Python PIL
这里是新手。
有形象
我在图像上添加了噪音,我需要用噪音(或类似的东西)来清除图像。
接下来是去噪算法:
If the brightness of the pixel is greater than the average brightness
of the local neighborhood, then the brightness of the pixel is
replaced with the average brightness of the surroundings.
from PIL import Image
import random
from multiprocessing import Pool
from multiprocessing.dummy import Pool as ThreadPool
img=Image.open('pic.bmp')
print(img.size)
randomenter=int(input('Enter numpix: '))
for numpix in range(0, randomenter):
x=random.randint(0,int(img.size[0]-1))
y=random.randint(0,int(img.size[1]-1))
r=random.randint(0,255)
g=random.randint(0,255)
b=random.randint(0,255)
img.putpixel((x,y),(r,g,b))
img.show()
img.save("noise.bmp", "BMP")
img2=Image.open("noise.bmp")
w, h = img2.size
pix=img2.copy()
for x in range(0,w-1):
if x-1>0 and x<w:
for y in range(0,h-1):
if y-1>0 and y<h:
local1=(0.3 * pix.getpixel((x-1,y-1))[0]) + (0.59 * pix.getpixel((x-1,y-1))[1]) + (0.11 * pix.getpixel((x-1,y-1))[2])
local2=(0.3 * pix.getpixel((x-1,y))[0]) + (0.59 * pix.getpixel((x-1,y))[1]) + (0.11 * pix.getpixel((x-1,y))[2])
local3=(0.3 * pix.getpixel((x-1,y+1))[0]) + (0.59 * pix.getpixel((x-1,y+1))[1]) + (0.11 * pix.getpixel((x-1,y+1))[2])
local4=(0.3 * pix.getpixel((x,y-1))[0]) + (0.59 * pix.getpixel((x,y-1))[1]) + (0.11 * pix.getpixel((x,y-1))[2])
LOCAL5=(0.3 * pix.getpixel((x,y))[0]) + (0.59 * pix.getpixel((x,y))[1]) + (0.11 * pix.getpixel((x,y))[2])
local6=(0.3 * pix.getpixel((x,y+1))[0]) + (0.59 * pix.getpixel((x,y+1))[1]) + (0.11 * pix.getpixel((x,y+1))[2])
local7=(0.3 * pix.getpixel((x+1,y-1))[0]) + (0.59 * pix.getpixel((x+1,y-1))[1]) + (0.11 * pix.getpixel((x+1,y-1))[2])
local8=(0.3 * pix.getpixel((x+1,y))[0]) + (0.59 * pix.getpixel((x+1,y))[1]) + (0.11 * pix.getpixel((x+1,y))[2])
local9=(0.3 * pix.getpixel((x+1,y+1))[0]) + (0.59 * pix.getpixel((x+1,y+1))[1]) + (0.11 * pix.getpixel((x+1,y+1))[2])
localall=(local1+local2+local3+local4+local6+local7+local8+local9)/8
if LOCAL5<localall:
img2.putpixel((x,y),(int(pix.getpixel((x,y))[0]*localall/LOCAL5),int(pix.getpixel((x,y))[1]*localall/LOCAL5),int(pix.getpixel((x,y))[2]*localall/LOCAL5)))
img2.show()
亮度变化瞬间出现问题。
官方码头没有关于这个案例的详细信息。
有什么解决办法吗?
首先,您需要创建图像副本以将数据写入:imgCopy = img.copy()
。
否则,您的噪声校正像素将影响尚未触及的像素的校正。
在你的 else 语句中,你只需要标准化你的中间像素,然后将它乘以你计算的平均亮度:
imgCopy[i, j] = imgCopy[i, j] * local / nuzh
这里是新手。 有形象 我在图像上添加了噪音,我需要用噪音(或类似的东西)来清除图像。 接下来是去噪算法:
If the brightness of the pixel is greater than the average brightness of the local neighborhood, then the brightness of the pixel is replaced with the average brightness of the surroundings.
from PIL import Image
import random
from multiprocessing import Pool
from multiprocessing.dummy import Pool as ThreadPool
img=Image.open('pic.bmp')
print(img.size)
randomenter=int(input('Enter numpix: '))
for numpix in range(0, randomenter):
x=random.randint(0,int(img.size[0]-1))
y=random.randint(0,int(img.size[1]-1))
r=random.randint(0,255)
g=random.randint(0,255)
b=random.randint(0,255)
img.putpixel((x,y),(r,g,b))
img.show()
img.save("noise.bmp", "BMP")
img2=Image.open("noise.bmp")
w, h = img2.size
pix=img2.copy()
for x in range(0,w-1):
if x-1>0 and x<w:
for y in range(0,h-1):
if y-1>0 and y<h:
local1=(0.3 * pix.getpixel((x-1,y-1))[0]) + (0.59 * pix.getpixel((x-1,y-1))[1]) + (0.11 * pix.getpixel((x-1,y-1))[2])
local2=(0.3 * pix.getpixel((x-1,y))[0]) + (0.59 * pix.getpixel((x-1,y))[1]) + (0.11 * pix.getpixel((x-1,y))[2])
local3=(0.3 * pix.getpixel((x-1,y+1))[0]) + (0.59 * pix.getpixel((x-1,y+1))[1]) + (0.11 * pix.getpixel((x-1,y+1))[2])
local4=(0.3 * pix.getpixel((x,y-1))[0]) + (0.59 * pix.getpixel((x,y-1))[1]) + (0.11 * pix.getpixel((x,y-1))[2])
LOCAL5=(0.3 * pix.getpixel((x,y))[0]) + (0.59 * pix.getpixel((x,y))[1]) + (0.11 * pix.getpixel((x,y))[2])
local6=(0.3 * pix.getpixel((x,y+1))[0]) + (0.59 * pix.getpixel((x,y+1))[1]) + (0.11 * pix.getpixel((x,y+1))[2])
local7=(0.3 * pix.getpixel((x+1,y-1))[0]) + (0.59 * pix.getpixel((x+1,y-1))[1]) + (0.11 * pix.getpixel((x+1,y-1))[2])
local8=(0.3 * pix.getpixel((x+1,y))[0]) + (0.59 * pix.getpixel((x+1,y))[1]) + (0.11 * pix.getpixel((x+1,y))[2])
local9=(0.3 * pix.getpixel((x+1,y+1))[0]) + (0.59 * pix.getpixel((x+1,y+1))[1]) + (0.11 * pix.getpixel((x+1,y+1))[2])
localall=(local1+local2+local3+local4+local6+local7+local8+local9)/8
if LOCAL5<localall:
img2.putpixel((x,y),(int(pix.getpixel((x,y))[0]*localall/LOCAL5),int(pix.getpixel((x,y))[1]*localall/LOCAL5),int(pix.getpixel((x,y))[2]*localall/LOCAL5)))
img2.show()
亮度变化瞬间出现问题。 官方码头没有关于这个案例的详细信息。 有什么解决办法吗?
首先,您需要创建图像副本以将数据写入:imgCopy = img.copy()
。
否则,您的噪声校正像素将影响尚未触及的像素的校正。
在你的 else 语句中,你只需要标准化你的中间像素,然后将它乘以你计算的平均亮度:
imgCopy[i, j] = imgCopy[i, j] * local / nuzh