我可以使用dcpm无损地解码视频

发布于 2025-02-02 12:53:04 字数 2562 浏览 2 评论 0原文

我正在尝试使用DCPM技术无误地编码和解码视频,该技术指出您将视频的第一帧发送为原样,然后将差异计算为currentframe -POSTERFRAME -POSTER FRAME FRAME和您发送此序列,直到视频已完全编码为止。在第一个文件中,我这样做,似乎可以正常工作。

现在,当我尝试解码时,我显示第一个帧,然后计算“ FirstFrame + NextFrame”,并显示此序列,直到结束,这应该导致真实视频,因为第一个帧完好无损,下一个是在第二帧和第一帧。但是,当我这样做的过程中,我会收到一堆噪音,并且视频并未无误地解码。这是为什么?

以下是编码器

import cv2
import numpy as np

video = cv2.VideoCapture('video.mp4')
previous_frame = None

video_codec = cv2.VideoWriter_fourcc(*'XVID')

# Setup the output
output = cv2.VideoWriter('dpcm_encoded_video.avi', video_codec, int(video.get(5)),
(int(video.get(3)), int(video.get(4))), False)

if not video.isOpened():
    print("Error while trying to play the video.")
i = 0
while video.isOpened():
    not_exception, current_frame = video.read()
    if not_exception:
        if previous_frame is None:  # First iteration
            current_frame = cv2.cvtColor(current_frame, cv2.COLOR_RGB2GRAY)
            previous_frame = current_frame
            output.write(current_frame)
            continue
        if i <= 217: #Video is 217 frames
            current_frame = cv2.cvtColor(current_frame, cv2.COLOR_RGB2GRAY)
            i += 1

        difference = current_frame - previous_frame
        cv2.imshow('Error frames', difference)
        output.write(difference)
        previous_frame = current_frame

        if cv2.waitKey(10) == ord('q'):
            break
    else:
        break

video.release()
cv2.destroyAllWindows()

和解码器

import cv2
# Load video
video = cv2.VideoCapture('dpcm_encoded_video.avi')

# Load the first frame of the video
success, first_frame = video.read()
first_frame = cv2.cvtColor(first_frame, cv2.COLOR_BGR2GRAY)

# Set video codec
video_codec = cv2.VideoWriter_fourcc(*'XVID')

# Setup the output
output = cv2.VideoWriter('dpcm_decoded_video.avi', video_codec, int(video.get(5)), (int(video.get(3)), int(video.get(4))), False)
output.write(first_frame)

frame_num = 0

previous_frame = first_frame

while video.isOpened():
    # Skip the first frame because we load it outside the loop
    if frame_num == 0:
        #success, current_frame = video.read()
        frame_num += 1
        continue

    success, current_frame = video.read()
    frame_num += 1

    if not success:
        break

    # Frame to BW
    current_frame = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY)

    # Sum frames
    current_frame = previous_frame + current_frame

    previous_frame = current_frame
    output.write(current_frame)

video.release()
output.release()

I am trying to encode and decode a video losslessly using the DCPM technique which states that you send the first frame of a video as it is and then you calculate the difference to the next frame as currentFrame - previousFrame and you send this sequence until the video has been fully encoded. In the first file I do this and it seems to work fine.

Now when I try to decode, I display the first frame and then compute `firstFrame + nextFrame' and display this sequence until the end which should result in the real video since the first frame is sent intact and the next one is the difference between the second frame and the first frame. But as I do this process I get a bunch of noise and the video isn't decoded losselessly. Why is that?

Below is the encoder

import cv2
import numpy as np

video = cv2.VideoCapture('video.mp4')
previous_frame = None

video_codec = cv2.VideoWriter_fourcc(*'XVID')

# Setup the output
output = cv2.VideoWriter('dpcm_encoded_video.avi', video_codec, int(video.get(5)),
(int(video.get(3)), int(video.get(4))), False)

if not video.isOpened():
    print("Error while trying to play the video.")
i = 0
while video.isOpened():
    not_exception, current_frame = video.read()
    if not_exception:
        if previous_frame is None:  # First iteration
            current_frame = cv2.cvtColor(current_frame, cv2.COLOR_RGB2GRAY)
            previous_frame = current_frame
            output.write(current_frame)
            continue
        if i <= 217: #Video is 217 frames
            current_frame = cv2.cvtColor(current_frame, cv2.COLOR_RGB2GRAY)
            i += 1

        difference = current_frame - previous_frame
        cv2.imshow('Error frames', difference)
        output.write(difference)
        previous_frame = current_frame

        if cv2.waitKey(10) == ord('q'):
            break
    else:
        break

video.release()
cv2.destroyAllWindows()

And the decoder

import cv2
# Load video
video = cv2.VideoCapture('dpcm_encoded_video.avi')

# Load the first frame of the video
success, first_frame = video.read()
first_frame = cv2.cvtColor(first_frame, cv2.COLOR_BGR2GRAY)

# Set video codec
video_codec = cv2.VideoWriter_fourcc(*'XVID')

# Setup the output
output = cv2.VideoWriter('dpcm_decoded_video.avi', video_codec, int(video.get(5)), (int(video.get(3)), int(video.get(4))), False)
output.write(first_frame)

frame_num = 0

previous_frame = first_frame

while video.isOpened():
    # Skip the first frame because we load it outside the loop
    if frame_num == 0:
        #success, current_frame = video.read()
        frame_num += 1
        continue

    success, current_frame = video.read()
    frame_num += 1

    if not success:
        break

    # Frame to BW
    current_frame = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY)

    # Sum frames
    current_frame = previous_frame + current_frame

    previous_frame = current_frame
    output.write(current_frame)

video.release()
output.release()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文