Error while using w, h = template.shape[::-1]
Question: I am getting an error:

    
    
    w, h = template.shape[::-1]
    AttributeError: 'NoneType' object has no attribute 'shape'
    

My code:

    
    
    import cv2
    import numpy as np
    
    img_rgb = cv2.imread('opencv-template-matching-python-tutorial.jpg')
    
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
    
    template = cv2.imread('opencv-template-for-matching.jpg',0)
    
    w, h = template.shape[::-1]
    res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
    
    threshold = 0.8
    
    loc = np.where( res >= threshold)
    
    for pt in zip(*loc[::-1]):
    
        cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)
    
        cv2.imshow('Detected',img_rgb)
    

How can I fix this problem?


Answer: I'm not too familiar with `opencv`, but that error means that
`cv2.imread('opencv-template-for-matching.jpg',0)` fails to read that file and
thus returns `None`.

Make sure that this file exists and in the supported format. From `imread`'s
[documentation](http://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#cv2.imread):

> The function imread loads an image from the specified file and returns it.
> **If the image cannot be read (because of missing file, improper
> permissions, unsupported or invalid format), the function returns an empty
> matrix ( Mat::data==NULL)**. Currently, the following file formats are
> supported: Windows bitmaps - *.bmp, *.dib (always supported) JPEG files -
> *.jpeg, *.jpg, *.jpe (see the Notes section) JPEG 2000 files - *.jp2 (see
> the Notes section) Portable Network Graphics - *.png (see the Notes section)
> Portable image format - *.pbm, *.pgm, *.ppm (always supported) Sun rasters -
> *.sr, *.ras (always supported) TIFF files - *.tiff, *.tif (see the Notes
> section)

