Initial commit

main
Tyrion 4 years ago
commit de19155021

8
.idea/.gitignore vendored

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="FacetManager">
<facet type="django" name="Django">
<configuration>
<option name="rootFolder" value="$MODULE_DIR$" />
<option name="settingsModule" value="djangoProject/settings.py" />
<option name="manageScript" value="$MODULE_DIR$/manage.py" />
<option name="environment" value="&lt;map/&gt;" />
<option name="doNotUseTestRunner" value="false" />
<option name="trackFilePattern" value="migrations" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="@types/jquery" level="application" />
<orderEntry type="library" name="jquery" level="application" />
</component>
<component name="TemplatesService">
<option name="TEMPLATE_CONFIGURATION" value="Django" />
<option name="TEMPLATE_FOLDERS">
<list>
<option value="$MODULE_DIR$/../djangoProject\templates" />
</list>
</option>
</component>
</module>

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptLibraryMappings">
<file url="file://$PROJECT_DIR$" libraries="{jquery}" />
<file url="PROJECT" libraries="{@types/jquery}" />
</component>
</project>

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (djangoProject)" project-jdk-type="Python SDK" />
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/djangoProject.iml" filepath="$PROJECT_DIR$/.idea/djangoProject.iml" />
</modules>
</component>
</project>

@ -0,0 +1,16 @@
"""
ASGI config for djangoProject project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/
"""
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangoProject.settings')
application = get_asgi_application()

@ -0,0 +1,129 @@
"""
Django settings for djangoProject project.
Generated by 'django-admin startproject' using Django 4.0.6.
For more information on this file, see
https://docs.djangoproject.com/en/4.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""
import os
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-ya9!!nl84l$a#)ez8np*9k4=y0vq8qv+dm)f+pub3x48^e7am&'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'djangoProject.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates']
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'djangoProject.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/
STATIC_URL = 'static/'
# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/media')

@ -0,0 +1,52 @@
"""djangoProject URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('', views.to_img_load),
path('histogram/', views.histogram),
path('greyHistogram/', views.greyHistogram),
path('colorHistogram/', views.colorHistogram),
path('piecewiseLinearProcessing/', views.piecewiseLinearProcessing),
path('enlarge/', views.enlarge),
path('move/', views.move),
path('spin/', views.spin),
path('horizontalFlip/', views.horizontalFlip),
path('verticalFlip/', views.verticalFlip),
path('crossFlip/', views.crossFlip),
path('affineTransformation/', views.affineTransformation),
path('enhance/', views.enhance),
path('robs/', views.robs),
path('sob/', views.sob),
path('lap/', views.lap),
path('log/', views.log),
path('cny/', views.cny),
path('MeanFilter/', views.MeanFilter),
path('MedFilter/', views.MedFilter),
path('HoughLineChange/', views.HoughLineChange),
path('erode/', views.erode),
path('dialate/', views.dialate),
path('sp_noise/', views.sp_noise),
path('gasuss_noise/', views.gasuss_noise),
path('highPassFilter/', views.highPassFilter),
path('IdealLowPassFiltering/', views.IdealLowPassFiltering),
path('butterworth_low_filter/', views.butterworth_low_filter),
path('IdealHighPassFiltering/', views.IdealHighPassFiltering),
path('butterworth_high_filter/', views.butterworth_high_filter),
path('sharpen/', views.sharpen)
]

