Ask Your Question
0

Edit video frames and write C++

asked 2018-04-21 13:42:51 -0500

bossu12 gravatar image

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 << ...
(more)
edit retag flag offensive close merge delete

Comments

uncomment line :

    //cvtColor(frame, frame, CV_BGR2GRAY);
//  GaussianBlur(frame, frame, Size(7, 7), 1.5, 1.5);
//  Canny(frame, frame, 0, 30, 3);

and add

cvtColor(frame,frame,CV_GRAY2BGR)

video frame must be in color

LBerger gravatar imageLBerger ( 2018-04-21 13:50:18 -0500 )edit

"it does not work." -- WHAT does not work ? what do you expect to happen ? be more clear here, please.

berak gravatar imageberak ( 2018-04-22 02:05:37 -0500 )edit

"F: / WAŻNE / Praca magisterska / Projekt pokrycie powierzchni / pokrycie powierzchni2 / Data / output.avi" -- that's not a valid path (you have to remove the spaces)

berak gravatar imageberak ( 2018-04-22 02:06:46 -0500 )edit

no problem with space here (opencv 4-dev with opencv-ffmpeg and openh264):

VideoCapture vin("f:/Mes video/CloneOpencv.mp4");
if (vin.isOpened())
{
    Mat frame;
    vin >> frame;
    cout << "Video Opened\n";
    VideoWriter vout;
    vout.open("f:/Mes video/Clone Opencv bis.mp4", VideoWriter::fourcc('H', '2', '6', '4'),20.,frame.size());
    if (vout.isOpened())
    {
        for (int i = 0; i < 100; i++)
        {
            vin >> frame;
            vout << frame;
        }
        vout.release();

    }
    else
        cout << "cannot open Video for writting\n";

}
else
{
    cout << "cannot open Video\n";
}
vin.release();
LBerger gravatar imageLBerger ( 2018-04-22 04:14:09 -0500 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2018-04-23 13:42:49 -0500

bossu12 gravatar image

"video frame must be in color", thank you LBerger. This is what did not work.

But now is the second question. How can I edit this video in such a way that it detects the occurrence of a given color and permanently changes the color of this pixel, eg black?

I detect a particular color, but with the new frame, color detection is anew in a new situation. I would like the previously detected pixel to be still in the given color.

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

if (!cap.isOpened())  // if not success, exit program
{
    cout << "ERROR: Cannot open the video file" << endl;
    return -1;
}

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


            //Analiza obrazu, i wpisanie elementow obrazu do macierzy pozycji
            for (int i = 0; i < frame.rows; i++) {
                // Podział obrazu na czesci, jesli w wyznaczonej czesci znaleziono ktorys z kolorow robotow to przyporzadkowanie kolejno warto 2,3,4 w macierzy pozycji
                for (int j = 0; j < frame.cols; j++) {
                    if (frame.at<Vec3b>(i, j)[0] >= 192 && frame.at<Vec3b>(i, j)[1] <= 130 && frame.at<Vec3b>(i, j)[2] <= 100) {
                        frame.at<Vec3b>(i, j)[0] = 255;
                        frame.at<Vec3b>(i, j)[1] = 255;
                        frame.at<Vec3b>(i, j)[2] = 255;
                            break;
                    }
                    if (frame.at<Vec3b>(i, j)[1] >= 150 && frame.at<Vec3b>(i, j)[0] <= 130 && frame.at<Vec3b>(i, j)[2] <= 100) {
                        frame.at<Vec3b>(i, j)[0] = 255;
                        frame.at<Vec3b>(i, j)[1] = 255;
                        frame.at<Vec3b>(i, j)[2] = 255;
                        break;
                    }
                    if (frame.at<Vec3b>(i, j)[2] >= 160 && frame.at<Vec3b>(i, j)[0] <= 130 && frame.at<Vec3b>(i, j)[1] <= 80) {
                        frame.at<Vec3b>(i, j)[0] = 255;
                        frame.at<Vec3b>(i, j)[1] = 255;
                        frame.at<Vec3b>(i, j)[2] = 255;
                        break;
                    }

                }
            }



    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 ...
(more)
edit flag offensive delete link more

Comments

use inRange function to get a mask and the use setTo to change pixel color

LBerger gravatar imageLBerger ( 2018-04-23 13:57:10 -0500 )edit

could you show it by example, or by my code? Because I still do not know how to cause the changed pixel value to be changed once and for all. The mask only works like a filter for a specific color.

bossu12 gravatar imagebossu12 ( 2018-04-24 15:27:41 -0500 )edit

this is how I use these two functions. Unfortunately, this does not work correctly because the mask detects the color, but the pixel values are not permanent. With each subsequent frame, the detected color disappears and will appear in a different place when the robot moves. How to make the changed pixel color stay until the end of the video.

inRange(frame, Scalar(180, 0, 0), Scalar(255, 130, 100), frame_czerwony);

frame.setTo(Scalar(0, 0, 0), frame);

Thank you for your help

bossu12 gravatar imagebossu12 ( 2018-04-30 13:08:36 -0500 )edit

If color is not constant you can update color using moving average

LBerger gravatar imageLBerger ( 2018-04-30 14:03:46 -0500 )edit

It is also no working, the color changes only for a moment and then disappears.

bossu12 gravatar imagebossu12 ( 2018-05-02 11:40:38 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2018-04-21 13:41:57 -0500

Seen: 2,346 times

Last updated: Apr 21 '18