# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtWidgets import QFileDialog
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import *
import sys
import os
import argparse
import cv2
import numpy as np


class Ui_MainWindow(QMainWindow):

    def __init__(self):
        super(Ui_MainWindow,self).__init__()
        self.setupUi(self)
        self.retranslateUi(self)

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1304, 746)
        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.toolbar = QtWidgets.QToolBox(self.centralWidget)
        self.toolbar.setGeometry(QtCore.QRect(1040, 0, 251, 531))
        self.toolbar.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.toolbar.setObjectName("toolbar")
        self.basicOperation = QtWidgets.QWidget()
        self.basicOperation.setGeometry(QtCore.QRect(0, 0, 251, 375))
        self.basicOperation.setObjectName("basicOperation")
        self.rotateButton = QtWidgets.QPushButton(self.basicOperation)
        self.rotateButton.setGeometry(QtCore.QRect(80, 170, 112, 34))
        self.rotateButton.setObjectName("rotateButton")
        self.rotateButton.clicked.connect(self.rotate)  # 旋转信息槽
        self.greyButton = QtWidgets.QPushButton(self.basicOperation)
        self.greyButton.setGeometry(QtCore.QRect(80, 220, 112, 34))
        self.greyButton.setObjectName("greyButton")
        self.greyButton.clicked.connect(self.turngrey)  # 灰度图像信息槽
        self.mirrorButton = QtWidgets.QPushButton(self.basicOperation)
        self.mirrorButton.setGeometry(QtCore.QRect(80, 270, 112, 34))
        self.mirrorButton.setObjectName("mirrorButton")
        self.mirrorButton.clicked.connect(self.mirror)  # 镜像信息槽
        self.biggerButton = QtWidgets.QPushButton(self.basicOperation)
        self.biggerButton.setGeometry(QtCore.QRect(40, 20, 75, 23))
        self.biggerButton.setObjectName("biggerButton")
        self.biggerButton.clicked.connect(self.bigger)  # 放大信息槽
        self.smallerButton = QtWidgets.QPushButton(self.basicOperation)
        self.smallerButton.setGeometry(QtCore.QRect(150, 20, 75, 23))
        self.smallerButton.setObjectName("smallerButton")
        self.smallerButton.clicked.connect(self.smaller)# 缩小信息槽
        self.upButton = QtWidgets.QPushButton(self.basicOperation)
        self.upButton.setGeometry(QtCore.QRect(40, 70, 75, 23))
        self.upButton.setObjectName("upButton")
        self.upButton.clicked.connect(self.up)          # 上移信息槽
        self.downButton = QtWidgets.QPushButton(self.basicOperation)
        self.downButton.setGeometry(QtCore.QRect(150, 70, 75, 23))
        self.downButton.setObjectName("downButton")
        self.downButton.clicked.connect(self.down)      # 下移信息槽
        self.leftButton = QtWidgets.QPushButton(self.basicOperation)
        self.leftButton.setGeometry(QtCore.QRect(40, 110, 75, 23))
        self.leftButton.setObjectName("leftButton")
        self.leftButton.clicked.connect(self.left)      # 左移信息槽
        self.rightButton = QtWidgets.QPushButton(self.basicOperation)
        self.rightButton.setGeometry(QtCore.QRect(150, 110, 75, 23))
        self.rightButton.setObjectName("rightButton")
        self.rightButton.clicked.connect(self.right)    # 右移信息槽
        self.toolbar.addItem(self.basicOperation, "")
        self.edgeDitect = QtWidgets.QWidget()
        self.edgeDitect.setGeometry(QtCore.QRect(0, 0, 98, 28))
        self.edgeDitect.setObjectName("edgeDitect")
        self.edgeButton_4 = QtWidgets.QPushButton(self.edgeDitect)
        self.edgeButton_4.setGeometry(QtCore.QRect(70, 20, 112, 34))
        self.edgeButton_4.setObjectName("edgeButton_4")
        self.edgeButton_4.clicked.connect(self.edge)    # 边缘检测信息槽
        self.toolbar.addItem(self.edgeDitect, "")
        self.enhance = QtWidgets.QWidget()
        self.enhance.setGeometry(QtCore.QRect(0, 0, 98, 28))
        self.enhance.setObjectName("enhance")
        self.sharpenButton = QtWidgets.QPushButton(self.enhance)
        self.sharpenButton.setGeometry(QtCore.QRect(80, 10, 112, 34))
        self.sharpenButton.setObjectName("sharpenButton")
        self.sharpenButton.clicked.connect(self.sharpen)  # 锐化信息槽
        self.smoothingButton = QtWidgets.QPushButton(self.enhance)
        self.smoothingButton.setGeometry(QtCore.QRect(80, 60, 112, 34))
        self.smoothingButton.setObjectName("smoothingButton")
        self.smoothingButton.clicked.connect(self.smoothing)  # 平滑信息槽
        self.toolbar.addItem(self.enhance, "")
        self.noiseReduction = QtWidgets.QWidget()
        self.noiseReduction.setGeometry(QtCore.QRect(0, 0, 98, 28))
        self.noiseReduction.setObjectName("noiseReduction")
        self.denoiseButton = QtWidgets.QPushButton(self.noiseReduction)
        self.denoiseButton.setGeometry(QtCore.QRect(80, 20, 112, 34))
        self.denoiseButton.setObjectName("denoiseButton")
        self.denoiseButton.clicked.connect(self.denoise)  # 降噪信息槽
        self.toolbar.addItem(self.noiseReduction, "")
        self.morphology = QtWidgets.QWidget()
        self.morphology.setGeometry(QtCore.QRect(0, 0, 98, 28))
        self.morphology.setObjectName("morphology")
        self.erosionButton = QtWidgets.QPushButton(self.morphology)
        self.erosionButton.setGeometry(QtCore.QRect(90, 10, 91, 31))
        self.erosionButton.setObjectName("erosionButton")
        self.erosionButton.clicked.connect(self.erosion)  # 腐蚀信息槽
        self.dilationButton = QtWidgets.QPushButton(self.morphology)
        self.dilationButton.setGeometry(QtCore.QRect(90, 70, 91, 31))
        self.dilationButton.setObjectName("dilationButton")
        self.dilationButton.clicked.connect(self.dilation)# 膨胀信息槽
        self.toolbar.addItem(self.morphology, "")
        self.filter = QtWidgets.QWidget()
        self.filter.setGeometry(QtCore.QRect(0, 0, 251, 305))
        self.filter.setObjectName("filter")
        self.oilpaintingButton = QtWidgets.QPushButton(self.filter)
        self.oilpaintingButton.setGeometry(QtCore.QRect(70, 20, 111, 31))
        self.oilpaintingButton.setObjectName("oilpaintingButton")
        self.oilpaintingButton.clicked.connect(self.oilpainting)    # 油画滤镜信息槽
        self.watercolorButton = QtWidgets.QPushButton(self.filter)
        self.watercolorButton.setGeometry(QtCore.QRect(70, 90, 111, 31))
        self.watercolorButton.setObjectName("watercolorButton")
        self.watercolorButton.clicked.connect(self.watercolor)      # 水彩滤镜信息槽
        self.toolbar.addItem(self.filter, "")
        self.saveButton = QtWidgets.QPushButton(self.centralWidget)
        self.saveButton.setGeometry(QtCore.QRect(1120, 540, 112, 34))
        self.saveButton.setObjectName("saveButton")
        self.saveButton.clicked.connect(self.save)  # 保存信息槽
        self.picture = QtWidgets.QLabel(self.centralWidget)
        self.picture.setGeometry(QtCore.QRect(20, 10, 1011, 671))
        self.picture.setObjectName("picture")
        MainWindow.setCentralWidget(self.centralWidget)
        self.menuBar = QtWidgets.QMenuBar(MainWindow)
        self.menuBar.setGeometry(QtCore.QRect(0, 0, 1304, 22))
        self.menuBar.setObjectName("menuBar")
        self.file = QtWidgets.QMenu(self.menuBar)
        self.file.setObjectName("file")
        MainWindow.setMenuBar(self.menuBar)
        self.mainToolBar = QtWidgets.QToolBar(MainWindow)
        self.mainToolBar.setObjectName("mainToolBar")
        MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
        self.statusBar = QtWidgets.QStatusBar(MainWindow)
        self.statusBar.setObjectName("statusBar")
        MainWindow.setStatusBar(self.statusBar)
        self.actionOpen_pic = QtWidgets.QAction(MainWindow)
        self.actionOpen_pic.setObjectName("actionOpen_pic")
        self.actionOpen_pic.triggered.connect(self.openpic) # 打开图片信息槽
        self.file.addAction(self.actionOpen_pic)
        self.menuBar.addAction(self.file.menuAction())

        self.retranslateUi(MainWindow)
        self.toolbar.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.rotateButton.setText(_translate("MainWindow", "旋转"))
        self.greyButton.setText(_translate("MainWindow", "转为灰度图像"))
        self.mirrorButton.setText(_translate("MainWindow", "镜像"))
        self.biggerButton.setText(_translate("MainWindow", "放大"))
        self.smallerButton.setText(_translate("MainWindow", "缩小"))
        self.upButton.setText(_translate("MainWindow", "上移"))
        self.downButton.setText(_translate("MainWindow", "下移"))
        self.leftButton.setText(_translate("MainWindow", "左移"))
        self.rightButton.setText(_translate("MainWindow", "右移"))
        self.toolbar.setItemText(self.toolbar.indexOf(self.basicOperation), _translate("MainWindow", "图像基本操作"))
        self.edgeButton_4.setText(_translate("MainWindow", "边缘检测"))
        self.toolbar.setItemText(self.toolbar.indexOf(self.edgeDitect), _translate("MainWindow", "边缘检测"))
        self.sharpenButton.setText(_translate("MainWindow", "图像锐化"))
        self.smoothingButton.setText(_translate("MainWindow", "图像平滑"))
        self.toolbar.setItemText(self.toolbar.indexOf(self.enhance), _translate("MainWindow", "图像增强"))
        self.denoiseButton.setText(_translate("MainWindow", "去除噪声"))
        self.toolbar.setItemText(self.toolbar.indexOf(self.noiseReduction), _translate("MainWindow", "噪声去除"))
        self.erosionButton.setText(_translate("MainWindow", "图像腐蚀"))
        self.dilationButton.setText(_translate("MainWindow", "图像膨胀"))
        self.toolbar.setItemText(self.toolbar.indexOf(self.morphology), _translate("MainWindow", "图像形态学操作"))
        self.oilpaintingButton.setText(_translate("MainWindow", "油画滤镜"))
        self.watercolorButton.setText(_translate("MainWindow", "水彩滤镜"))
        self.toolbar.setItemText(self.toolbar.indexOf(self.filter), _translate("MainWindow", "添加滤镜"))
        self.saveButton.setText(_translate("MainWindow", "保存修改"))
        self.picture.setText(_translate("MainWindow", ""))
        self.file.setTitle(_translate("MainWindow", "文件"))
        self.actionOpen_pic.setText(_translate("MainWindow", "打开图片"))

    def openpic(self):
        print(cv2.__file__)
        address = QFileDialog.getOpenFileName(self, "选取图片", "./", "Image files(*.png *.jpg *.jpeg *.gif);;all files(*.*)")
        self.filename = address[0]
        if self.filename:
            print(self.filename)
            self.pic = QtGui.QPixmap(self.filename)
            self.picture.setPixmap(self.pic)
            self.newPic = cv2.imread(self.filename)
            # print("1")
            cv2.imwrite('./tempPic.png', self.newPic)
            # print("2")

    def save(self):
        saveAddress = QFileDialog.getSaveFileName(self, "保存文件", "./", "Image files(*.png *.jpg *.jpeg *.gif);;all files(*.*)")
        print(saveAddress[0])
        if saveAddress[0]:
            self.newPic = cv2.imread('./tempPic.png')
            cv2.imwrite(saveAddress[0], self.newPic)

    def rotate(self):
        img = cv2.imread('./tempPic.png')  # 图像旋转
        res = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
        cv2.imwrite("./tempPic.png", res)
        self.newPic = cv2.imread('./tempPic.png')
        self.pic = QtGui.QPixmap('./tempPic.png')
        self.picture.setPixmap(self.pic)

    def smoothing(self):
        getpic = cv2.imread('./tempPic.png')  # 图像平滑
        smoothpic = cv2.bilateralFilter(getpic, 5, 255, 10)  # 双边滤波
        cv2.imwrite("./tempPic.png", smoothpic)
        self.newPic = cv2.imread('./tempPic.png')
        self.pic = QtGui.QPixmap('./tempPic.png')
        self.picture.setPixmap(self.pic)

    def sharpen(self):
        getpic = cv2.imread('./tempPic.png')  # 图像锐化
        sharpen_op = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], dtype=np.float32)
        sharpen_image = cv2.filter2D(getpic, cv2.CV_32F, sharpen_op)
        sharpen_image = cv2.convertScaleAbs(sharpen_image)
        cv2.imwrite("./tempPic.png", sharpen_image)
        self.newPic = cv2.imread('./tempPic.png')
        self.pic = QtGui.QPixmap('./tempPic.png')
        self.picture.setPixmap(self.pic)

    def bigger(self):
        getpic = cv2.imread('./tempPic.png')  # 图像放大
        res = cv2.resize(getpic, None, fx=1.3, fy=1.3, interpolation=cv2.INTER_LINEAR)
        cv2.imwrite("./tempPic.png", res)
        self.newPic = cv2.imread('./tempPic.png')
        self.pic = QtGui.QPixmap('./tempPic.png')
        self.picture.setPixmap(self.pic)

    def smaller(self):
        getpic = cv2.imread('./tempPic.png')  # 图像缩小
        res = cv2.resize(getpic, None, fx=3/4, fy=3/4, interpolation=cv2.INTER_LINEAR)
        cv2.imwrite("./tempPic.png", res)
        self.newPic = cv2.imread('./tempPic.png')
        self.pic = QtGui.QPixmap('./tempPic.png')
        self.picture.setPixmap(self.pic)

    def oilpainting(self):
        img = cv2.imread('./tempPic.png', 1)  # 油画滤镜
        dst = cv2.xphoto.oilPainting(img, 3, 1)
        cv2.imwrite("./tempPic.png", dst)
        self.newPic = cv2.imread('./tempPic.png')
        self.pic = QtGui.QPixmap('./tempPic.png')
        self.picture.setPixmap(self.pic)

    def watercolor(self):
        img = cv2.imread('./tempPic.png', 1)  # 水彩滤镜
        dst =cv2.stylization(img, sigma_s=200, sigma_r=0.4)
        cv2.imwrite("./tempPic.png", dst)
        self.newPic = cv2.imread('./tempPic.png')
        self.pic = QtGui.QPixmap('./tempPic.png')
        self.picture.setPixmap(self.pic)

    def turngrey(self):
        img = cv2.imread('./tempPic.png',cv2.IMREAD_GRAYSCALE)  # 灰度图像
        cv2.imwrite("./tempPic.png", img)
        self.newPic = cv2.imread('./tempPic.png')
        self.pic = QtGui.QPixmap('./tempPic.png')
        self.picture.setPixmap(self.pic)

    def mirror(self):
        img = cv2.imread('./tempPic.png', 1)  # 左右镜像
        res = cv2.flip(img, 1)
        cv2.imwrite("./tempPic.png", res)
        self.newPic = cv2.imread('./tempPic.png')
        self.pic = QtGui.QPixmap('./tempPic.png')
        self.picture.setPixmap(self.pic)

    def up(self):
        img = cv2.imread('./tempPic.png', 1)  # 图像上移
        rows, cols, channels = img.shape
        M = np.float32([[1, 0, 0], [0, 1, -10]])
        res = cv2.warpAffine(img, M, (cols, rows))
        cv2.imwrite("./tempPic.png", res)
        self.newPic = cv2.imread('./tempPic.png')
        self.pic = QtGui.QPixmap('./tempPic.png')
        self.picture.setPixmap(self.pic)

    def down(self):
        img = cv2.imread('./tempPic.png', 1)  # 图像下移
        rows, cols, channels = img.shape
        M = np.float32([[1, 0, 0], [0, 1, 10]])
        res = cv2.warpAffine(img, M, (cols, rows))
        cv2.imwrite("./tempPic.png", res)
        self.newPic = cv2.imread('./tempPic.png')
        self.pic = QtGui.QPixmap('./tempPic.png')
        self.picture.setPixmap(self.pic)

    def left(self):
        img = cv2.imread('./tempPic.png', 1)  # 图像左移
        rows, cols, channels = img.shape
        M = np.float32([[1, 0, -10], [0, 1, 0]])
        res = cv2.warpAffine(img, M, (cols, rows))
        cv2.imwrite("./tempPic.png", res)
        self.newPic = cv2.imread('./tempPic.png')
        self.pic = QtGui.QPixmap('./tempPic.png')
        self.picture.setPixmap(self.pic)

    def right(self):
        img = cv2.imread('./tempPic.png', 1)  # 图像右移
        rows, cols, channels = img.shape
        M = np.float32([[1, 0, 10], [0, 1, 0]])
        res = cv2.warpAffine(img, M, (cols, rows))
        cv2.imwrite("./tempPic.png", res)
        self.newPic = cv2.imread('./tempPic.png')
        self.pic = QtGui.QPixmap('./tempPic.png')
        self.picture.setPixmap(self.pic)

    def edge(self):
        img = cv2.imread('./tempPic.png', 1)  # Sobel边缘检测
        img1 = cv2.resize(img, dsize=None, fx=1, fy=1)
        img2 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
        img3 = cv2.Sobel(img2, cv2.CV_64F, 0, 1, ksize=5)
        cv2.imwrite("./tempPic.png", img3)
        self.newPic = cv2.imread('./tempPic.png')
        self.pic = QtGui.QPixmap('./tempPic.png')
        self.picture.setPixmap(self.pic)

    def denoise(self):
        img = cv2.imread('./tempPic.png', 1)  # 图片降噪
        dst = cv2.fastNlMeansDenoisingColored(img, None, 3, 3, 7, 21)
        cv2.imwrite('./tempPic.png', dst)
        self.newPic = cv2.imread('./tempPic.png')
        self.pic = QtGui.QPixmap('./tempPic.png')
        self.picture.setPixmap(self.pic)

    def erosion(self):
        img = cv2.imread('./tempPic.png', 1)  # 图片腐蚀
        kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
        dst = cv2.erode(img, kernel)
        cv2.imwrite('./tempPic.png', dst)
        self.newPic = cv2.imread('./tempPic.png')
        self.pic = QtGui.QPixmap('./tempPic.png')
        self.picture.setPixmap(self.pic)

    def dilation(self):
        img = cv2.imread('./tempPic.png', 1)  # 图片膨胀
        kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
        dst = cv2.dilate(img, kernel)
        cv2.imwrite('./tempPic.png', dst)
        self.newPic = cv2.imread('./tempPic.png')
        self.pic = QtGui.QPixmap('./tempPic.png')
        self.picture.setPixmap(self.pic)

qapp = QApplication(sys.argv)
app = Ui_MainWindow()
app.show()
sys.exit(qapp.exec_())
