Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

python - What is the problem with buttons connection in PyQt5?

I have the problem that I cannot connect the implementation between two buttons. After I pressed the "Grayscale" button and got the grayscale image, I pressed the "Canny" button but the user interface suddenly closed. I don't know what's wrong in my code.

def getImage(self):
    global fname
    fname = QFileDialog.getOpenFileName(self, 'Open file', 
       'C:\Usersinil-pingDesktopCODE',"Image Files (*.jpg *.gif *.bmp *.png)")
    pixmap = QPixmap(fname[0])
    self.label.setPixmap(QPixmap(pixmap))
    self.resize(pixmap.width(), pixmap.height())

def Grayscale(self):
    global edges
    edges = cv2.imread(fname[0], 0)
    edges = cv2.GaussianBlur(edges, (5, 5), 0)
    height, width = edges.shape[:2]
    ret,edges = cv2.threshold(edges,150,255,cv2.THRESH_BINARY)
    kernel = np.ones((5,5),np.uint8)
    edges = cv2.morphologyEx(edges, cv2.MORPH_OPEN, kernel)
    edges = cv2.morphologyEx(edges, cv2.MORPH_OPEN, kernel)

    edges = QImage(edges, width, height, QImage.Format_Grayscale8)
    pixmap = QPixmap.fromImage(edges)
    self.label.setPixmap(pixmap)
    self.resize(pixmap.width(), pixmap.height())

def Canny(self):
    edges2 = cv2.imread(edges[0],-1)
    edges2 = cv2.Canny(edges2,180,200)

    edges2 = QImage(edges2, width, height, QImage.Format_Grayscale8)
    pixmap = QPixmap.fromImage(edges2)
    self.label.setPixmap(pixmap)
    self.resize(pixmap.width(), pixmap.height())

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are a lot of problems with your code (including the fact that you didn't provide a minimal, reproducible example), some of them explaining the crash or possibly leading to another one (I've marked them with [*]):

  • as already pointed out, avoid using globals whenever possible (which is almost always), use class attributes instead;
  • you are continuously overwriting those globals, making everything very confusing also for debugging; in fact you're using edges for both a numpy array and a QImage;
  • [*] you are trying to cv2.imread(edges[0],-1), but not only imread expects a string, but, as a result of the previous point, at that point edges is even a QImage;
  • [*] some variables are not declared in the scope of the function (width/height in the Canny function);
  • you should really avoid using capitalized names for functions and variables, as they are usually only for class names and builtin constants;
  • there are some issues in the conversion back to QImage, I suppose that's due to the various transformations you're applying to to the array (some of them also seem unnecessary) that are not very friendly with the Format_Grayscale8 format, perhaps you should better investigate about that;

Here's a possible improvement over your code. I'm adding the widget creation parts, as it was missing in the original example.

class ShowImage(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        layout = QVBoxLayout(self)
        self.label = QLabel()
        layout.addWidget(self.label)

        self.getImageBtn = QPushButton()
        layout.addWidget(self.getImageBtn)
        self.getImageBtn.clicked.connect(self.getImage)

        self.grayBtn = QPushButton('gray')
        layout.addWidget(self.grayBtn)
        self.grayBtn.clicked.connect(self.grayScale)

        self.cannyBtn = QPushButton('canny')
        layout.addWidget(self.cannyBtn)
        self.cannyBtn.clicked.connect(self.canny)

        self.fileName = None
        self.edges = None

    def getImage(self):
        fname = QFileDialog.getOpenFileName(self, 'Open file', 
           'C:\Usersinil-pingDesktopCODE',"Image Files (*.jpg *.gif *.bmp *.png)")
        if fname[0]:
            self.fileName = fname[0]
            pixmap = QPixmap(self.fileName)
            self.label.setPixmap(pixmap)

    def grayScale(self):
        if not self.fileName:
            return
        edges = cv2.imread(self.fileName, 0)
        edges = cv2.GaussianBlur(edges, (5, 5), 0)
        ret,edges = cv2.threshold(edges, 150, 255, cv2.THRESH_BINARY)
        kernel = np.ones((5, 5), np.uint8)
        edges = cv2.morphologyEx(edges, cv2.MORPH_OPEN, kernel)
        edges = cv2.morphologyEx(edges, cv2.MORPH_OPEN, kernel)

        self.edges = edges
        height, width = edges.shape[:2]
        image = QImage(edges, width, height, QImage.Format_Grayscale8)
        pixmap = QPixmap.fromImage(image)
        self.label.setPixmap(pixmap)

    def canny(self):
        if self.edges is None:
            return
        edges2 = cv2.Canny(self.edges, 180, 200)
        height, width = edges2.shape[:2]

        edges2 = QImage(edges2, width, height, QImage.Format_Grayscale8)
        pixmap = QPixmap.fromImage(edges2)
        self.label.setPixmap(pixmap)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...