PDA

View Full Version : save AVI (ic control) + show frame by frame with openCV



moisdc
March 2, 2011, 00:53:28
Dear colleagues,

I would like to save a AVI with IC Control. Moreover, I am wondering if I could show frame by frame with openCV.
I have got two successfull programs: one to save AVI and other to show frame by frame.

I have tried to join it, but this code does not work well.
Have two mistakes:
1
if (result == false)
{
printf("FAILED\n");
//cin.get();
}
2
img->imageData = (char*)pSink2->getLastAcqMemBuffer()->getPtr();

I attach the code.
Thanks in advance
PS: sorry for my poor experience



#include "stdafx.h"
#include "tisudshl.h"
#include "cv.h"
#include "highgui.h"
#include <math.h>
#define Resultado "Resultado"

int _tmain(int argc, _TCHAR* argv[])
{
bool result;
result = DShowLib::InitLibrary( "ISB3200016679");
if (result == false)
{
printf("FAILED\n");
cin.get();
}

// Abriendo un dispositivo
Grabber grabber;
if( !setupDeviceFromFile( grabber ) )
{
return -1;
}
// Abriendo un dispositivo


// Seleccionar un codec
tCodecListPtr pCodecs = Codec::getAvailableCodecs();

// Display all codec names on the screen.
int choice = presentUserChoice( toStringArrayPtrListPtr( pCodecs ) );
if( choice == -1 )
{
return -1;
}
// Seleccionar un codec

// GRABACION DEL VIDEO
// Create an MediaStreamSink to record an AVI file with the selected CoDec
tMediaStreamSinkPtr pSink = MediaStreamSink::create(
MediaStreamContainer::create( MSC_AviContainer ), pCodecs->at( choice ) );

// Set the filename.
pSink->setFilename( "prueba.avi" );



// The sink is initially paused, so that no video data is written to the file.
pSink->setSinkMode( GrabberSinkType::ePAUSE );

// Set pSink as the current sink.
grabber.setSinkType( pSink );

// Start the live mode. The live video will be displayed but no images will be written
// to the AVI file because pSink is in pause mode.
if( !grabber.startLive( true ) )
{
std::cerr << grabber.getLastError().toString() << std::endl;
return -1;
}

std::cout << "Press [enter] to start capturing!";
std::cin.get();

// probando ic control
grabber.setOverlayBitmapPathPosition( ePP_NONE );
tFrameHandlerSinkPtr pSink2 = FrameHandlerSink::create( eRGB24, 2 );
pSink2->setSnapMode( true );
grabber.setSinkType( pSink2 );
result = grabber.prepareLive( false );
if (result == false)
{
printf("FAILED\n");
//cin.get();
}


FrameTypeInfo info;
pSink2->getOutputFrameType( info );
BYTE* pBuf;
pBuf = new BYTE[info.buffersize];
tMemBufferCollectionPtr pCollection = MemBufferCollection::create( info, 1, &pBuf );
pSink2->setMemBufferCollection( pCollection );
// probando ic control

// openCV
IplImage *img = 0;
CvSize size = cvSize(info.dim.cx,info.dim.cy);
img = cvCreateImage( size, IPL_DEPTH_8U , 3 );
cvNamedWindow( Resultado, CV_WINDOW_AUTOSIZE);
// openCV

// Start the sink. The image stream is written to the AVI file.
pSink->setSinkMode(GrabberSinkType::eRUN );
while(1)
{
// acquisition image
pSink2->snapImages( 1 );
img->imageData = (char*)pSink2->getLastAcqMemBuffer()->getPtr();
cvConvertImage(img,imgNDG,CV_RGB2GRAY);
cvFlip( img, img );
cvShowImage( Resultado, img );

char key = cvWaitKey(10);
if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC', q o Q
break;
}


std::cout << "Video recording started." << std::endl;
std::cout << "Press [enter] to stop capturing!";
std::cin.get();

// Pause the sink. This stops writing the image stream to the AVI file.
// A subsequent call to setSinkMode with GrabberSinkType::eRUN as the
// parameter would restart AVI recording.
pSink->setSinkMode(GrabberSinkType::ePAUSE );

std::cout << "Video recording stopped." << std::endl;

// Stop the live mode. This stops writing images to the AVI file if the mode is not
// GrabberSinkType::ePAUSE. The AVI file is closed.
grabber.stopLive();
// GRABACION DEL VIDEO

/* free memory */
cvReleaseImage(&img);
cvDestroyAllWindows();

delete [] pBuf;

return 0;
}

Stefan Geissler
March 2, 2011, 11:35:22
Hello Moises,

Your code wont work. You create a MediaStreamSink and assign it to the grabber object. So far, so well. Then you start the live video, which is fine too. But then you create a second sink (pSink2) and assign it to the grabber object. This overwrites the Mediastremsink and you can not save an avi file any more.

It seems, you want use the OpenCV Window only for display. Thus you may consider to not use IC Imaging Control and try OpenCV code only. I suppose you can find samples for OpenCV AVI capture in the internet.