今天学习图像处理的时候出现了问题,照着书上的代码打了一遍竟然报错了,思考了好久也不知道怎么处理,希望各位帮帮忙
import cv2
import os
import sys
PIC_PATH = "Screenshots\\"
pic_file=os.listdir(PIC_PATH)
width,height=100,100
same_pic_index=[]
imgs=[]
has_same=set()
count=len(pic_file)
if count==0:
print("没有图像")
sys.exit(0)
for file_name in pic_file:
pic_name=PIC_PATH+file_name
img=cv2.imread(pic_name)
img=cv2.resize(img,(width,height))
imgs.append(img)
for i in range(0,count-1):
if i in has_same:
continue
templ=imgs[i]
same=[i]
for j in range(0+i+1,count):
if j in has_same:
continue
pic=imgs[j]
results=cv2.matchTemplate(pic,templ,cv2.TM_CCORR_NORMED)
if results>0.9:
same.append(j)
has_same.add(i)
has_same.add(j)
if len(same)>1:
same_pic_index.append(same)
for same_list in same_pic_index:
text="相同的照片:"
for same in same_list:
text+=str(pic_file[same])+","
print(text)
报错文字版:
C:\Users\admin\.conda\envs\pytorch2\python.exe E:\pythonProject2\imagetest.py
Traceback (most recent call last):
File "E:\pythonProject2\imagetest.py", line 21, in <module>
img=cv2.resize(img,(width,height))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\resize.cpp:4062: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'
根据提供的参考资料和问题描述,无法确定报错信息与OpenCV单模板匹配之间的关联。因此我无法给出具体的解决方案。