00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 #if (defined(STOP_BEFORE_EXIT) || defined(LOOP_TEST))
00066 #include <conio.h>
00067 #endif
00068
00069 #ifdef STOP_BEFORE_EXIT
00070
00071 #define EXIT_MACRO printf("Press any key to exit...\n"); \
00072 while (kbhit()) {getch();} \
00073 getch();
00074 #else
00075 #define EXIT_MACRO
00076 #endif
00077
00078
00079
00080
00081 #include <stdio.h>
00082 #include "VidCapture.h"
00083
00084
00085
00086 #include "CVUtil.h"
00087
00092 bool TestIt();
00093
00100 bool enumCallback( const char* devname,
00101 void* userParam);
00102
00110 bool capCallback ( CVRES status,
00111 CVImage* image,
00112 void* userParam);
00113
00120 void SlowNegateImage(CVImage* image);
00121
00122
00123 char gDeviceName[256];
00124
00125
00126 int gFrameNum = 0;
00127
00128
00129 int gDevNum = 0;
00130
00131
00137 void main()
00138 {
00139 printf("-------------------------------------------------------\n");
00140 printf("CodeVis VidCapture Test Program \n");
00141 printf("%s",kVIDCAPTURE_STRING);
00142 printf("-------------------------------------------------------\n");
00143
00144 #ifdef LOOP_TEST
00145 printf("Looping until you press a key.\n");
00146
00147 while (!kbhit())
00148 {
00149
00150 memset(gDeviceName,0,sizeof(gDeviceName));
00151 gFrameNum = 0;
00152 gDevNum = 0;
00153 #endif // LOOP_TEST
00154
00155 static int testRunNumber = 1;
00156
00157 if (false == TestIt())
00158 {
00159 printf("************* Test run %d failed!\n", testRunNumber);
00160 EXIT_MACRO
00161 return;
00162 }
00163 else
00164 {
00165 printf("Test run %d succeeded.\n", testRunNumber++);
00166 }
00167
00168 #ifdef LOOP_TEST
00169 }
00170 #endif
00171
00172 EXIT_MACRO
00173 }
00174
00175
00176
00177 bool TestIt()
00178 {
00179
00180
00181
00182 CVVidCapture* vidCap = CVPlatform::GetPlatform()->AcquireVideoCapture();
00183
00184
00185
00186 if (CVSUCCESS(vidCap->Init()))
00187 {
00188 printf("Initialized video capture object.\n");
00189 }
00190 else
00191 {
00192 printf("Error initializing video capture object.\n");
00193
00194
00195
00196
00197
00198
00199 CVPlatform::GetPlatform()->Release(vidCap);
00200 return false;
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 memset(gDeviceName,0,256);
00214
00215 int numDevices = 0;
00216 if (CVFAILED(vidCap->GetNumDevices(numDevices)))
00217 {
00218 printf("Failed enumerating devices.\n");
00219 vidCap->Uninit();
00220 CVPlatform::GetPlatform()->Release(vidCap);
00221 return false;
00222 }
00223
00224 CVVidCapture::VIDCAP_DEVICE devInfo;
00225
00226 int curDevIndex = 0;
00227 for (curDevIndex = 0; curDevIndex < numDevices; curDevIndex++)
00228 {
00229 if (CVSUCCESS(vidCap->GetDeviceInfo(curDevIndex,devInfo)))
00230 {
00231 printf("Device %d: %s\n",curDevIndex,devInfo.DeviceString);
00232 }
00233 }
00234
00235
00236 if (CVSUCCESS(vidCap->Connect(1)))
00237 {
00238 int devNameLen = 0;
00239 vidCap->GetDeviceName(0,devNameLen);
00240 devNameLen++;
00241 char *devName = new char[devNameLen];
00242 vidCap->GetDeviceName(devName,devNameLen);
00243
00244 printf("Connection succeeded to %s.\n",devName);
00245 delete [] devName;
00246 }
00247 else
00248 {
00249 printf("Connection failed.\n");
00250 vidCap->Uninit();
00251 CVPlatform::GetPlatform()->Release(vidCap);
00252 return false;
00253 }
00254
00255
00256
00257
00258
00259 CVVidCapture::VIDCAP_MODE modeInfo;
00260 int numModes = 0;
00261 vidCap->GetNumSupportedModes(numModes);
00262
00263
00264 for (int curmode = 0; curmode < numModes; curmode++)
00265 {
00266 if (CVSUCCESS(vidCap->GetModeInfo(curmode, modeInfo)))
00267 {
00268 printf( "Available mode: %d, %dx%d @ %d frames/sec (%s)\n",
00269 curmode,
00270 modeInfo.XRes,
00271 modeInfo.YRes,
00272 modeInfo.EstFrameRate,
00273 vidCap->GetFormatModeName(modeInfo.InputFormat));
00274 }
00275 }
00276
00277
00278 if (CVFAILED(vidCap->SetMode(0)))
00279
00280 {
00281 printf("Error setting video mode.\n");
00282 }
00283
00284
00285 if (CVFAILED(vidCap->GetCurrentMode(modeInfo)))
00286 {
00287 printf("Error activating mode!\n");
00288 }
00289 else
00290 {
00291 printf("Activated mode: %dx%d\n",modeInfo.XRes, modeInfo.YRes);
00292 }
00293
00294
00295
00296 long minval,maxval;
00297 if (CVSUCCESS(vidCap->GetPropertyInfo( CVVidCapture::CAMERAPROP_CONTRAST,
00298 0,
00299 0,
00300 &minval,
00301 &maxval)))
00302 {
00303 printf("Contrast supported\n", minval, maxval);
00304 vidCap->SetProperty( CVVidCapture::CAMERAPROP_CONTRAST,
00305 (minval+maxval)/2);
00306 }
00307 else
00308 {
00309 printf("Contrast unsupported.\n");
00310 }
00311
00312
00313 if (CVSUCCESS(vidCap->GetPropertyInfo( CVVidCapture::CAMERAPROP_BRIGHT,
00314 0,
00315 0,
00316 &minval,
00317 &maxval)))
00318 {
00319 printf("Brightness supported\n", minval, maxval);
00320 vidCap->SetProperty( CVVidCapture::CAMERAPROP_BRIGHT,
00321 (minval+maxval)/2);
00322 }
00323 else
00324 {
00325 printf("Brightness unsupported.\n");
00326 }
00327
00328
00329
00330 CVImage* grabImage = 0;
00331
00332 printf("Grabbing grey image...\n");
00333 if (CVSUCCESS(vidCap->Grab(CVImage::CVIMAGE_GREY, grabImage)))
00334 {
00335 if (CVFAILED(CVImage::Save("GreyGrab",grabImage)))
00336 {
00337 printf("Failed saving grey image.");
00338 }
00339
00340
00341 CVImage::ReleaseImage(grabImage);
00342 }
00343 else
00344 {
00345 printf("Grey Grab failed.\n");
00346 }
00347
00348
00349 printf("Grabbing color image...\n");
00350 if (CVSUCCESS(vidCap->Grab(CVImage::CVIMAGE_RGB24, grabImage)))
00351 {
00352 if (CVFAILED(CVImage::Save("ColorGrab",grabImage)))
00353 {
00354 printf("Failed saving color image.");
00355 }
00356
00357
00358 CVImage::ReleaseImage(grabImage);
00359 }
00360 else
00361 {
00362 printf("Color Grab failed.\n");
00363 }
00364
00365
00366
00367
00368
00369
00370 bool started = false;
00371
00372 printf("Starting grey image capture...\n");
00373
00374 started = CVSUCCESS(vidCap->StartImageCap(CVImage::CVIMAGE_GREY, capCallback,0));
00375 if (!started)
00376 {
00377 printf("Error starting capture....\n");
00378 }
00379 else
00380 {
00381
00382 Sleep(2000);
00383
00384
00385 printf("Stopping image capture...\n");
00386 vidCap->Stop();
00387 }
00388
00389
00390 printf("Starting color image capture...\n");
00391 started = CVSUCCESS(vidCap->StartImageCap(CVImage::CVIMAGE_RGB24, capCallback,0));
00392 if (!started)
00393 {
00394 printf("Error starting capture....\n");
00395 }
00396 else
00397 {
00398
00399 Sleep(2000);
00400
00401
00402 printf("Stopping image capture...\n");
00403 vidCap->Stop();
00404 }
00405
00406
00407 printf("Disconnecting...\n");
00408 vidCap->Disconnect();
00409
00410
00411 printf("Uninitializing video capture...\n");
00412 vidCap->Uninit();
00413
00414
00415 CVPlatform::GetPlatform()->Release(vidCap);
00416 return true;
00417 }
00418
00419
00420
00421
00422
00423
00424 bool enumCallback(const char* devname, void* userParam)
00425 {
00426
00427
00428 if (gDevNum <= 1)
00429 {
00430 strcpy(gDeviceName,devname);
00431 }
00432
00433 gDevNum++;
00434
00435 printf("Device: %s\n",devname);
00436 return true;
00437 }
00438
00439
00440
00441
00442
00443
00444
00445
00446 bool capCallback( CVRES status,
00447 CVImage* imagePtr,
00448 void* userParam)
00449 {
00450 static int xOffset = 0;
00451 static int yOffset = 0;
00452
00453
00454
00455 if (CVSUCCESS(status))
00456 {
00457 char framePath[_MAX_PATH];
00458 sprintf(framePath,"cap_frame_%d",gFrameNum);
00459
00460 CVAssert(imagePtr != 0, "This shouldn't happen. Bad image pointer.");
00461
00462 printf("Captured frame %d.\n", gFrameNum);
00463
00464
00465 CVImage* subImg = 0;
00466 int subWidth = imagePtr->Width() / 4;
00467 int subHeight = imagePtr->Height() / 5;
00468
00469 if (CVFAILED(CVImage::CreateSub( imagePtr,
00470 subImg,
00471 xOffset,
00472 yOffset,
00473 subWidth,
00474 subHeight)))
00475 {
00476 printf("Failed creating sub image!\n");
00477 }
00478 else
00479 {
00480
00481 SlowNegateImage(subImg);
00482
00483
00484 CVImage *subSubImg = 0;
00485 if (CVFAILED(CVImage::CreateSub( subImg,
00486 subSubImg,
00487 5,
00488 5,
00489 subWidth-10,
00490 subHeight-10)))
00491 {
00492 printf("Failed creating sub image of sub image\n");
00493
00494 CVImage::ReleaseImage(subImg);
00495 }
00496 else
00497 {
00498
00499
00500 CVImage::ReleaseImage(subImg);
00501
00502
00503
00504
00505 SlowNegateImage(subSubImg);
00506
00507
00508 CVImage::ReleaseImage(subSubImg);
00509 }
00510
00511
00512 xOffset += subWidth;
00513 yOffset += subHeight;
00514
00515 if (imagePtr->Width() - xOffset < subWidth)
00516 {
00517 xOffset = 0;
00518 }
00519 if (imagePtr->Height() - yOffset < subHeight)
00520 {
00521 yOffset = 0;
00522 }
00523
00524 }
00525
00526
00527 if (CVFAILED(CVImage::Save(framePath, imagePtr)))
00528 {
00529 printf("Failed saving image!\n");
00530 }
00531
00532 gFrameNum++;
00533 }
00534 else
00535 {
00536
00537
00538
00539
00540
00541
00542
00543 printf("Capture failure in callback! Did you disconnect camera?\n");
00544 }
00545
00546
00547 if (gFrameNum == 25)
00548 {
00549 printf("Halting prematurely to test callback exit codes.\n");
00550 printf("Returning false here should abort the capture.\n");
00551
00552
00553 return false;
00554 }
00555 return true;
00556 }
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566 void SlowNegateImage(CVImage* image)
00567 {
00568
00569
00570
00571
00572 for (int x = 0; x < image->Width(); x++)
00573 {
00574 for (int y=0; y < image->Height(); y++)
00575 {
00576 float r,g,b;
00577 if (CVFAILED(image->GetPixel(x,y,r,g,b)))
00578 {
00579 printf("GetPixel Error!\n");
00580 }
00581 else
00582 {
00583
00584
00585 r = 255.0f - r;
00586 g = 255.0f - g;
00587 b = 255.0f - b;
00588
00589 if (CVFAILED(image->SetPixel(x,y,r,g,b)))
00590 {
00591 printf("SetPixel Error!\n");
00592 }
00593 }
00594 }
00595 }
00596 }