From be7406ce14b27024e58e21ef579aa1beb5bebcab Mon Sep 17 00:00:00 2001 From: charlie <1753524606@qq.com> Date: Thu, 21 Jul 2022 10:26:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=BE=B9=E7=BC=98=E6=A3=80?= =?UTF-8?q?=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- basic/urls.py | 1 + basic/views.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/basic/urls.py b/basic/urls.py index db55d27..de46ef1 100644 --- a/basic/urls.py +++ b/basic/urls.py @@ -16,4 +16,5 @@ urlpatterns = [ path('affine', views.affine), path('histogram', views.histogram), path('plt', views.piecewise_linear_transform), + path('ed', views.edge_detection), ] diff --git a/basic/views.py b/basic/views.py index 8c27eb2..26ef1cd 100644 --- a/basic/views.py +++ b/basic/views.py @@ -487,6 +487,75 @@ def piecewise_linear_transform(request): return HttpResponse([{"orig": image, "hist": ret_name1}, {"orig": out_name, "hist": ret_name2}]) return HttpResponse('请使用POST方法') +@csrf_exempt +def edge_detection(request): + if request.method == 'POST': + para = json.loads(request.body) + image = para['img'] + img = cv2.imread(PREFIX + image, 0) + operator = para['operator'] + if operator == 'Roberts': + kernelx = np.array([[-1, 0], [0, 1]], dtype=int) + kernely = np.array([[0, -1], [1, 0]], dtype=int) + x = cv2.filter2D(img, cv2.CV_16S, kernelx) + y = cv2.filter2D(img, cv2.CV_16S, kernely) + absX = cv2.convertScaleAbs(x) + absY = cv2.convertScaleAbs(y) + img = cv2.addWeighted(absX, 0.5, absY, 0.5, 0) + elif operator == 'Sobel': + x = cv2.Sobel(img, cv2.CV_16S, 1, 0) + y = cv2.Sobel(img, cv2.CV_16S, 0, 1) + absX = cv2.convertScaleAbs(x) + absY = cv2.convertScaleAbs(y) + img = cv2.addWeighted(absX, 0.5, absY, 0.5, 0) + elif operator == 'Laplacian': + img_gaussianBlur = cv2.GaussianBlur(img, (5, 5), 0) + dst = cv2.Laplacian(img_gaussianBlur, cv2.CV_16S, ksize=3) + img = cv2.convertScaleAbs(dst) + elif operator == 'LoG': + grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) + img = cv2.copyMakeBorder(grayImage, 2, 2, 2, 2, borderType=cv2.BORDER_REPLICATE) + img = cv2.GaussianBlur(img, (3, 3), 0, 0) + m1 = np.array( + [[0, 0, -1, 0, 0], [0, -1, -2, -1, 0], [-1, -2, 16, -2, -1], [0, -1, -2, -1, 0], [0, 0, -1, 0, 0]], + dtype=np.int32) + image1 = np.zeros(img.shape).astype(np.int32) + h, w, _ = img.shape + for i in range(2, h - 2): + for j in range(2, w - 2): + image1[i, j] = np.sum(m1 * img[i - 2:i + 3, j - 2:j + 3, 1]) + img = cv2.convertScaleAbs(image1) + elif operator == 'Canny': + tmp = cv2.GaussianBlur(img, (3, 3), 0) + # 3. 求x,y方向的Sobel算子 + gradx = cv2.Sobel(tmp, cv2.CV_16SC1, 1, 0) + grady = cv2.Sobel(tmp, cv2.CV_16SC1, 0, 1) + # 4. 使用Canny函数处理图像,x,y分别是3求出来的梯度,低阈值50,高阈值150 + img = cv2.Canny(gradx, grady, 50, 150) + elif operator == 'Enhance': + h, w = img.shape + gradient = np.zeros((h, w)) + img = img.astype('float') + for i in range(h - 1): + for j in range(w - 1): + gx = abs(img[i + 1, j] - img[i, j]) + gy = abs(img[i, j + 1] - img[i, j]) + gradient[i, j] = gx + gy + sharp = img + gradient + sharp = np.where(sharp > 255, 255, sharp) + sharp = np.where(sharp < 0, 0, sharp) + gradient = gradient.astype('uint8') + sharp = sharp.astype('uint8') + sharp_name = getImageName() + DEFAULT_FORMAT + gradient_name = getImageName() + DEFAULT_FORMAT + cv2.imwrite(PREFIX + sharp_name, sharp) + cv2.imwrite(PREFIX + gradient_name, gradient) + ret = [{"sharp": sharp_name, "gradient": gradient_name}] + return HttpResponse(ret) + filename = getImageName() + DEFAULT_FORMAT + cv2.imwrite(PREFIX + filename, img) + return HttpResponse(filename) + return HttpResponse('请使用POST方法') def r(request): return render(request, 'upload.html')