Ask Your Question
0

OpenCV video writer changes video on save

asked 2014-06-04 00:25:29 -0600

I trying to load video, do some color manipulations and save it.

I change first color component, save video, open it again, but color component is different now!

int main(int argv, char * argc) {
    std::string inVideo = "sample_sorenson.mov";
    std::string outVideo = "out.avi";



    cv::VideoCapture capturer(inVideo);
    if (!capturer.isOpened()) {
        std::cerr << "Failed to open video file";
        return 1;
    }



    cv::VideoWriter writer;
    writer.open(outVideo,
        CV_FOURCC_DEFAULT,
        capturer.get(CV_CAP_PROP_FPS),
        cv::Size(capturer.get(CV_CAP_PROP_FRAME_WIDTH), capturer.get(CV_CAP_PROP_FRAME_HEIGHT)));
    if (!writer.isOpened()) {
        std::cerr << "Failed to write video file";
        return 1;
    }



    cv::Mat frame;
    while (capturer.read(frame))
    {
        for (auto frameIt = frame.begin<cv::Vec3b>(), frameEnd = frame.end<cv::Vec3b>();
            frameIt != frameEnd;
            ++frameIt)
        {
            (*frameIt)[0] = 0;  // some actions here, but for simplification just 0
        }
        writer.write(frame);
    }
    capturer.release();
    writer.release();



    cv::VideoCapture outCapturer(outVideo);  // open written video
    if (!outCapturer.isOpened()) {
        std::cerr << "Failed to open video file";
        return 1;
    }
    while (outCapturer.read(frame))
    {
        for (auto frameIt = frame.begin<cv::Vec3b>(), frameEnd = frame.end<cv::Vec3b>();
            frameIt != frameEnd;
            ++frameIt)
        {
            assert((*frameIt)[0] == 0);  // NOT ZERO!!! WHY?!?!?!
        }
    }
}

The color is changing, but [0] is not zero... Please help!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-06-04 12:51:08 -0600

Most of video compressing encoders process video in YUV color space, but not in RGB. Also video compression in most cases is loosy compression. It means that encoder changes pixel values to decrease data entropy and adopt frames for better compression. It is normal behavior.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-06-04 00:25:29 -0600

Seen: 530 times

Last updated: Jun 04 '14