diff --git a/basic/urls.py b/basic/urls.py index 671dbb8..45f38e1 100644 --- a/basic/urls.py +++ b/basic/urls.py @@ -22,4 +22,5 @@ urlpatterns = [ path('sharpen', views.sharpen), path('morphology', views.morphology), path('noise', views.noise), + path('filter', views.filtration) ] diff --git a/basic/views.py b/basic/views.py index 078f7cc..5a0da96 100644 --- a/basic/views.py +++ b/basic/views.py @@ -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')