PDA

View Full Version : C++ Console App without MFC



drgolf
July 3, 2010, 16:51:52
Hi,
I need a simple console app without MFC that just grabs an image and stores it in memory.

I looked in the documentation and found instructions on including h / lib files. However, this was when I was first getting started. Now, I can't find those instructions....

drgolf
July 3, 2010, 20:49:23
Here's what I'm talking about :)

Stefan Geissler
July 5, 2010, 09:26:15
Hello

First of all, you do not need the MemBufferCollection. It is already embedded in the FramehandlerSink pSink. In order to copy the image image data in the OpenCV image, you can not pass a "pBuff", becauset this class contains some more data than the image data.
However, after calling pSink->snapImages() you use pSink->getLastAcqMemBuffer() for accessing the last snapped image buffer.

Thus you write:



grabber.startLive();
pSink->snapImages(1);
memcpy((unsigned char *)img->imageData, pSink->getLastAcqMemBuffer()->getPtr(), img->width*img->height);
grabber.stopLive();


If you want to save the memcpy, you can pass the pointer to the image data in pSink directly to your OpenCV image:

img->imageData = (char*)pSink->getLastAcqMemBuffer()->getPtr();

drgolf
July 5, 2010, 14:45:56
Hi Stephan,
THANKS.

One more question.
I'm using an external trigger. I have a loop of 10. I noticed I had to use the Sleep command after grabber.startLive() or it would snap an image before being triggered. Am I doing something wrong?

THANKS



result = grabber.setExternalTrigger(true);

grabber.startLive();
Sleep(1000);

for (int i=0; i<10; i++)
{
printf("We are waiting for trigger\n");
pSink->snapImages(1);
img->imageData = (char*)pSink->getLastAcqMemBuffer()->getPtr();

cvNamedWindow( "image", 1 );
cvShowImage("image", img);
cvWaitKey(0);
}

grabber.stopLive();
grabber.closeDev();

Stefan Geissler
July 6, 2010, 14:46:25
Hello

using SnapImages together with external trigger is no good idea. Please use the GrabberListener, shown in the callback sameple instead. Do not forget to call p_sink->snapMode(false), thus all images are saved automatically and GrabberListener::frameReady is called automatically.

drgolf
July 10, 2010, 13:35:46
Thanks Stephan,
I'm only wanting to snap 1 image at a time.
With this case do I need the listener?
I attached my code

Stefan Geissler
July 12, 2010, 09:24:58
Hello,

Your code will work. I suggest to check the return value of

pSink->snapImages(1);
and handle the image only, if it returns true.

A listener is usefull in a bigger program with user interface, because the snapImages() call blocks your application. That means in an GUI program, it blocks the application, thus it can not be ended, except using the Windows Task Manager. (In case no image is provided.)

moisdc
February 23, 2011, 12:53:59
Dear Sir or Madam,

I am Moises, a Master's Candidate in Canary Island (Spain).

I am using visual studio 2008 (vc8) and openCV 2.1. Also, I have a DFG/USB2, Video to USB2Conv.

I need to use opencv from an acquired image with my DFG/USB2.

I have run the algorithm of this forum in my computer because I think it is perfect for my application. I have this error:

static void _Xran()
{ // report an out_of_range error
_THROW(out_of_range, "invalid vector<T> subscript");
}

Thanks in advance
Sincerely

Moises

Stefan Geissler
February 23, 2011, 14:59:57
Hello Moises,

The "static void _Xran()" seems to be part of the std::stringbase namespace. That is, what I found by searching in the internet about this. However, without a callstack we can not say who threw the exception. That means, I have no idea what happened on your computer, because I do not know, what you do and where the exception was thrown.

Related links may are:
http://stackoverflow.com/questions/2896754/link-error-after-update-from-vs2008-to-vs2010
http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/5ac28a31-3339-4db1-90f4-68edad360309
http://connect.microsoft.com/VisualStudio/feedback/details/521500/compatibility-with-libs-built-with-older-version

You may als ask the OpenCV support.

moisdc
February 23, 2011, 15:30:46
Thank Stefan Geissler for your response.

I have got to solve this. I don't know what happened, but I changed this number (27 -> 5):

// 27 is maximum width and height
Grabber::tVidFmtListPtr pVidFmtList = grabber.getAvailableVideoFormats();
grabber.setVideoFormat(pVidFmtList->at(5));

Attach my code (it is similar to the code attached above).

Yours
-- Moises Diaz-Cabrera
moisdc@gmail.com

Stefan Geissler
February 23, 2011, 15:58:33
Hello,

are you sure, you have 5 video formats? Did you check, whether this exists? I mean, you receive an "out of range" error message.

You may also write printf("%s\n", pVidFmtList->at(5).c_str()); to see the contents.

moisdc
February 23, 2011, 16:21:52
Hi Stefan.
I have put the number 10 insted of 5.
The information is:

Scream #0.0: Video: rawvideo, yuv420p, 640x480, q=2-31, 19660 kb/s, 90k tbn, Y800 (720x288)

Stefan Geissler
February 23, 2011, 16:49:42
And what is with number 5?
What video capture device do you use? The text looks very strange to me, because it should have been "Y800 (720x288)" only.

moisdc
February 23, 2011, 16:56:15
Curiously, now in my program appears this information: Y800 (720x288).

However, I don't understand the number 5 or 10 or 27 in this code part (I have just put the number 10):

// 27 is maximum width and height
Grabber::tVidFmtListPtr pVidFmtList = grabber.getAvailableVideoFormats();
grabber.setVideoFormat(pVidFmtList->at(10));
printf("%s\n", pVidFmtList->at(10).c_str());

Stefan Geissler
February 23, 2011, 17:44:48
The numer is an index. It seems you have 28 video formats. So you should be able to enumerate them starting at 0 ending at 27.

redbus
April 13, 2012, 14:41:29
Hello,
I am trying to capture frames form the camera and use these frames in opencv. But the frames come flipped. Do you have any suggestion to capture them non-flipped?
Here is my code:

bool result;
result = DShowLib::InitLibrary();
if (result == false)
{
printf("FAILED\n");
cin.get();
}

DShowLib::Grabber grabber;
if( !setupDeviceFromFile( grabber ) )
{
return -1;
}

//grabber.setOverlayBitmapPathPosition( ePP_NONE );

tFrameHandlerSinkPtr pSink = FrameHandlerSink::create( eRGB24, 2 );

pSink->setSnapMode(true);
grabber.setSinkType( pSink );

result = grabber.prepareLive( false ); // this allows getting info
if (result == false)
{
printf("FAILED\n");
cin.get();
}

FrameTypeInfo info;
pSink->getOutputFrameType( info );

grabber.startLive();
IplImage* img=cvCreateImage( cvSize(info.dim.cx,info.dim.cy), IPL_DEPTH_8U , 3 );

for(;;)
{

pSink->snapImages(1);

img->imageData = (char*)pSink->getLastAcqMemBuffer()->getPtr();


// Process Image

cvShowImage( "Image", img);
cvWaitKey(1);
}
grabber.stopLive();

Stefan Geissler
April 13, 2012, 16:27:43
Hello,

I do not know, why OpenCV handles the images flipped. Therefore, you may flip them using a suitable OpenCV flip function.

cvConvertImage( const CvArr* src, CvArr* dst, int flags=0 );

cvConvertImage( img,img,CV_CVTIMG_FLIP)