我想叠加一组给定的相同尺寸的图像(AT&T面部图像数据库)。我已编写代码来执行此操作,其工作方式如下:
错误:
imstack = cv2.resize(imstack,(97,113))cv2.error:/build/opencv-RI6cfE/opencv-2.4.9.1+dfsg1/modules/imgproc/src/imgwarp.cpp:1834:错误:(-215)ssize .area()> 0在函数调整大小
import cv2
import numpy as np
import os
fnames =['~/Downloads/1.pgm','~/Downloads/2.pgm','~/Downloads/3.pgm']
imstack=cv2.imread('~/Downloads/4.pgm')
imstack=cv2.resize(imstack,(97,113))
for path in fnames:
im=cv2.imread(os.path.expanduser(path))
imstack=cv2.addWeighted(imstack,0.5,im,0.5,0)
imstack=cv2.resize(imstack,(97,113))
cv2.imshow('compiledimg.jpg',imstack)
k = cv2.waitKey(0) & 0xFF
if k == 27:
cv2.destroyAllWindows()
elif k == ord('s'):
cv2.imwrite('compiledimg.jpg',imstack)
cv2.destroyAllWindows()
我能够摆脱这个错误。主要是我犯了两个错误:
其中4.pgm是基本图像,而2.pgm是我将使用测试的图像,所以我没有把它包含在将被覆盖的图像的路径中。
代码的临时部分如下:
path='test/'
#Appends all the absolute paths in the list image_paths
image_paths = [os.path.join(path, f) for f in os.listdir(path) if not f.endswith('2.pgm')]
#initializing a base_image over which other images will be superimposed
base_image=cv2.imread('4.pgm')
#resizing the base image so it matches the size of the database pics (113(rows),97(columns))
base_image=cv2.resize(base_image,(97,113))
#cv2.imshow('lol',base_image) //for testing purposes