Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

frames video frames and write C++

Hello, I would like to rewrite the video from the link in such a way as to mark the route of each of the colors of the robots. I load my video, edit it, and try to save, it does not work. However, when I do not do anything about this video, the recording proceeds normally. How can I edit the code in such a way that it detects a given color, and it has its appearance on the screen until the end of the duration of the video creating a robot route? This is video: MyVideo This is my code:

using namespace cv;

using namespace std;

string intToString(int number) {

std::stringstream ss;

ss << number;

return ss.str();

}

int main(int argc, char* argv[]) {

bool recording = false;

bool startNewRecording = false;

int inc = 0;

bool firstRun = true;


VideoCapture cap("F:/WAŻNE/Praca magisterska/Projekt pokrycie powierzchni/pokrycie powierzchni2/Data/44.mp4");
VideoWriter oVideoWriter;//create videoWriter object, not initialized yet


namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video

cout << "Frame Size = " << dWidth << "x" << dHeight << endl;

//set framesize for use with videoWriter
Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));


for (;;) {
    Mat frame;

    bool bSuccess = cap.read(frame); // read a new frame from video
    cap >> frame; // get a new frame from camera
    //cvtColor(frame, frame, CV_BGR2GRAY);
//  GaussianBlur(frame, frame, Size(7, 7), 1.5, 1.5);
//  Canny(frame, frame, 0, 30, 3);
    if (!bSuccess) //if not success, break loop
    {
        cout << "ERROR: Cannot read a frame from video file" << endl;
        break;
    }
    if (startNewRecording == true) {


        oVideoWriter = VideoWriter("F: / WAŻNE / Praca magisterska / Projekt pokrycie powierzchni / pokrycie powierzchni2 / Data / output.avi" + intToString(inc) + ".avi", CV_FOURCC('D', 'I', 'V', '3'), 20, frameSize, true); //initialize the VideoWriter object 
        recording = true;
        startNewRecording = false;
        cout << "New video file created F:/MyVideo" + intToString(inc) + ".avi " << endl;


        if (!oVideoWriter.isOpened()) //if not initialize the VideoWriter successfully, exit the program
        {
            cout << "ERROR: Failed to initialize video writing" << endl;
            getchar();
            return -1;
        }

    }
    //if we're in recording mode, write to file
    if (recording) {

        oVideoWriter.write(frame);
        //show "REC" in top left corner in red
        //be sure to do this AFTER you write to the file so that "REC" doesn't show up
        //on the recorded video.
        putText(frame, "REC", Point(0, 60), 2, 2, Scalar(0, 0, 255), 2);


    }
    imshow("MyVideo", frame); //show the frame in "MyVideo" window
//  imshow("edges", edges);

    switch (waitKey(10)) {

    case 114:
        //'r' has been pressed.
        //toggle recording mode
        recording = !recording;

        if (firstRun == true) {

            cout << "New Recording Started" << endl;
            oVideoWriter = VideoWriter("F:/WAŻNE/Praca magisterska/Projekt pokrycie powierzchni/pokrycie powierzchni2/Data/output.avi", CV_FOURCC('D', 'I', 'V', '3'), 20, frameSize, true);

            if (!oVideoWriter.isOpened())
            {
                cout << "ERROR: Failed to initialize video writing" << endl;
                getchar();
                return -1;
            }
            firstRun = false;


        }
        else {
            if (!recording)cout << "Recording Stopped" << endl;

            else cout << "Recording Started" << endl;
        }
        break;

    case 110:
        //'n' has been pressed
        //start new video file
        startNewRecording = true;
        cout << "New Recording Started" << endl;
        //increment video file name
        inc += 1;
        break;
    case 27:
        //'esc' has been pressed
        //exit program.
        cout << "Exit Program" << endl;
        return 0;



    }


}

return 0;

}