Unable to output gray scale video using opencv on beaglebone black

asked 2017-11-08 20:59:07 -0500

SJ24 gravatar image

I'm trying to record a video using webcam and convert it to gray scale video using opencv on beablebone black. I am able to create the video in color but when I try to convert it to gray scale it is not working. Its just creating a .avi file with 6kB size. Below is the code I'm using. TIA

 #include "opencv2/opencv.hpp" 
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <time.h>
using namespace std;
using namespace cv;
#define MAX_FRAME 40

int main(int, char**)
{
    // capturing video feed as vcap
    VideoCapture vcap(0);
    // verifying video feed and printing an error message in case of failure
    if(!vcap.isOpened())
    {
        cout << "Error opening video stream or file" << endl; return -1;
    }
    // We multiply the number of pixels (in both x and y) by this value
    double sizeMultiplier = .25;
    // getting the width and height of the frames
    int frame_width = vcap.get(CV_CAP_PROP_FRAME_WIDTH);
    int frame_height = vcap.get(CV_CAP_PROP_FRAME_HEIGHT);
    frame_width = frame_width*sizeMultiplier;
    frame_height = frame_height*sizeMultiplier;
    // recording the video as out_canny.avi
    VideoWriter video("output.avi", CV_FOURCC('M','J','P','G'), 5, Size(frame_width,frame_height),true);


     Mat frame, frame_small, gray;
    for(int numFrame = 0; numFrame < MAX_FRAME; numFrame++)
    {
        vcap >> frame; // video capturing into the matrix frame
        //cvtColor(frame, gray, CV_BGR2GRAY); 
        //blur(frame, frame, Size(3, 3));
        //Canny(frame, frame, 0, 30, 3);
        resize(frame,frame_small,Size(), sizeMultiplier,sizeMultiplier);
        cvtColor(frame_small, frame_small, CV_BGR2GRAY); //Not Working
        video.write(frame_small); // recording the resulting video
    }
    //vcap.release();
    return 0;
}
edit retag flag offensive close merge delete

Comments

if you care to read docs, last arg should be false, to write grayscale frames.

(not sure though, if that is supported on your os)

berak gravatar imageberak ( 2017-11-08 21:25:45 -0500 )edit

I'm using windows and it is supported on windows but when I change it to to false its creating a .avi file with a completely dark video.

SJ24 gravatar imageSJ24 ( 2017-11-08 21:34:50 -0500 )edit

Hi Berak, Its working now, but I am getting a replicated video(3 same videos in 1). Is this because I'm converting from 3 channel to 1 channel

SJ24 gravatar imageSJ24 ( 2017-11-08 22:06:04 -0500 )edit

tried your code on win, works fine for me (given you set the flag correctly)

berak gravatar imageberak ( 2017-11-09 07:30:07 -0500 )edit