Class: OpenCV::CvCapture

Inherits:
Data
  • Object
show all
Defined in:
ext/opencv/cvcapture.cpp,
ext/opencv/cvcapture.cpp

Overview

Class for video capturing from video files or cameras

Constant Summary collapse

INTERFACE =

:any, :mil, :vfw, :v4l, :v4l2, :fireware, :ieee1394, :dc1394, :cmu1394, :stereo, :tyzx, :tyzx_left, :tyzx_right, :tyzx_color, :tyzx_z, :qt, :qtuicktime

video_interface

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.open(dev = nil) ⇒ CvCapture

Open video file or a capturing device for video capturing

Parameters:

  • dev (String, Fixnum, Simbol, nil) (defaults to: nil)

    Video capturing device

    • If dev is a string (i.e “stream.avi”), reads video stream from a file.

    • If dev is a number or symbol (included in CvCapture::INTERFACE), reads video stream from a device.

    • If dev is a nil, same as CvCapture.open(:any)

Returns:


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'ext/opencv/cvcapture.cpp', line 50

VALUE
rb_open(int argc, VALUE *argv, VALUE self)
{
  VALUE device;
  rb_scan_args(argc, argv, "01", &device);
  CvCapture *capture = 0;
  sCvCapture *scap = new sCvCapture();
  try {
    switch (TYPE(device)) {
    case T_STRING:
      capture = cvCaptureFromFile(StringValueCStr(device));
      break;
    case T_FIXNUM:
      capture = cvCaptureFromCAM(FIX2INT(device));
      break;
    case T_SYMBOL: {
      VALUE cap_index = rb_hash_lookup(rb_const_get(rb_class(), rb_intern("INTERFACE")), device);
      if (NIL_P(cap_index))
        rb_raise(rb_eArgError, "undefined interface.");
      capture = cvCaptureFromCAM(NUM2INT(cap_index));
      break;
    }
    case T_NIL:
      capture = cvCaptureFromCAM(CV_CAP_ANY);
      break;
    }
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  if (!capture)
    rb_raise(rb_eStandardError, "Invalid capture format.");
  scap->ptr = capture;
  scap->opened = true;
  return Data_Wrap_Struct(rb_klass, 0, cvcapture_free, scap);
}

Instance Method Details

#avi_ratioNumber

Get relative position of video file

Returns:

  • (Number)

    Relative position of video file (0: Start of the film, 1: End of the film)


268
269
270
271
272
# File 'ext/opencv/cvcapture.cpp', line 268

VALUE
rb_get_avi_ratio(VALUE self)
{
  return rb_get_capture_property(self, CV_CAP_PROP_POS_AVI_RATIO);
}

#avi_ratio=Number

Set relative position of video file

Parameters:

  • value (Number)

    Relative position of video file (0: Start of the film, 1: End of the film)

Returns:

  • (Number)

280
281
282
283
284
# File 'ext/opencv/cvcapture.cpp', line 280

VALUE
rb_set_avi_ratio(VALUE self, VALUE value)
{
  return rb_set_capture_property(self, CV_CAP_PROP_POS_AVI_RATIO, value);
}

#brightnessNumber

Get brightness of the image (only for cameras)

Returns:

  • (Number)

    Brightness


462
463
464
465
466
# File 'ext/opencv/cvcapture.cpp', line 462

VALUE
rb_get_brightness(VALUE self)
{
  return rb_get_capture_property(self, CV_CAP_PROP_BRIGHTNESS);
}

#closeboolean

Releases an opened video file or a capturing device

Returns:

  • (boolean)

    False if the device was already closed


92
93
94
95
96
97
98
99
100
101
102
103
# File 'ext/opencv/cvcapture.cpp', line 92

VALUE
rb_close(VALUE self)
{
  sCvCapture *scap;
  Data_Get_Struct(self, sCvCapture, scap);
  if (scap->opened) {
    scap->opened = false;
    cvReleaseCapture(&scap->ptr);
    return true;
  } else
    return false;
}

#contrastNumber

Get contrast of the image (only for cameras)

Returns:

  • (Number)

    Contrast


474
475
476
477
478
# File 'ext/opencv/cvcapture.cpp', line 474

VALUE
rb_get_contrast(VALUE self)
{
  return rb_get_capture_property(self, CV_CAP_PROP_CONTRAST);
}

#convert_rgbBoolean

Get boolean flags indicating whether images should be converted to RGB

Returns:

  • (Boolean)

    Whether images should be converted to RGB


533
534
535
536
537
538
539
540
541
542
543
544
# File 'ext/opencv/cvcapture.cpp', line 533

VALUE
rb_get_convert_rgb(VALUE self)
{
  int flag = 0;
  try {
    flag = (int)cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_CONVERT_RGB);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return flag ? Qtrue : Qfalse;
}

#exposureNumber

Get exposure (only for cameras)

Returns:

  • (Number)

    Exposure


521
522
523
524
525
# File 'ext/opencv/cvcapture.cpp', line 521

VALUE
rb_get_exposure(VALUE self)
{
  return rb_get_capture_property(self, CV_CAP_PROP_EXPOSURE);
}

#formatNumber

Get format of images returned by CvCapture#retrieve

Returns:

  • (Number)

    format


438
439
440
441
442
# File 'ext/opencv/cvcapture.cpp', line 438

VALUE
rb_get_format(VALUE self)
{
  return rb_get_capture_property(self, CV_CAP_PROP_FORMAT);
}

#fourccNumber

Get 4 character code of codec. see www.fourcc.org/

Returns:

  • (Number)

    Codec code


411
412
413
414
415
416
417
418
# File 'ext/opencv/cvcapture.cpp', line 411

VALUE
rb_get_fourcc(VALUE self)
{
  char str[4];
  double fourcc = cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_FOURCC);
  sprintf(str, "%s", (char*)&fourcc);
  return rb_str_new2(str);
}

