从网上找了很多关于图片处理的资料,整合了下,就有了这个简单的课程设计。
代码直接附上
# @Author:linjiaxiaozhu
import cv2
from matplotlib import pyplot as plt
import numpy as np
from math import *
path = input("请输入你要上传的图片:")
while True:
print('操作提示【1:均值模糊;2:描边;3:旋转90度;4:双边滤波;5:裁剪;6:提亮】')
num = int(input("请输入你想要的操作(输入数字):"))
str = ['均值模糊', '描边', '旋转90度', '双边滤波', '裁剪','提亮']
while num not in range(1, 7):
num = int(input("不存在该操作,请重新输入想要的操作:"))
print('你要对图片进行的操作是:' + str[num - 1])
cv_img = cv2.imdecode(np.fromfile(path,dtype=np.uint8),-1) #解决中文路径的问题
img = cv2.cvtColor(cv_img, cv2.COLOR_RGB2BGR) #原图
y, x = img.shape[0:2] #求出上传图片的长x宽y
if num==1:
img1=cv2.blur(img, (1, 15)) #(1, 15)是垂直方向模糊
titles = ["yuantu", "mohu"]
elif num==2:
img1= cv2.copyMakeBorder(img,30,30,30,30,cv2.BORDER_CONSTANT,value=[252,157,154]) #40是描的4条边的粗细,value是颜色值
titles = ["yuantu", "miaobian"]
elif num == 3:
heightNew = int(x * fabs(sin(radians(90))) + y * fabs(cos(radians(90))))
widthNew = int(y * fabs(sin(radians(90))) + x * fabs(cos(radians(90))))
matRotation = cv2.getRotationMatrix2D((x / 2, y / 2), 90, 1)#(x/2,y/2)是旋转点的坐标,旋转90度
matRotation[0, 2] += (widthNew - x) / 2 # 重点在这步
matRotation[1, 2] += (heightNew - y) / 2 # 重点在这步
img1 = cv2.warpAffine(img, matRotation, (widthNew, heightNew), borderValue=(255, 255, 255))
titles = ["yuantu", "xuanzhuan"]
elif num == 4:
img1 = cv2.bilateralFilter(img, 0, 100, 15) # 双边滤波
titles = ["yuantu", "lvbo"]
elif num == 5:
img1= img[50:int(x),50:int(y)] # 裁剪坐标为[x0:x1, y0:y1]
titles = ["yuantu", "caijian"]
elif num == 6:
img1 = cv2.addWeighted(img, 0.7, img, 0.4, 3)# 括号内参数分别为:图1,图1的权重,图2,图2的权重,权重和添加的值为3
titles = ["yuantu", "tiliang"]
images = [img, img1]
for i in range(2):
plt.subplot(1, 2, i + 1), plt.imshow(images[i]), plt.title(titles[i])
plt.xticks([]), plt.yticks([])
plt.show()
记得先安装相应的包哦
(因为是用了封装好的函数,具体的参数代表什么意思还不懂,等研究透了再来一篇教程吧)