如何使用 openCV 保存裁剪后的图像?

How to save cropped image using openCV?

我正在使用 python 中的 opencv 和 face_recognition 库捕获图像, 这是我的程序片段

while True:
ret,frame = video_capture.read()
rgb_frame = frame[:,:,::-1]
face_locations = fr.face_locations(rgb_frame)
face_encodings = fr.face_encodings(rgb_frame,face_locations)
cv2.imwrite('test.png', frame)
if len(face_encodings)!=0:
    print(face_locations)
    temp = list(face_locations[0])
    img = cv2.imread('test.png')
    cropped = img[temp[0]:temp[2],temp[0]:temp[1]]
    cv2.imshow("cropped", cropped)
    cv2.imwrite('face.png',cropped)

我正在尝试将裁剪后的图像保存到我的目录中。但是上面的代码不起作用。你知道如何使用 opencv 保存裁剪后的图像吗?

试试这个 cv2.imwrite('path/to/face.png',cropped)