【问题标题】:openCV imwrite writing 0 kb image framesopenCV imwrite 写入 0 kb 图像帧
【发布时间】:2018-03-02 05:10:46
【问题描述】:

我正在运行以下代码将视频转换为帧。问题是它正在创建大小为 0 KB 的图像文件,当我打开它时没有显示任何内容。我不明白是什么造成了问题。我需要安装任何图像编解码器吗?

    '''
    Using OpenCV takes a mp4 video and produces a number of images. I am using OpenCV 3.3.0 version and Python 2.7


    Which will produce a folder called data with the images, There will be 2000+ images for example.mp4.
    '''
    import cv2
import numpy as np
import os

# Playing video from file:

try:
        cap = cv2.VideoCapture('aa.mkv')
except:
        print "Could not open video file"
        raise
print cap.grab()

try:
    if not os.path.exists('data'):
        os.makedirs('data')
except OSError:
    print ('Error: Creating directory of data')

currentFrame = 0
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    if not frame is None:
        # Saves image of the current frame in jpg file
        name = './data/frame' + str(currentFrame) + '.jpg'
        print ('Creating...' + name)
        cv2.imwrite(name, frame)
        #cv2.imshow(name, frame)
    else:
        break

    # To stop duplicate images
    currentFrame += 1

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

【问题讨论】:

  • 你的 imwrite 命令在哪里?
  • 如果您检查您的frame,您会发现它是空白的。或者,您可以检查您的 ret 并查看它是 False,即未读取该帧。
  • ret 也不返回任何东西。
  • 我的程序是正确的,我错过了在问题中包含 imwrite() 。实际问题出在 OpenCV 版本上。 pip install opencv-python 现在一切运行良好。

标签: python-2.7 opencv opencv3.3


【解决方案1】:

您没有使用 imwrite 函数来写入帧。此外,您的 imshow 函数名称拼写错误。我已经对您的代码进行了更改。试试这个:

import cv2
import numpy as np
import os

# Playing video from file:
cap = cv2.VideoCapture('aa.mkv')

try:
    if not os.path.exists('data'):
        os.makedirs('data')
except OSError:
    print ('Error: Creating directory of data')

currentFrame = 0
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    if not frame is None:
        # Saves image of the current frame in jpg file
        name = './data/frame' + str(currentFrame) + '.jpg'
        print ('Creating...' + name)
        cv2.imwrite(name, frame)
        cv2.imshow(name, frame)
    else:
        break

    # To stop duplicate images
    currentFrame += 1

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

【讨论】:

  • 我猜 OP 的代码中有 imwrite,这就是 w 的来源。
  • 我收到此错误。 cv2.error: C:\build\master_winpack-bindings-win32-vc14-static\opencv\modules\highgui\src\window.cpp:325: 错误: (-215) size.width>0 && size.height>0在函数 cv::imshow
  • 我对代码进行了更改。现在检查一下。还要检查视频的路径是否正确。
  • 本次程序执行没有任何错误,但没有输出。它没有写任何帧。编辑之前的代码只写了一个 0 字节的图像 frame0。视频的位置是正确的,它与我的程序运行的位置相同。
  • 尝试在任何其他视频上运行代码并检查它是否正常工作。我已经对其进行了测试,在我的视频上运行良好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-17
  • 1970-01-01
  • 1970-01-01
  • 2016-08-28
  • 2017-07-07
相关资源
最近更新 更多