Python 如何将QLabel传递给openCV cv2.imread

Python 如何将QLabel传递给openCV cv2.imread,python,python-2.7,opencv,image-processing,pyqt,Python,Python 2.7,Opencv,Image Processing,Pyqt,我正在尝试使用pyqt4导入图像,我正在使用Qlabel显示它。 现在我想对这张图像做一些预处理,即灰度、噪声去除和分割。 并在单击工具栏中的“操作”按钮时显示更改 这是我做的代码,它不起作用 from PyQt4 import QtCore, QtGui from main_window import Ui_MainWindow import cv2 class module_one(QtGui.QMainWindow, Ui_MainWindow): "Module One i.e

我正在尝试使用pyqt4导入图像,我正在使用Qlabel显示它。 现在我想对这张图像做一些预处理,即灰度、噪声去除和分割。 并在单击工具栏中的“操作”按钮时显示更改

这是我做的代码,它不起作用

from PyQt4 import QtCore, QtGui
from main_window import Ui_MainWindow
import cv2

class module_one(QtGui.QMainWindow, Ui_MainWindow):
    "Module One i.e Pre-processing of the Project Handwriting analysis"
    def __init__(self,parent=None):
        "Initilization of Class module_one" 
        super(module_one,self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.createActions()
    #Open Function  
    def open(self):
        filename = QtGui.QFileDialog.getOpenFileName(self, "Open Image", QtCore.QDir.currentPath(), "Image Files (*.jpg *.jpeg)")
        if filename:
            image = QtGui.QPixmap(filename)
            self.ui.label.setPixmap(image)
            if image.isNull():
                QtGui.QMessageVox.information(self,"Image Viewer","Cannot load %s."%filename)
                return

    #Function that will convert the image into grayscale
    def gray_scale(self):
        image = cv2.imread(self.ui.label) **#Gives Error here**
        gray_scale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        self.ui.label.setPixmap(gray_scale)

    #Function that will connect all buttons to its function             
    def createActions(self):
        self.ui.actionOpen.triggered.connect(self.open)
        self.ui.actionExit.triggered.connect(QtGui.qApp.quit)
        self.ui.actionGray_Scale_Conversion.triggered.connect(self.gray_scale)
这会产生以下错误

image = cv2.imread(self.ui.label)
TypeError: expected string or Unicode object, QLabel found
我知道我正在传递Qlabel及其所需的字符串或unicode对象。 但是,我不知道如何解决这个问题。 请任何人告诉我怎么做,或者如果你有更好的解决方案,请提出

注意:我使用的是PyQt4 Designer和OpenCV版本2.4.8

AFAIK,
cv2.imread()
需要一个文件名,因此它抛出类型错误:需要字符串或Unicode对象,QLabel找到


因此,在您的情况下,传递文件名比传递QLabel
(self.ui.label)
AFAIK更有意义,
cv2.imread()
需要文件名,因此需要字符串或Unicode对象?因此,在您的情况下,传递文件名比传递QLabelTank更有意义!我想出来了…太好了,在这种情况下,我可以把我的评论作为一个答案发布,这样你就可以接受它,让这个问答结束。