【问题标题】:read image using openCV from QFileDialog Python使用来自 QFileDialog Python 的 openCV 读取图像
【发布时间】:2019-11-22 13:00:06
【问题描述】:

我正在尝试对上传的图像应用预处理。 但图像不是用opencv读取的,所有预处理都没有应用。
PyQt5 - 主窗口:

fname = QFileDialog.getOpenFileName(self, 'Open file', 'c:\\', "Image files (*.jpg *.gif *.png)")
imagePath = fname[0]

extractedText=imageToText.getImage(imagePath)

----另一个图像类--------

def getImage(string):
        img= cv2.imread(string)
        finalImage = pre_processing(img)
        extractedText = "anything"
        return extractedText 
    def pre_processing(img):
        resized = img
        if (img.shape[1] > 500) and (img.shape[0] > 500): #error (no shape)
           resized = cv2.resize(img,None, fx=0.8,fy=0.8, interpolation = cv2.INTER_CUBIC)

        gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)  # error in cvtcolor
        th_val, th_img = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
        blur = cv2.GaussianBlur(th_img, (3, 3), 0)
        return blur

是路径的问题吗?或者文件类型与opencv不兼容? 在我使用 cv2.imread('test.jpg') 之前 但是现在使用“QFileDialog.getOpenFileName”没有任何效果 来自(imagePath)的问题

错误:

OpenCV(3.4.2)
c:\miniconda3\conda-bld\opencv-suite_1534379934306\work\modules\imgproc\src\color.hpp:253:
error: (-215:Assertion failed)
VScn::contains(scn) && VDcn::contains(dcn) && VDepth::contains(depth)
in function 'cv::CvtHelper<struct cv::Set<3,4,-1>,struct cv::Set<1,-1,-1>,struct cv::Set<0,2,5>,2>::CvtHelper'

【问题讨论】:

  • 你看过什么样的图片? print(imagePath) 的输出是什么?
  • 这是其中之一,在我应用 PyQt 之前,代码运行良好,我使用的是 cv2.imread('test.jpg') ,但现在使用 "QFileDialog.getOpenFileName" 并非一切正常,我认为它的问题@eyllanesc
  • imagePath 的输出:C:/Users/Desktop/lab/test.jpg @eyllanesc
  • 如果你直接使用cv2.imread('C:/Users/Desktop/lab/test.jpg')路由,只去掉QFileDialog,你有同样的问题吗? PyQt4 还是 PyQt5?
  • 不,我现在尝试 cv2.imread('C:/Users/Desktop/lab/test.jpg') 没有错误@eyllanesc - PyQT5

标签: python opencv pyqt anaconda


【解决方案1】:

以下代码从 QFileDialog 提供的路径打开图像。目前尚不清楚您的 imageToText 类在做什么,所以问题可能出在那儿。改用 cv2.imread() 并使用返回的矩阵。

class(QMainWindow):
__init__(self):
    self.openFile = QAction(QIcon('open.png'), 'Open', self)
    self.openFile.setShortcut('Ctrl+l')
    self.openFile.setStatusTip('Open new File')
    self.openFile.triggered.connect(self.showDialog)
    menubar = self.menuBar()
    fileMenu = menubar.addMenu('&File')
    fileMenu.addAction(self.openFile)
    self.setObjectName("MainWindow")
    self.resize(800, 600)

def showDialog(self):
    fname = QFileDialog.getOpenFileName(self, 'open file', '/home')
    print(fname[0])
    import cv2
    a = cv2.imread(fname[0])
    cv2.imshow("a", a)
    cv2.waitKey(0)

【讨论】:

  • 预处理的错误仍然出现,像这样一个错误:OpenCV(3.4.2) c:\miniconda3\conda-bld\opencv-suite_1534379934306\work\modules\imgproc\src\color .hpp:253: 错误: (-215:Assertion failed) VScn::contains(scn) && VDcn::contains(dcn) && VDepth::contains(depth) in function 'cv::CvtHelper,struct cv::Set,struct cv::Set,2>::CvtHelper' 我读到这个错误是因为路径@fogx
  • 那么您选择的图片无效。 fname[0] 以字符串形式返回完整的绝对路径,您可以在 print 语句的命令行中看到该路径。也可能更新openCV ...最新版本是4.1.2
猜你喜欢
  • 1970-01-01
  • 2019-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-27
  • 2020-08-13
  • 2017-11-23
  • 1970-01-01
相关资源
最近更新 更多