pass cv2 image issue in PyQt5

0

I'm barely new about pyQt5. When i'm trying to pass an image in a function pyCharm finish with an exit code -1073740791 (0xC0000409).

with cmd it said:

_grayImage = _imageToCanny
NameError: name '_imageToCanny' is not defined

it seems that if i load the image with imread in the same function it works. But if i load an image in a different function (using global) it sayd that the the variable is empty or give me this error.

    def doCanny(self):
        global _imageToCanny, _grayImage, _edgeResult
        global _lowerThreshold, _upperThreshold
        #_grayImage = cv2.imread("test.jpg")
        _grayImage = _imageToCanny
        _grayImage = cv2.cvtColor(_grayImage, cv2.COLOR_BGR2GRAY)
        blurred = cv2.GaussianBlur(_grayImage, (3, 3), 0)
        _edgeResult = cv2.Canny(blurred, _lowerThreshold, 200)
        self.displayImageX(_edgeResult)

Here is the code:

'''

import sys
import cv2
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi


class testWin(QMainWindow):
    _imageToCanny = None
    _grayImage = None
    _edgeResult = None
    _lowerThreshold = 100
    _upperThreshold = 200
    _boolX2 = True
    _boolX3 = False
    _boolXFree = False
    _xKernel = 3
    _yKernel = 3

    def __init__(self):
        super(testWin, self).__init__()
        loadUi("dlgCannyV2.ui", self)
        self.activateSlotPointer()
        self.setUI()

    def initVariable(self):
        global _imageToCanny, _grayImage, _edgeResult
        global _lowerThreshold, _upperThreshold
        global _boolX2, _boolX3, _boolXFree
        global _xKernel, _yKernel
        self._imageToCanny = None
        self._grayImage = None
        self._edgeResult = None
        self._lowerThreshold = 100
        self._upperThreshold = 200
        self._boolX2 = True
        self._boolX3 = False
        self._boolXFree = False
        self._xKernel = 3
        self._yKernel = 3

    def activateSlotPointer(self):
        # VIEWER POINTER
        self.viewerCanny = self.findChild(QtWidgets.QLabel, 'viewerCanny')
        # BTN POINTER
        self.BTN_accept = self.findChild(QtWidgets.QPushButton, 'canny_btnAccept')
        self.BTN_cancel = self.findChild(QtWidgets.QPushButton, 'canny_btnCancel')
        self.BTN_tryAuto = self.findChild(QtWidgets.QPushButton, 'canny_btnTryAuto')
        # SLIDER POINTER
        self.SLD_LowerValue = self.findChild(QtWidgets.QSlider, 'canny_sldLowThreshold')
        self.SLD_UpperValue = self.findChild(QtWidgets.QSlider, 'canny_sldUpperThreshold')
        # LBL POINTER
        self.LBL_lowerValue = self.findChild(QtWidgets.QLabel, 'canny_lbLowThreshold')
        self.LBL_upperValue = self.findChild(QtWidgets.QLabel, 'canny_lblUpperThreshold')
        # LBL POINTER
        self.RAD_X2 = self.findChild(QtWidgets.QRadioButton, 'canny_chkX2')
        self.RAD_X3 = self.findChild(QtWidgets.QRadioButton, 'canny_chkX3')
        self.RAD_XFREE = self.findChild(QtWidgets.QRadioButton, 'canny_chkXFree')

    def setUI(self):
        self.setWindowTitle("test PyQt5 with MAtPlotlib")
        global _lowerThreshold, _upperThreshold
        # Button -> Function
        self.BTN_accept.clicked.connect(self.doAccept)
        self.BTN_cancel.clicked.connect(self.doCancel)
        self.BTN_tryAuto.clicked.connect(self.doTryAuto)
        # Slider -> Function
        self.SLD_LowerValue.setProperty('maximum', 500)
        self.SLD_LowerValue.valueChanged.connect(self.setLowerThreshold)
        self.SLD_LowerValue.setValue(self._lowerThreshold)
        self.SLD_UpperValue.setProperty('maximum', 500)
        self.SLD_UpperValue.valueChanged.connect(self.setUpperThreshold)
        self.SLD_UpperValue.setValue(self._upperThreshold)
        # Label -> Function
        self.LBL_lowerValue.setText(str(self._lowerThreshold))
        self.LBL_upperValue.setText(str(self._upperThreshold))
        # RadioButton -> Function
        self.RAD_X2.setChecked(False)
        self.RAD_X3.setChecked(False)
        self.RAD_XFREE.setChecked(True)

    def setImage(self, image=None):
        global _imageToCanny, _grayImage, _edgeResult
        _imageToCanny = image
        _grayImage = _imageToCanny.copy()
        _edgeResult = _imageToCanny.copy()
        self.displayImageX(_imageToCanny)

    def doAccept(self):
        pass

    def doCancel(self):
        self.close()

    def doTryAuto(self):
        pass

    def setLowerThreshold(self):
        global _lowerThreshold,_upperThreshold
        _lowerThreshold = self.SLD_LowerValue.value()
        _upperThreshold = self.SLD_UpperValue.value()
        self.LBL_lowerValue.setText(str(_lowerThreshold))
        self.doCanny()

    def setUpperThreshold(self):
        global _lowerThreshold,_upperThreshold
        _lowerThreshold = self.SLD_LowerValue.value()
        _upperThreshold = self.SLD_UpperValue.value()
        self.LBL_upperValue.setText(str(_upperThreshold))
        self.doCanny()

    def x2Select(self):
        if self.RAD_X2.setChecked(False):
            self.RAD_X2.setChecked(True)
            self.RAD_X3.setChecked(False)
            self.RAD_XFREE.setChecked(False)

    def x3Select(self):
        if self.RAD_X3.setChecked(False):
            self.RAD_X3.setChecked(True)
            self.RAD_X2.setChecked(False)
            self.RAD_XFREE.setChecked(False)

    def xFreeSelect(self):
        if self.RAD_XFREE.setChecked(False):
            self.RAD_Xq.setChecked(False)
            self.RAD_X2.setChecked(False)
            self.RAD_XFREE.setChecked(True)

        # function from:
        # https://www.pyimagesearch.com/2015/04/06/zero-parameter-automatic-canny-edge-detection-with-python-and-opencv/
        #

    ##################################
    #
    #   CANNY PROCESSING
    #
    ##################################

    def doCanny(self):
        global _imageToCanny, _grayImage, _edgeResult
        global _lowerThreshold, _upperThreshold
        #_grayImage = cv2.imread("test.jpg")
        _grayImage = _imageToCanny
        _grayImage = cv2.cvtColor(_grayImage, cv2.COLOR_BGR2GRAY)
        blurred = cv2.GaussianBlur(_grayImage, (3, 3), 0)
        _edgeResult = cv2.Canny(blurred, _lowerThreshold, 200)
        self.displayImageX(_edgeResult)

    ##################################
    #
    #   GRAPHIC ENGINE
    #
    ##################################

    def testImage(self):
        pixmap = QPixmap('test.jpg')
        self.viewerCanny.setPixmap(pixmap)
        # self.resize(pixmap.width(), pixmap.height())

    def displayImageX(self, image):
        qformat = QImage.Format_Indexed8
        if len(image.shape) == 3:  # rows[0],cols[1],channels[2]
            if (image.shape[2]) == 4:
                qformat = QImage.Format_RGBA8888
            else:
                qformat = QImage.Format_RGB888
        img = QImage(image, image.shape[1], image.shape[0], image.strides[0], qformat)
        # BGR > RGB
        img = img.rgbSwapped()
        self.viewerCanny.setPixmap(QPixmap.fromImage(img))
        self.viewerCanny.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)



app = QApplication(sys.argv)
win = testWin()
image = cv2.imread("test.jpg", cv2.IMREAD_ANYCOLOR)
win.setImage(image)
win.show()
sys.exit(app.exec_())

'''

this is the dlgCannyV2.ui:

'''

python
pyqt5
opencv-python
asked on Stack Overflow Jun 10, 2020 by Alessio Michelassi • edited Jun 10, 2020 by Alessio Michelassi

1 Answer

0

it seems the major issue was with the environment. I don't know why but it messing up with the global variable. I started again with a new virtual environment made some test and now it works...

'''

import cv2

class testClass():
    _testVariable = None

    def __init__(self):
      pass

    def setValue(self, value):
        global _testVariable
        _testVariable = value

    def func(self):
      global _testVariable
      _testVariable = 12

    def printValue(self):
        global _testVariable
        print (_testVariable)

#######

test = testClass()
test.func()
test.printValue()
image = cv2.imread("test.jpg")
test.setValue(image)
test.printValue()

'''

answered on Stack Overflow Jun 11, 2020 by Alessio Michelassi

User contributions licensed under CC BY-SA 3.0