-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
123 lines (108 loc) · 3.94 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import requests
import string
from flask import Flask, render_template, request, redirect, url_for, flash
import numpy as np
import cv2
import os
from PIL import Image
from skimage.exposure import rescale_intensity
app = Flask(__name__)
app.config['DEBUG'] = True
# prevent cached responses
if app.config["DEBUG"]:
@app.after_request
def after_request(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate, public, max-age=0"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
@app.route('/')
def index_get():
# Remove the new image on refresh on main page
if os.path.exists('static/newImage.jpg'):
os.remove('static/newImage.jpg')
return render_template('main.html')
@app.route('/newImage/<name>')
def add_newImg(name):
return render_template('output.html', type=name)
@app.route('/convolutions', methods=["POST"])
def apply_convolution():
# Remove the new image from directory on submit
if os.path.exists('static/newImage.jpg'):
os.remove('static/newImage.jpg')
# Read original image in greyscale
originalImage = cv2.imread('static/iu.jpg',cv2.IMREAD_GRAYSCALE)
# getting the name from the POST request
name = request.form['convolution_type']
kernel = pick_convolution_type(name)
newImage = convolve(originalImage, kernel)
cv2.imwrite('static/newImage.jpg'.format(kernel),newImage)
return redirect(url_for('add_newImg', name=(name)))
# KERNELS
def Identity():
identity = np.array([[0,0,0],[0,1,0],[0,0,0]],dtype='int')
return identity
def Sharpen():
sharpen = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]],dtype='int')
return sharpen
def EdgeDetection():
edgedetect = np.array([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],dtype='int')
return edgedetect
def BoxBlur():
boxblur = np.array([[1,1,1],[1,1,1],[1,1,1]],dtype='int') / 9
return boxblur
def GaussianBlur():
gblur = np.array([[1,2,1],[2,4,2],[1,2,1]],dtype='int') / 16
return gblur
def LeftSobel():
leftsobel = np.array([[1,0,-1],[2,0,-2],[1,0,-1]],dtype='int')
return leftsobel
def RightSobel():
rightsobel = np.array([[-1,0,1],[-2,0,2],[-1,0,1]],dtype='int')
return rightsobel
def TopSobel():
topsobel = np.array([[1,2,1],[0,0,0],[-1,-2,-1]],dtype='int')
return topsobel
def BottomSobel():
bottomsobel = np.array([[-1,-2,-1],[0,0,0],[1,2,1]],dtype='int')
return bottomsobel
kernelDict = {
"Identity": Identity,
"Sharpen": Sharpen,
"EdgeDetection": EdgeDetection,
"BoxBlur": BoxBlur,
"GaussianBlur": GaussianBlur,
"LeftSobel": LeftSobel,
"RightSobel": RightSobel,
"TopSobel": TopSobel,
"BottomSobel": BottomSobel,
}
# Function to access kernel dictionary for corresponding kernel
def pick_convolution_type(name):
return kernelDict[name]()
# Convolution function
def convolve(img,kernel):
# establish variables for the image height and length
(imgHeight,imgLength) = img.shape[:2]
# establish variables for the kernel height and length
(kHeight,kLength) = kernel.shape[:2]
# A padded border is needed to deal with edge cases so
# we first calculate the thickness of the pad
pad = (kLength-1)//2
# Make a border with thickness of the aforementioned pad
img = cv2.copyMakeBorder(img,pad,pad,pad,pad,cv2.BORDER_REPLICATE)
# establish numpy 2D array the size of the original image
final = np.zeros((imgHeight,imgLength),dtype='float32')
# apply convolutions from left to right, then up to down
for i in range(pad,imgHeight+pad):
for j in range(pad,imgLength+pad):
# define region of interest
region = img[i-pad:i+pad+1,j-pad:j+pad+1]
# sum up the new value of the center element
newVal = (region*kernel).sum()
final[i-pad,j-pad] = newVal
# normalize pixel value to lie in range 0-255
final = rescale_intensity(final,in_range=(0,255))
final = (final * 255).astype("uint8")
# return manipulated image
return final