Reddy2810
Reddy2810

Reputation: 310

Videowriter function for greyscale image using OpenCV function

I have a GigaE camera and which gives me a greyscale image and I want to record it as a video and process it later.

So as initial step I tried recording video using my webcam it worked and if i convert it into greyscale before writing it into video. I am not getting any video. My code is below

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

VideoCapture cap(0); 
VideoWriter writer;

if (!cap.isOpened())  
{
    cout << "not opened" << endl;
    return -1;
}

char* windowName = "Webcam Feed";
namedWindow(windowName, CV_WINDOW_AUTOSIZE); 


string filename = "D:\myVideo_greyscale.avi";
int fcc = CV_FOURCC('8', 'B', 'P', 'S');
int fps = 30;
Size frameSize(cap.get(CV_CAP_PROP_FRAME_WIDTH),cap.get(CV_CAP_PROP_FRAME_HEIGHT));
writer = VideoWriter(filename,-1,fps,frameSize);

if(!writer.isOpened())
{
    cout<<"Error not opened"<<endl;
    getchar();
    return -1;
}


while (1) 
{

    Mat frame;

    bool bSuccess = cap.read(frame); 

    if (!bSuccess) 
    {
        cout << "ERROR READING FRAME FROM CAMERA FEED" << endl;
        break;
    }
    cvtColor(frame, frame, CV_BGR2GRAY);
    writer.write(frame);
    imshow(windowName, frame); 
}

return 0;

}`

I used fcc has -1 tried all the possibilities none of them are able to record video. I also tried creating a grayscale video using opencv for fcc has CV_FOURCC('8','B','P','S') but it did not help me.

I get this error in debug after using the breakpointenter image description here

Upvotes: 3

Views: 2228

Answers (1)

Micka
Micka

Reputation: 20130

VideoWriter has an optional parameter which tells whether the video is grayscale or color. Default ist color = true. Try

bool isColor = false;
writer = VideoWriter(filename,-1,fps,frameSize, isColor);

Upvotes: 4

Related Questions