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;
}
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;
}