PDA

View Full Version : Help wanted from opencvlibrary Guru WebCam image... to avi..


patrik08
17th May 2007, 21:59
I have build successfull http://sourceforge.net/projects/opencvlibrary/ to grab image from my notebook webcam ....

i hack this lib code to is possibel to run on window MINGW compiler ...
i not tested on linux but i am sure is run on /dev/videoxx

subversion ....
https://qt-webdav.svn.sourceforge.net/svnroot/qt-webdav/webcam_qt4_demo_mingw/

the qt4 demo is running .... and display a live image motion ...

Now my question, how i can grab more image to compose a avi/mpg4 film? with or withtout audio?






QtGLContext::QtGLContext(QWidget* parent /*= 0*/,
const QGLWidget* sharedWidget /*= 0*/, Qt::WFlags f /*= 0*/) : QGLWidget(parent, sharedWidget, f), m_imgP(0), m_mousePressed(false)
{

//////qDebug() << "### total cams = " << ncams;


capture = cvCaptureFromCAM( CV_CAP_ANY );

if (capture) {
SelfUpdate();
}
}

void QtGLContext::SelfUpdate()
{

IplImage* imgPtr = cvQueryFrame( capture );
setImage((unsigned char*)imgPtr->imageData, imgPtr->width, imgPtr->height, imgPtr->nChannels * imgPtr->depth , true);
QTimer::singleShot(80, this, SLOT(SelfUpdate()));
}



bool QtGLContext::setImage(
unsigned char* imgP,
const int width,
const int height,
const int pixeldepth,
const bool flipped /*=false*/)
{
m_imgP = imgP; m_width = width; m_height = height; m_pixeldepth = pixeldepth;
m_flipped = flipped;
this->updateGL();

// only 8, 24 and 32 bit images are supported
if (!imgP || (pixeldepth!=8 && pixeldepth!=24 && pixeldepth!=32))
return false;
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
// calculate texture size
m_texWidth = NextLargerPowerOfTwo(m_width);
m_texHeight = NextLargerPowerOfTwo(m_height);
// create texture memory
unsigned char* textureGL = new GLubyte[m_texHeight*m_texWidth* (pixeldepth>>3)];
// calculate texture coordinates for image
m_texUpperLeftX = float (m_texWidth-m_width) / (float) (m_texWidth);
m_texUpperLeftY = float (m_texHeight-m_height) / (float) (m_texHeight);
m_texLowerRightX = 1.0; // (float) (_texWidth) / (float) _height;
m_texLowerRightY = 1.0; // (float) (_texHeight) / (float) _width;

// tell OpenGL which texture "id" we will be working with.:
glBindTexture(GL_TEXTURE_2D, m_texNameGL);
// tell OpenGL that the pixel data which is going to be passed to it is aligned in byte order:
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

// set the various parameters for the current OpenGL texture:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

// tell OpenGL how the texture will act when it is rendered into a scene:
// The GL_MODULATE attribute allows you to apply effects such as lighting
// and coloring to your texture. If you do not want lighting and coloring to effect
// your texture and you would like to display the texture unchanged when coloring
// is applied replace GL_MODULATE with GL_DECAL
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

//glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, _texWidth, _texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, img->data());
switch(m_pixeldepth) {
case 8:
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_texWidth, m_texHeight, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE,textureGL);
break;
case 24:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_texWidth,m_texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, textureGL);
break;
case 32:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_texWidth,m_texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureGL);
break;
default:
glDisable(GL_TEXTURE_2D);
delete[] textureGL;
return false;
}



glDisable(GL_TEXTURE_2D);
//glFlush();
delete[] textureGL;
resetBox();
return true;
}

marcel
17th May 2007, 22:24
IplImage* imgPtr = cvQueryFrame( capture );
I am not really sure, but it seems that the image data ( pixel data ) is imgPtr->imageData.
I see you already have the image dimensions and the bit depth.

I suppose you will need a third party library to create an avi ( mplayer maybe? last time I checked it had the ability to encode video, or any other library you find fit ).

You could store raw frame data ( imgPtr->imageData ) in some buffers - and as the buffers get filled, you write them somewhere on disk, and later use them to make the avi. Or you could make the avi on the fly. Many roads lead to Rome, as you put it.

patrik08
18th May 2007, 01:52
IplImage* imgPtr = cvQueryFrame( capture );
I am not really sure, but it seems that the image data ( pixel data ) is imgPtr->imageData.
I see you already have the image dimensions and the bit depth.

I suppose you will need a third party library to create an avi ( mplayer maybe? last time I checked it had the ability to encode video, or any other library you find fit ).
.

To grab and display image is not the problem

take a shoot here....


void QtGLContext::GrabImage()
{
qDebug() << "### GrabImage gooo ";
const QString unixt = QString("_%1").arg(unixtime);
adesso.setDate(QDate::currentDate());
const QString oggi = adesso.toString("d.M.yyyy");
IplImage* img = cvQueryFrame( capture );
QString cfileName = QFileDialog::getSaveFileName(this, "Export image",oggi+unixt+".bmp", "*.bmp");
if (!cfileName.endsWith(".bmp")) {
cfileName = cfileName+".bmp";
}
QByteArray infile = cfileName.toAscii();
cvSaveImage(infile.data(),img);
QImage *i=new QImage(cfileName);
const QString pngfileName = cfileName.replace(".bmp",".png");
if (!i->isNull()) {
qDebug() << "### valid img ";
i->save(pngfileName,"PNG",100);
} else {
qDebug() << "### noooo valid img ";
return;
}
}



Take avi from cam !!! or grab from source avi ... i not know....

i found
http://www.koders.com/cpp/fid8AC9F0D0E9CF1DE033BC39D113C030C5E42EF904.aspx
http://forum.hardware.fr/hfr/Programmation/C-2/opencv-sujet_90460_1.htm





void QtGLContext::GrabAvi()
{
StopGrab(); /* dont display live cam opengl */
qDebug() << "### avi gooo ";

int isColor=1;
int fps=15;
int frameW=640;
int frameH=480;
int nFrames=8;

CvVideoWriter* writer = cvCreateVideoWriter("out.avi", CV_FOURCC('M','J','P','G'),fps,cvSize(frameW,frame H),isColor);

for(int i=0;i<nFrames;i++){
qDebug() << "### Frames " << i;
IplImage* img = cvQueryFrame( capture );
cvWriteFrame(writer, img);//add the frame to the file
}
qDebug() << "### loop end ..... ";
cvReleaseVideoWriter(&writer);
qDebug() << "### after write avi ";
//////// cvReleaseCapture(&capture); /* crash all */
/////StartGrab();
}