@ -0,0 +1,938 @@
import base64
import random
from tkinter import Image
import numpy as np
import cv2
import sys
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
from matplotlib import pyplot as plt
from djangoProject import settings
# 主页面
def to_img_load(request):
return render(request, 'upload.html')
# 绘制直方图
def histogram(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
print(name)
path = settings.MEDIA_ROOT + '/' + image.name
with open(path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(path)
color = ('b', 'g', 'r')
for i, col in enumerate(color):
hist = cv2.calcHist([img], [i], None, [256], [0, 256])
plt.plot(hist, color=col)
plt.xlim([0, 256])
plt.savefig(path)
try:
data = {'state': 'static/media/'+name}
except:
data = {'state': 0}
return JsonResponse(data)
# 灰度直方图
def greyHistogram(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + image.name
print(img_path)
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img_PIL = Image.open(img_path)
img = np.array(img_PIL)
img_gray = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
hist = cv.calcHist([img_gray], [0], None, [256], [0, 255])
plt.plot(hist)
plt.xlim([0, 255])
plt.savefig(img_path)
try:
data = {'state': 'static/media/'+name}
except:
data = {'state': 0}
return JsonResponse(data)
# 彩色直方图
def colorHistogram(request):
if request.method == 'POST':
color = ["r", "g", "b"]
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + image.name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
b, g, r = cv2.split(img)
img = cv2.merge([r, g, b])
# 绘制彩色直方图,需要对每个通道进行遍历,并且找到最大值和最小值
for index, c in enumerate(color):
hist = cv2.calcHist([img], [index], None, [256], [0, 255])
plt.plot(hist, color=c)
plt.xlim([0, 255])
plt.savefig(img_path)
try:
data = {'state': 'static/media/'+name}
except:
data = {'state': 0}
return JsonResponse(data)
# 分段线性处理
def piecewiseLinearProcessing(request):
# 定义图像数据的路径
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path, 0)
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]
if pix < 50:
out[i][j] = 0.5 * pix
elif pix < 150:
out[i][j] = 3.6 * pix - 310
else:
out[i][j] = 0.238 * pix + 194
out = np.around(out)
out = out.astype(np.uint8)
cv2.imwrite(img_path, out)
try:
data = {'state': 'static/media/'+name}
except:
data = {'state': 0}
return JsonResponse(data)
# 放大图像至原来的两倍,使用双线性插值法????
def enlarge(request):
# 定义图像数据的路径
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
l, w, h = img.shape
img = cv2.resize(img, (0, 0), fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
cv2.imwrite(img_path, img)
try:
data = {'state': 'static/media/'+name}
except:
data = {'state': 0}
return JsonResponse(data)
# 图片平移 构建移动矩阵,x轴左移 10 个像素y轴下移 30 个
def move(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
height, width, channel = img.shape
M = np.float32([[1, 0, 30], [0, 1, 60]])
img = cv2.warpAffine(img, M, (width, height))
cv2.imwrite(img_path, img)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# 构建矩阵旋转中心坐标为处理后图片长宽的一半旋转角度为45度缩放因子为1
def spin(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
height, width, channel = img.shape
rows, cols, depth = img.shape
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 45, 1)
dst = cv2.warpAffine(img, M, (width, height))
cv2.imwrite(img_path, dst)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# 水平翻转
def horizontalFlip(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
horizontal = cv2.flip(img, 1, dst=None)
cv2.imwrite(img_path, horizontal)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# 垂直翻转
def verticalFlip(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
vertical = cv2.flip(img, 0, dst=None)
cv2.imwrite(img_path, vertical)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# 对角线翻转
def crossFlip(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
cross = cv2.flip(img, -1, dst=None)
cv2.imwrite(img_path, cross)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# 仿射变换
def affineTransformation(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
img = cv2.resize(img, (256, 256))
rows, cols = img.shape[: 2]
# 设置图像仿射变化矩阵
post1 = np.float32([[50, 50], [200, 50], [50, 200]])
post2 = np.float32([[10, 100], [200, 50], [100, 250]])
M = cv2.getAffineTransform(post1, post2)
# 图像仿射变换,及保存
result = cv2.warpAffine(img, M, (rows, cols))
cv2.imwrite(img_path, result)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# 图片增强
def enhance(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
# 1. 灰度模式读取图像图像名为CRH
CRH = cv2.imread(img_path, 0)
# 2. 计算图像梯度。首先要对读取的图像进行数据变换,因为使用了
# numpy对梯度进行数值计算所以要使用
# CRH.astype('float')进行数据格式变换。
row, col = CRH.shape
CRH_f = np.copy(CRH)
CRH_f = CRH_f.astype('float')
gradient = np.zeros((row, col))
for x in range(row - 1):
for y in range(col - 1):
gx = abs(CRH_f[x + 1, y] - CRH_f[x, y])
gy = abs(CRH_f[x, y + 1] - CRH_f[x, y])
gradient[x, y] = gx + gy
# 3. 对图像进行增强增强后的图像变量名为sharp
sharp = CRH_f + gradient
sharp = np.where(sharp > 255, 255, sharp)
sharp = np.where(sharp < 0, 0, sharp)
# 数据类型变换
gradient = gradient.astype('uint8')
sharp = sharp.astype('uint8')
cv2.imwrite(img_path, sharp)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# robs算子图片增强
def robs(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
# 1. 灰度化处理图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 2. Roberts算子
kernelx = np.array([[-1, 0], [0, 1]], dtype=int)
kernely = np.array([[0, -1], [1, 0]], dtype=int)
# 3. 卷积操作
x = cv2.filter2D(gray, cv2.CV_16S, kernelx)
y = cv2.filter2D(gray, cv2.CV_16S, kernely)
# 4. 数据格式转换
absx = cv2.convertScaleAbs(x)
absy = cv2.convertScaleAbs(y)
Roberts = cv2.addWeighted(absx, 0.5, absy, 0.5, 0)
cv2.imwrite(img_path, Roberts)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# 使用 Sobel 算子提取边缘
def sob(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
# 1. 灰度化处理图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 2. 求Sobel 算子
sx = cv2.Sobel(gray, cv2.CV_16S, 1, 0)
sy = cv2.Sobel(gray, cv2.CV_16S, 0, 1)
# 3. 数据格式转换
absx = cv2.convertScaleAbs(sx)
absy = cv2.convertScaleAbs(sy)
# 4. 组合图像
Sobel = cv2.addWeighted(absx, 0.5, absy, 0.5, 0)
cv2.imwrite(img_path, Sobel)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# 使用 Laplacian 算子提取边缘
def lap(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
# 1. 灰度化处理图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 2. 高斯滤波
grayG = cv2.GaussianBlur(gray, (5, 5), 0)
fltI = cv2.Laplacian(grayG, cv2.CV_16S, ksize=3)
# 3. 拉普拉斯算法
Laplacian = cv2.convertScaleAbs(fltI)
cv2.imwrite(img_path, Laplacian)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# 使用 LoG 算子提取边缘
def log(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
gray = cv2.copyMakeBorder(gray, 2, 2, 2, 2, borderType=cv2.BORDER_REPLICATE)
image = cv2.GaussianBlur(gray, (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]])
row = image.shape[0]
col = image.shape[1]
image1 = np.zeros(image.shape)
for i in range(2, row - 2):
for j in range(2, col - 2):
image1[i, j] = np.sum(m1 * image[i - 2:i + 3, j - 2:j + 3, 1])
image1 = cv2.convertScaleAbs(image1)
cv2.imwrite(img_path, image1)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# Canny 边缘检测过程
def cny(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
src = cv2.imread(img_path)
blur = cv2.GaussianBlur(src, (3, 3), 0)
gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
gradx = cv2.Sobel(gray, cv2.CV_16SC1, 1, 0)
grady = cv2.Sobel(gray, cv2.CV_16SC1, 0, 1)
edge_output = cv2.Canny(gradx, grady, 50, 150)
cv2.imwrite(img_path, edge_output)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# 均值滤波器
def MeanFilter(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
w = gray.shape[1]
h = gray.shape[0]
newing = np.array(gray)
lbing = np.zeros((h + 2, w + 2), np.float32)
tmping = np.zeros((h + 2, w + 2))
myh = h + 2
myw = w + 2
tmping[1:myh - 1, 1:myw - 1] = newing[0:h, 0:w]
a = 1 / 8.0
kernel = a * np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]])
for y in range(1, myh - 1):
for x in range(1, myw - 1):
lbing[y, x] = np.sum(kernel * tmping[y - 1:y + 2, x - 1:x + 2])
result = np.array(lbing[1:myh - 1, 1:myw - 1], np.uint8)
cv2.imwrite(img_path, result)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# 中值滤波器
def MedFilter(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
w = img.shape[1]
h = img.shape[0]
median = np.array(img)
for y in range(1, w):
for x in range(1, h):
median[x, y] = np.median(img[x - 1:x + 2, y - 1:y + 2])
cv2.imwrite(img_path, median)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# Hough线段变化
def HoughLineChange(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
img = cv2.GaussianBlur(img, (3, 3), 0)
edges = cv2.Canny(img, 50, 150, apertureSize=3)
lines = cv2.HoughLines(edges, 1, np.pi/2, 118)
result = img.copy()
for i_line in lines:
for line in i_line:
rho = line[0]
theta = line[1]
if (theta < (np.pi / 4.)) or (theta > (3. * np.pi / 4.0)): # 垂直直线
pt1 = (int(rho / np.cos(theta)), 0)
pt2 = (int((rho - result.shape[0] * np.sin(theta)) / np.cos(theta)), result.shape[0])
cv2.line(result, pt1, pt2, (0, 0, 255))
else:
pt1 = (0, int(rho / np.sin(theta)))
pt2 = (result.shape[1], int((rho - result.shape[1] * np.cos(theta)) / np.sin(theta)))
cv2.line(result, pt1, pt2, (0, 0, 255), 1)
minLineLength = 200
maxLineGap = 15
linesP = cv2.HoughLinesP(edges, 1, np.pi/180, 80, minLineLength, maxLineGap)
result_P = img.copy()
for i_P in linesP:
for x1, y1, x2, y2 in i_P:
cv2.line(result_P, (x1, y1), (x2, y2), (0, 255, 0), 3)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite(img_path, result_P)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# 腐蚀
def erode(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
src = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 使用一个5x5的交叉型结构元核心在几何中心对二值图片src进行腐蚀
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5))
erosion = cv2.erode(src, kernel)
cv2.imwrite(img_path, erosion)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# 膨胀
def dialate(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
src = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 使用一个5x5的交叉型结构元核心在几何中心对二值图片src进行膨胀
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5))
dilation = cv2.dilate(src, kernel)
cv2.imwrite(img_path, dilation)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# 开操作######
def openMorphing(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
retval, dst = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 定义十字形结构元素
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(10,10))
im_op = cv2.morphologyEx(dst, cv2.MORPH_OPEN,kernel)
cv2.imwrite(img_path, im_op)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# 闭操作
def closeMorphing(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
retval, dst = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 定义十字形结构元素
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (10, 10))
im_cl = cv2.morphologyEx(dst, cv2.MORPH_CLOSE, kernel)
cv2.imwrite(img_path, im_cl)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# 雪花噪声
def sp_noise(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
prob = 0.2
# 待输出的图片
output = np.zeros(img.shape, np.uint8)
# 遍历图像,获取叠加噪声后的图像
thres = 1 - prob
for i in range(img.shape[0]):
for j in range(img.shape[1]):
rdn = random.random()
if rdn < prob:
output[i][j] = 0
elif rdn > thres:
output[i][j] = 255
else:
output[i][j] = img[i][j]
cv2.imwrite(img_path, output)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# 添加高斯噪声,mean : 均值 , var : 方差
def gasuss_noise(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
image = np.array(img / 255, dtype=float)
var = 0.001
mean = 0
noise = np.random.normal(mean, var ** 0.5, image.shape)
out = image + noise
if out.min() < 0:
low_clip = -1.
else:
low_clip = 0.
out = np.clip(out, low_clip, 1.0)
out = np.uint8(out * 255)
cv2.imwrite(img_path, out)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# 高通滤波
def highPassFilter(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
output = np.zeros(image.shape, np.uint8)
for i in range(image.shape[0]):
for j in range(image.shape[1]):
if 200 < image[i][j]:
output[i][j] = image[i][j]
else:
output[i][j] = 0
cv2.imwrite(img_path, output)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
##########
##########
# 空域锐化
##########
#########
# Roberts因子锐化
def Roberts(image):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
img_Roberts = np.zeros(img.shape, dtype=int)
for i in range(1, img.shape[0] - 1): # 第一列和最后一列不用处理
for j in range(1, img.shape[1] - 1):
img_Roberts[i][j] = abs((int)(img[i + 1][j + 1]) - (int)(img[i][j])) + abs(
(int)(img[i + 1][j]) - (int)(img[i][j + 1]))
result = np.array(img_Roberts)
plt.subplot(244), plt.title("4.Roberts锐化后"), plt.axis('off')
plt.imshow(img_Roberts, cmap="gray")
plt.show()
def sharpen(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32) # 锐化
dst = cv2.filter2D(img, -1, kernel=kernel)
cv2.imwrite(img_path, dst)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
##########
##########
# 频域锐化
##########
#########
# 理想低通滤波
def IdealLowPassFiltering(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
f_shift = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 设置滤波半径
D0 = 80
# 初始化
m = f_shift.shape[0]
n = f_shift.shape[1]
h1 = np.zeros((m, n))
x0 = np.floor(m / 2)
y0 = np.floor(n / 2)
for i in range(m):
for j in range(n):
D = np.sqrt((i - x0) ** 2 + (j - y0) ** 2)
if D <= D0:
h1[i][j] = 1
result = np.multiply(f_shift, h1)
cv2.imwrite(img_path, result)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# Butterworth低通滤波器
def butterworth_low_filter(request):
"""
生成一个Butterworth低通滤波器并返回
"""
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
D0 = 20
rank = 2
h, w = img.shape[:2]
filter_img = np.zeros((h, w))
u = np.fix(h / 2)
v = np.fix(w / 2)
for i in range(h):
for j in range(w):
d = np.sqrt((i - u) ** 2 + (j - v) ** 2)
filter_img[i, j] = 1 / (1 + 0.414 * (d / D0) ** (2 * rank))
cv2.imwrite(img_path, filter_img)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
##########
##########
# 频域平滑
##########
#########
# 理想高通滤波
def IdealHighPassFiltering(request):
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
f_shift = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 设置滤波半径
D0 = 80
# 初始化
m = f_shift.shape[0]
n = f_shift.shape[1]
h1 = np.zeros((m, n))
x0 = np.floor(m / 2)
y0 = np.floor(n / 2)
for i in range(m):
for j in range(n):
D = np.sqrt((i - x0) ** 2 + (j - y0) ** 2)
if D >= D0:
h1[i][j] = 1
result = np.multiply(f_shift, h1)
cv2.imwrite(img_path, result)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)
# Butterworth高通滤波器
def butterworth_high_filter(request):
"""
生成一个Butterworth高通滤波器并返回
"""
if request.method == 'POST':
image = request.FILES.get('picture')
name = image.name
img_path = settings.MEDIA_ROOT + '/' + name
with open(img_path, 'wb') as pic:
for c in image.chunks():
pic.write(c)
img = cv2.imread(img_path)
D0 = 40
rank = 2
h, w = img.shape[:2]
filter_img = np.zeros((h, w))
u = np.fix(h / 2)
v = np.fix(w / 2)
for i in range(h):
for j in range(w):
d = np.sqrt((i - u) ** 2 + (j - v) ** 2)
filter_img[i, j] = 1 / (1 + (D0 / d) ** (2 * rank))
cv2.imwrite(img_path, filter_img)
try:
data = {'state': 'static/media/' + name}
except:
data = {'state': 0}
return JsonResponse(data)

@ -0,0 +1,18 @@
"""
WSGI config for djangoProject project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangoProject.settings')
application = get_wsgi_application()

@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangoProject.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 278 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 272 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 399 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 257 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 268 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 339 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 167 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 157 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 337 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 459 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 614 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 95 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 711 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 583 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

@ -0,0 +1,97 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form enctype="multipart/form-data">
{% csrf_token %}
<img id="image" width="700" height="500" />
<br/>
<input type="file" onchange="selectImage(this);" name="picture" id="picture"/>
<br/>
<input id="submit-btn" type="button" value="提交">
</form>
<div>
<label>处理方式</label>
<select id="select">
<option id="histogram" value="histogram">直方图</option>
<option id="greyHistogram" value="greyHistogram">灰度直方图</option>
<option id="colorHistogram" value="colorHistogram">彩色直方图</option>
<option id="piecewiseLinearProcessing" value="piecewiseLinearProcessing">分段线性处理</option>
<option id="enlarge" value="enlarge">放大</option>
<option id="move" value="move">平移</option>
<option id="spin" value="spin">旋转</option>
<option id="horizontalFlip" value="horizontalFlip">水平镜像</option>
<option id="verticalFlip" value="verticalFlip">垂直镜像</option>
<option id="crossFlip" value="crossFlip">对角线镜像</option>
<option id="affineTransformation" value="affineTransformation">仿射变换</option>
<option id="enhance" value="enhance">图片增强</option>
<option id="robs" value="robs">使用 Roberts 算法提取图像边缘</option>
<option id="sob" value="sob">使用 Sobel 算子提取边缘。</option>
<option id="lap" value="lap">使用 Laplacian 算子提取边缘</option>
<option id="log" value="log">使用 LoG 算子提取边缘</option>
<option id="cny" value="cny">使用Canny 算子提取边缘</option>
<option id="MeanFilter" value="MeanFilter">均值滤波器</option>
<option id="MedFilter" value="MedFilter">中值滤波器</option>
<option id="HoughLineChange" value="HoughLineChange">Hough线段变化</option>
<option id="erode" value="erode">腐蚀</option>
<option id="dialate" value="dialate">膨胀</option>
<option id="sp_noise" value="sp_noise">雪花噪声</option>
<option id="gasuss_noise" value="gasuss_noise">高斯噪声</option>
<option id="highPassFilter" value="highPassFilter">高通滤波</option>
<option id="IdealLowPassFiltering" value="IdealLowPassFiltering">理想低通滤波</option>
<option id="butterworth_low_filter" value="butterworth_low_filter">Butterworth低通滤波器</option>
<option id="IdealHighPassFiltering" value="IdealHighPassFiltering">理想高通滤波</option>
<option id="butterworth_high_filter" value="butterworth_high_filter">Butterworth高通滤波器</option>
<option id="sharpen" value="sharpen">锐化</option>
</select>
</div>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script>
$('#submit-btn').on('click',function () {
var formData = new FormData();
var select = document.getElementById("select")
var url='/'+select.options[select.selectedIndex].value+'/'
formData.append("picture",$("#picture")[0].files[0]);
formData.append("csrfmiddlewaretoken",$("[name='csrfmiddlewaretoken']").val());
$.ajax({
processData:false,
contentType:false,
url:url,
type:'post',
data:formData,
success:function (arg) {
if (arg.state === 0){
alert('提交失败')
}
else {
alert('提交成功')
var picture=document.getElementById("image")
picture.src=arg.state
}
}
})
});
</script>
<script>
let image = '';
function selectImage(file) {
if (!file.files || !file.files[0]) {
return;
}
var reader = new FileReader();
reader.onload = function (evt) {
document.getElementById('image').src = evt.target.result;
image = evt.target.result;
}
reader.readAsDataURL(file.files[0]);
}
</script>
</body>
</html>
Loading…
Cancel
Save