MasteringOpenCV / code

Code for the book "Mastering OpenCV with Practical Computer Vision Projects" by Packt Publishing 2012.
Other
2.73k stars 1.65k forks source link

RGB video source problem #35

Closed Reena24 closed 10 years ago

Reena24 commented 10 years ago

Hi the PTAMM code started to work finally in Windows and I was finally able to get the video source from usb web cam using the code

include “VideoSource.h”

//#include

include

include

include

include

include

include

using namespace CVD; using namespace std; using namespace GVars3; using namespace cv;

define OPENCV_VIDEO_W 640

define OPENCV_VIDEO_H 480

namespace PTAMM { VideoSource::VideoSource() { cout << " VideoSource_Linux: Opening video source…" <isOpened()){ cerr << "Unable to get the camera" << endl; exit(-1); } cout << " … got video source." << endl; mirSize = ImageRef(OPENCV_VIDEO_W, OPENCV_VIDEO_H); };

ImageRef VideoSource::Size() { return mirSize; };

void conversionNB(Mat frame, Image &imBW){ Mat clone = frame.clone(); Mat_& framep = (Mat&)clone; for (int i = 0; i < OPENCV_VIDEO_H; i++){ for (int j = 0; j < OPENCV_VIDEO_W; j++){ imBW[i][j] = (frame_p(i,j)[0] + frame_p(i,j)[1] + frame_p(i,j)[2]) / 3; } }

}

void conversionRGB(Mat frame, Image &imRGB){ Mat clone = frame.clone(); Mat_& framep = (Mat&)clone; for (int i = 0; i < OPENCV_VIDEO_H; i++){ for (int j = 0; j < OPENCV_VIDEO_W; j++){ imRGB[i][j].red = frame_p(i,j)[2]; imRGB[i][j].green = frame_p(i,j)[1]; imRGB[i][j].blue = frame_p(i,j)[0]; } } }

void VideoSource::GetAndFillFrameBWandRGB(Image &imBW, Image &imRGB) { Mat frame; VideoCapture* cap = (VideoCapture)mptr; cap >> frame; conversionNB(frame, imBW); conversionRGB(frame, imRGB); } }

But the video source for PTAMM is black and white how do I get the an RGB video source instead of a gray image as video source. I think I need to add the line cvCvtColor(&frame,&frame,CV_GRAY2BGR);

but it dosnt work cause I get OpenCV assertion error. Could someone tell me how do I get RGB type video Input Source.

BloodAxe commented 10 years ago

OpenCV video capture capture frames in RGB format by default, so there is no need to make any conversion if you need RGB data for your algorithm.