Advertisement
Guest User

si

a guest
Jun 27th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.61 KB | None | 0 0
  1. import imutils
  2. from PyQt5 import QtCore, QtGui, QtWidgets, uic
  3. from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QFileDialog, QMessageBox
  4. import sys
  5. import qdarkstyle
  6. import cv2
  7. import ntpath
  8. from skimage.measure import compare_ssim
  9.  
  10. class Ssim(QWidget):
  11.     def __init__(self):
  12.         super().__init__()# Mostrar la ventana para solicitar las imagenes
  13.         self.ventana = uic.loadUi("./presentacion/SSIM.ui") # Cargamos la interfaz para SSIM
  14.         self.imageA = None
  15.         self.imageB = None
  16.         self.ventana.imgA.setEnabled(False)
  17.         self.ventana.imgB.setEnabled(False)
  18.         self.ventana.btnA.clicked.connect(self.onClickOpenA)
  19.         self.ventana.btnB.clicked.connect(self.onClickOpenB)
  20.         self.ventana.btnAccion.clicked.connect(self.onClickDemostracion)
  21.  
  22.     def onClickOpenA(self):
  23.         x = self.openF()
  24.         if x != "":
  25.             self.imageA = x
  26.             self.ventana.imgA.setText(ntpath.basename(self.imageA))
  27.  
  28.     def onClickOpenB(self):
  29.         x = self.openF()
  30.         if x != "":
  31.             self.imageB = x
  32.             self.ventana.imgB.setText(ntpath.basename(self.imageB))
  33.  
  34.  
  35.     def openF(self):
  36.         options = QFileDialog.Options()
  37.         options |= QFileDialog.DontUseNativeDialog
  38.         fileName, _ = QFileDialog.getOpenFileName(self, "Seleccione el archivo de Imagen", "",
  39.                                                   "Archivos de imagenes (*.png *.svg *.jpg);;BitMap (*.bmp)", options=options)
  40.         if fileName:
  41.             return fileName
  42.         else:
  43.             return ""
  44.  
  45.     def onClickDemostracion(self):
  46.         if self.imageA is not None and self.imageB is not None:
  47.             QMessageBox.about(self, "Mensaje", "Presione la tecla 'n' para avanzar")
  48.             self.ventana.btnAccion.setEnabled(False)
  49.             respaldo = cv2.imread(self.imageA)
  50.             imageA = cv2.imread(self.imageA)
  51.             imageB = cv2.imread(self.imageB)
  52.             cv2.imshow('Imagen A', imageA)
  53.             cv2.imshow('Imagen B', imageB)
  54.             k = chr(cv2.waitKey()) #Esperamos a que el usuario aprete la tecla n
  55.             if k == 'n':
  56.                 cv2.destroyAllWindows()
  57.                 imageA = cv2.blur(imageA, (5, 5))
  58.                 imageB = cv2.blur(imageB, (5, 5))
  59.                 cv2.imshow('Filtrado de Imagen A', imageA)
  60.                 cv2.imshow('Filtrado de Imagen B', imageB)
  61.                 k = chr(cv2.waitKey())
  62.                 if k == 'n':
  63.                     cv2.destroyAllWindows()
  64.                     imageA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
  65.                     imageB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)
  66.                     cv2.imshow('Binarizacion de Imagen A', imageA)
  67.                     cv2.imshow('Binarizacion de Imagen B', imageB)
  68.                     k = chr(cv2.waitKey())
  69.                     if k == 'n':
  70.                         cv2.destroyAllWindows()
  71.                         (score, diff) = compare_ssim(imageA, imageB, full=True)
  72.                         diff = (diff * 255).astype("uint8")
  73.                         thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
  74.                         cv2.imshow("SSIM", diff)
  75.                         cv2.imshow("Extración", thresh)
  76.                         k = chr(cv2.waitKey())
  77.                         if k == 'n':
  78.                             cv2.destroyAllWindows()
  79.                             contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_LIST,
  80.                                                                    cv2.CHAIN_APPROX_SIMPLE)
  81.                             #cnts = imutils.grab_contours(contours)
  82.                             cnts = contours
  83.                             for c in cnts:
  84.                                 (x, y, w, h) = cv2.boundingRect(c)
  85.                                 cv2.rectangle(respaldo, (x, y), (x + w, y + h), (0, 0, 255), 3)
  86.                             cv2.imshow("Resultado", respaldo)
  87.                             k = chr(cv2.waitKey())
  88.                             #guardar = cv2.imwrite('SSIM.png', respaldo) #Borrar el # para que se guarde la imagen en el disco duro
  89.                             if k == 'n':
  90.                                 cv2.destroyAllWindows()
  91.                                 QMessageBox.about(self, "SSIM", "El valor SSIM entre las imagenes es de: " + str(score))
  92.                                 self.ventana.btnAccion.setEnabled(True)
  93.  
  94. if __name__ == "__main__":
  95.     app = QtWidgets.QApplication(sys.argv)
  96.     ventana = Ssim()
  97.     app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
  98.     ventana.ventana.show()
  99.     app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement