问题遇到的现象和发生背景
opencv python 截取图像部分区域保存报错215
问题相关代码,请勿粘贴截图
#计算轮廓外接矩形
for cnt in contours:
x,y,w,h=cv2.boundingRect(cnt)
tmp3=frame.copy()
if x>0 and x+w<640 and w*h>300:
c=c+1
rest=cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
rest1=cv2.rectangle(opening,(x,y),(x+w,y+h),(255,255,255),1)
rest2=cv2.rectangle(opening1,(x,y),(x+w,y+h),(255,255,255),1)
gray = cv2.cvtColor(rest, cv2.COLOR_BGR2GRAY)
cv2.imwrite("E:/pythonpractice/xiamiaopicture/{}.jpg".format(str(c)),frame)
frame2=cv2.imread("E:/pythonpractice/xiamiaopicture/{}.jpg".format(str(c)))
img_cut = frame2[x:x + w, y:y + h]
cv2.imwrite("E:/pythonpractice/xiamiaocut/{}.jpg".format(str(c)), img_cut)
运行结果及报错内容
Traceback (most recent call last):
File "E:/pythonpractice/虾苗计数-图像截取/main.py", line 82, in
cv2.imwrite("E:/pythonpractice/xiamiaocut/{}.jpg".format(str(c)), img_cut)
cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-u4kjpz2z\opencv\modules\imgcodecs\src\loadsave.cpp:803: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'
我的解答思路和尝试过的方法
尝试过更改路径中的中文,可以保存原视频中的每一帧的整体图片,但是截取的部分保存不了,截取的部分没有超过图像产宽范围。