完成均值类滤波器

master
charlie 4 years ago
parent 17848028ea
commit 99a287e55a

@ -22,4 +22,5 @@ urlpatterns = [
path('sharpen', views.sharpen),
path('morphology', views.morphology),
path('noise', views.noise),
path('filter', views.filtration)
]

@ -1,4 +1,5 @@
import json
import math
import random
import uuid
@ -867,5 +868,64 @@ def noise(request):
return HttpResponse('请使用POST方法')
def process_geometric_mean(img):
output = np.zeros(img.shape, np.uint8)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
ji = 1.0
for m in range(-1, 2):
if 0 <= j + m < img.shape[1]:
ji *= img[i][j + m]
output[i][j] = math.pow(ji, 1 / 3)
return output
def process_arithmetic_mean(img):
output = np.zeros(img.shape, np.uint8)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
sum = 0
for m in range(-1, 2):
for n in range(-1, 2):
if 0 <= i + m < img.shape[0] and 0 <= j + n < img.shape[1]:
sum += img[i + m][j + n]
output[i][j] = int(sum / 9)
return output
def process_harmonic_filter(img):
output = np.zeros(img.shape, np.uint8)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
sum = 0
for m in range(-1, 2):
for n in range(-1, 2):
if 0 <= i + m < img.shape[0] and 0 <= j + n < img.shape[1]:
sum += 1 / img[i + m][j + n]
output[i][j] = int(9 / sum)
return output
@csrf_exempt
def filtration(request):
if request.method == 'POST':
para = json.loads(request.body)
image = para['img']
img = cv2.imread(PREFIX + image, 0)
op = para['type']
if op == 'mean':
mean_type = para['mean_type']
if mean_type == 'arithmetic':
img = process_arithmetic_mean(img)
elif mean_type == 'geometric':
img = process_geometric_mean(img)
elif mean_type == 'harmonic':
img = process_harmonic_filter(img)
filename = getImageName() + DEFAULT_FORMAT
cv2.imwrite(PREFIX + filename, img)
return HttpResponse(filename)
return HttpResponse('请使用POST方法')
def r(request):
return render(request, 'upload.html')

Loading…
Cancel
Save