Module: Framegrabber

Defined in:
lib/framegrabber.rb

Class Method Summary collapse

Class Method Details

.grab_frameObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'ext/grab_frame_ext/grab_frame_ext.cpp', line 39

static VALUE grab_frame(VALUE _self) {
  Mat frame = grab();

  VALUE result = rb_ary_new_capa(frame.cols);

  if(frame.empty()) {
    return result;
  }

  int i = 0, j = 0;
  for(i=0; i<frame.rows; ++i) {
    VALUE row = rb_ary_new_capa(frame.cols);
    for(j=0; j<frame.cols; j++) {
      VALUE pixel = rb_ary_new_capa(3);
      unsigned char * p = frame.ptr(i, j); // Y first, X after
      rb_ary_store(pixel, 2, ULL2NUM(p[2])); // R
      rb_ary_store(pixel, 1, ULL2NUM(p[1])); // G
      rb_ary_store(pixel, 0, ULL2NUM(p[0])); // B

      rb_ary_store(row, j, pixel);
    }
    rb_ary_store(result, i, row);
  }
  return result;
}

.open(deviceID) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'ext/grab_frame_ext/grab_frame_ext.cpp', line 9

static VALUE open(VALUE _self, VALUE deviceID) {
    int camID = NUM2INT(deviceID);
    Mat frame;
    cap.open(camID);

    cap.read(frame);
    sleep(1);

    if (!cap.isOpened()) {
      rb_raise(rb_eRuntimeError, "Unable to open camera %d", camID);
    }
    return Qnil;
}

.releaseObject



23
24
25
26
# File 'ext/grab_frame_ext/grab_frame_ext.cpp', line 23

static VALUE release(VALUE _self) {
    cap.release();
    return Qnil;
}