#fpsNumber

Get frame rate

Returns:

  • (Number)

    Frame rate


386
387
388
389
390
# File 'ext/opencv/cvcapture.cpp', line 386

VALUE
rb_get_fps(VALUE self)
{
  return rb_get_capture_property(self, CV_CAP_PROP_FPS);
}

#fps=Number

Set frame rate

Parameters:

  • value (Number)

    Frame rate

Returns:

  • (Number)

399
400
401
402
403
# File 'ext/opencv/cvcapture.cpp', line 399

VALUE
rb_set_fps(VALUE self, VALUE value)
{
  return rb_set_capture_property(self, CV_CAP_PROP_FPS, value);
}

#frame_countNumber

Get number of frames in video file.

Returns:

  • (Number)

    Number of frames


426
427
428
429
430
# File 'ext/opencv/cvcapture.cpp', line 426

VALUE
rb_get_frame_count(VALUE self)
{
  return rb_get_capture_property(self, CV_CAP_PROP_FRAME_COUNT);
}

#framesNumber

Get 0-based index of the frame to be decoded/captured next

Returns:

  • (Number)

    0-based index of the frame to be decoded/captured next


244
245
246
247
248
# File 'ext/opencv/cvcapture.cpp', line 244

VALUE
rb_get_frames(VALUE self)
{
  return rb_get_capture_property(self, CV_CAP_PROP_POS_FRAMES);
}

#frames=Number

Set 0-based index of the frame to be decoded/captured next

Parameters:

  • value (Number)

    0-based index of the frame to be decoded/captured next

Returns:

  • (Number)

257
258
259
260
261
# File 'ext/opencv/cvcapture.cpp', line 257

VALUE
rb_set_frames(VALUE self, VALUE value)
{
  return rb_set_capture_property(self, CV_CAP_PROP_POS_FRAMES, value);
}

#gainNumber

Get gain of the image (only for cameras)

Returns:

  • (Number)

    Gain


509
510
511
512
513
# File 'ext/opencv/cvcapture.cpp', line 509

VALUE
rb_get_gain(VALUE self)
{
  return rb_get_capture_property(self, CV_CAP_PROP_GAIN);
}

#grabBoolean

Grabs the next frame from video file or capturing device.

Returns:

  • (Boolean)

    If grabbing a frame successed, returns true, otherwise returns false.


111
112
113
114
115
116
117
118
119
120
121
122
# File 'ext/opencv/cvcapture.cpp', line 111

