Problem whith GetFrameImage
Posted: Tue Oct 23, 2007 2:01 am
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;
}
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;
}