python - OpenCV python 错误 (-215) 照片找到 50% 的时间

标签 python opencv

使用 Opencv 3.1.0-dev,python 2.7.3 通过 OS X 终端运行

我正在通过自己制作的拼接程序运行图像,效果很好。我使用 argparse 来制作它,所以我只需要传递一个文件夹位置,它就会使用该文件夹中的所有照片进行拼接。我有两个测试图像,效果很好,并在不同的文件夹中使用不同数量的图像制作了一些其他示例,并且工作正常,但我现在遇到了一个问题,我收到了这个错误:

img1 =  cv2.resize(cv2.imread(os.path.join(path,imagesToStitch[0]),1),imageSize)
cv2.error: /Users/chrisradford/opencv/modules/imgproc/src/imgwarp.cpp:3490: error: (-215) ssize.width > 0 && ssize.height > 0 in function resize.

这是我的代码:

import os
import cv2
import argparse
from StitchingMaster import Stitcher

#initalize objects
stitcher = Stitcher()
ap = argparse.ArgumentParser()
ap.add_argument("-1", "--first", required=True)
args = vars(ap.parse_args())
#Define variables
imageSize = (1800,1200)                     #size of image to be passed      to stitcher
showMatches = False                         #True if wish to see   matches; False otherwise
keypoints = []
descriptors = []
resultImageSize = (1200,900)                #Size of final image to be displated and saved
imagesToStitch = os.listdir(args["first"])  #list of images in folder
path = os.path.abspath(args["first"])       #Folder path

#----Base Case[0-1]----#
if len(imagesToStitch) < 2:
    print "Not enough images to stitch"
    quit()
#----Base Case[2]----#
else:
    img1 =  cv2.resize(cv2.imread(os.path.join(path,imagesToStitch[0]),1),imageSize)
    img2 = cv2.resize(cv2.imread(os.path.join(path,imagesToStitch[1]),1),imageSize)
    #result = stitched image
    (result,keypoints,descriptors) = stitcher.stitch([img1,img2],showMatches,keypoints,descriptors)

错误出现在

之后
else: 
    img1 = cv2.resize....

我制作了另一个程序,可以在那个位置打开图像并调整它的大小,它工作正常。当我使用 os.path.abspath()os.listdir() 时,会不会是文件夹的命名约定搞砸了?

非常感谢任何帮助。

最佳答案

经过进一步分析,我能够确定问题出在:

os.listdir(args["first"])

此功能还可以提取隐藏文件。由于我在 OS X 上运行,它还提取了名为的隐藏文件:

.DS_Store 

为了解决这个问题,我创建了一个简单的 for 循环检查器,用于删除任何以“.”开头的此类文件。从我的列表中。

 for files in fileList:
     if not files.startswith('.'):
         imagesToStitch.append(files)

关于python - OpenCV python 错误 (-215) 照片找到 50% 的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41666634/

相关文章:

python - 具有浮点索引的二维矩阵的径向轮廓

一旦找到解决方案,Python就停止递归

c++ - OpenCV cv::findcontours StackOverflow 异常

java - 标准化图像描述符 OpenCV Java

python - 列表索引必须是整数,而不是列表 - 在 python 中使用 Dict

python - 在 Python 中合并两个文件并排序

python - 机器学习算法将结果存储在哪里?

opencv - 您需要多久为 ArUco 进行一次相机校准?

python - 无法使用opencv获得我想要的分辨率

opencv - 在 Raspberry Pi 2 上使用 TBB 编译 OpenCV 3