Learner
Learner

Reputation: 157

how do I correctly pass my pathL and pathR to load my images in opencv?

I am new to python and I am using opencv. I have one button(btn_Convertir) that calls the method sustractBands that calls the method createImage, but it receives two parameters, the paths from two images that I am loading using QFileDialog. I want to pass the two paths as parameters in order to use imread from opencv, and I also want to use my methods createImageRed and createCyan, but I always get an error, how can I do it in python? Please help

this is my code

import sys
from PyQt4 import QtGui, uic, QtCore
import cv2
import numpy as np

class Window(QtGui.QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        uic.loadUi('UI.ui', self)
        self.cb_tipos.addItems(["a", "b", "c"])
        self.show()

        self.abrir_izq.clicked.connect(self.openFileL)
        self.abrir_der.clicked.connect(self.openFileR)
        self.rb_matrices.clicked.connect(self.cambiarEstadoCombo)

        self.btn_Convertir.clicked.connect(self.sustractBands, pathL, pathR)

    def openFileL(self):
        pathL = QtGui.QFileDialog.getOpenFileName(self, "Open image","",'Images (* .jpg)')
        pixmap = QtGui.QPixmap(pathL)
        print self.im_izq.size()
        pixmap.scaled(self.im_izq.size(), QtCore.Qt.IgnoreAspectRatio, QtCore.Qt.FastTransformation)
        self.im_izq.setPixmap(pixmap)

    def openFileR(self):
        pathR = QtGui.QFileDialog.getOpenFileName(self, "Open image","",'Images (* .jpg)')
        pixmapd = QtGui.QPixmap(pathR)
        pixmapd.scaled(self.im_der.size(), QtCore.Qt.KeepAspectRatio, QtCore.Qt.FastTransformation)
        self.im_der.setPixmap(pixmapd)

    def createImage(imgL, imgR):
        imgFinal = createImageRed(imgL) + createImageCyan(imgR)
        cv2.imwrite('Final.jpg', imgFinal)
        cv2.imshow("Finished", imgFinal)
        cv2.waitKey() 

    def createImageRed(imgL):
        imgL[:, :, 0] = 0
        imgL[:, :, 1] = 0
        return imgL 

    def createImageCyan(imgR):
        imgR[:, :, 2] = 0
        return imgR    

    def sustractBands(self, pathL, pathR):
        imgL = cv2.imread(pathL, cv2.IMREAD_COLOR)
        imgR = cv2.imread(pathR, cv2.IMREAD_COLOR)
        createImage(imgL, imgR)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec_())

Upvotes: 0

Views: 98

Answers (1)

Xavier Combelle
Xavier Combelle

Reputation: 11195

When you call a method you have to prefix it with the object you want to call it. Here the object is self, so instead of simply calling createImage(imgL, imgR) you have to call it like this self.createImage(imgL, imgR) same things for createImageRed and createImageCyan

as noted as a comment to this answer you still have undefined names pathL and pathR you must replace them by self.pathL and self.pathR in the openFile* methods and in the sustractBands, not using them as parameters of substractBands and finaly connecting it with

self.btn_Convertir.clicked.connect(self.sustractBands)

It's because the variables are by default locals to the methods and to be able to go out of a method you have to, in order or goodness either return them, use attribute (which I did) or declare them as global

Upvotes: 2

Related Questions