今天调试下面这段代码的时候遇到了个小问题,在自己多次调试后终于找到解决方案,下面分享给大家:
调试代码:
res_path='G:/coding_data/Model_Zoo/PoolNet/training_testing_data/DUTS-TE/DUTS-TE-Results/'
gt_path='G:/coding_data/Model_Zoo/PoolNet/training_testing_data/DUTS-TE/DUTS-TE-Mask/'
res_list=os.listdir(res_path)
#print(res_list)
mae=[]
for i in range(len(res_list)):
#print('res_list',res_list[i])
r_name=res_path+res_list[i]
#print('r_name:',r_name)
g_name=gt_path+res_list[i][:-13]+'.png'
#print('res_list[i]:', res_list[i])
#print(g_name)
res=cv2.imread(r_name)
h,w,_=res.shape
res = cv2.cvtColor(res, cv2.COLOR_RGB2GRAY)
res=res/255
gt=cv2.imread(g_name)
gt = cv2.cvtColor(gt, cv2.COLOR_RGB2GRAY)
gt=gt/255
mae.append(sum(sum(abs(res-gt)))/(h*w))
print(sum(sum(abs(res-gt)))/(h*w))
return sum(mae)/len(mae)
问题:
cv2.error: OpenCV(3.4.2) c:\projects\opencv-python\opencv\modules\imgproc\src\color.hpp:253: error: (-215:Assertion failed) VScn::contains(scn) && VDcn::contains(dcn) && VDepth::contains(depth) in function 'cv::CvtHelper<struct cv::Set<3,4,-1>,struct cv::Set<1,-1,-1>,struct cv::Set<0,2,5>,2>::CvtHelper'
针对该问题,查阅博客给出的方案都是检查路径名称,一般是路径名称不对。
我在多次检查确认路径名称后,发现代码的路径并没有问题,最后通过输出每次读入 r_name和 g_name的图片名称找到问题所在:
该问题产生的原因为:
两个路径下图片不对应,即:res_path 和 gt_path 下的图片不是一一对应的,存在缺少图片的情况
解决方案:
删掉文件夹中缺少的图片或者补充多余的图片即可