import math
from matplotlib import pyplot as plt
import cv2
import os
import numpy as np
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QMainWindow, QFileDialog, QMessageBox
from main import Ui_MainWindow
from detect import detect_thread
class imagewindow(Ui_MainWindow,QMainWindow):
def __init__(self):
super(imagewindow,self).__init__()
self.setupUi(self)
self.setWindowTitle("Opencv图像处理")
self.label.setScaledContents(True)
#用户缩放
self.x=0.9
self.y=0.9
#旋转起始角度
self.angle=90
#鼠标事件
self.en = False
self.progressBar.setValue(0)
self.pushButton.clicked.connect(self.openImage)
self.pushButton_2.clicked.connect(self.imageTogray) #灰度
self.pushButton_3.clicked.connect(self.Sketch) #素描
self.pushButton_4.clicked.connect(self.Nostalgia) #怀旧
self.pushButton_5.clicked.connect(self.paint) #油漆
self.pushButton_6.clicked.connect(self.Ground_glass) #毛玻璃
self.pushButton_7.clicked.connect(self.relief) #浮雕
self.pushButton_8.clicked.connect(self.threshold) #阈值化
self.pushButton_9.clicked.connect(self.zoom) #缩放
self.pushButton_10.clicked.connect(self.rotate) #旋转
self.pushButton_11.clicked.connect(self.GaussianFiltering) #高斯滤波
self.pushButton_12.clicked.connect(self.meanFiltering) #均值滤波
self.pushButton_13.clicked.connect(self.boxFiltering) #方框滤波
self.pushButton_14.clicked.connect(self.medianFiltering) #中值滤波
self.pushButton_15.clicked.connect(self.Lowpassfiltering) #低通滤波
self.pushButton_16.clicked.connect(self.Flooding) #泛洪
self.pushButton_17.clicked.connect(self.erode) #腐蚀
self.pushButton_18.clicked.connect(self.Canny) #Canny边缘算法
self.pushButton_19.clicked.connect(self.dilate) #膨胀
self.pushButton_20.clicked.connect(self.save_image) #保存处理后的图像
self.pushButton_21.clicked.connect(self.Filter)#滤镜
self.pushButton_22.clicked.connect(self.light)#光照
self.pushButton_23.clicked.connect(self.fleetingTime)#流连
self.pushButton_24.clicked.connect(self.gradient)#梯度运算
self.pushButton_25.clicked.connect(self.close)#闭运算
self.pushButton_26.clicked.connect(self.open)#开运算
self.pushButton_27.clicked.connect(self.Mosaic)#马赛克
self.pushButton_28.clicked.connect(self.quantification)#量化
self.pushButton_29.clicked.connect(self.topHat)#顶帽运算
self.pushButton_30.clicked.connect(self.hoffcircle)#霍夫圆检测
self.pushButton_31.clicked.connect(self.hightpassfiltering)#高通滤波
self.pushButton_32.clicked.connect(self.hoffline)#霍夫直线检测
self.pushButton_33.clicked.connect(self.blackHot)#黑帽运算
self.pushButton_34.clicked.connect(self.FourierTransform)#傅里叶变换
self.pushButton_35.clicked.connect(self.inFourierTransform)#傅里叶反变换
def faceRecognition(self):
try:
self.label_2.setScaledContents(True)
# 读取图片
img = cv2.imread(self.path)
img = cv2.resize(img, (240, 240), interpolation=cv2.INTER_CUBIC)
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gradX = cv2.Sobel(gray, ddepth=cv2.CV_32F, dx=1, dy=0, ksize=-1)
gradY = cv2.Sobel(gray, ddepth=cv2.CV_32F, dx=0, dy=1, ksize=-1)
# subtract the y-gradient from the x-gradient
gradient = cv2.subtract(gradX, gradY)
gradient = cv2.convertScaleAbs(gradient)
blurred = cv2.blur(gradient, (9, 9))
(_, thresh) = cv2.threshold(blurred, 90, 255, cv2.THRESH_BINARY)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25, 25))
closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
# perform a series of erosions and dilations
closed = cv2.erode(closed, None, iterations=4)
closed = cv2.dilate(closed, None, iterations=4)
(cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
c = sorted(cnts, key=cv2.contourArea, reverse=True)[0]
# compute the rotated bounding box of the largest contour
rect = cv2.minAreaRect(c)
box = np.int0(cv2.boxPoints(rect))
# draw a bounding box arounded the detected barcode and display the image
cv2.drawContours(img, [box], -1, (0, 255, 0), 3)
h, w = img.shape[:2] # 获取图像的长和宽
qimg = QImage(img, w, h, QImage.Format_BGR888)
qpix = QPixmap.fromImage(qimg)
self.label_2.setPixmap(qpix)
except:
QMessageBox.about(self, "提示", "请先选择人脸照片!")
#自定义傅里叶变换函数
def dft(self,img):
H, W, channel = img.shape
# Prepare DFT coefficient
G = np.zeros((H, W, channel), dtype=np.complex)
# prepare processed index corresponding to original image positions
x = np.tile(np.arange(W), (H, 1))
y = np.arange(H).repeat(W).reshape(H, -1)
self.create_thread()
# dft
# pragma omp parallel for
for c in range(channel):
for v in range(H):
for u in range(W):
G[v, u, c] = np.sum(img[..., c] * np.exp(-2j * np.pi * (x * u / W + y * v / H))) / np.sqrt(H * W)
if v % (int(H / 100)) == 0:
self.detectThread.timerEvent()
self.detectThread.quit()
return G
#傅里叶变换
def FourierTransform(self):
try:
self.label_2.setScaledContents(True)
# 读取图片
img = cv2.imread(self.path)
img = cv2.resize(img, (100, 100), interpolation=cv2.INTER_CUBIC)
# 傅里叶库函数调用
#dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
#dftshift = np.fft.fftshift(dft)
#res1 = 20 * np.log(cv2.magnitude(dftshift[:, :, 0], dftshift[:, :, 1]))
# 自定义傅里叶变换函数调用
self.result5 = self.dft(img)
fshift = np.fft.fftshift(self.result5)
fimg = np.log(np.abs(fshift))
plt.imshow(fimg, 'gray')
plt.axis('off')
plt.savefig("f.png", bbox_inches='tight', pad_inches=0)
self.label_2.setPixmap(QPixmap("f.png"))
os.remove("f.png")
except:
QMessageBox.about(self, "提示", "请先选择图片!")
#自定义傅里叶反变换函数
def idft(self,G):
# prepare out image
H, W, channel = G.shape
out = np.zeros((H, W, channel), dtype=np.float32)
# 准备与原始图像位置相对应的处理索引
x = np.tile(np.arange(W), (H, 1))
y = np.arange(H).repeat(W).reshape(H, -1)
self.create_thread()
# idft
for c in range(channel):
for v in range(H):
for u in range(W):
out[v, u, c] = np.abs(np.sum(G[..., c] * np.exp(2j * np.pi * (x * u / W + y * v / H)))) / np.sqrt(W * H)
if v % (int(H / 100)) == 0:
self.detectThread.timerEvent()
self.detectThread.quit()
# clipping
out = np.clip(out, 0, 255)
out = out.astype(np.uint8)
return out
#傅里叶反变换
def inFourierTransform(self):
try:
self.label_2.setScaledContents(True)
# 傅里叶库函数调用
#ishift = np.fft.ifftshift(self.result5)
#iimg = cv2.idft(ishift)
陈一月的编程岁月
- 粉丝: 1530
- 资源: 24
最新资源
- VC 通用控件编程实例代码
- MATLAB环境下基于随机游走拉普拉斯算子的快速谱聚类方法 算法运行环境为MAYLAB R2018A,执行基于随机游走拉普拉斯算子的快速谱聚类方法 for i=1:c plot(X(labe
- 提高Python网络编程实战视频教程网络抓取爬虫10django-.avi
- 考虑风光消纳的自适应电动汽车优化调度 基于蒙特卡洛,采用copula函数和fuzzy-kmeans生成风光典型场景 多类型电动汽车采用分时电价调度,目标函数考虑上级电网出力、峰谷差惩罚费用、风光调度
- 提高Python网络编程实战视频教程网络抓取爬虫11twisted-.avi
- pscad仿真 采用pscad搭建220kv三相空载输电线路,仿真合空线,切空线过电压,仿真避雷器,合闸电阻法抑制合闸过电压,仿真控制断路器三相分别在线路相电压为0,30,60,90分合闸的抑制过电压
- 蜗轮齿轮箱电机sw21可编辑全套技术资料100%好用.zip
- 自抗扰控制,幅频特性曲线,传函推导,pid等效,跟踪曲线,抗扰曲线
- 提高Python网络编程实战视频教程网络抓取爬虫12Twisted综合应用-.avi
- 微电网两阶段鲁棒优化经济调度方法 参考文献:微电网两阶段鲁棒优化经济调度方法 matlab+yalmip+cplex 代码主要考虑了分布式电源和负荷的不确定性,通过对两阶段鲁棒优化模型的求解,微电网能
- Python培训之美眉图片下载爬虫 01 构造淘宝模特美眉列表页.flv
- Prompt工程-AI开发-可置顶粘贴小工具
- 通过场分布得到光子晶体的色散
- 2022年国内新能源汽车市场展望
- Python培训之美眉图片下载爬虫 02 在线读取某列表页内容.flv
- 永磁同步电机死区效应补偿策略研究仿真,该仿真利用已知的死区时间,直接将补偿时间补到三相占空比中,无需知道额外的参数 采用参考电流判断电流的方向,避免传统根据实际电流判断方向在零电流箝位的误差影响
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