diff --git a/basic/views.py b/basic/views.py index 5a0da96..75f30f4 100644 --- a/basic/views.py +++ b/basic/views.py @@ -906,6 +906,65 @@ def process_harmonic_filter(img): return output +def get_middle(array): + # 列表的长度 + length = len(array) + # 对列表进行选择排序,获得有序的列表 + for i in range(length): + for j in range(i + 1, length): + # 选择最大的值 + if array[j] > array[i]: + # 交换位置 + temp = array[j] + array[j] = array[i] + array[i] = temp + return array[int(length / 2)] + + +def process_max_sort(img): + output = np.zeros(img.shape, np.uint8) + array = [] + for i in range(img.shape[0]): + for j in range(img.shape[1]): + array.clear() + 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]: + array.append(img[i + m][j + n]) + array.sort(reverse=True) + output[i][j] = max(array[0], img[i][j]) + return output + + +def process_min_sort(img): + output = np.zeros(img.shape, np.uint8) + array = [] + for i in range(img.shape[0]): + for j in range(img.shape[1]): + array.clear() + 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]: + array.append(img[i + m][j + n]) + array.sort() + output[i][j] = min(array[0], img[i][j]) + return output + + +def process_median_sort(img): + output = np.zeros(img.shape, np.uint8) + array = [] + for i in range(img.shape[0]): + for j in range(img.shape[1]): + array.clear() + 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]: + array.append(img[i + m][j + n]) + output[i][j] = get_middle(array) + return output + + @csrf_exempt def filtration(request): if request.method == 'POST': @@ -913,14 +972,21 @@ def filtration(request): image = para['img'] img = cv2.imread(PREFIX + image, 0) op = para['type'] + detail_type = para['detail_type'] if op == 'mean': - mean_type = para['mean_type'] - if mean_type == 'arithmetic': + if detail_type == 'arithmetic': img = process_arithmetic_mean(img) - elif mean_type == 'geometric': + elif detail_type == 'geometric': img = process_geometric_mean(img) - elif mean_type == 'harmonic': + elif detail_type == 'harmonic': img = process_harmonic_filter(img) + elif op == 'sort': + if detail_type == 'max': + img = process_max_sort(img) + elif detail_type == 'median': + img = process_median_sort(img) + elif detail_type == 'min': + img = process_min_sort(img) filename = getImageName() + DEFAULT_FORMAT cv2.imwrite(PREFIX + filename, img) return HttpResponse(filename)