OpenCV Python not opening images with imread()

后端 未结 6 476
长发绾君心
长发绾君心 2020-12-17 00:14

I\'m not entirely sure why this is happening but I am in the process of making a program and I am having tons of issues trying to get opencv to open images using imread. I k

相关标签:
6条回答
  • 2020-12-17 00:54

    My suggestion for everyone facing the same problem is to try this:

    cv2.imshow("image", img)
    

    The img is keyword. Never forget.

    0 讨论(0)
  • 2020-12-17 00:55

    When you get error like this AttributeError: 'NoneType' object has no attribute 'shape'

    Try with new_image=image.copy

    0 讨论(0)
  • 2020-12-17 01:01

    Use cv2.imread() with a proper picture. Also use either // or \ appropriately. For Example:

        img = cv2.imread(location_to_image_with_appropriate_file_delimitation)
        h, w, c = img.shape
        print(h, w, c)
    
    
    0 讨论(0)
  • 2020-12-17 01:08

    From my experience, file paths that are too long (OS dependent) can also cause cv2.imread() to fail.

    Also, when it does fail, it often fails silently, so it is hard to even realize that it failed, and usually something further the the code will be what sparks the error.

    Hope this helps.

    0 讨论(0)
  • 2020-12-17 01:13

    Probably you have problem with special meaning of \ in text - like \t or \n

    Use \\ in place of \

    imgloc = "F:\\Kyle\\Desktop\\Coinjar\\Test images\\ten.png"
    

    or r''

    imgloc = r"F:\Kyle\Desktop\Coinjar\Test images\ten.png"
    

    EDIT:

    Some modules accept even / - like in Linux path

    imgloc = "F:/Kyle/Desktop/Coinjar/Test images/ten.png"
    
    0 讨论(0)
  • 2020-12-17 01:19

    Take care to :

    • try imread() with a reliable picture,
    • and the correct path in your context like (see Kyle772 answer). For me either //or \.

    I lost a couple of hours trying with 2 images saved from a left click in a browser. As soon as I took a personal camera image, it works fine.

    Spyder screen shot

        #context  windows10 / anaconda / python 3.2.0
        import cv2
        print(cv2.__version__) # 3.2.0
        imgloc = "D:/violettes/Software/Central/test.jpg" #this path works fine.  
        # imgloc = "D:\\violettes\\Software\\Central\\test.jpg"   this path works fine also. 
        #imgloc = "D:\violettes\Software\Central\test.jpg" #this path fails.
    
        img = cv2.imread(imgloc)
        height, width, channels = img.shape
        print (height, width, channels)
    

    python opencv image-loading imread

    0 讨论(0)
提交回复
热议问题