【问题标题】:conversion from opencv image to jpeg image in python在python中从opencv图像转换为jpeg图像
【发布时间】:2015-11-04 16:47:39
【问题描述】:

我正在从视频文件中抓取帧,如下所示:

def capture_frame(file):
        capture = cv.CaptureFromFile("video.mp4")
        cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_POS_MSEC)
        cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_POS_MSEC, 90000)
        frame = cv.QueryFrame(capture)
        return frame

帧类型为cv2.cv.iplimage。如何在不保存的情况下将这种类型的图像转换为 jpeg 图像?

谢谢,

【问题讨论】:

  • 我认为你只能在保存时转换为JPEG! JPEG 是压缩程序而不是 opencv 图像类型
  • 正如我在你之前的问题中已经提到的,你应该使用imencode
  • 我找不到一个很好的例子来说明我如何使用它。
  • 使用上述方法后出现分段错误错误

标签: python image opencv video-capture


【解决方案1】:

你试过只写块吗?

with open('filename.jpeg', wb+') as destination:
            for chunk in image_file.chunks():
                destination.write(chunk)

这里也有一个值得一看的,它本身就使用了 opencv http://answers.opencv.org/question/115/opencv-python-save-jpg-specifying-quality-gives-systemerror/。虽然,不确定您为什么要尝试将 .mp4 保存到 .jpeg...

【讨论】:

  • 安德鲁,你误解了我的意思。我不想保存文件。我必须抓取框架,并将其作为 jpg 文件作为参数发送到另一个函数。
  • 我希望你真的回答了最初的问题,因为我也被困在这个问题上。我需要将cv2.cv.iplimage 转换为 jpeg/array-of-bytes。到目前为止,我在堆栈溢出中找不到任何实际执行此操作的内容。
【解决方案2】:

以下内容应为您提供图像的 jpeg 表示的字节。

def capture_frame(file):
        capture = cv.CaptureFromFile(file)
        cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_POS_MSEC)
        cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_POS_MSEC, 90000)
        frame = cv.QueryFrame(capture)
        return cv.EncodeImage('.jpg', frame).tostring()

capture_frame("video.mp4")

我已将EncodeImage 的结果写入以二进制 (open('somefile','wb')) 打开的文件中,从而生成有效的 JPEG。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    • 2017-07-24
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 2012-11-14
    相关资源
    最近更新 更多