VALUE
rb_grab(VALUE self)
{
  int grab = 0;
  try {
    grab = cvGrabFrame(CVCAPTURE(self));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return grab ? Qtrue : Qfalse;
}

#heightNumber

Get height of frames in the video stream.

Returns:

  • (Number)

    Height of frames in the video stream.


361
362
363
364
365
# File 'ext/opencv/cvcapture.cpp', line 361

VALUE
rb_get_height(VALUE self)
{
  return rb_get_capture_property(self, CV_CAP_PROP_FRAME_HEIGHT);
}

#height=Number

Set height of frames in the video stream.

Parameters:

  • value (Number)

    Height of frames

Returns:

  • (Number)

374
375
376
377
378
# File 'ext/opencv/cvcapture.cpp', line 374

VALUE
rb_set_height(VALUE self, VALUE value)
{
  return rb_set_capture_property(self, CV_CAP_PROP_FRAME_HEIGHT, value);
}

#hueNumber

Get hue of the image (only for cameras)

Returns:

  • (Number)

    Hue


497
498
499
500
501
# File 'ext/opencv/cvcapture.cpp', line 497

VALUE
rb_get_hue(VALUE self)
{
  return rb_get_capture_property(self, CV_CAP_PROP_HUE);
}

#millisecondNumber

Get film current position in milliseconds or video capture timestamp.

Returns:

  • (Number)

    Current position of the video file in milliseconds or video capture timestamp


219
220
221
222
223
# File 'ext/opencv/cvcapture.cpp', line 219

VALUE
rb_get_millisecond(VALUE self)
{
  return rb_get_capture_property(self, CV_CAP_PROP_POS_MSEC);
}

#millisecond=Number

Set film current position in milliseconds or video capture timestamp.

Parameters:

  • value (Number)

    Position in milliseconds or video capture timestamp.

Returns:

  • (Number)

232
233
234
235
236
# File 'ext/opencv/cvcapture.cpp', line 232

VALUE
rb_set_millisecond(VALUE self, VALUE value)
{
  return rb_set_capture_property(self, CV_CAP_PROP_POS_MSEC, value);
}

#modeNumber

Get a backend-specific value indicating the current capture mode

Returns:

  • (Number)

    Backend-specific value indicating the current capture mode


450
451
452
453
454
# File 'ext/opencv/cvcapture.cpp', line 450

VALUE
rb_get_mode(VALUE self)
{
  return rb_get_capture_property(self, CV_CAP_PROP_MODE);
}

#queryIplImage?

Grabs, decodes and returns the next video frame.

Returns:

  • (IplImage)

    Next video frame

  • (nil)

    Failed to read next video frame


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'ext/opencv/cvcapture.cpp', line 163

VALUE
rb_query(VALUE self)
{
  VALUE image = Qnil;
  IplImage *frame = NULL;
  try {
    if (!(frame = cvQueryFrame(CVCAPTURE(self)))) {
      return Qnil;
    }
    image = cIplImage::new_object(frame->width, frame->height,
				  CV_MAKETYPE(IPL2CV_DEPTH(frame->depth), frame->nChannels));
    if (frame->origin == IPL_ORIGIN_TL) {
      cvCopy(frame, CVARR(image));
    }
    else {
      cvFlip(frame, CVARR(image));
    }
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return image;
}

#rectificationNumber

Get rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)

Returns:

  • (Number)

    Rectification flag


552
553
554
555
556
# File 'ext/opencv/cvcapture.cpp', line 552

VALUE
rb_get_rectification(VALUE self)
{
  return rb_get_capture_property(self, CV_CAP_PROP_RECTIFICATION);
}

#retrieveIplImage?

Decodes and returns the grabbed video frame.

Returns:

  • (IplImage)

    Grabbed video frame

  • (nil)

    Failed to grabbing a frame


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'ext/opencv/cvcapture.cpp', line 131

VALUE
rb_retrieve(VALUE self)
{
  VALUE image = Qnil;
  IplImage *frame = NULL;
  try {
    if (!(frame = cvRetrieveFrame(CVCAPTURE(self)))) {
      return Qnil;
    }
    image = cIplImage::new_object(frame->width, frame->height,
				  CV_MAKETYPE(IPL2CV_DEPTH(frame->depth), frame->nChannels));
    if (frame->origin == IPL_ORIGIN_TL) {
      cvCopy(frame, CVARR(image));
    }
    else {
      cvFlip(frame, CVARR(image));
    }
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return image;

}

#saturationNumber

Get saturation of the image (only for cameras)

Returns:

  • (Number)

    Saturation


486
487
488
489
490
# File 'ext/opencv/cvcapture.cpp', line 486

VALUE
rb_get_saturation(VALUE self)
{
  return rb_get_capture_property(self, CV_CAP_PROP_SATURATION);
}

#sizeSize

Get size of frames in the video stream.

Returns:

  • (Size)

    Size of frames in the video stream.


292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'ext/opencv/cvcapture.cpp', line 292

VALUE
rb_get_size(VALUE self)
{
  CvSize size;
  try {
    CvCapture* self_ptr = CVCAPTURE(self);
    size = cvSize((int)cvGetCaptureProperty(self_ptr, CV_CAP_PROP_FRAME_WIDTH),
		  (int)cvGetCaptureProperty(self_ptr, CV_CAP_PROP_FRAME_HEIGHT));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return cCvSize::new_object(size);
}

#size=Number

Set size of frames in the video stream.

Parameters:

  • value (CvSize)

    Size of frames

Returns:

  • (Number)

314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'ext/opencv/cvcapture.cpp', line 314

VALUE
rb_set_size(VALUE self, VALUE value)
{
  double result = 0;
  CvSize size = VALUE_TO_CVSIZE(value);
  try {
    CvCapture* self_ptr = CVCAPTURE(self);
    cvSetCaptureProperty(self_ptr, CV_CAP_PROP_FRAME_WIDTH, size.width);
    result = cvSetCaptureProperty(self_ptr, CV_CAP_PROP_FRAME_HEIGHT, size.height);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return DBL2NUM(result);
}

#widthNumber

Get width of frames in the video stream.

Returns:

  • (Number)

    Width of frames in the video stream.


336
337
338
339
340
# File 'ext/opencv/cvcapture.cpp', line 336

VALUE
rb_get_width(VALUE self)
{
  return rb_get_capture_property(self, CV_CAP_PROP_FRAME_WIDTH);
}

#width=Number

Set width of frames in the video stream.

Parameters:

  • value (Number)

    Width of frames

Returns:

  • (Number)

349
350
351
352
353
# File 'ext/opencv/cvcapture.cpp', line 349

VALUE
rb_set_width(VALUE self, VALUE value)
{
  return rb_set_capture_property(self, CV_CAP_PROP_FRAME_WIDTH, value);
}