Ask Your Question

Revision history [back]

Detection of Black Pixels in Video

Hi everyone!

I still have problems with the property FPS, because I can't get it autocatically.

[avi @ 0x8e14540] time base not set
Could not open the output video for write: /home/margarida/Output.avi

Additionally, my goal is only to find colour frames, i.e. I need to remove all the frames constituted with black pixels from my input videos. And I get the error:

Segmentation fault (core dumped)

Does anyone detects any error? Or as a solution for my problems? Thanks in advance.

My code is:

int main(int argc, char* argv[]) {
    VideoCapture cap;
    VideoWriter outputVideo;
    string name;
    Mat frame;
    int j;

    for (j = 1; j < argc; j++) {

        printf("%d: %s\n", j, argv[j]);

        string str1 = argv[j];
        unsigned found = str1.find_last_of("/");
        name = str1.substr(0, found);
        string newname = name + "/Output.avi";
        cout << newname << '\n';

        cap.open(argv[j]);
        if (!cap.isOpened()) {
            cout << "Could not open the input video: " << argv[j] << endl;
            return -1;
        }

        //codec type
        int ex = CV_FOURCC('X', 'V', 'I', 'D');
        //transform from int to char via Bitwise operators
        char EXT[] = { (char) (ex & 0XFF), (char) ((ex & 0XFF00) >> 8),
                (char) ((ex & 0XFF0000) >> 16),
                (char) ((ex & 0XFF000000) >> 24), 0 };

        cout << "Input Codec: " << EXT << endl;

        //acquire input size
        Size S = Size((int) cap.get(CV_CAP_PROP_FRAME_WIDTH),
                (int) cap.get(CV_CAP_PROP_FRAME_HEIGHT));

        //get fps
        //double fps = cap.get(CV_CAP_PROP_FPS);

        // Open the output
        //outputVideo.open(newname, ex, fps, S, true);
        outputVideo.open(newname, ex, fps, S, true);

        if (!outputVideo.isOpened()) {
            cout << "Could not open the output video for write: " << newname
                    << endl;
            return -1;
        }

        while (true) {
            cap >> frame;
            if (frame.empty()) {
                cerr << "didnt catch frame" << endl;
                break;
            }

            for (int i = 0; i < frame.cols; i++) {
                for (int j = 0; j < frame.rows; j++) {

                    //se todos os pixeis forem == 0 [preto] - next
                    if ((frame.at < Vec3b > (i, j).val[0] != 0)
                            && (frame.at < Vec3b > (i, j).val[1] != 0)
                            && (frame.at < Vec3b > (i, j).val[2] != 0)) {

                        Mat frame1 = frame.clone();

                        imshow("Output", frame1);
                        out.write(frame1);

                    }
                }
            }
        }

        return 0;
    }

Detection of Black Pixels in Video

Hi everyone!

I still have problems with the property FPS, because I can't get it autocatically.

[avi @ 0x8e14540] time base not set
Could not open the output video for write: /home/margarida/Output.avi

Additionally, my goal is only to find colour frames, i.e. I need to remove all the frames constituted with black pixels from my input videos. And I get the error:

Segmentation fault (core dumped)

Does anyone detects any error? Or as a solution for my problems? Thanks in advance.

My code is:

int main(int argc, char* argv[]) {
    VideoCapture cap;
    VideoWriter outputVideo;
    string name;
    Mat frame;
    int j;

    for (j = 1; j < argc; j++) {

        printf("%d: %s\n", j, argv[j]);

        string str1 = argv[j];
        unsigned found = str1.find_last_of("/");
        name = str1.substr(0, found);
        string newname = name + "/Output.avi";
        cout << newname << '\n';

        cap.open(argv[j]);
        if (!cap.isOpened()) {
            cout << "Could not open the input video: " << argv[j] << endl;
            return -1;
        }

        //codec type
        int ex = CV_FOURCC('X', 'V', 'I', 'D');
        //transform from int to char via Bitwise operators
        char EXT[] = { (char) (ex & 0XFF), (char) ((ex & 0XFF00) >> 8),
                (char) ((ex & 0XFF0000) >> 16),
                (char) ((ex & 0XFF000000) >> 24), 0 };

        cout << "Input Codec: " << EXT << endl;

        //acquire input size
        Size S = Size((int) cap.get(CV_CAP_PROP_FRAME_WIDTH),
                (int) cap.get(CV_CAP_PROP_FRAME_HEIGHT));

        //get fps
        //double fps = cap.get(CV_CAP_PROP_FPS);

        // Open the output
        //outputVideo.open(newname, ex, fps, S, true);
        outputVideo.open(newname, ex, fps, S, true);

        if (!outputVideo.isOpened()) {
            cout << "Could not open the output video for write: " << newname
                    << endl;
            return -1;
        }

        while (true) {
            cap >> frame;
            if (frame.empty()) {
                cerr << "didnt catch frame" << endl;
                break;
            }

            for (int i = 0; i < frame.cols; i++) {
                for (int j = 0; j < frame.rows; j++) {

                    //se todos os pixeis forem == //if all the pixels != 0 [preto] [black] - next
clone frame
                    if ((frame.at < Vec3b > (i, j).val[0] != 0)
                            && (frame.at < Vec3b > (i, j).val[1] != 0)
                            && (frame.at < Vec3b > (i, j).val[2] != 0)) {

                        Mat frame1 = frame.clone();

                        imshow("Output", frame1);
                        out.write(frame1);

                    }
                }
            }
        }

        return 0;
    }

Detection of Black Pixels in Video

Hi everyone!

I still have problems with the property FPS, because I can't get it autocatically.

[avi @ 0x8e14540] time base not set
Could not open the output video for write: /home/margarida/Output.avi

Additionally, my goal is only to find colour frames, i.e. I need to remove all the frames constituted with black pixels from my input videos. And I get the error:

Segmentation fault (core dumped)

Does anyone detects any error? Or as a solution for my problems? Thanks in advance.

My code is:

int main(int argc, char* argv[]) {
    VideoCapture cap;
    VideoWriter outputVideo;
    string name;
    Mat frame;
    int j;

    for (j = 1; j < argc; j++) {

        printf("%d: %s\n", j, argv[j]);

        string str1 = argv[j];
        unsigned found = str1.find_last_of("/");
        name = str1.substr(0, found);
        string newname = name + "/Output.avi";
        cout << newname << '\n';

        cap.open(argv[j]);
        if (!cap.isOpened()) {
            cout << "Could not open the input video: " << argv[j] << endl;
            return -1;
        }

        //codec type
        int ex = CV_FOURCC('X', 'V', 'I', 'D');
        //transform from int to char via Bitwise operators
        char EXT[] = { (char) (ex & 0XFF), (char) ((ex & 0XFF00) >> 8),
                (char) ((ex & 0XFF0000) >> 16),
                (char) ((ex & 0XFF000000) >> 24), 0 };

        cout << "Input Codec: " << EXT << endl;

        //acquire input size
        Size S = Size((int) cap.get(CV_CAP_PROP_FRAME_WIDTH),
                (int) cap.get(CV_CAP_PROP_FRAME_HEIGHT));

        //get fps
        //double fps = cap.get(CV_CAP_PROP_FPS);

        // Open the output
        //outputVideo.open(newname, ex, fps, S, true);
        outputVideo.open(newname, ex, fps, 13, S, true);

        if (!outputVideo.isOpened()) {
            cout << "Could not open the output video for write: " << newname
                    << endl;
            return -1;
        }

        while (true) {
            cap >> frame;
            if (frame.empty()) {
                cerr << "didnt catch frame" << endl;
                break;
            }

            for (int i = 0; i < frame.cols; i++) {
                for (int j = 0; j < frame.rows; j++) {

                    //if all the pixels != 0 [black] - clone frame
                    if ((frame.at < Vec3b > (i, j).val[0] != 0)
                            && (frame.at < Vec3b > (i, j).val[1] != 0)
                            && (frame.at < Vec3b > (i, j).val[2] != 0)) {

                        Mat frame1 = frame.clone();

                        imshow("Output", frame1);
                        out.write(frame1);

                    }
                }
            }
        }

        return 0;
    }