【发布时间】:2021-12-03 10:29:51
【问题描述】:
当我尝试导出以下视频时,它只显示一帧 12 秒,我想显示原始视频但它只显示一帧
下面附上代码:
ret, frame = day_video.read()
height, width = frame.shape[:2]
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('output_day.avi', fourcc, 20, (width,height))
while day_video.isOpened():
ret, frame = day_video.read()
if not ret:
out.release()
break
new_output = cv2.cvtColor(result, cv2.COLOR_GRAY2BGR)
out.write(new_output)
out.release()```
我得到的输出只有 12 秒的同一帧。 [![示例输出][1]][1]
我用来增强原始艺术品的代码
cap = cv2.VideoCapture('/content/drive/Shareddrives/Computer Vision/Assignment 2/Q1_day_video.avi')
while(cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
sobelx = cv2.Sobel(gray, cv2.CV_8U, 1, 0)
sobely = cv2.Sobel(gray, cv2.CV_8U, 0, 1)
sobel_all = cv2.add(sobelx, sobely)
(ret_otsu,thresh_otsu) = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
print(ret_otsu)
# add the threshold effect with sobel filter
result = cv2.add(sobel_all, thresh_otsu)
cv2_imshow(result)
break
cap.release()
cv2.destroyAllWindows()
我做了以下更改,但它只是保持无限加载
cap = cv2.VideoCapture('/content/drive/Shareddrives/Computer Vision/Assignment 2/Q1_day_video.avi')
ret, frame = cap.read()
height, width, _ = frame.shape
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('output_day.avi', fourcc, 20, (width,height))
while(cap.isOpened()):
ret, frame = cap.read()
sobelx = cv2.Sobel(gray, cv2.CV_8U, 1, 0)
sobely = cv2.Sobel(gray, cv2.CV_8U, 0, 1)
sobel_all = cv2.add(sobelx, sobely)
(ret_otsu,thresh_otsu) = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# add the threshold effect with sobel filter
result = cv2.add(sobel_all, thresh_otsu)
new_output = cv2.cvtColor(result, cv2.COLOR_GRAY2BGR)
new2_output = new2_output + new_output
if not ret:
out.release()
break
out.write(new3_output)
cap.release()```
[1]: https://i.stack.imgur.com/N0wCx.png
【问题讨论】:
-
您在第一个视频帧后打破循环...将
cv2_imshow(result)之后的break替换为if not ret:break。cv2_imshow的实现在哪里?它应该类似于cv2.imshow('result', result)cv2.waitKey(1) -
while(cap.isOpened()): ret, frame = cap.read() sobelx = cv2.Sobel(gray, cv2.CV_8U, 1, 0) sobely = cv2.Sobel(gray, cv2.CV_8U, 0, 1) sobel_all = cv2.add(sobelx, sobely) (ret_otsu,thresh_otsu) = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) result = cv2.add(sobel_all, thresh_otsu) new_output = cv2.cvtColor(result, cv2.COLOR_GRAY2BGR) new2_output = new2_output + new_output if not ret: out.release() break out.write(new3_output) cap.release()一直在无限加载,是不是我在特定部分出错了? -
@Rotem,cv_imshow 是 colabs 的替代品(不能在那里使用 opencv 的 gui)
标签: python opencv computer-vision opencv3.0