Problem whith GetFrameImage

yermani
Posts: 5
Joined: Tue Oct 23, 2007 1:23 am

Problem whith GetFrameImage

Post by yermani »

hello everybody,
I'm trying to use the GetFrameImage methode. I don't understand the error risen. Can someone help me? thanks!

Here is the error:
([color:#FF0000]see red line in the code[/color])Access violation reading location 0x0000daf0

And here is the code:

//#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "optitrack.h"

//OpenCV
#include
#include
#include


INPCameraCollection *pCameraCollection;
INPCamera *pCamera;
INPCameraFrame *pFrame;

int main(int argc, char* argv[])
{
HRESULT hr;
LONG count;
LONG misc;
LONG waittime;
LONG framerate;

BYTE *pBuffer;

int running = 1;
int toerrors = 0;

printf("Hello World!\n");

CoInitialize(NULL); // Startup Microsoft crap
//CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
//CoInitializeEx(NULL, COINIT_MULTITHREADED);


CoCreateInstance(__uuidof(NPCameraCollection) , NULL, CLSCTX_ALL,__uuidof(INPCameraCollection), (void **) &pCameraCollection);
CoCreateInstance(__uuidof(NPCamera) , NULL, CLSCTX_ALL,__uuidof(INPCamera) , (void **) &pCamera);
CoCreateInstance(__uuidof(NPCameraFrame) , NULL, CLSCTX_ALL,__uuidof(INPCameraFrame) , (void **) &pFrame);

// find all the cameras:
pCameraCollection->Enum();

pCameraCollection->get_Count(&count);
printf("Nbr of cameras = %ld \n",count);

if (count Item(0, &pCamera);

pCamera->get_SerialNumber(&misc);
printf("serial number = %ld \n", misc);

// Open camera:
pCamera->Open();
printf("Camera->Open \n");

// do not suppresses empty camera frames
#if 0
voption.vt = VT_BOOL;
voption.boolVal = VARIANT_TRUE;
pCamera->SetOption(NP_OPTION_SEND_EMPTY_FRAMES, voption);
printf("Camera->SetOption \n");
#endif

// Frame rate:
pCamera->get_FrameRate(&framerate);
printf("Frame Rate = %ld \n", framerate);


/////////////////////////////
pCamera->Start();
printf("Camera->Start \n");

waittime = 10; // value in milliseconds; 0 = don't wait; INFINITE = wait till data

running = 600; // run ~1 minute for waittime=10 ms

// frame width:
LONG pVal;
pCamera->get_Width(&pVal);
int width=(int)pVal;
printf("width:%d \n",pVal);

// frame height:
pCamera->get_Height(&pVal);
int height=(int)pVal;
printf("height:%d \n",pVal);

// Memory allocation
[color:#009900]pBuffer=(BYTE*)malloc((width*height)*(sizeof(BYTE)));//pBuffer=(BYTE*)malloc(sizeof(BYTE));[/color]
if (pBuffer==NULL){
printf("Echec Malloc \n");
exit(EXIT_FAILURE);
}

// OpenCv Font
CvFont myFont;
cvInitFont(&myFont,CV_FONT_HERSHEY_PLAIN,1.0f,1.0f,0,2,8);

// OpenCv image structure Initialisation
IplImage* frame = cvCreateImage(cvSize (width, height), IPL_DEPTH_8U, 1);
unsigned char *data =(unsigned char *)(frame->imageData);

// OpenCV window
cvNamedWindow("Acquisition", CV_WINDOW_AUTOSIZE);

CvPoint pt1, pt2;
pt1.x=0;
pt1.y=0;
pt2.x=width;
pt2.y=height;
cvRectangle(frame,pt1,pt2,CV_RGB(0,0,0),-1,8,0);
cvShowImage("Acquisition",frame);
cvWaitKey(0);

//__________________________________________________________________________________________
// start acquisition
while(running--) {

// Get Frame:
hr=pCamera->GetFrame(waittime, &pFrame);

if (FAILED(hr)) {
fprintf(stderr,"ERROR:Camera->GetFrame %x \n", hr);

switch(hr) {
case E_POINTER:
fprintf(stderr, "E_POINTER \n"); break;

case NP_E_DEVICE_DISCONNECTED:
fprintf(stderr, "NP_E_DEVICE_DISCONNECTED \n"); break;

case NP_E_DEVICE_NOT_SUPPORTED:
fprintf(stderr, "NP_E_DEVICE_NOT_SUPPORTED \n"); break;

case HRESULT_FROM_WIN32(ERROR_TIMEOUT):
fprintf(stderr, "HRESULT_FROM_WIN32(ERROR_TIMEOUT)\n");
toerrors++;
break;

default:
fprintf(stderr, "default \n"); break;
}
}

// render an image into a buffer
[color:#FF0000]pCamera->GetFrameImage(pFrame, width, height, 0, 8, &pBuffer[0]);[/color]

printf(".");

// Affectation on an OpenCV structrure
for (int i=0; iGetSafeHwnd();
//pCamera->GetFrame(DrawFrame(pFrame, hWnd); // essayer plutot d'utiliser les fen�tres d'OpenCV


pFrame->Free();
}// End of acquisition_________________________________________________________________________



// Goodbye
printf("\n\n%d timeouts \n", toerrors);
CvPoint pt;
pt.x=width/2;
pt.y=height/2;
cvPutText(frame,"--END--",pt,&myFont,CV_RGB(0,0,0));
cvShowImage("Acquisition",frame);
printf("\n Press key to quit!!\n");
// Attente:
cvWaitKey(0);

// shut everything down
free(pBuffer);
pCamera->Stop();
pCamera->Close();
cvReleaseImage(&frame);
cvDestroyWindow("Acquisition");
CoUninitialize();

return 0;
}
Birch
Posts: 1139
Joined: Thu Jan 30, 2003 5:00 am
Location: Corvallis, Oregon

Re: Problem whith GetFrameImage

Post by Birch »

Try adding a check for (pFrame != 0) before calling GetFrameImage, that should help verify that you have a valid frame to operate on.
yermani
Posts: 5
Joined: Tue Oct 23, 2007 1:23 am

Re: Problem whith GetFrameImage

Post by yermani »

Sorry, I just see your answer. Yes that is the mistake. Thanks for your help.
symfonysid
Posts: 20
Joined: Fri Nov 09, 2007 7:39 am

Re: Problem whith GetFrameImage

Post by symfonysid »

Hi,

I work with yermani, and we're still having some unexpected problems with the GetFrameImage method. There are a number of cases where an access violation occurs in GetFrameImage although the pFrame and pCamera objects are fine. For instance, when the camera is plugged into a laptop and the laptop is not plugged into a power socket, then it's possible to create a pCamera and pFrame object without any problems (return code is S_OK and they are not null), but when these objects are used by calling GetFrameImage, there is an access violation error. Um... In this case one would expect that the function to create the pCamera object would return an error, and if not that, then GetFrameImage would return an error so that one could catch the problem and alert the program's user. But instead there is an access violation inside the method itself, and everything goes down... The same thing happens as well when the wait time is set to too small or too large a value. When it is set to 1 msec or 100 msecs, there is an access violation in the GetFrameImage method rather than this method returning an error. There are possibly some further cases where exceptions arise when using this method, but we haven't yet been able to localize their cause... With regards to the first problem, I was hoping that there might be a method we could call to check to see whether the camera was in a valid state. With regards to the second problem, I was hoping you might be able to explain why this is happening.
beckdo
Posts: 520
Joined: Tue Jan 02, 2007 2:02 pm

Re: Problem whith GetFrameImage

Post by beckdo »

Are you saying that when the camera is plugged into wall power the exception goes away?
hpcv
Posts: 58
Joined: Tue Oct 23, 2007 2:09 pm

Re: Problem whith GetFrameImage

Post by hpcv »

I have had some issues myself regarding grounding. Basically, all cameras would go down if I touched the metal casing of one of the V100 cameras, and to get it back up a restart of the Point Cloud or Rigid Body tool was needed. I worked around this by keeping my hands off the cameras, but just so you know that grounding may be an issue here.
Jim
Posts: 1395
Joined: Mon Oct 14, 2002 5:00 am
Location: Corvallis, Oregon
Contact:

Re: Problem whith GetFrameImage

Post by Jim »

Hello:

I have tried to isolate the grounding problem here, and have not been able to see what is happening. The cases are isolated from the board internals, and decoupled, the GND should go from the internal boards to the USB hub. I have seen it where I touched the case once and the LED display went away, but the camera was still there. With all other cameras in the field I haven't heard of any problems (hundreds), but it could be there in some situations. Just to let you know that I am aware of what you are saying. Thanks.
symfonysid
Posts: 20
Joined: Fri Nov 09, 2007 7:39 am

Re: Problem whith GetFrameImage

Post by symfonysid »

Hi,

Well, although the grounding problem sounds interesting, it is actually a different problem than the one I mentioned. I only meant to say that when the camera is plugged into a laptop, and the laptop is not plugged into the wall, then the camera doesn't work properly. This is in and of itself not surprising, because of course the camera can't work if it's not getting enough power from the USB port. But what is surprising is that we don't notice this problem until we call GetFrameImage() when an exception is thrown internally in the method and everything goes down. I was hoping there was a way we could check for this condition beforehand so that we could give the user an error message.

Thanks,
Greg
hpcv
Posts: 58
Joined: Tue Oct 23, 2007 2:09 pm

Re: Problem whith GetFrameImage

Post by hpcv »

You can ask Windows whether the laptop is plugged into the wall, I think. Not exactly what you ask for, but depending on your situation it might suffice.
Jim
Posts: 1395
Joined: Mon Oct 14, 2002 5:00 am
Location: Corvallis, Oregon
Contact:

Re: Problem whith GetFrameImage

Post by Jim »

Oh, thanks for the explanation. Never mind about that "other" grounding problem! :)

As you suggest, this does make sense, if you don't have enough power the cameras won't work to spec. We have found that you can't be sure what is happening on different computers USB ports, so we usually use our D-Link powered USB hubs, which work really well and are cheap. We also use a lot of Dell laptops and Shuttle computers and they are all fine.

For this issue I suspect that the camera is operating to some degree with the low power, but might be in an unstable condition. If powering the laptop fixes the issue, I suspect that the onboard USB is not providing 500ma as per USB spec.

You could try turning off the IR LEDs first, and then calling GetFrame and see if that makes a difference, just to isolate the power issue more. The IR LEDs take most of the power.
Post Reply