Results 1 to 3 of 3

Thread: Help wanted from opencvlibrary Guru WebCam image... to avi..

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Help wanted from opencvlibrary Guru WebCam image... to avi..

    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.ne...t4_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?


    Qt Code:
    1. QtGLContext::QtGLContext(QWidget* parent /*= 0*/,
    2. const QGLWidget* sharedWidget /*= 0*/, Qt::WFlags f /*= 0*/) : QGLWidget(parent, sharedWidget, f), m_imgP(0), m_mousePressed(false)
    3. {
    4.  
    5. //////qDebug() << "### total cams = " << ncams;
    6.  
    7.  
    8. capture = cvCaptureFromCAM( CV_CAP_ANY );
    9.  
    10. if (capture) {
    11. SelfUpdate();
    12. }
    13. }
    14.  
    15. void QtGLContext::SelfUpdate()
    16. {
    17.  
    18. IplImage* imgPtr = cvQueryFrame( capture );
    19. setImage((unsigned char*)imgPtr->imageData, imgPtr->width, imgPtr->height, imgPtr->nChannels * imgPtr->depth , true);
    20. QTimer::singleShot(80, this, SLOT(SelfUpdate()));
    21. }
    22.  
    23.  
    24.  
    25. bool QtGLContext::setImage(
    26. unsigned char* imgP,
    27. const int width,
    28. const int height,
    29. const int pixeldepth,
    30. const bool flipped /*=false*/)
    31. {
    32. m_imgP = imgP; m_width = width; m_height = height; m_pixeldepth = pixeldepth;
    33. m_flipped = flipped;
    34. this->updateGL();
    35.  
    36. // only 8, 24 and 32 bit images are supported
    37. if (!imgP || (pixeldepth!=8 && pixeldepth!=24 && pixeldepth!=32))
    38. return false;
    39. glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
    40. // calculate texture size
    41. m_texWidth = NextLargerPowerOfTwo(m_width);
    42. m_texHeight = NextLargerPowerOfTwo(m_height);
    43. // create texture memory
    44. unsigned char* textureGL = new GLubyte[m_texHeight*m_texWidth* (pixeldepth>>3)];
    45. // calculate texture coordinates for image
    46. m_texUpperLeftX = float (m_texWidth-m_width) / (float) (m_texWidth);
    47. m_texUpperLeftY = float (m_texHeight-m_height) / (float) (m_texHeight);
    48. m_texLowerRightX = 1.0; // (float) (_texWidth) / (float) _height;
    49. m_texLowerRightY = 1.0; // (float) (_texHeight) / (float) _width;
    50.  
    51. // tell OpenGL which texture "id" we will be working with.:
    52. glBindTexture(GL_TEXTURE_2D, m_texNameGL);
    53. // tell OpenGL that the pixel data which is going to be passed to it is aligned in byte order:
    54. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    55.  
    56. // set the various parameters for the current OpenGL texture:
    57. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    58. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    59. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    60. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    61.  
    62. // tell OpenGL how the texture will act when it is rendered into a scene:
    63. // The GL_MODULATE attribute allows you to apply effects such as lighting
    64. // and coloring to your texture. If you do not want lighting and coloring to effect
    65. // your texture and you would like to display the texture unchanged when coloring
    66. // is applied replace GL_MODULATE with GL_DECAL
    67. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    68.  
    69. //glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, _texWidth, _texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, img->data());
    70. switch(m_pixeldepth) {
    71. case 8:
    72. glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_texWidth, m_texHeight, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE,textureGL);
    73. break;
    74. case 24:
    75. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_texWidth,m_texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, textureGL);
    76. break;
    77. case 32:
    78. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_texWidth,m_texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureGL);
    79. break;
    80. default:
    81. glDisable(GL_TEXTURE_2D);
    82. delete[] textureGL;
    83. return false;
    84. }
    85.  
    86.  
    87.  
    88. glDisable(GL_TEXTURE_2D);
    89. //glFlush();
    90. delete[] textureGL;
    91. resetBox();
    92. return true;
    93. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help wanted from opencvlibrary Guru WebCam image... to avi..

    Qt Code:
    1. IplImage* imgPtr = cvQueryFrame( capture );
    To copy to clipboard, switch view to plain text mode 
    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.

  3. #3
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help wanted from opencvlibrary Guru WebCam image... to avi..

    Quote Originally Posted by marcel View Post
    Qt Code:
    1. IplImage* imgPtr = cvQueryFrame( capture );
    To copy to clipboard, switch view to plain text mode 
    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....

    Qt Code:
    1. void QtGLContext::GrabImage()
    2. {
    3. qDebug() << "### GrabImage gooo ";
    4. const QString unixt = QString("_%1").arg(unixtime);
    5. adesso.setDate(QDate::currentDate());
    6. const QString oggi = adesso.toString("d.M.yyyy");
    7. IplImage* img = cvQueryFrame( capture );
    8. QString cfileName = QFileDialog::getSaveFileName(this, "Export image",oggi+unixt+".bmp", "*.bmp");
    9. if (!cfileName.endsWith(".bmp")) {
    10. cfileName = cfileName+".bmp";
    11. }
    12. QByteArray infile = cfileName.toAscii();
    13. cvSaveImage(infile.data(),img);
    14. QImage *i=new QImage(cfileName);
    15. const QString pngfileName = cfileName.replace(".bmp",".png");
    16. if (!i->isNull()) {
    17. qDebug() << "### valid img ";
    18. i->save(pngfileName,"PNG",100);
    19. } else {
    20. qDebug() << "### noooo valid img ";
    21. return;
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

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

    i found
    http://www.koders.com/cpp/fid8AC9F0D...5E42EF904.aspx
    http://forum.hardware.fr/hfr/Program...et_90460_1.htm




    Qt Code:
    1. void QtGLContext::GrabAvi()
    2. {
    3. StopGrab(); /* dont display live cam opengl */
    4. qDebug() << "### avi gooo ";
    5.  
    6. int isColor=1;
    7. int fps=15;
    8. int frameW=640;
    9. int frameH=480;
    10. int nFrames=8;
    11.  
    12. CvVideoWriter* writer = cvCreateVideoWriter("out.avi", CV_FOURCC('M','J','P','G'),fps,cvSize(frameW,frameH),isColor);
    13.  
    14. for(int i=0;i<nFrames;i++){
    15. qDebug() << "### Frames " << i;
    16. IplImage* img = cvQueryFrame( capture );
    17. cvWriteFrame(writer, img);//add the frame to the file
    18. }
    19. qDebug() << "### loop end ..... ";
    20. cvReleaseVideoWriter(&writer);
    21. qDebug() << "### after write avi ";
    22. //////// cvReleaseCapture(&capture); /* crash all */
    23. /////StartGrab();
    24. }
    To copy to clipboard, switch view to plain text mode 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.