一、算数均值
import numpy as np
from cv2 import cv2
def a_mean(img, kernel_size):
G_mean_img = np.zeros(img.shape)
k = int((kernel_size - 1) / 2)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if i < k or i > (img.shape[0] - k - 1) or j < k or j > (img.shape[1] - k - 1):
G_mean_img[i][j] = img[i][j]
else:
for n in range(kernel_size):
for m in range(kernel_size):
G_mean_img[i][j] += float(1 / (kernel_size * kernel_size) * img[i - k + n][j - k + m])
G_mean_img = np.uint8(G_mean_img)
return G_mean_img
if __name__ == "__main__":
img = cv2.imread('data.jpg', 0)
G_mean_img_3 = a_mean(img, kernel_size=3)
cv2.imshow("source", img)
cv2.imshow("a", G_mean_img_3)
cv2.waitKey(0)
print("算数均值滤波完成")
二、几何均值滤波
import numpy as np
from cv2 import cv2
def b_mean(img, kernel_size):
G_mean_img = np.ones(img.shape)
k = int((kernel_size - 1) / 2)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if i < k or i > (img.shape[0] - k - 1) or j < k or j > (img.shape[1] - k - 1):
G_mean_img[i][j] = img[i][j]
else:
for n in range(kernel_size):
for m in range(kernel_size):
G_mean_img[i][j] *= float(img[i - k + n][j - k + m])
G_mean_img[i][j] = pow(G_mean_img[i][j], 1 / (kernel_size * kernel_size))
G_mean_img = np.uint8(G_mean_img)
return G_mean_img
if __name__ == "__main__":
img = cv2.imread('data.jpg', 0)
G_mean_img_3 = b_mean(img, kernel_size=5)
cv2.imshow("source", img)
cv2.imshow("b", G_mean_img_3)
cv2.waitKey(0)
print("几何均值滤波完成")
三、调和均值滤波
import numpy as np
from cv2 import cv2
def c_mean(img, kernel_size):
G_mean_img = np.zeros(img.shape)
k = int((kernel_size - 1) / 2)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if i < k or i > (img.shape[0] - k - 1) or j < k or j > (img.shape[1] - k - 1):
G_mean_img[i][j] = img[i][j]
else:
for n in range(kernel_size):
for m in range(kernel_size):
if img[i - k + n][j - k + m] == 0:
G_mean_img[i][j] = 0
break
else:
G_mean_img[i][j] += 1 / float(img[i - k + n][j - k + m])
else:
continue
break
if G_mean_img[i][j] != 0:
G_mean_img[i][j] = (kernel_size * kernel_size) / G_mean_img[i][j]
G_mean_img = np.uint8(G_mean_img)
return G_mean_img
if __name__ == "__main__":
img = cv2.imread('data.jpg', 0)
G_mean_img_3 = c_mean(img, kernel_size=5)
cv2.imshow("source", img)
cv2.imshow("c", G_mean_img_3)
cv2.waitKey(0)
print("调和均值滤波完成")
四、逆调和均值滤波
import numpy as np
from cv2 import cv2
def d_mean(img, kernel_size, Q):
G_mean_img = np.zeros(img.shape)
k = int((kernel_size - 1) / 2)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if i < k or i > (img.shape[0] - k - 1) or j < k or j > (img.shape[1] - k - 1):
G_mean_img[i][j] = img[i][j]
else:
result_top = 0
result_down = 0
for n in range(kernel_size):
for m in range(kernel_size):
if Q > 0:
result_top += pow(float(img[i - k + n][j - k + m]), Q + 1)
result_down += pow(float(img[i - k + n][j - k + m]), Q)
else:
if img[i - k + n][j - k + m] == 0:
G_mean_img[i][j] = 0
break
else:
result_top += pow(float(img[i - k + n][j - k + m]), Q + 1)
result_down += pow(float(img[i - k + n][j - k + m]), Q)
else:
continue
break
else:
if result_down != 0:
G_mean_img[i][j] = result_top / result_down
G_mean_img = np.uint8(G_mean_img)
return G_mean_img
if __name__ == "__main__":
img = cv2.imread('data.jpg', 0)
G_mean_img_3 = d_mean(img, kernel_size=9, Q=-1.5)
cv2.imshow("source", img)
cv2.imshow("c", G_mean_img_3)
cv2.waitKey(0)
print("逆调和均值滤波完成")
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)