我正在尝试从多个视频中提取第一帧并将其保存在特定的文件夹中。现在我可以使提取部分正常工作了,但是我的保存是BGR而不是首选的RGB(如果我是对的话)。尽管这些帧在笔记本中显示为RGB,但不显示为BGR。另外我还需要添加一些可变的文件名,因为此刻它保存了帧,但仍覆盖相同的帧。你们能帮我解决两个具体问题吗?这是我到目前为止所得到的:
已解决:我得到了保存的工作文件,输出文件和颜色
img_rows,img_cols=200,200
listing = os.listdir(r'C:\Users/Me\SVWnew\archery\train')
# Create a counter
counter = 0
for vid in listing:
vid = r"C:/Users/Me/SVWnew/archery/train/"+vid
cap = cv2.VideoCapture(vid)
for k in range(1):
ret, frame = cap.read()
rgb =cv2.resize(frame,(img_rows,img_cols))
plt.imshow(rgb)
plt.xticks([]), plt.yticks([])
plt.show()
pathOut = r"C:/Users/Me/SVWnew - Copy/archery/train"
cv2.imwrite(pathOut + "/frame%d.jpg" % counter, rgb)
counter += 1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
答案 0 :(得分:1)
此代码将解决覆盖问题:
img_rows,img_cols=200,200
listing = os.listdir(r'C:\Users\Me\Desktop\SVWnew\archery\train')
# Create a counter
counter = 0
for vid in listing:
vid = r"C:/Users/Me/Desktop/SVWnew/archery/train/"+vid
cap = cv2.VideoCapture(vid)
for k in range(1):
ret, frame = cap.read()
frame=cv2.resize(frame,(img_rows,img_cols))
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
plt.imshow(rgb)
plt.xticks([]), plt.yticks([])
plt.show()
# Create a file name
currentFileName = "snapshot" + str(counter) + ".jpg"
# Save the image
cv2.imwrite(currentFileName, rgb)
# Increment the counter
counter = counter + 1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()