diff --git a/basic/urls.py b/basic/urls.py index cd10eb5..db55d27 100644 --- a/basic/urls.py +++ b/basic/urls.py @@ -15,4 +15,5 @@ urlpatterns = [ path('flip', views.flip), path('affine', views.affine), path('histogram', views.histogram), + path('plt', views.piecewise_linear_transform), ] diff --git a/basic/views.py b/basic/views.py index c589a96..8c27eb2 100644 --- a/basic/views.py +++ b/basic/views.py @@ -427,17 +427,66 @@ def histogram(request): if request.method == 'POST': para = json.loads(request.body) image = para['img'] - img = cv2.imread(PREFIX + image) + color = para['color'] histogram_name = getImageName() + DEFAULT_FORMAT if color: - hist_cover_rgb(img, histogram_name) + img = cv2.imread(PREFIX + image) + hist_cover_rgb(img, PREFIX + histogram_name) else: - img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) - hist_cover_gray(img_gray, PREFIX + histogram_name) + img = cv2.imread(PREFIX + image, 0) + hist_cover_gray(img, PREFIX + histogram_name) return HttpResponse(histogram_name) return HttpResponse('请使用POST方法') +def grayHist(img, filename): + plt.figure(filename, figsize=(16, 8)) + h, w = img.shape[:2] + pixelSequence = img.reshape(h * w, 1) + numberBins = 256 + histogram, bins, patch = plt.hist(pixelSequence, numberBins) + plt.xlabel("gray label") + plt.ylabel("number of pixels") + plt.axis([0, 255, 0, np.max(histogram)]) + # 打印输出峰值 + plt.savefig(filename) + + +@csrf_exempt +def piecewise_linear_transform(request): + if request.method == 'POST': + para = json.loads(request.body) + image = para['img'] + img = cv2.imread(PREFIX + image, 0) + funcs = para['func'] + ret_name1 = getImageName() + DEFAULT_FORMAT + ret_name2 = getImageName() + DEFAULT_FORMAT + out_name = getImageName() + DEFAULT_FORMAT + h, w = img.shape[:2] + out = np.zeros(img.shape, np.uint8) + for i in range(h): + for j in range(w): + pix = img[i][j] + last_limit = float("-inf") + for func in funcs: + limit = func['limit'] + k = func['k'] + b = func['b'] + if limit == -1: + if pix > last_limit: + out[i][j] = k * pix + b + else: + if last_limit <= pix < limit: + out[i][j] = k * pix + b + last_limit = limit + out = np.around(out).astype(np.uint8) + cv2.imwrite(PREFIX + out_name, out) + grayHist(img, PREFIX + ret_name1) + grayHist(out, PREFIX + ret_name2) + return HttpResponse([{"orig": image, "hist": ret_name1}, {"orig": out_name, "hist": ret_name2}]) + return HttpResponse('请使用POST方法') + + def r(request): return render(request, 'upload.html')