下面的代码是针对图像左上角的盐和胡椒噪声消除的自适应方法编写的。它是噪声消除算法的完整代码的一部分,由多个函数组成 - 每个函数用于图像的各个部分,例如左上角,右上角,上中间等......,最后是中间这是图像的大部分。
该算法基于一种基于称为基于修改决策的部分修剪全局均值滤波器(MDBPTGMF)的方法的自适应方法。
代码运行并在680x387图像上进行测试,概率为10%的椒盐噪声,但在前10秒后,它开始急剧减速。大约不到一分钟后,在(y,x)=(2,50)左右,每5秒钟速度变得不到1个像素。
我认为它必须是填满的临时内存并减慢进程。 ' #Initialize值'中的所有值在处理每个像素之后或期间,部分被替换或重置。
这可能是速度放缓的原因之一?
from List import *
from Kernel import *
from pixel_op import *
l = List()
knl = Kernel()
po = pixel_op()
class adaptive_filter:
# Upper left corner
def removeNoise_UpperLeftCorner(self, image):
fp = None # Filtered pixel value
w = 3 # Current window size
w_max = 9 # The maximum window size
h = 2 # Kernel's increment
v = [] # The array of elements or pixels of current window
uv = [] # The array of uncorrupted pixel of current window
w_max_center = (w_max-1)/2
for y in range(w_max_center):
for x in range(w_max_center):
n = 0 # The number of uncorrupted pixel in current window
p = po.getGray(image, y, x)# The processing pixel
gray_mat = knl.getGrayMat_UpperLeftCorner(image, y, x, w) # The kernel with gray values
v = knl.getGrayListFromGrayMat(gray_mat)
# If the pixel's value is between 0 and 255, it is identified as an uncorrupted pixel
# and left unchanged
if p > 0 and p < 255:
continue
else:
for i in range(len(v)):
if v[i] != 0 or v[i] != 255:
uv.append(v[i])
n += 1
if n >= w:
median = l.getMed(uv)
fp = median
po.insertGrayIntoPixel(image, y, x, fp)
else:
while w < w_max and n < w:
w += h
if w < w_max and n >= w:
median = l.getMed(uv)
fp = median
po.insertGrayIntoPixel(image, y, x, fp)
elif w == w_max:
gray_mat = knl.getGrayMat_UpperLeftCorner(image, y, x, w_max)
v = knl.getGrayListFromGrayMat(gray_mat)
if n < w and n != 0:
mean = l.getMean(uv)
fp = mean
po.insertGrayIntoPixel(image, y, x, fp)
elif all(p==0 or p==255 for p in v):
mean = l.getMean(v)
fp = mean
po.insertGrayIntoPixel(image, y, x, fp)
elif all(p==0 for p in v):
fp = 255
po.insertGrayIntoPixel(image, y, x, fp)
elif all(p==255 for p in v):
fp = 0
po.insertGrayIntoPixel(image, y, x, fp)
w = 3 # Reset current window
导入的文件: List.py
class List:
# Selection sort a list
def sortList(self, list):
# Selection sort all values in the list
list_count = len(list)
for i in range(list_count):
min = list[i]
min_j = i
for j in range(i+1,list_count):
if list[j] < min:
min = list[j]
min_j = j
# Swap
temp = list[i]
list[i] = min
list[min_j] = temp
return list
# Return the minimum gray value in the list of gray values
def getMin(self, list):
# Selection sort the minimum value in the list
min = list[0]
for i in range(1, len(list)):
if list[i] <= min:
min = int(list[i])
else:
continue
return min
# Return the maximum gray value in the list of gray values
def getMax(self, list):
# Selection sort the minimum value in the list
max = list[0]
for i in range(1, len(list)):
if list[i] >= max:
max = int(list[i])
else:
continue
return max
def getMean(self, list):
sum = 0
list_count = len(list)
for i in range(list_count):
sum += list[i]
mean = sum/list_count
return mean
# Return the median gray value in the list of gray values
def getMed(self, list):
sorted_list = self.sortList(list) # The sorted list
sorted_list_count = len(sorted_list)
sorted_list_count = int(sorted_list_count) # The number of values in the list
med = None # The median
if sorted_list_count % 2 == 0:
med_pos_1 = int(sorted_list_count/2 - 1)
med_pos_2 = int(sorted_list_count/2)
med_1 = float(sorted_list[med_pos_1])
med_2 = float(sorted_list[med_pos_2])
med = (med_1 + med_2)/2
else:
med_pos = (sorted_list_count-1)/2
med = sorted_list[med_pos]
return med
Kernel.py
from pixel_op import *
po = pixel_op()
class Kernel:
# Create the kernel of mat_size x mat_size with None values
def getEmptyMat(self, mat_size):
mat = [] # The kernel
# Create the kernel
for j in range(mat_size): # Number of rows
mat.append([])
for i in range(mat_size): # Number of columns
mat[j].append(None)
return mat
# GET LIST WITH GRAY VALUES
# Upper left corner
def getGrayListFromGrayMat(self, gray_mat):
gray_list = [] # The list with gray values
# If the value in the kernel is an integer: add it to the list of gray values
for i in range(len(gray_mat)):
for j in range(len(gray_mat[i])):
if gray_mat[i][j] == None:
continue
else:
gray_list.append(gray_mat[i][j])
return gray_list
# GET KERNEL WITH GRAY VALUES
# Upper left corner
def getGrayMat_UpperLeftCorner(self, image, y, x, mat_size):
gray_mat = self.getEmptyMat(mat_size) # Empty kernel
mat_center = (mat_size-1)/2 # The kernel's center position
# The potential number of rows/columns outside image
nrows_outside_image = mat_center-y # The number of rows outside image
ncols_outside_image = mat_center-x # The number of columns outside umage
for i in range(nrows_outside_image, mat_size): # Range of rows in the kernel
for j in range(ncols_outside_image, mat_size): # Range of columns in the kernel
# Insert gray value
gray = po.getGray(image, y-(mat_center-i), x-(mat_center-j))
gray_mat[i][j] = gray
return gray_mat
pixel_op.py
class pixel_op:
# Return gray value of a pixel
def getGray(self, image, y, x):
try:
image[y,x,0] == image[y,x,1]
image[y,x,0] == image[y,x,2]
image[y,x,1] == image[y,x,2]
except ValueError:
print 'The pixel is not in grayscale'
return image[y,x,0]
# Insert a gray value into a pixel
def insertGrayIntoPixel(self, image, y, x, gray_value):
#print 'ImageFilter > insertGrayIntoPixel'
image[y,x] = gray_value
答案 0 :(得分:0)
这些可能是罪魁祸首:
for y in range(w_max_center):
for x in range(w_max_center):
...
for i in range(len(v)):
...
while w < w_max and n < w:
...
all(p==0 or p==255 for p in v) # several similar lines
目前还不清楚以下是什么,但可能值得检查其执行时间/内存占用量:
p = po.getGray(image, y, x)# The processing pixel
gray_mat = knl.getGrayMat(image, y, x, w) # The kernel with gray values
v = knl.getGrayListFromGrayMat(gray_mat)
我不知道是否有必要拨打getGrayListFromGrayMat
16次?