1 // PTGrey.cpp : Defines the entry point for the DLL application.
21 #include "Digiclops.h"
23 #if ( DIGICLOPS_VERSION > 200 )
28 #define PTGREYAPI __declspec(dllexport)
31 PTGREYAPI const char* GetErrorMessage();
33 HANDLE PTGreyProcessorThread;
35 DigiclopsContext digiclops = NULL;
36 TriclopsContext tcontext = NULL;
37 #if ( DIGICLOPS_VERSION > 200 )
38 PgrGuiContext cameraControlContext = NULL;
43 char *PGreyData, *PGData;
45 TriclopsColorImage t24Image;
46 TriclopsImage t8Image;
47 TriclopsInput TriclopsIn[4];
48 TriclopsError tcError;
50 TriclopsImage16 tImage16;
51 TriclopsInput* TriclopsBuffer = 0;
52 int PTFrameCount = 0, PTPrevFrameCount = 0, PTFreeFrameCount = 0, PTFrameNumber;
55 static int RawResolution = 0, StereoResolution = 0;
57 #define _PT_MAX_THREADS 10
58 int threadIds[_PT_MAX_THREADS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
59 int threadRefCount[_PT_MAX_THREADS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
60 int activeThreadCount = 0;
62 HANDLE PTGreyDataMutex = 0;
63 HANDLE PTGreyFrameMutex = 0;
65 static char PTGreyErrorString[9128];
67 static IplImage* temp;
68 static IplImage* temp8;
70 const char* config_filename = 0;
74 void copyTriclopsInput(TriclopsInput tInput, TriclopsInput* tOutput);
75 void WriteDisparityFile(TriclopsImage16* depthImage16, const char* filename);
76 void WritePointFile(TriclopsImage16 *image, char *filename);
77 void WriteColorPointFile(TriclopsImage16 *image, TriclopsColorImage *colorImage, char *filename);
78 BOOL PTRectifyInputDisparity();
80 PTGREYAPI const char* PTGreyGetErrorMessage()
82 return PTGreyErrorString;
84 PTGREYAPI void ClearErrorMessage()
86 PTGreyErrorString[0] = 0;
89 static void SetErrorMessage(const char* str, ...)
92 va_start(valist, str);
93 vsprintf(PTGreyErrorString, str, valist);
96 DWORD __stdcall CameraProcessor(void*);
98 /*BOOL APIENTRY DllMain( HANDLE hModule,
99 DWORD ul_reason_for_call,
106 void SetParameters () {
107 if(triclopsSetDisparity(tcontext, PGC->MinDisparity, PGC->MaxDisparity))
108 SetErrorMessage("triclopsSetDisparity setting error.");
109 if (triclopsSetTextureValidation(tcontext, PGC->TextureValidation != 0))
110 SetErrorMessage("triclopsSetTextureValidation setting error.");
111 if(triclopsSetTextureValidationThreshold(tcontext, PGC->TextureValidationThreshold))
112 SetErrorMessage("triclopsSetTextureValidationThreshold setting error. \n");
113 if(triclopsSetTextureValidationMapping(tcontext, PGC->TextureValidationMapping))
114 SetErrorMessage("triclopsSetTextureValidationMapping setting error. \n");
116 if(triclopsSetUniquenessValidation(tcontext, PGC->UniquenessValidation != 0))
117 SetErrorMessage("triclopsSetUniquenessValidation setting error. \n");
118 if(triclopsSetUniquenessValidationThreshold(tcontext, PGC->UniquenessValidationThreshold))
119 SetErrorMessage("triclopsSetUniquenessValidationThreshold setting error. \n");
120 if(triclopsSetUniquenessValidationMapping(tcontext, PGC->UniquenessValidationMapping))
121 SetErrorMessage("triclopsSetUniquenessValidationMapping setting error. \n");
123 if(triclopsSetStereoMask(tcontext, PGC->StereoMask))
124 SetErrorMessage("triclopsSetStereoMask setting error. \n");
125 if(triclopsSetEdgeMask(tcontext, PGC->EdgeMask))
126 SetErrorMessage("triclopsSetEdgeMask setting error. \n");
128 if(triclopsSetEdgeCorrelation(tcontext, PGC->EdgeCorrelation != 0))
129 SetErrorMessage("triclopsSetEdgeCorrelation setting error. \n");
130 if (triclopsSetSubpixelInterpolation(tcontext, PGC->SubPixelInterpolation != 0))
131 SetErrorMessage("triclopsSetSubpixelInterpolation setting error. \n");
132 if(triclopsSetStrictSubpixelValidation(tcontext, PGC->StrictSubPixelValidation != 0))
133 SetErrorMessage("triclopsSetStrictSubpixelValidation setting error. \n");
135 if(triclopsSetRectify(tcontext, PGC->Rectify != 0))
136 SetErrorMessage("triclopsSetRectify setting error. \n");
137 if(triclopsSetLowpass( tcontext, PGC->LowPass != 0))
138 SetErrorMessage("triclopsSetLowpass setting error. \n");
139 if(triclopsSetDoStereo( tcontext, PGC->DoStereo != 0))
140 SetErrorMessage("triclopsSetDoStereo setting error. \n");
141 if(triclopsSetDebug(tcontext, PGC->PGDebug != 0))
142 SetErrorMessage("triclopsSetDebug( setting error. \n");
145 PTGREYAPI BOOL PTGreyInitCamera()
147 HANDLE hFileMappingObject;
148 DigiclopsError digiclopsError;
149 TriclopsError triclopsError;
151 // Clear error string
154 // allocate the Point Grey camera shared memory.
155 hFileMappingObject = CreateFileMapping(HANDLE(-1), 0, PAGE_READWRITE, 0, 0xa000000, "PointGreySharedMemory");
156 int e = GetLastError();
157 PGreyData = (char*) MapViewOfFile(hFileMappingObject, FILE_MAP_ALL_ACCESS, 0, 0, 0);
158 PGC = (PGClient *) PGreyData; // map server control structure in our shared buffer.
159 PGS = (PGServer *) ((int)PGreyData + sizeof(PGClient)); // map client control structure in our shared buffer.
161 PGS->BackGroundAvailable = 0; //
162 PGS->Initializing = true;
163 PGData = (char *) ((int)PGreyData + sizeof(PGClient) + sizeof(PGServer) + (32 - sizeof(PGClient) % 32)); // aligned buffer
164 PGS->DigiclopsBase = (char *) PGreyData; // allow vb code to translate our buffer addresses
166 // create the Digiclops context
167 if(digiclopsError = digiclopsCreateContext(&digiclops))
169 SetErrorMessage ("Error - digiclopsCreateContext: %s", digiclopsErrorToString(digiclopsError));
173 #if ( DIGICLOPS_VERSION < 200 )
174 // connect to the camera - note this assumes that the Digiclops is device 0
176 if(digiclopsError = digiclopsInitialize(digiclops, 0))
178 SetErrorMessage ("Error - digiclopsInitialize: %s", digiclopsErrorToString(digiclopsError));
182 // connect to the camera -
183 // this call pops up a dialog that allows the user to select the
184 // Digiclops from a list on the bus
185 PgrGuiContext pgrGuiContext;
186 PgrGuiError pgrGuiError = pgrguiCreateContext( &pgrGuiContext,
188 DIGICLOPS_INITIALIZE_DIALOG );
189 if ( pgrGuiError != PGRGUI_ok )
191 SetErrorMessage ("Error - PGR Gui Create context: unknown error" );
192 pgrguiDestroyContext( pgrGuiContext );
196 pgrguiShowModal( pgrGuiContext, &bResult );
199 SetErrorMessage ("Error - Select Digiclops: cancelled by user" );
200 pgrguiDestroyContext( pgrGuiContext );
203 pgrguiDestroyContext( pgrGuiContext );
206 /* Load the configuration file for the PointGrey camera */
207 #if ( DIGICLOPS_VERSION < 200 )
208 if(config_filename == 0 ||
209 (triclopsError = triclopsGetDefaultContextFromFile(&tcontext, const_cast<char*>(config_filename))))
211 SetErrorMessage ("Error - triclopsGetDefaultContextFromFile: %s", triclopsErrorToString(triclopsError));
215 // if the config file name is null, try getting the configuration from the
217 if( config_filename == 0 )
219 if ( digiclopsGetTriclopsContextFromCamera( digiclops, &tcontext) )
221 SetErrorMessage ("Error - digiclopsGetTriclopsContextFromCamera: " );
225 // try to read the configuration from a file
228 if ( (triclopsError = triclopsGetDefaultContextFromFile(&tcontext, const_cast<char*>(config_filename))))
230 SetErrorMessage ("Error - triclopsGetDefaultContextFromFile: %s", triclopsErrorToString(triclopsError));
236 if(digiclopsError = digiclopsStart(digiclops))
238 SetErrorMessage ("Error - digiclopsStart: %s", digiclopsErrorToString(digiclopsError));
242 #if ( DIGICLOPS_VERSION < 200 )
243 // this call is obsolete for v2.0+ as the system automatically detects
245 if(digiclopsError = digiclopsSetCameraType(digiclops, DIGICLOPS_COLOR))
247 SetErrorMessage ("Error - digiclopsSetCameraType: %s", digiclopsErrorToString(digiclopsError));
252 if(digiclopsError = digiclopsSetImageTypes(digiclops, ALL_IMAGES))
254 SetErrorMessage ("Error - digiclopsSetImageTypes: %s", digiclopsErrorToString(digiclopsError));
258 // Initialize settings...
261 // Reallocate everything based on new rows and cols (if they changed).
262 PGS->OutputBuffers[0] = PGData;
263 for(int i = 1; i < PG_IMAGE_MAX; i++)
265 PGS->OutputBuffers[i] = PGS->OutputBuffers[i - 1] + PGC->MaxCols*PGC->MaxRows*PTGreyGetPixelSize(i - 1);
267 for(i = 0; i < PG_IMAGE_MAX; i++)
269 PGS->DataBuffers[i] = new char[PGC->MaxCols*PGC->MaxRows*PTGreyGetPixelSize(i)];
272 if (PGC->MaxCols*PGC->MaxRows*31 >= PGC->Total_Memory)
274 SetErrorMessage("Insufficient buffer allocated. Len = 0x%x\n", PGC->ncols*PGC->nrows*42 ); Sleep (4000); return 1;
278 PGS->Initializing = true;
279 PGC->NewParameters = 0; // reset for the next time.
281 memset(PGS->NewData, 0, PG_IMAGE_MAX*sizeof(int));
283 #if ( DIGICLOPS_VERSION < 200 )
284 if (PGC->nrows == 640) // Only 2 choices for raw input format.
286 if(digiclopsError = digiclopsSetImageResolution(digiclops, (DigiclopsImageResolution) DIGICLOPS_640x480))
288 SetErrorMessage ("Error - digiclopsSetImageResolution: %s", digiclopsErrorToString(digiclopsError));
294 if(digiclopsError = digiclopsSetImageResolution(digiclops, (DigiclopsImageResolution) DIGICLOPS_320x240))
296 SetErrorMessage ("Error - digiclopsSetImageResolution: %s", digiclopsError);
301 if (PGC->nrows > 320) // Only 2 choices for raw input format.
303 if(digiclopsError = digiclopsSetImageResolution(digiclops, DIGICLOPS_FULL))
305 SetErrorMessage ("Error - digiclopsSetImageResolution: %s", digiclopsErrorToString(digiclopsError));
311 if(digiclopsError = digiclopsSetImageResolution(digiclops, DIGICLOPS_HALF))
313 SetErrorMessage ("Error - digiclopsSetImageResolution: %s", digiclopsError);
319 if(triclopsError = triclopsSetResolution(tcontext, PGC->DRows, PGC->DCols))
321 SetErrorMessage ("Error - triclopsSetResolution: %s", triclopsErrorToString(triclopsError));
325 PGS->Initializing = false;
329 if(triclopsError = triclopsSetImageBuffer(tcontext, (unsigned char *) PGS->DataBuffers[PT_EDGE_IMAGE],
330 TriImg_EDGE, TriCam_REFERENCE))
332 SetErrorMessage ("Error - triclopsSetImageBuffer: %s", triclopsErrorToString(triclopsError));
336 if(triclopsSetResolutionAndPrepare(tcontext, PGC->DRows, PGC->DCols, PGC->nrows, PGC->ncols))
338 SetErrorMessage ("Error - triclopsSetResolutionAndPrepare: %s", triclopsErrorToString(triclopsError)); // for 16-bit disparity buffer
342 triclopsSetSurfaceValidationSize(tcontext, 1000);
343 triclopsSetSurfaceValidationMapping(tcontext, 0xff);
344 triclopsSetSurfaceValidation(tcontext, 1);
346 temp = cvCreateImage(cvSize(PGC->DCols, PGC->DRows), IPL_DEPTH_16U, 1);
347 temp8 = cvCreateImage(cvSize(PGC->DCols, PGC->DRows), IPL_DEPTH_8U, 1);
348 iplSet(temp8, PGC->MinDisparity);
350 // must supply threadId for people running Win95/98
352 PTGreyProcessorThread = CreateThread(0, 0, LPTHREAD_START_ROUTINE(CameraProcessor), 0, 0, &threadId);
353 if ( PTGreyProcessorThread == 0 )
355 DWORD errorId = GetLastError();
356 SetErrorMessage( "Fatal error : CreateThread failed : Error = %d", errorId );
359 PTGreyDataMutex = CreateMutex(0, FALSE, "PTGreyDataMutex");
364 static void _LoadRawImage (int ImageIndex)
366 assert(ImageIndex >= 0 && ImageIndex < 4);
368 DigiclopsError digiclopsError;
369 TriclopsInput& TCIn = TriclopsIn[ImageIndex];
371 int ImageType = PG_DIGICLOPS_IMAGE(ImageIndex);
373 if(digiclopsError = digiclopsExtractTriclopsInput(digiclops, ImageType, &TCIn))
375 SetErrorMessage("Error - digiclopsExtractTriclopsInput: %s", digiclopsErrorToString(digiclopsError));
378 memcpy (PGS->DataBuffers[ImageIndex], (unsigned char *) TCIn.u.rgb32BitPacked.data, TCIn.ncols*TCIn.nrows*4); // 4 bytes per pixel
381 static void LoadRawImage (int ImageIndex) // used to copy raw images from triclops area to shared memory.
383 if(!PGC->ImageList[ImageIndex])
388 _LoadRawImage(ImageIndex);
391 static void LoadComputedImage(int ImageIndex) // used to get Rectified or Edge images.
393 DigiclopsError digiclopsError;
394 TriclopsError triclopsError;
395 if(!PGC->ImageList[ImageIndex])
400 if(ImageIndex == PT_EDGE_IMAGE)
402 if (triclopsError = triclopsGetImage(tcontext, TriImg_EDGE, TriCam_REFERENCE, &t8Image))
404 SetErrorMessage("Error - triclopsGetImage: %s", triclopsErrorToString(triclopsError));
406 memcpy (PGS->DataBuffers[ImageIndex], (unsigned char *) t8Image.data, PGC->DCols*PGC->DRows);
408 else if(ImageIndex == PT_RECTIFIED_IMAGE)
410 PTGreyImageType raw = PT_RIGHT_RAW_IMAGE;
411 TriclopsInput& TCIn = TriclopsIn[raw];
412 if(!PGC->ImageList[raw])
414 // Since this view has not been requested, the digiclopsExtractTriclops has
415 // not been called for it
416 if(digiclopsError = digiclopsExtractTriclopsInput(
417 digiclops, PG_DIGICLOPS_IMAGE(ImageIndex), &TCIn))
418 SetErrorMessage("Error - digiclopsExtractTriclopsInput: %s", digiclopsErrorToString(digiclopsError));
420 if(triclopsError = triclopsPreprocess(tcontext, &TCIn))
421 SetErrorMessage("Error - triclopsPreprocess: %s", triclopsErrorToString(triclopsError));
422 #if ( DIGICLOPS_VERSION < 200 )
423 triclopsRectifyColorImage (tcontext, &TCIn, &t24Image);
425 triclopsRectifyColorImage (tcontext, TriCam_REFERENCE, &TCIn, &t24Image);
427 for (int i = 0, j = 0; i < t24Image.nrows * t24Image.ncols; i++, j += 4)
429 PGS->DataBuffers[ImageIndex][j + 2] = t24Image.red[i];
430 PGS->DataBuffers[ImageIndex][j + 1] = t24Image.green[i];
431 PGS->DataBuffers[ImageIndex][j + 0] = t24Image.blue[i];
440 DWORD __stdcall CameraProcessor(void*)
443 hThread = GetCurrentThread();
444 // int e = SetThreadAffinityMask(hThread, 1); // just the first processor please.
445 CloseHandle (hThread);
448 PGS->BackGroundAvailable = 0;
452 DigiclopsError digiclopsError;
453 TriclopsError triclopsError;
455 if (PGC->NewParameters)
456 { // New settings are present for the camera.
461 return 0; // Requested termination.
463 if(digiclopsError = digiclopsGrabImage(digiclops))
465 SetErrorMessage("Error - digiclopsGrabImage: %s", digiclopsErrorToString(digiclopsError));
469 LoadRawImage(PT_TOP_RAW_IMAGE); // Look for any raw images.
470 LoadRawImage(PT_LEFT_RAW_IMAGE);
471 LoadRawImage(PT_RIGHT_RAW_IMAGE);
475 // Require memory deallocation:
476 for(int i = 0; i < PTFreeFrameCount*4; i++)
478 switch(TriclopsBuffer[i].inputType)
481 free(TriclopsBuffer[i].u.rgb.red);
482 free(TriclopsBuffer[i].u.rgb.green);
483 free(TriclopsBuffer[i].u.rgb.blue);
485 case TriInp_RGB_32BIT_PACKED:
486 free(TriclopsBuffer[i].u.rgb32BitPacked.data);
491 delete TriclopsBuffer;
493 PTFreeFrameCount = 0;
494 PTPrevFrameCount = PTFrameCount; // Initialize pre-frame-count here if frame count = 0
497 if(PTFrameCount != 0)
499 // Loading raw images
500 if(!PGC->ImageList[0])
501 if(digiclopsError = digiclopsExtractTriclopsInput(digiclops, TOP_IMAGE, &TriclopsIn[0]))
502 SetErrorMessage("Error - digiclopsExtractTriclopsInput: %s",
503 digiclopsErrorToString(digiclopsError));
504 if(!PGC->ImageList[1])
505 if(digiclopsError = digiclopsExtractTriclopsInput(digiclops, TOP_IMAGE, &TriclopsIn[1]))
506 SetErrorMessage("Error - digiclopsExtractTriclopsInput: %s",
507 digiclopsErrorToString(digiclopsError));
508 if(!PGC->ImageList[2])
509 if(digiclopsError = digiclopsExtractTriclopsInput(digiclops, TOP_IMAGE, &TriclopsIn[2]))
510 SetErrorMessage("Error - digiclopsExtractTriclopsInput: %s",
511 digiclopsErrorToString(digiclopsError));
512 if(digiclopsError = digiclopsExtractTriclopsInput(digiclops, STEREO_IMAGE, &TriclopsIn[3]))
513 SetErrorMessage("Error - digiclopsExtractTriclopsInput: %s", digiclopsErrorToString(digiclopsError));
515 if(PTFrameNumber == 0)
517 // This is the first time, let's allocate the memory:
518 TriclopsBuffer = new TriclopsInput[PTFrameCount*4];
519 memset(TriclopsBuffer, 0, PTFrameCount*4*sizeof(TriclopsInput));
523 for(int i = 0; i < 4; i++)
525 copyTriclopsInput(TriclopsIn[i], &TriclopsBuffer[4*PTFrameNumber + i]);
528 if(PTFrameNumber == PTFrameCount)
530 // Stop saving frames...
531 PTPrevFrameCount = PTFrameCount;
537 if (PGC->ComputedView != 0 )
539 TriclopsInput& TCIn = TriclopsIn[3]; // For stereo image
540 // If any of the computed views have been requested, then fill it in.
544 // The stereo image has not been extracted yet...
545 if(digiclopsError = digiclopsExtractTriclopsInput(digiclops, STEREO_IMAGE, &TCIn))
546 SetErrorMessage("Error - digiclopsExtractTriclopsInput: %s", digiclopsErrorToString(digiclopsError));
548 if(triclopsError = triclopsPreprocess(tcontext, &TCIn))
549 SetErrorMessage("Error - triclopsPreprocess: %s", triclopsErrorToString(triclopsError));
550 if(triclopsError = triclopsStereo(tcontext))
551 SetErrorMessage("Error - triclopsStereo: %s", triclopsErrorToString(triclopsError));
553 // The disparity, point cloud and background views are interrelated. All need the image16 buffer.
554 if(triclopsError = triclopsGetImage16(tcontext, TriImg16_DISPARITY, TriCam_REFERENCE, &tImage16))
555 SetErrorMessage("Error - triclopsGetImage16: %s", triclopsErrorToString(triclopsError));
557 if (PGC->ImageList[PT_DISPARITY_IMAGE] != 0 )
560 for (int i=0 ;i<PGC->DRows*PGC->DCols; i++)
562 int HighByte = tImage16.data[i];
563 PGS->DataBuffers[PT_DISPARITY_IMAGE][i] = (tImage16.data[i] < 0xff00) ? (char) (HighByte >> 8) : (char) PGC->MinDisparity;
567 IplImage* temp2 = cvCreateImageHeader(cvSize(PGC->DCols, PGC->DRows), IPL_DEPTH_8U, 1);
568 cvSetImageData(temp2, (void*)PGS->DataBuffers[PT_DISPARITY_IMAGE], PGC->DCols);
570 IplImage* temp1 = cvCreateImageHeader(cvSize(PGC->DCols, PGC->DRows), IPL_DEPTH_16U, 1);
571 cvSetImageData(temp1, tImage16.data, tImage16.rowinc);
572 IplImage* mask = iplCreateImageHeader(
573 1, 0, IPL_DEPTH_1U, "GRAY", "GRAY",
574 IPL_DATA_ORDER_PIXEL, IPL_ORIGIN_TL,
575 IPL_ALIGN_DWORD, PGC->DCols, PGC->DRows, NULL, NULL,
577 iplAllocateImage(mask, 0, 0);
579 iplCopy(temp1, temp);
580 iplRShiftS(temp, temp, 8);
581 iplConvert(temp, temp2);
582 iplThreshold(temp, mask, 0x0ff0);
583 cvThreshold(temp2, temp2, 0xf0, 0, CV_THRESH_TOZERO_INV);
584 temp8->maskROI = mask;
585 temp2->maskROI = mask;
586 iplOr(temp8, temp2, temp2);
589 iplDeallocate(mask, IPL_IMAGE_ALL);
590 cvReleaseImageHeader(&temp1);
591 cvReleaseImageHeader(&temp2);
595 if (PGC->ImageList[PT_DISPARITY_OBJ_IMAGE] != 0 )
597 memcpy(PGS->DataBuffers[PT_DISPARITY_OBJ_IMAGE],
598 PGS->DataBuffers[PT_DISPARITY_IMAGE],
599 PGC->DRows*PGC->DCols*PTGreyGetPixelSize(PT_DISPARITY_IMAGE));
602 // Get the edge image
603 LoadComputedImage(PT_EDGE_IMAGE);
605 // If they have requested a color rectified image or rectified image, provide that as well.
606 LoadComputedImage(PT_RECTIFIED_IMAGE);
608 if (PGC->ImageList[PT_BACKGROUND_IMAGE] != 0)
610 memcpy (PGS->DataBuffers[PT_BACKGROUND_IMAGE], (unsigned char *) tImage16.data, PGC->DRows*PGC->DCols*2);
611 PGS->BackGroundAvailable = 1; // Unavailable until we are done averaging.
612 PGC->ImageList[PT_BACKGROUND_IMAGE] = 0;
616 if (PGC->ImageList[PT_DISPARITY_OBJ_IMAGE] && PGS->BackGroundAvailable)
617 // calculate object's disparity
619 for (int i=0; i<PGC->DRows*PGC->DCols; i++)
621 unsigned char* bkg = (unsigned char*)PGS->DataBuffers[PT_BACKGROUND_IMAGE];
622 PGS->DataBuffers[PT_DISPARITY_OBJ_IMAGE][i] = (char) PGC->MinDisparity;
623 if (tImage16.data[i] < 0xff00)
625 // if the point is valid
626 if ((tImage16.data[i] >> 8) > PGC->DisparityThreshold &&
627 (abs(tImage16.data[i] - bkg[i]) >> 8) > PGC->DifferenceThreshold)
629 PGS->DataBuffers[PT_DISPARITY_OBJ_IMAGE][i] = (tImage16.data[i] >> 8);
634 // disparity image rectification procedure
635 if(PGC->ImageList[/*PT_RIGHT_RAW_IMAGE*/PT_RECTIFIED_IMAGE] !=0 && PGC->RectifiedDisparity != 0)
637 // PTRectifyInputDisparity();
640 if(PGC->ImageList[PT_POINTCLOUD_IMAGE])
643 xyzTuple* OGLCloud = (xyzTuple *) PGS->DataBuffers[PT_POINTCLOUD_IMAGE];
644 for (i=0; i<PGC->DRows*PGC->DCols; i++)
646 if (PGS->DataBuffers[PT_DISPARITY_OBJ_IMAGE][i] != PGC->MinDisparity)
648 triclopsRCD16ToXYZ ( tcontext, i/PGC->DCols, i%PGC->DCols, tImage16.data[i], &OGLCloud[Cnt].x, &OGLCloud[Cnt].y, &OGLCloud[Cnt].z);
649 if (OGLCloud[Cnt].z > 0 && OGLCloud[Cnt].z < PGC->ZCapValue)
654 { // make the cleaned up image even cleaner.
655 // PGS->DataBuffers[PT_DISPARITY_IMAGE][i] = (char) PGC->MinDisparity;
656 PGS->DataBuffers[PT_DISPARITY_OBJ_IMAGE][i] = (char) PGC->MinDisparity;
664 // WriteDisparityFile(&tImage16, "e:\\users\\vic\\cvl\\matlab\\data\\disparity.bmp");
667 PGS->ticks = clock();
670 PGS->BackGroundAvailable = 0; // need to recapture background.
674 PTGreyFreezeData(INFINITE);
675 for(int i = 0; i < PG_IMAGE_MAX; i++)
677 if(PGC->ImageList[i])
678 memcpy(PGS->OutputBuffers[i], PGS->DataBuffers[i],
679 PTGreyGetWidth(i)*PTGreyGetHeight(i)*PTGreyGetPixelSize(i));
681 memset(PGS->NewData, 255, PG_IMAGE_MAX*sizeof(int));
682 PTGreyUnFreezeData();
688 PTGREYAPI long PTGreyGetDataSize(int dataType)
692 case PT_TOP_RAW_IMAGE:
693 case PT_LEFT_RAW_IMAGE:
694 case PT_RIGHT_RAW_IMAGE:
696 return PGC->nrows*PGC->ncols*3;
698 case PT_RECTIFIED_IMAGE:
699 case PT_DISPARITY_IMAGE:
700 case PT_DISPARITY_OBJ_IMAGE:
702 return PGC->DRows*PGC->DCols;
704 case PT_BACKGROUND_IMAGE:
705 return PGC->DRows*PGC->DCols*2;
707 case PT_POINTCLOUD_IMAGE:
708 return PGC->DCols*PGC->DRows*12;
716 PTGREYAPI BOOL PTGreyGetData(int dataType, void* pData)
718 int _size = PTGreyGetDataSize(dataType);
719 int width = PTGreyGetWidth(dataType);
720 int height = PTGreyGetHeight(dataType);
723 SetErrorMessage("Data type %d not supported.", dataType);
727 if(!PGC->ImageList[dataType])
729 SetErrorMessage("Data type %d is not selected. Use PTGreySelectDataType.", dataType);
733 WaitForSingleObject(PTGreyDataMutex, INFINITE);
738 case PT_TOP_RAW_IMAGE:
739 case PT_LEFT_RAW_IMAGE:
740 case PT_RIGHT_RAW_IMAGE:
741 case PT_RECTIFIED_IMAGE:
743 for(i = 0; i < width*height; i++)
745 ((char*)pData)[3*i + 2] = PGS->OutputBuffers[dataType][4*i + 2];
746 ((char*)pData)[3*i + 1] = PGS->OutputBuffers[dataType][4*i + 1];
747 ((char*)pData)[3*i] = PGS->OutputBuffers[dataType][4*i];
751 case PT_DISPARITY_IMAGE:
752 case PT_DISPARITY_OBJ_IMAGE:
754 memcpy(pData, PGS->OutputBuffers[dataType], _size);
762 PGS->NewData[dataType] = 0;
764 ReleaseMutex(PTGreyDataMutex);
769 PTGREYAPI BOOL PTGreyFreezeData(int milliSec)
771 int wait = WaitForSingleObject(PTGreyDataMutex, milliSec);
772 if(wait == WAIT_OBJECT_0)
782 PTGREYAPI void PTGreyUnFreezeData()
784 ReleaseMutex(PTGreyDataMutex);
787 PTGREYAPI const char* PTGreyGetDataPointer(int dataType)
789 if(dataType < 0 || dataType > PG_IMAGE_MAX)
792 PGS->NewData[dataType] = 0;
793 return PGS->OutputBuffers[dataType];
796 PTGREYAPI int PTGreyGetWidth(int dataType)
800 case PT_TOP_RAW_IMAGE:
801 case PT_RIGHT_RAW_IMAGE:
802 case PT_LEFT_RAW_IMAGE:
806 case PT_RECTIFIED_IMAGE:
807 case PT_DISPARITY_IMAGE:
808 case PT_DISPARITY_OBJ_IMAGE:
810 case PT_POINTCLOUD_IMAGE:
819 PTGREYAPI int PTGreyGetHeight(int dataType)
823 case PT_TOP_RAW_IMAGE:
824 case PT_RIGHT_RAW_IMAGE:
825 case PT_LEFT_RAW_IMAGE:
829 case PT_RECTIFIED_IMAGE:
830 case PT_DISPARITY_IMAGE:
831 case PT_DISPARITY_OBJ_IMAGE:
833 case PT_POINTCLOUD_IMAGE:
842 PTGREYAPI void PTGreySelectDataType(int dataType, int option)
844 PTGreyFreezeData(INFINITE);
845 if(dataType < 0 || dataType > PG_IMAGE_MAX)
850 PGC->ImageList[dataType] = option;
852 PGC->ComputedView = 0;
853 for(int i = PTGreyImageType(3); i < PG_IMAGE_MAX; i++)
855 PGC->ComputedView = PGC->ComputedView | PGC->ImageList[i];
858 PTGreyUnFreezeData();
861 PTGREYAPI int PTGreyIsNewData(int dataType)
863 if(dataType < 0 || dataType >= PG_IMAGE_MAX)
865 return PGS->NewData[dataType];
868 PTGREYAPI long PTGreyGetPixelSize(int dataType)
872 case PT_TOP_RAW_IMAGE:
873 case PT_LEFT_RAW_IMAGE:
874 case PT_RIGHT_RAW_IMAGE:
875 case PT_RECTIFIED_IMAGE:
880 case PT_DISPARITY_IMAGE:
881 case PT_DISPARITY_OBJ_IMAGE:
884 case PT_BACKGROUND_IMAGE:
887 case PT_POINTCLOUD_IMAGE:
896 PTGREYAPI BOOL PTGreyGetProperty(const char* name, void* buffer)
898 if(strcmp(name, "MinDisparity") == 0)
900 *(int*)buffer = PGC->MinDisparity;
902 else if(strcmp(name, "MaxDisparity") == 0)
904 *(int*)buffer = PGC->MaxDisparity;
906 else if(strcmp(name, "StereoResolution") == 0)
908 *(int*)buffer = PGC->DCols == 640 ? 2 : PGC->DCols == 320 ? 1 : 0;
910 else if(strcmp(name, "RawResolution") == 0)
912 *(int*)buffer = PGC->ncols == 640 ? 1 : 0;
914 else if(strcmp(name, "DifferenceThreshold") == 0)
916 *(float*)buffer = (float)PGC->DifferenceThreshold;
918 else if(strcmp(name, "DisparityThreshold") == 0)
920 *(float*)buffer = (float)PGC->DisparityThreshold;
922 else if(strcmp(name, "DepthThreshold") == 0)
924 *(float*)buffer = PGC->ZCapValue;
926 else if(strcmp(name, "CameraBrightness") == 0)
930 #if ( DIGICLOPS_VERSION < 200 )
931 digiclopsGetVideoProperty(digiclops, DIGICLOPS_BRIGHTNESS, &value, &isauto);
933 digiclopsGetCameraProperty(digiclops, DIGICLOPS_GAIN, &value, &isauto);
936 *(int*)buffer = isauto ? -value : value;
938 else if(strcmp(name, "CameraIris") == 0)
942 #if ( DIGICLOPS_VERSION < 200 )
943 digiclopsGetVideoProperty(digiclops, DIGICLOPS_IRIS, &value, &isauto);
945 digiclopsGetCameraProperty(digiclops, DIGICLOPS_SHUTTER, &value, &isauto);
948 *(int*)buffer = isauto ? -value : -value;
950 else if(strcmp(name, "CameraExposure") == 0)
954 #if ( DIGICLOPS_VERSION < 200 )
955 digiclopsGetVideoProperty(digiclops, DIGICLOPS_EXPOSURE, &value, &isauto);
957 digiclopsGetCameraProperty(digiclops, DIGICLOPS_AUTO_EXPOSURE, &value, &isauto);
960 *(int*)buffer = isauto ? -value : -value;
962 else if(strcmp(name, "") == 0)
964 *(int*)buffer = PGC->RectifiedDisparity;
974 PTGREYAPI BOOL PTGreySetProperty(const char* name, void* buffer)
977 PTGreyFreezeData(INFINITE);
978 if(strcmp(name, "MinDisparity") == 0)
980 PGC->MinDisparity = *(int*)buffer;
982 else if(strcmp(name, "MaxDisparity") == 0)
984 PGC->MaxDisparity = *(int*)buffer;
986 else if(strcmp(name, "StereoResolution") == 0)
988 StereoResolution = *(int*)buffer;
990 else if(strcmp(name, "RawResolution") == 0)
992 RawResolution = *(int*)buffer;
994 else if(strcmp(name, "DifferenceThreshold") == 0)
996 PGC->DifferenceThreshold = (int)*(float*)buffer;
998 else if(strcmp(name, "DisparityThreshold") == 0)
1000 PGC->DisparityThreshold = (int)*(float*)buffer;
1002 else if(strcmp(name, "DepthThreshold") == 0)
1004 PGC->ZCapValue = *(float*)buffer;
1006 else if(strcmp(name, "CameraBrightness") == 0)
1008 int input = *(int*)buffer;
1009 long value = abs(input);
1010 bool isauto = !!(input & 0x80000000);
1011 #if ( DIGICLOPS_VERSION < 200 )
1012 digiclopsSetVideoPropertyAuto(digiclops, DIGICLOPS_BRIGHTNESS, isauto);
1013 digiclopsSetVideoProperty(digiclops, DIGICLOPS_BRIGHTNESS, value);
1015 digiclopsSetCameraProperty(digiclops, DIGICLOPS_GAIN, value, isauto);
1018 else if(strcmp(name, "CameraIris") == 0)
1020 int input = *(int*)buffer;
1021 long value = abs(input);
1022 bool isauto = !!(input & 0x80000000);
1023 #if ( DIGICLOPS_VERSION < 200 )
1024 digiclopsSetVideoPropertyAuto(digiclops, DIGICLOPS_IRIS, isauto);
1025 digiclopsSetVideoProperty(digiclops, DIGICLOPS_IRIS, value);
1027 digiclopsSetCameraProperty(digiclops, DIGICLOPS_SHUTTER, value, isauto);
1030 else if(strcmp(name, "CameraExposure") == 0)
1032 int input = *(int*)buffer;
1033 long value = abs(input);
1034 bool isauto = !!(input & 0x80000000);
1035 #if ( DIGICLOPS_VERSION < 200 )
1036 digiclopsSetVideoPropertyAuto(digiclops, DIGICLOPS_EXPOSURE, isauto);
1037 digiclopsSetVideoProperty(digiclops, DIGICLOPS_EXPOSURE, value);
1039 digiclopsSetCameraProperty(digiclops, DIGICLOPS_AUTO_EXPOSURE, value, isauto);
1042 else if(strcmp(name, "") == 0)
1044 PGC->RectifiedDisparity = *(int*)buffer;
1047 PTGreyUnFreezeData();
1053 PGC->Total_Memory = 0xa000000;
1056 switch(RawResolution)
1071 switch(StereoResolution)
1090 PGC->MinDisparity = 2;
1091 PGC->MaxDisparity = 80;
1093 PGC->SubPixelInterpolation = 1;
1094 PGC->StrictSubPixelValidation = 1;
1096 PGC->StereoMask = 15;
1101 PGC->EdgeCorrelation = 1;
1104 PGC->UniquenessValidation = 0;
1105 PGC->UniquenessValidationThreshold = 0.8f;
1106 PGC->UniquenessValidationMapping = 129;
1108 PGC->TextureValidation = 0;
1109 PGC->TextureValidationThreshold = 110;
1110 PGC->TextureValidationMapping = 130;
1113 PGC->DifferenceThreshold = 10;
1114 PGC->DisparityThreshold = 10;
1115 PGC->ZBucketMin = 50;
1117 PGC->RectifiedDisparity = 1; // rectification disparity option
1120 PTGREYAPI void PTGreyShowPropertyPage()
1122 #if (DIGICLOPS_VERSION < 200 )
1123 digiclopsDisplayPropertyPages(digiclops);
1125 PTGreyShowWhiteBalance();
1130 PTGREYAPI void PTGreyShowWhiteBalance()
1132 #if (DIGICLOPS_VERSION < 200 )
1133 digiclopsDisplayWhiteBalancePropPages(digiclops);
1135 PgrGuiError pgrGuiError;
1136 if ( cameraControlContext == NULL )
1138 pgrGuiError = pgrguiCreateContext( &cameraControlContext,
1140 DIGICLOPS_CAMERA_SETTINGS_DIALOG );
1141 if ( pgrGuiError != PGRGUI_ok )
1143 SetErrorMessage( "Create Camera Control Context failed : %d",
1145 pgrguiDestroyContext( cameraControlContext );
1146 cameraControlContext = NULL;
1151 // check window state
1153 pgrguiGetWindowState( cameraControlContext, &bShowing );
1156 // already showing, so lets pop it down
1157 pgrguiHideModeless( cameraControlContext );
1161 pgrguiShowModeless( cameraControlContext, NULL );
1166 PTGREYAPI void PTGreySetTriclopsInputBuffer(int frames_count)
1168 PTGreyFreezeData(INFINITE);
1169 PTFrameCount = frames_count;
1170 PTFreeFrameCount = PTPrevFrameCount;
1171 PTFrameNumber = 0; /* Start grabbing frames */
1172 PTGreyUnFreezeData();
1175 PTGREYAPI int PTGreyGetFrameNumber()
1177 return PTFrameCount ? PTFrameNumber : -1;
1180 void copyTriclopsInput(TriclopsInput tInput, TriclopsInput* tOutput)
1182 memcpy(tOutput, &tInput, sizeof(TriclopsInput));
1183 if(tInput.inputType == TriInp_RGB_32BIT_PACKED)
1185 int size = tInput.nrows*tInput.ncols*4;
1186 tOutput->u.rgb32BitPacked.data = malloc(size);
1187 memcpy(tOutput->u.rgb32BitPacked.data, tInput.u.rgb32BitPacked.data, size);
1191 int size = tInput.nrows*tInput.ncols;
1192 tOutput->u.rgb.red = malloc(size);
1193 tOutput->u.rgb.green = malloc(size);
1194 tOutput->u.rgb.blue = malloc(size);
1195 memcpy(tOutput->u.rgb.red, tInput.u.rgb.red, size);
1196 memcpy(tOutput->u.rgb.green, tInput.u.rgb.green, size);
1197 memcpy(tOutput->u.rgb.blue, tInput.u.rgb.blue, size);
1201 PTGREYAPI void* PTGreyGetTriclopsBuffer()
1203 return TriclopsBuffer;
1206 PTGREYAPI void PTGreyProcessFrames(void* _buffer, int grabCount, const char* dirName)
1208 TriclopsInput* buffer = (TriclopsInput*)_buffer;
1209 // write the data to a file
1210 for (int i = 0; i < grabCount; i++)
1212 char filename[_MAX_PATH];
1213 // TriclopsInput triclopsTopInput = buffer[4*i];
1214 // TriclopsInput triclopsLeftInput = buffer[4*i + 1];
1215 TriclopsInput triclopsRightInput = buffer[4*i + 2];
1216 TriclopsInput triclopsStereoInput = buffer[4*i+3];
1218 // preprocessing the images
1219 triclopsPreprocess( tcontext, &triclopsStereoInput ) ;
1221 // stereo processing
1222 triclopsStereo( tcontext ) ;
1224 // rectify right color image
1225 sprintf(filename,"%sright_color_rect_%05d.ppm",dirName,i);
1226 printf("writing image %s\n",filename);
1227 TriclopsColorImage colorImage;
1228 #if ( DIGICLOPS_VERSION < 200 )
1229 triclopsRectifyColorImage( tcontext, &triclopsRightInput, &colorImage ) ;
1231 triclopsRectifyColorImage( tcontext, TriCam_REFERENCE, &triclopsRightInput, &colorImage ) ;
1233 triclopsSaveColorImage(&colorImage, filename);
1236 // retrieve the interpolated depth image from the tcontext
1237 TriclopsImage16 depthImage16;
1238 triclopsGetImage16( tcontext, TriImg16_DISPARITY, TriCam_REFERENCE, &depthImage16 ) ;
1240 if (PGS->BackGroundAvailable)
1242 for(int i = 0; i < PGC->DRows*PGC->DCols; i++)
1244 if (depthImage16.data[i] < 0xff00)
1246 unsigned char* bkg = (unsigned char*)PGS->DataBuffers[PT_BACKGROUND_IMAGE];
1247 // if the point is valid
1248 if ((depthImage16.data[i] >> 8) < PGC->DisparityThreshold ||
1249 (abs(tImage16.data[i] - bkg[i]) >> 8) < PGC->DifferenceThreshold)
1251 // Make the pixel invalid...
1252 depthImage16.data[i] = 0xff00;
1258 // sprintf(filename, "%sdisparity_%05d.bmp", dirName, i);
1259 // WriteDisparityFile(&depthImage16, filename);
1261 sprintf(filename,"%spoints_%05d.txt",dirName,i);
1262 printf("writing point file %s\n",filename);
1263 WritePointFile(&depthImage16,filename);
1265 sprintf(filename,"%scolor_points_%05d.txt",dirName,i);
1266 printf("writing point file %s\n",filename);
1267 WriteColorPointFile(&depthImage16,&colorImage,filename);
1271 void WriteDisparityFile(TriclopsImage16* depthImage16, const char* filename)
1273 int w = depthImage16->ncols;
1274 int h = depthImage16->nrows;
1275 // Prepare IplImage header
1276 IplImage* image = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 1);
1277 for(int j = 0; j < h; j++)
1279 for(int i = 0; i < w; i++)
1281 int index = j*w + i;
1282 int disparity = depthImage16->data[j*w + i];
1283 disparity = disparity < 0xff00 ? disparity >> 8 : PGC->MinDisparity;
1284 image->imageData[j*image->widthStep + i] = char(disparity);
1288 save_iplimage(filename, image);
1292 void WritePointFile(TriclopsImage16 *image, char *filename)
1299 filestream = fopen(filename,"w");
1300 FILE* f = fopen("disparity.txt", "w");
1303 int pointCount = PGC->nrows*PGC->ncols;
1304 // fprintf(filestream,"%d\n",pointCount);
1306 // determine the number of pixels spacing per row
1307 int pixelinc = image->rowinc/2;
1308 for ( i = 0; i < image->nrows; i++ )
1310 unsigned short * row = image->data + i * pixelinc;
1311 for ( j = 0; j < image->ncols; j++ )
1315 // filter invalid points
1316 if ( disparity < 0xFF00 )
1318 triclopsRCD16ToXYZ( tcontext, j , i, disparity, &x, &y, &z );
1319 if (z > 0 && z < PGC->ZCapValue) fprintf(filestream, "%f %f %f %d %d\n",x, y, z, i, j);
1320 // fprintf(f, "%d ", char(disparity >> 8));
1323 fprintf(f, "%d ", char(PGC->MinDisparity));
1333 void WriteColorPointFile(TriclopsImage16 *image, TriclopsColorImage *colorImage, char *filename)
1341 filestream = fopen(filename,"w");
1344 int pointCount = PGC->nrows*PGC->ncols;
1345 // fprintf(filestream,"%d\n",pointCount);
1347 // determine the number of pixels spacing per row
1349 int pixelinc = image->rowinc/2;
1350 for ( i = 0; i < image->nrows; i++ )
1352 unsigned short * row = image->data + i * pixelinc;
1353 for ( j = 0; j < image->ncols; j++ )
1357 // filter invalid points
1358 if ( disparity < 0xFF00 )
1360 triclopsRCD16ToXYZ( tcontext, i , j, disparity, &x, &y, &z );
1361 if (z > 0 && z < PGC->ZCapValue) {
1362 r = (int) colorImage->red[pixelIndex];
1363 g = (int) colorImage->green[pixelIndex];
1364 b = (int) colorImage->blue[pixelIndex];
1365 fprintf( filestream, "%d %d %f %f %f %d %d %d\n", x, y, z, r, g, b );
1370 // go to the next pixel
1379 PTGREYAPI int PTGreyGetPointCount()
1384 PTGREYAPI void* PTGreyGetTriclopsImage16()
1389 PTGREYAPI __int64 PTGreyGetTime()
1391 return __int64(TriclopsIn[3].timeStamp.sec)*1000 + __int64(TriclopsIn[3].timeStamp.u_sec)/1000;
1394 BOOL PTRectifyInputDisparity()
1397 IplImage *inputImage, *image_hsv, *img_p[2], *img_v, *image_disp, *contour_mask;
1398 // IplImage *resizeImage;
1399 CvSeq *contour, *cont_max;
1401 double parameter[2];
1403 // unsigned char *clean;
1408 CvMemStorage* temp_storage = cvCreateMemStorage( 65536 );
1409 int header_size = sizeof(CvContour);
1413 // Ncols = PGC->ncols;
1414 // Nrows = PGC->nrows;
1416 inputImage = cvCreateImage(cvSize(Ncols, Nrows), IPL_DEPTH_8U, 3);
1418 // resizeImage = cvCreateImage(cvSize(PGC->DCols, PGC->DRows), IPL_DEPTH_8U, 3);
1420 char* data = inputImage->imageData;
1421 int step = inputImage->widthStep;
1423 for (i = 0; i < PGC->DRows; i++)
1425 for (j = 0; j < PGC->DCols; j++)
1427 int index = i*step + 3*j;
1428 int outind = i*PGC->DCols + j;
1430 data[index] = t24Image.red[outind];
1431 data[index + 1] = t24Image.green[outind];
1432 data[index + 2] = t24Image.blue[outind];
1437 image_hsv = iplCreateImageHeader(3, 0, IPL_DEPTH_8U, "HSV", "HSV", IPL_DATA_ORDER_PIXEL, IPL_ORIGIN_TL, IPL_ALIGN_QWORD,
1438 PGC->DCols, PGC->DRows, NULL, NULL, NULL, NULL);
1439 printf("\n Create image_hsv header !!!");
1440 iplAllocateImage(image_hsv, 0, 0 ); // allocate image data
1442 inputImage->roi = 0;
1443 // resizeImage->roi = 0;
1445 // m_outputImage = cvCreateImage(cvSize(m_ncols, m_nrows), IPL_DEPTH_8U, 3);
1446 // m_outputImage->roi = 0;
1448 // iplResize(inputImage, resizeImage, Ncols, PGC->DCols, Nrows, PGC->DRows, IPL_INTER_NN);
1450 iplRGB2HSV(inputImage, image_hsv);
1451 // iplRGB2HSV(resizeImage, image_hsv);
1453 img_p[0] = cvCreateImage(cvSize(PGC->DCols, PGC->DRows), IPL_DEPTH_8U, 1);
1454 img_p[1] = cvCreateImage(cvSize(PGC->DCols, PGC->DRows), IPL_DEPTH_8U,1);
1455 img_v = cvCreateImage(cvSize(PGC->DCols, PGC->DRows), IPL_DEPTH_8U,1);
1457 // printf("\n Create img_p !!!");
1459 cvCvtPixToPlane(image_hsv, img_p[0], img_p[1], img_v, NULL);
1462 parameter[0] = (double)2; /* size */
1463 parameter[1] = (double)10; /* minDisp */
1464 cvAdaptiveThreshold( img_p[1], img_v, (double)255, CV_STDDEV_ADAPTIVE_THRESH, CV_THRESH_BINARY, parameter);
1466 // iplThreshold(img_p[1], img_p[1], 110);
1467 cvFindContours (/*img_p[1]*/img_v, temp_storage, &contour, header_size,
1468 CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE);
1470 // float parameter =1.;
1471 // cvApproxPoly (contour, header_size, temp_storage, &contour_a, (CvPolyApproxMethod)0, parameter );
1473 // find the contour with maximum disparity
1474 double max_disparity = 0;
1476 contour_mask = cvCreateImage(cvSize(PGC->DCols, PGC->DRows), IPL_DEPTH_8U, 1);
1478 image_disp = cvCreateImageHeader(cvSize(PGC->DCols, PGC->DRows), IPL_DEPTH_8U, 1);
1480 cvSetImageData( image_disp, (char*) PGS->DataBuffers[PT_DISPARITY_OBJ_IMAGE], PGC->DCols );
1482 // image_disp1 = cvCreateImage(cvSize(PGC->DCols, PGC->DRows), IPL_DEPTH_8U, 1);
1484 // iplResize(image_disp, image_disp1, PGC->DCols, PGC->DCols, PGC->DRows, PGC->DRows, IPL_INTER_NN);
1488 while(contour!=NULL)
1490 if( contour->total > 30)
1492 iplSet(contour_mask, 0/*PGC->MinDisparity*/);
1493 big_t = (CvPoint*)malloc(contour->total * sizeof(CvPoint));
1495 cvCvtSeqToArray(contour, big_t );
1496 cvFillPoly (contour_mask, &big_t, &contour->total, 255, 1 /*PGC->MaxDisparity*/);
1497 // m_outputImage->roi->coi = 1;
1499 iplAnd(image_disp, contour_mask, contour_mask);
1500 double mean = cvMean(contour_mask);
1502 if(mean > max_disparity)
1504 max_disparity = mean;
1509 contour = contour->h_next;
1512 iplSet(contour_mask, 0);
1513 big_t = (CvPoint*)malloc(cont_max->total * sizeof(CvPoint));
1515 // printf("\n cont_max !!! min_disp = %d max_disparity = %f cont = %d", PGC->MinDisparity, max_disparity, cont_max->total);
1517 cvCvtSeqToArray(cont_max, big_t );
1518 cvFillPoly (contour_mask, &big_t, &cont_max->total, 255, 1);
1520 iplSubtractS(image_disp, image_disp,PGC->MinDisparity, false);
1521 iplAnd(image_disp, contour_mask, image_disp);
1522 iplAddS(image_disp, image_disp, PGC->MinDisparity);
1526 // iplResize(image_disp1, image_disp, PGC->DCols, PGC->DCols, PGC->DRows, PGC->DRows, IPL_INTER_NN);
1527 // printf("\n Resize !!!");
1529 cvReleaseImage(&img_p[0]);
1530 cvReleaseImage(&img_p[1]);
1531 cvReleaseImage(&img_v);
1532 iplDeallocateImage(image_hsv);
1533 cvReleaseImageHeader(&image_disp);
1534 // cvReleaseImage(&image_disp1);
1535 cvReleaseImage(&contour_mask);
1537 cvReleaseMemStorage( &temp_storage );
1543 PTGREYAPI void PTGreyExitCamera(int millisec)
1545 cvReleaseImage(&temp);
1547 // for(int i = 0; i < PG_IMAGE_MAX; i++)
1549 // delete PGS->DataBuffers[i];
1552 PGC->PGTerminate = 1;
1553 WaitForSingleObject(PTGreyProcessorThread, millisec);
1556 PTGREYAPI void* PTGreyGetTriclopsContext()
1561 PTGREYAPI void PTGreySetConfigFileName(const char* config)
1565 config_filename = strdup(config);