error: (-215:Assertion failed)的两种解决方案

问题描述:在对视频分帧读取进行差值哈希算法比较时出现读取错误现象,具体代码内容和报错如下:

import os
import cv2
import numpy as np
import subprocess
import matplotlib.pyplot as plt

os.chdir(r'e:/users/may')#固定工作文件路径
v_path='ghz.mp4'
image_save='./pic/' #新建一个所有图片的文件
cap=cv2.VideoCapture(v_path)
frame_count=cap.get(cv2.CAP_PROP_FRAME_COUNT)

for i in range(int(frame_count)):
    _,img=cap.read()
    cv2.imwrite(r'./pic/image{}.jpg'.format(i),img) #用img代替{}

def dHash(img):
    img=cv2.resize(img,(9,8))
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    hash_str=''
    for i in range(8):        
        for j in range(8):
            if gray[i,j]>gray[i,j+1]:
                hash_str=hash_str+'1'
            else:
                hash_str=hash_str+'0'
    return hash_str

#遍历pic中所有图片,比较前后张图片相似度,画图
diff=[]
_,img1=cap.read()
for i in range(int(frame_count)-1):
    _,img2=cap.read()
    hash1=dHash(img1)
    hash2=dHash(img2)
    n=cmpHash(hash1,hash2)
    diff.append(n)
    img1=img2

x=np.arange(0,len(diff),1)
print(len(x))
print(len(diff))
plt.plot(x,diff,'g-')
plt.show()
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_11856/1932059239.py in 
     32 for i in range(int(frame_count)-1):
     33     _,img2=cap.read()
---> 34     hash1=dHash(img1)
     35     hash2=dHash(img2)
     36     n=cmpHash(hash1,hash2)

~\AppData\Local\Temp/ipykernel_11856/1932059239.py in dHash(img)
     16 
     17 def dHash(img):
---> 18     img=cv2.resize(img,(9,8))
     19     gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
     20     hash_str=''

error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\resize.cpp:4052: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

而在差值哈希算法运行前,均值哈希算法可以正常运行:

import os
import cv2
import numpy as np
import matplotlib.pyplot as plt

def aHash(img):
    img=cv2.resize(img,(8,8))
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    hash_str=''
    avg=np.mean(gray)
    for i in range(8):
        for j in range(8):
            if gray[i,j]>avg:
                hash_str=hash_str+'1'
            else:
                hash_str=hash_str+"0"
    return hash_str

def cmpHash(hash1,hash2):
    n=0
    if len(hash1)!=len(hash2):
        return -1
    for i in range(len(hash1)):
        if hash1[i]!=hash2[i]:
            n=n+1
    n=n/len(hash1)
    return n

os.chdir(r'e:/users/may')#固定工作文件路径
v_path='ghz.mp4'
if not os.path.exists("./images"):
    os.mkdir("./images")
imge_save='./images'

cap=cv2.VideoCapture(v_path)
frame_count=cap.get(cv2.CAP_PROP_FRAME_COUNT)
print(frame_count)
diff=[]
_,img1=cap.read()
for i in range(int(frame_count)-1):
    _,img2=cap.read()
    hash1=aHash(img1)
    hash2=aHash(img2)
    n=cmpHash(hash1,hash2)
    diff.append(n)
    if n>0.3:
        #print(n)
        cv2.imwrite(r'./images/image{}.jpg'.format(i),img1)
    #print(i)
    img1=img2

x=np.arange(0,len(diff),1)
print(len(x))
print(len(diff))
plt.plot(x,diff,'r-')
plt.show()

反复仔细观察上述两段代码之后判断是img图片路径读取失败,下方为两种解决方法:

1. 删除报错语句的下方部分:

for i in range(int(frame_count)):
    _,img=cap.read()
    cv2.imwrite(r'./pic/image{}.jpg'.format(i),img) #用img代替{}

2. 将循环调用的部分加入地址限制:

import os
import cv2
import numpy as np
import subprocess
import matplotlib.pyplot as plt

def dHash(img):
    img=cv2.resize(img,(9,8))
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    hash_str=''
    for i in range(8):        
        for j in range(8):
            if gray[i,j]>gray[i,j+1]:
                hash_str=hash_str+'1'
            else:
                hash_str=hash_str+'0'
    return hash_str

#遍历pic中所有图片,比较前后张图片相似度,画图
diff1=[]
for i in range(int(frame_count)-1):
    img1=cv2.imread('./pic/image{}.jpg'.format(i))
    img2=cv2.imread('./pic/image{}.jpg'.format(i+1))
    hash1=dHash(img1)
    hash2=dHash(img2)
    n=cmpHash(hash1,hash2)
    diff1.append(n)
    
x1=np.arange(0,len(diff1),1)
print(len(x1))
print(len(diff1))
plt.plot(x1,diff1,'g-')
plt.show()

值得注意的是如果需要用cap.read()读取图片,一定不能缺少以下语句:

cap=cv2.VideoCapture(v_path)
frame_count=cap.get(cv2.CAP_PROP_FRAME_COUNT)

但是如果用cv2.imread就无需

你可能感兴趣的:(python,开发语言)