简体   繁体   中英

read image using openCV from QFileDialog Python

i am trying to apply pre-processing to the uploaded image. but the image is not read with opencv, all preprocessing is not applying.
PyQt5 - mainWindow:

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

extractedText=imageToText.getImage(imagePath)

----another class for image --------

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

is it the problem from the path? or the file type is not compatible with opencv? before i was using cv2.imread('test.jpg') but now with "QFileDialog.getOpenFileName" nothing is working well The problem from (imagePath)

Error:

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'

The following code opens a image from the path supplied by QFileDialog. Its not clear what your imageToText class is doing, so the problem probably lies in there. Use cv2.imread() instead and work with the returned matrix.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM