Ask Your Question

Revision history [back]

OpenCV video writer changes video on save

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!