You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

471 lines
16 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import json
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from pywt import dwt2, idwt2
from .helper import *
PREFIX = 'media/'
DEFAULT_FORMAT = '.jpg'
@csrf_exempt
def upload(request):
if request.method == 'POST':
imageName = get_image(request)
if imageName == '':
return HttpResponse('Not supported image format')
return HttpResponse(imageName)
return HttpResponse('Please use POST')
@csrf_exempt
def basic_drawing(request):
if request.method == 'POST':
para = json.loads(request.body)
draw_type = para['type']
if draw_type == 'bg':
return HttpResponse(process_bg())
else:
if draw_type == 'line':
return HttpResponse(process_line(para))
elif draw_type == 'rectangle':
return HttpResponse(process_rectangle(para))
elif draw_type == 'circle':
return HttpResponse(process_circle(para))
elif draw_type == 'ellipse':
return HttpResponse(process_ellipse(para))
elif draw_type == 'polylines':
return HttpResponse(process_polylines(para))
elif draw_type == 'text':
return HttpResponse(process_text(para))
return HttpResponse('Please use POST')
@csrf_exempt
def rgb_color_space(request, color):
if request.method == 'POST':
para = json.loads(request.body)
image_name = para['img']
img = cv2.imread(PREFIX + image_name)
data = None
if color == 'r':
data = img[:, :, 2]
elif color == 'g':
data = img[:, :, 1]
elif color == 'b':
data = img[:, :, 0]
if para.get('filter', False):
data = process_filter_rgb(para, data)
filename = color + '-' + image_name
cv2.imwrite(PREFIX + filename, data)
return HttpResponse(filename)
return HttpResponse('Please use POST')
@csrf_exempt
def hsv_color_space(request, color):
if request.method == 'POST':
para = json.loads(request.body)
image_name = para['img']
img = cv2.imread(PREFIX + image_name)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
data = None
if color == 'h':
data = hsv[:, :, 0]
elif color == 's':
data = hsv[:, :, 1]
elif color == 'v':
data = hsv[:, :, 2]
elif color == 'filter':
upper_bound = para.get('ub', [0, 0, 0])
lower_bound = para.get('lb', [200, 40, 100])
data = cv2.inRange(hsv, np.array(upper_bound), np.array(lower_bound))
filename = color + '-' + image_name
cv2.imwrite(PREFIX + filename, data)
return HttpResponse(filename)
return HttpResponse('Please use POST')
@csrf_exempt
def basic_operation(request):
if request.method == 'POST':
para = json.loads(request.body)
operator = para['operator']
img = para['img']
has_color = para['color']
base = para['base']
if operator == 'and':
return HttpResponse(logic_and(img, has_color, base))
elif operator == 'or':
return HttpResponse(logic_or(img, has_color, base))
elif operator == 'not':
return HttpResponse(logic_not(img, has_color, base))
elif operator == 'add':
return HttpResponse(arithmetic_add(img, has_color, base))
elif operator == 'sub':
return HttpResponse(arithmetic_sub(img, has_color, base))
elif operator == 'multi':
return HttpResponse(arithmetic_multi(img, has_color, base))
elif operator == 'div':
return HttpResponse(arithmetic_div(img, has_color, base))
return HttpResponse('Please use POST')
@csrf_exempt
def resize(request):
if request.method == 'POST':
para = json.loads(request.body)
image = para['img']
img = cv2.imread(PREFIX + image)
size = para.get('size', None)
if size is not None:
height, width = img.shape[:2]
if width * height >= size[0] * size[1]:
interpolation = cv2.INTER_AREA
else:
interpolation = cv2.INTER_CUBIC
img = cv2.resize(img, tuple(size), interpolation)
else:
multiple = para['multiple']
if multiple < 1:
interpolation = cv2.INTER_AREA
else:
interpolation = cv2.INTER_CUBIC
img = cv2.resize(img, (0, 0), fx=multiple, fy=multiple, interpolation=interpolation)
return HttpResponse(save_image(img))
return HttpResponse('Please use POST')
@csrf_exempt
def rotate(request):
if request.method == 'POST':
para = json.loads(request.body)
image = para['img']
img = cv2.imread(PREFIX + image)
angle = para['angle']
img = rotate_bound(img, angle)
return HttpResponse(save_image(img))
return HttpResponse('Please use POST')
@csrf_exempt
def translation(request):
if request.method == 'POST':
para = json.loads(request.body)
image = para['img']
img = cv2.imread(PREFIX + image)
offset = para['offset']
height, width = img.shape[:2]
M = np.float32([[1, 0, offset[0]], [0, 1, offset[1]]])
img = cv2.warpAffine(img, M, (width, height))
return HttpResponse(save_image(img))
return HttpResponse('Please use POST')
@csrf_exempt
def flip(request):
if request.method == 'POST':
para = json.loads(request.body)
image = para['img']
img = cv2.imread(PREFIX + image)
flip_code = para.get('flip', 1)
img = cv2.flip(img, flip_code)
return HttpResponse(save_image(img))
return HttpResponse('Please use POST')
@csrf_exempt
def affine(request):
if request.method == 'POST':
para = json.loads(request.body)
image = para['img']
img = cv2.imread(PREFIX + image)
before = np.float32(para['before'])
after = np.float32(para['after'])
rows, cols = img.shape[:2]
M = cv2.getAffineTransform(before, after)
img = cv2.warpAffine(img, M, (rows, cols))
return HttpResponse(save_image(img))
return HttpResponse('Please use POST')
@csrf_exempt
def histogram(request):
if request.method == 'POST':
para = json.loads(request.body)
image = para['img']
color = para['color']
histogram_name = get_image_name() + DEFAULT_FORMAT
if color:
img = cv2.imread(PREFIX + image)
hist_cover_rgb(img, PREFIX + histogram_name)
else:
img = cv2.imread(PREFIX + image, 0)
hist_cover_gray(img, PREFIX + histogram_name)
return HttpResponse(histogram_name)
return HttpResponse('Please use POST')
@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 = get_image_name() + DEFAULT_FORMAT
ret_name2 = get_image_name() + DEFAULT_FORMAT
out_name = get_image_name() + 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('Please use 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':
img = process_roberts(img)
elif operator == 'Sobel':
img = process_sobel(img)
elif operator == 'Laplacian':
img_gaussianBlur = cv2.GaussianBlur(img, (5, 5), 0)
img = process_laplacian(img_gaussianBlur)
elif operator == 'LoG':
img = process_LoG(img)
elif operator == 'Canny':
img = process_Canny(img)
# 这里由于返回格式的不同会在process_enhance之中直接返回不在往后执行
elif operator == 'Enhance':
process_enhance(img)
return HttpResponse(save_image(img))
return HttpResponse('Please use POST')
@csrf_exempt
def line_change_detection(request):
if request.method == 'POST':
para = json.loads(request.body)
image = para['img']
img = cv2.imread(PREFIX + image)
use_p = para['use_p']
img = cv2.GaussianBlur(img, (3, 3), 0)
edges = cv2.Canny(img, 50, 150, apertureSize=3)
if use_p:
img = process_lcd_with_p(edges, img)
else:
img = process_lcd_without_p(edges, img)
return HttpResponse(save_image(img))
return HttpResponse('Please use POST')
@csrf_exempt
def smooth(request):
if request.method == 'POST':
para = json.loads(request.body)
image = para['img']
img = cv2.imread(PREFIX + image, 0)
# frequency domain
fd = para['fd']
my_filter = para['filter']
if fd:
if my_filter == 'ideal_low_pass_filter':
d0 = para.get('d0', 20)
img = filter_use_smooth(img, ideal_low_filter(img, d0))
elif my_filter == 'butterworth_low_pass_filter':
d0 = para.get('d0', 20)
rank = para.get('rank', 2)
img = filter_use_smooth(img, butterworth_low_pass_filter(img, d0, rank))
elif my_filter == 'gauss_low_pass_filter':
d0 = para.get('d0', 20)
img = filter_use_smooth(img, gauss_low_pass_filter(img, d0))
else:
size = para['size']
if my_filter == 'average':
img = cv2.blur(img, (size, size))
elif my_filter == 'median':
img = cv2.medianBlur(img, size)
return HttpResponse(save_image(img))
return HttpResponse('Please use POST')
@csrf_exempt
def sharpen(request):
if request.method == 'POST':
para = json.loads(request.body)
image = para['img']
img = cv2.imread(PREFIX + image, 0)
# frequency domain
fd = para['fd']
my_filter = para['filter']
if fd:
if my_filter == 'ideal_high_pass_filter':
d0 = para.get('d0', 40)
img = filter_use_sharpen(img, ideal_high_pass_filter(img, d0))
elif my_filter == 'butterworth_high_pass_filter':
d0 = para.get('d0', 20)
rank = para.get('rank', 2)
img = filter_use_sharpen(img, butterworth_high_pass_filter(img, d0, rank))
elif my_filter == 'gauss_high_pass_filter':
d0 = para.get('d0', 20)
img = filter_use_sharpen(img, gauss_high_pass_filter(img, d0))
else:
if my_filter == 'Roberts':
img = process_roberts(img)
elif my_filter == 'Sobel':
img = process_sobel(img)
elif my_filter == 'Prewitt':
img = process_prewitt(img)
elif my_filter == 'Laplacian':
img = process_laplacian(img)
return HttpResponse(save_image(img))
return HttpResponse('Please use POST')
@csrf_exempt
def morphology(request):
if request.method == 'POST':
para = json.loads(request.body)
image = para['img']
size = para.get('size', 5)
img = cv2.imread(PREFIX + image, cv2.IMREAD_UNCHANGED)
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (size, size))
op = para['type']
if op == 'open':
img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
elif op == 'close':
img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
elif op == 'erode':
img = cv2.erode(img, kernel)
elif op == 'dilate':
img = cv2.dilate(img, kernel)
return HttpResponse(save_image(img))
return HttpResponse('Please use POST')
@csrf_exempt
def noise(request):
if request.method == 'POST':
para = json.loads(request.body)
image = para['img']
img = cv2.imread(PREFIX + image)
op = para['type']
if op == 'gauss':
img = process_gauss_noise(img)
elif op == 'salt_and_pepper':
img = process_salt_and_peper_noise(img, para)
return HttpResponse(save_image(img))
return HttpResponse('Please use POST')
@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']
detail_type = para['detail_type']
if op == 'mean':
if detail_type == 'arithmetic':
img = process_arithmetic_mean(img)
elif detail_type == 'geometric':
img = process_geometric_mean(img)
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)
elif op == 'choice':
if detail_type == 'low':
limit = para.get('limit', 20)
img = process_low_choice(img, limit)
elif detail_type == 'high':
limit = para.get('limit', 200)
img = process_high_choice(img, limit)
elif detail_type == 'pass':
min_r = para.get('min', 20)
max_r = para.get('max', 200)
img = process_pass_choice(img, min_r, max_r)
elif detail_type == 'stop':
min_r = para.get('min', 20)
max_r = para.get('max', 200)
img = process_stop_choice(img, min_r, max_r)
return HttpResponse(save_image(img))
return HttpResponse('Please use POST')
@csrf_exempt
def wavelet(request):
if request.method == 'POST':
para = json.loads(request.body)
image = para['img']
img = cv2.imread(PREFIX + image, 0)
# 对img进行haar小波变换
cA, (cH, cV, cD) = dwt2(img, 'haar')
rimg = idwt2((cA, (cH, cV, cD)), 'haar')
ret = [
{"low": save_image(np.uint8(cA / np.max(cA) * 255)),
"horizontal_high": save_image(np.uint8(cH / np.max(cH) * 255)),
"vertical_high": save_image(np.uint8(cV / np.max(cV) * 255)),
"diagonal_high": save_image(np.uint8(cD / np.max(cD) * 255)),
"refactor": save_image(np.uint8(rimg))
}
]
return HttpResponse(ret)
return HttpResponse('Please use POST')
@csrf_exempt
def fourier(request):
if request.method == 'POST':
para = json.loads(request.body)
image = para['img']
img = cv2.imread(PREFIX + image, 0)
# 傅里叶
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20 * np.log(np.abs(fshift))
# 逆傅里叶
ishift = np.fft.ifftshift(f)
iimg = np.fft.ifft2(ishift)
iimg = np.abs(iimg)
ret = [{"fourier_transform": save_image(magnitude_spectrum), "inverse": save_image(iimg)}]
return HttpResponse(ret)
return HttpResponse('Please use POST')
def load(request):
return render(request, 'upload.html')