【发布时间】:2014-12-27 18:56:24
【问题描述】:
我正在尝试使用 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 对象。 但是,我不知道如何解决这个问题。 请任何人告诉我该怎么做,或者如果您有更好的解决方案,请提出。
P.S:我使用的是 PyQt4 Designer 和 OpenCV 2.4.8 版
【问题讨论】:
-
AFAIK,
cv2.imread()需要一个文件名,因此是字符串还是 Unicode 对象?因此,在您的情况下,传递 filename 比传递 QLabel 更有意义 -
谢谢!我想通了……
-
太好了,在这种情况下,我可以发表我的评论作为答案,以便您接受并结束此问答。
标签: python python-2.7 opencv image-processing pyqt