⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hv_dc1394.cpp

📁 tracciatore di mani con webcam
💻 CPP
📖 第 1 页 / 共 2 页
字号:
  int                       i;  for (i = 0; i < numCameras; i++) {    dc1394_dma_unlisten(handles[cameras[i].port], &cameras[i]);    dc1394_dma_release_camera(handles[cameras[i].port], &cameras[i]);  }  for (i = 0; i < numPorts; i++)    raw1394_destroy_handle(handles[i]);  if ((void *) window != NULL)    XUnmapWindow(display, window);  if (display != NULL)    XFlush(display);  if (frame_buffer != NULL)    free(frame_buffer);  // OpenCV cleanup  for (i = 0; i < numCameras; i++) {    cvReleaseImageHeader(&iplImages[i]);  }}voidhandle_events(){  XEvent                    xev;  while (XPending(display) > 0) {    XNextEvent(display, &xev);    switch (xev.type) {    case ConfigureNotify:      width = xev.xconfigure.width;      height = xev.xconfigure.height;      //      capture_frame();      //      process_frame();      display_frame();      break;    case KeyPress:      switch (XKeycodeToKeysym(display, xev.xkey.keycode, 0)) {      case XK_q:      case XK_Q:      case XK_Escape:        cleanup();        exit(0);        break;      case XK_r:      case XK_R:	hvStopRecognition();	hvStartRecognition();        break;      case XK_u:      case XK_U:        if (hvCanCorrectDistortion()) {          hvCorrectDistortion(!hvIsCorrectingDistortion());        }        break;      case XK_comma:      case XK_less:        width = (width*3)/4;        height = (height*3)/4;        XResizeWindow(display, window, width, height);        //        capture_frame();        //        process_frame();        display_frame();        break;      case XK_period:      case XK_greater:        width = (int) (1.25 * width);        height = (int) (1.25 * height);        XResizeWindow(display, window, width, height);        //        capture_frame();        //        process_frame();        display_frame();        break;      case XK_0:	hvSetOverlayLevel(0);        break;      case XK_1:	hvSetOverlayLevel(1);        break;      case XK_2:	hvSetOverlayLevel(2);        break;      case XK_3:	hvSetOverlayLevel(3);        break;      }      break;    }  }}voidQueryXv(){  int                       num_adaptors;  int                       num_formats;  XvImageFormatValues      *formats = NULL;  int                       i, j;  char                      xv_name[5];  XvQueryAdaptors(display, DefaultRootWindow(display),		  (unsigned int*) &num_adaptors, &info);  for (i = 0; i < num_adaptors; i++) {    formats = XvListImageFormats(display, info[i].base_id, &num_formats);    for (j = 0; j < num_formats; j++) {      xv_name[4] = 0;      memcpy(xv_name, &formats[j].id, 4);      if (formats[j].id == format) {        if (verbose) 	  fprintf(stderr, "using Xv format 0x%x %s %s\n", formats[j].id,		  xv_name,		  (formats[j].format == XvPacked) ? "packed" : "planar");        if (adaptor < 0)          adaptor = i;      }    }  }  XFree(formats);  if (adaptor < 0)    fprintf(stderr, "No suitable Xv adaptor found");}/* trap ctrl-c */voidsignal_handler(int sig){  signal(SIGINT, SIG_IGN);  cleanup();  exit(0);}voidset_manual_exposure_gain(int camID, int exposure, int gain){  int                       success =    dc1394_auto_on_off(handles[cameras[camID].port], cameras[camID].node,                       FEATURE_EXPOSURE, DC1394_FALSE);  if (success != DC1394_SUCCESS) {    fprintf(stderr, "can not turn off camera auto exposure\n");    return;  }  success =    dc1394_set_exposure(handles[cameras[camID].port], cameras[camID].node,                        exposure);  if (success != DC1394_SUCCESS) {    fprintf(stderr, "set_exposure returned %d\n", success);    return;  }  success =    dc1394_set_gain(handles[cameras[camID].port], cameras[camID].node, gain);  if (success != DC1394_SUCCESS) {    fprintf(stderr, "set_gain returned %d\n", success);    return;  }}intmain(int argc, char *argv[]){  XGCValues                 xgcv;  long                      background = 0x010203;  unsigned int              channel;  unsigned int              speed;  int                       i, p, cn;  raw1394handle_t           raw_handle;  struct raw1394_portinfo   ports[MAX_PORTS];  get_options(argc, argv);  /* process options */  switch (fps) {  case 1:    fps = FRAMERATE_1_875;    break;  case 3:    fps = FRAMERATE_3_75;    break;  case 15:    fps = FRAMERATE_15;    break;  case 30:    fps = FRAMERATE_30;    break;  case 60:    fps = FRAMERATE_60;    break;  default:    fps = FRAMERATE_7_5;    break;  }  switch (res) {  case 1:    res = MODE_640x480_YUV411;    device_width = 640;    device_height = 480;    format = XV_YUY2;    break;  case 2:    res = MODE_640x480_RGB;    device_width = 640;    device_height = 480;    format = XV_YUY2;    break;  default:    res = MODE_320x240_YUV422;    device_width = 320;    device_height = 240;    format = XV_UYVY;    break;  }  /* get the number of ports (cards) */  raw_handle = raw1394_new_handle();  if (raw_handle == NULL) {    perror("Unable to aquire a raw1394 handle\n");    perror("did you load the drivers?\n");    exit(-1);  }  numPorts = raw1394_get_port_info(raw_handle, ports, numPorts);  raw1394_destroy_handle(raw_handle);  if (verbose) printf("number of ports = %d\n", numPorts);  /* get dc1394 handle to each port */  for (p = 0; p < numPorts; p++) {    int                       camCount;    handles[p] = dc1394_create_handle(p);    if (handles[p] == NULL) {      perror("Unable to aquire a raw1394 handle\n");      perror("did you load the drivers?\n");      cleanup();      exit(-1);    }    /* get the camera nodes and describe them as we find them */    camera_nodes = dc1394_get_camera_nodes(handles[p], &camCount, verbose);    /* setup cameras for capture */    for (i = 0; i < camCount; i++) {      cameras[numCameras].node = camera_nodes[i];      if (dc1394_get_camera_feature_set          (handles[p], cameras[numCameras].node,           &features) != DC1394_SUCCESS) {        printf("unable to get feature set\n");      } else if (verbose) {        dc1394_print_feature_set(&features);      }      if (dc1394_get_iso_channel_and_speed          (handles[p], cameras[numCameras].node, &channel,           &speed) != DC1394_SUCCESS) {        printf("unable to get the iso channel number\n");        cleanup();        exit(-1);      }      if (dc1394_dma_setup_capture          (handles[p], cameras[numCameras].node, i + 1 /*channel */ ,           FORMAT_VGA_NONCOMPRESSED, res, SPEED_400, fps, NUM_BUFFERS,           DROP_FRAMES, device_name,           &cameras[numCameras]) != DC1394_SUCCESS) {        fprintf(stderr,                "unable to setup camera - check line %d of %s to make sure\n",                __LINE__, __FILE__);        perror("that the video mode, framerate and format are supported\n");        printf("by your camera(s)\n");        cleanup();        exit(-1);      }      /*have the camera start sending us data */      if (dc1394_start_iso_transmission          (handles[p], cameras[numCameras].node) != DC1394_SUCCESS) {        perror("unable to start camera iso transmission\n");        cleanup();        exit(-1);      }      numCameras++;    }  }  fflush(stdout);  if (numCameras < 1) {    perror("no cameras found :(\n");    cleanup();    exit(-1);  }  //set_manual_exposure_gain(0, 440, 30);  switch (format) {  case XV_YV12:    set_frame_length(device_width * device_height * 3 / 2, numCameras);    break;  case XV_YUY2:  case XV_UYVY:    set_frame_length(device_width * device_height * 2, numCameras);    break;  default:    fprintf(stderr, "Unknown format set (internal error)\n");    exit(255);  }  /* create OpenCV image wrappers */  for (cn = 0; cn < MAX_CAMERAS; cn++) {    if (cn < numCameras) {      iplImages[cn] =        cvCreateImage(cvSize(device_width, device_height),		      IPL_DEPTH_8U, 3);      readOnlyImg =        cvCreateImageHeader(cvSize(device_width, device_height),                            IPL_DEPTH_8U, 3);    } else {      iplImages[cn] = NULL;    }  }  /* initialize handvu */  hvInitialize(device_width, device_height);  hvLoadConductor(string(conductor_fname));  hvStartRecognition();  hvSetOverlayLevel(3);  if (async_processing) {    hvAsyncSetup(num_async_bufs, displayCallback);    hvAsyncGetImageBuffer(&m_async_image, &m_async_bufID);    if (sync_display) async_display_image = cvCloneImage(iplImages[0]);  }    /* make the window */  display = XOpenDisplay(getenv("DISPLAY"));  if (display == NULL) {    fprintf(stderr, "Could not open display \"%s\"\n", getenv("DISPLAY"));    cleanup();    exit(-1);  }  QueryXv();  if (adaptor < 0) {    cleanup();    exit(-1);  }  width = device_width;  height = device_height * numCameras;  window =    XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0,                        width, height, 0, WhitePixel(display,                                                     DefaultScreen                                                     (display)), background);  XSelectInput(display, window, StructureNotifyMask | KeyPressMask);  XMapWindow(display, window);  connection = ConnectionNumber(display);  gc = XCreateGC(display, window, 0, &xgcv);  /* local main event loop */  while (1) {    if (async_processing) {      // asynchronous processing in HandVu      capture_frame();      process_frame();      if (sync_display) display_frame();      handle_events();          } else {      // synchronous processing in HandVu      capture_frame();      process_frame();      display_frame();      handle_events();    }    /* XPending */  }                             /* while not interrupted */  exit(0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -