|
|
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.get('type', 'bg')
|
|
|
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', [200, 40, 100])
|
|
|
lower_bound = para.get('lb', [0, 0, 0])
|
|
|
data = cv2.inRange(hsv, np.array(lower_bound), np.array(upper_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.get('operator', 'and')
|
|
|
img = para['img']
|
|
|
has_color = para.get('color', 0)
|
|
|
base = para.get('base', 1)
|
|
|
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.get('multiple', 2)
|
|
|
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.get('angle', 45)
|
|
|
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.get('offset', [30, 60])
|
|
|
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.get('before', [[50, 50], [200, 50], [50, 200]]))
|
|
|
after = np.float32(para.get('after', [[10, 100], [200, 50], [100, 250]]))
|
|
|
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.get('color', False)
|
|
|
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.get('func', [
|
|
|
{
|
|
|
"limit": 50,
|
|
|
"k": 0.5,
|
|
|
"b": 0
|
|
|
},
|
|
|
{
|
|
|
"limit": 150,
|
|
|
"k": 3.6,
|
|
|
"b": -310
|
|
|
|
|
|
},
|
|
|
{
|
|
|
"limit": -1,
|
|
|
"k": 0.238,
|
|
|
"b": 194
|
|
|
}
|
|
|
])
|
|
|
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.get('operator', 'Roberts')
|
|
|
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':
|
|
|
return 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.get('use_p', True)
|
|
|
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.get('fd', True)
|
|
|
my_filter = para.get('filter', 'ideal_low_pass_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.get('size', 3)
|
|
|
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.get('fd', True)
|
|
|
my_filter = para.get('filter', ideal_high_pass_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, 0)
|
|
|
retval, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
|
|
|
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (size, size))
|
|
|
op = para.get('type', 'open')
|
|
|
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.get('type', 'gauss')
|
|
|
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.get('type', 'mean')
|
|
|
detail_type = para.get('detail_type', 'arithmetic')
|
|
|
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':
|
|
|
print(request.body)
|
|
|
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, '小波变换.html')
|