我试图在python 3.5 opencv 2上将8个不同的.png图像读成numpy数组。
import numpy as np
import cv2
import os
使用'os'类方法,我可以从目录中选择所有.png文件,并使用cv2.imread读取它们。
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)
但是,所有这些图像都存储为列表。当我使用
将列表转换为numpy数组时itg = np.asarray(imgTrainGray)
我确实得到了一系列图像,遗憾的是我无法使用
显示cv2.imshow ('compiled',itg)
我得到的错误是:在函数cv2.imshow中不支持“mat data type = 17”。
我知道如果所有图像都是统一的形状,我可以使用numpy.concatenate。无论如何,目标是将8个不同的图像组合成一个,并从该图像中提取关键点和特征描述符作为查询图像,用于从网络摄像头流中检测对象。
我无法弄清楚如何解决问题,以便我有一个可用的合成图像,包含许多单独的图像。
或者,是否有完全不同的方式来实现我的意思?
完整的参考代码在这里:
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 < thres_dist]
MIN_MATCH_COUNT = 8
if len(good)>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()