Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import imutils
- from PyQt5 import QtCore, QtGui, QtWidgets, uic
- from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QFileDialog, QMessageBox
- import sys
- import qdarkstyle
- import cv2
- import ntpath
- from skimage.measure import compare_ssim
- class Ssim(QWidget):
- def __init__(self):
- super().__init__()# Mostrar la ventana para solicitar las imagenes
- self.ventana = uic.loadUi("./presentacion/SSIM.ui") # Cargamos la interfaz para SSIM
- self.imageA = None
- self.imageB = None
- self.ventana.imgA.setEnabled(False)
- self.ventana.imgB.setEnabled(False)
- self.ventana.btnA.clicked.connect(self.onClickOpenA)
- self.ventana.btnB.clicked.connect(self.onClickOpenB)
- self.ventana.btnAccion.clicked.connect(self.onClickDemostracion)
- def onClickOpenA(self):
- x = self.openF()
- if x != "":
- self.imageA = x
- self.ventana.imgA.setText(ntpath.basename(self.imageA))
- def onClickOpenB(self):
- x = self.openF()
- if x != "":
- self.imageB = x
- self.ventana.imgB.setText(ntpath.basename(self.imageB))
- def openF(self):
- options = QFileDialog.Options()
- options |= QFileDialog.DontUseNativeDialog
- fileName, _ = QFileDialog.getOpenFileName(self, "Seleccione el archivo de Imagen", "",
- "Archivos de imagenes (*.png *.svg *.jpg);;BitMap (*.bmp)", options=options)
- if fileName:
- return fileName
- else:
- return ""
- def onClickDemostracion(self):
- if self.imageA is not None and self.imageB is not None:
- QMessageBox.about(self, "Mensaje", "Presione la tecla 'n' para avanzar")
- self.ventana.btnAccion.setEnabled(False)
- respaldo = cv2.imread(self.imageA)
- imageA = cv2.imread(self.imageA)
- imageB = cv2.imread(self.imageB)
- cv2.imshow('Imagen A', imageA)
- cv2.imshow('Imagen B', imageB)
- k = chr(cv2.waitKey()) #Esperamos a que el usuario aprete la tecla n
- if k == 'n':
- cv2.destroyAllWindows()
- imageA = cv2.blur(imageA, (5, 5))
- imageB = cv2.blur(imageB, (5, 5))
- cv2.imshow('Filtrado de Imagen A', imageA)
- cv2.imshow('Filtrado de Imagen B', imageB)
- k = chr(cv2.waitKey())
- if k == 'n':
- cv2.destroyAllWindows()
- imageA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
- imageB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)
- cv2.imshow('Binarizacion de Imagen A', imageA)
- cv2.imshow('Binarizacion de Imagen B', imageB)
- k = chr(cv2.waitKey())
- if k == 'n':
- cv2.destroyAllWindows()
- (score, diff) = compare_ssim(imageA, imageB, full=True)
- diff = (diff * 255).astype("uint8")
- thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
- cv2.imshow("SSIM", diff)
- cv2.imshow("Extración", thresh)
- k = chr(cv2.waitKey())
- if k == 'n':
- cv2.destroyAllWindows()
- contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_LIST,
- cv2.CHAIN_APPROX_SIMPLE)
- #cnts = imutils.grab_contours(contours)
- cnts = contours
- for c in cnts:
- (x, y, w, h) = cv2.boundingRect(c)
- cv2.rectangle(respaldo, (x, y), (x + w, y + h), (0, 0, 255), 3)
- cv2.imshow("Resultado", respaldo)
- k = chr(cv2.waitKey())
- #guardar = cv2.imwrite('SSIM.png', respaldo) #Borrar el # para que se guarde la imagen en el disco duro
- if k == 'n':
- cv2.destroyAllWindows()
- QMessageBox.about(self, "SSIM", "El valor SSIM entre las imagenes es de: " + str(score))
- self.ventana.btnAccion.setEnabled(True)
- if __name__ == "__main__":
- app = QtWidgets.QApplication(sys.argv)
- ventana = Ssim()
- app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
- ventana.ventana.show()
- app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement