Combining multiple images to a numpy array on python with opencv2
<p>I am trying to read 8 different .png images into a numpy array on python 3.5 opencv 2. </p>

<pre><code>import numpy as np
import cv2
import os
</code></pre>

<p>Using the 'os' class methods, I am able to pick all the .png files from a directory and read them using cv2.imread. </p>

<pre><code>imgTrainGray = list ()
mypath = os.path.dirname('image/directory')

for item in os.listdir(mypath):
if '.png' in item:  
    image = cv2.imread((os.path.join(mypath, item)),0)
    if image is not None:
       imgTrainGray.append(image)
       itg = np.asarray(imgTrainGray)
cv2.imshow ('compiled',itg)
</code></pre>

<p>However, all these images are stored as a list. When I convert the list to a numpy array using</p>

<pre><code>itg = np.asarray(imgTrainGray)
</code></pre>

<p>I do get an array of images, unfortunately which I am not able to display using</p>

<pre><code>cv2.imshow ('compiled',itg)
</code></pre>

<p>The error I get is: 'mat data type = 17 is not supported' at the function cv2.imshow.</p>

<p>I am aware that if all the images are of uniform shape, I could have used numpy.concatenate. Anyhow, the aim is to combine 8 different images to one and to extract keypoints and feature descriptors from this image as a query image for detecting an object from a webcam stream.</p>

<p>I am not able to figure out how to solve the issue so that I have an usable composite image of many individual images.</p>

<p>Or, is there an entirely different way to accomplish what I mean to do?</p>

<p>Full code for reference is here:</p>

<pre><code>import numpy as np
import cv2
import os
from matplotlib import pyplot as plt


ESC=27   
camera = cv2.VideoCapture(0)

cv2.ocl.setUseOpenCL(False)# workaround

det = cv2.xfeatures2d.SURF_create(80)
bf = cv2.BFMatcher(cv2.NORM_L2, crossCheck=False)
thresfac = 1.5

imgTrainGray = list ()

mypath = os.path.dirname ('directoy/to/images')
for item in os.listdir(mypath):
    if '.png' in item:  # this could be more correctly done with os.path.splitext
        image = cv2.imread((os.path.join(mypath, item)),0)
        if image is not None:
           imgTrainGray.append(image)
           itg = np.asarray(imgTrainGray)
#itg = np.array(cv2.imread(item) for item in os.listdir(mypath) if '.png' in item )

cv2.imshow ('compiled',itg)

kpTrain, desTrain = det.detectAndCompute (imgTrainGray,None)

while True:

    ret, imgCamColor = camera.read()
    imgCamGray = cv2.cvtColor(imgCamColor, cv2.COLOR_BGR2GRAY)

    kpCam, desCam = det.detectAndCompute (imgCamGray,None)

    matches = bf.match(desTrain,desCam)

    dist = [m.distance for m in matches]
    #print ('distances = ',dist)
    thres_dist = (sum(dist) / len(dist)) * thresfac
    print('sum of distance = ',sum(dist))
    print('length of distance = ',len(dist))
    print('threshold distance = ',thres_dist)
    good = [m for m in matches if m.distance &lt; thres_dist]   


    MIN_MATCH_COUNT = 8
    if len(good)&gt;MIN_MATCH_COUNT:
        src_pts = np.float32([ kpTrain[m.queryIdx].pt for m in good ]).reshape(-1,1,2)
        dst_pts = np.float32([ kpCam[m.trainIdx].pt for m in good ]).reshape(-1,1,2)

        M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)
        matchesMask = mask.ravel().tolist()

        h,w = imgTrainGray.shape[:2]
        #pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
        pts = np.float32([ [0,0],[0,h],[w,h],[w,0] ]).reshape(-1,1,2)
        dst = cv2.perspectiveTransform(pts,M)
        imgCamGray = cv2.polylines(imgCamColor,[np.int32(dst)],True,(200,150,120),2, cv2.LINE_8)

    else:
        print ('Not enough matches are found - %d/%d' % (len(good),MIN_MATCH_COUNT))
        matchesMask = None

    draw_params = dict(matchColor = (0,255,0), # draw matches in green color
                        singlePointColor = None,
                        matchesMask = matchesMask, # draw only inliers
                        flags = 2)

    img3 = cv2.drawMatches(imgTrainGray,kpTrain,imgCamColor,kpCam,good,None,**draw_params)

    cv2.imshow('matches',img3)

    key = cv2.waitKey(20)                                 
    if key == ESC:
        break

cv2.destroyAllWindows()
camera.release()
</code></pre>