Class: OpenCV::GUI::Window

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

Overview

Simple Window wedget to show images(CvMat/IplImage).

Sample:

image = OpenCV::IplImage::load("opencv.bmp")     #=> load image
window = OpenCV::GUI::Window.new("simple viewer")#=> create new window named "simaple viewer"
window.show(image)                               #=> show image

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(name, flags = CV_WINDOW_AUTOSIZE) ⇒ Object

Creates a window.

Parameters:

  • name (String)

    Name of the window in the window caption that may be used as a window identifier.

  • flags (Integer) (defaults to: CV_WINDOW_AUTOSIZE)

    Flags of the window. The supported flags are:

    • CV_WINDOW_AUTOSIZE - If this is set, the window size is automatically adjusted to fit the displayed image, and you cannot change the window size manually.

    • CV_WINDOW_NORMAL - If this is set, the user can resize the window (no constraint).

    • CV_WINDOW_OPENGL - If this is set, the window will be created with OpenGL support.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'ext/opencv/window.cpp', line 70

VALUE
rb_initialize(int argc, VALUE *argv, VALUE self)
{
  VALUE name, flags;
  rb_scan_args(argc, argv, "11", &name, &flags);
  Check_Type(name, T_STRING);
  char* name_str = StringValueCStr(name);
  if (cvGetWindowHandle(name_str) != NULL) {
    rb_raise(rb_eStandardError, "window name should be unique.");
  }      
  
  int mode = CV_WINDOW_AUTOSIZE;
  if (argc == 2) {
    Check_Type(flags, T_FIXNUM);
    mode = FIX2INT(flags);
  }

  Window* self_ptr = WINDOW(self);
  self_ptr->name = name;
  self_ptr->trackbars = rb_ary_new();
  self_ptr->blocks = rb_ary_new();
  try {
    cvNamedWindow(name_str, mode);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  num_windows++;
  return self;
}

Class Method Details

.destroy_allObject

Destorys all the windows.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'ext/opencv/window.cpp', line 131

VALUE
rb_destroy_all(VALUE klass)
{
  if (num_windows > 0) {
    try {
      cvDestroyAllWindows();
    }
    catch (cv::Exception& e) {
      raise_cverror(e);
    }
    num_windows = 0;
  }
  return Qnil;
}

Instance Method Details

#alive?Boolean

Return alive status of window. Return true if alive, otherwise return false.

Returns:

  • (Boolean)


104
105
106
107
108
109
# File 'ext/opencv/window.cpp', line 104

VALUE
rb_alive_q(VALUE self)
{
  const char* name_str = GET_WINDOW_NAME(self);
  return (cvGetWindowHandle(name_str) == NULL) ? Qfalse : Qtrue;
}

#destroyObject

Destroys a window. alive status of window be false.



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'ext/opencv/window.cpp', line 114

VALUE
rb_destroy(VALUE self)
{
  const char* name_str = GET_WINDOW_NAME(self);
  try {
    cvDestroyWindow(name_str);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  num_windows--;
  return self;
}

#move(point) ⇒ Object #move(x, y) ⇒ Object

Moves window to the specified position.

Overloads:

  • #move(point) ⇒ Object

    Parameters:

    • point (CvPoint)

      The new coordinate of the window.

  • #move(x, y) ⇒ Object

    Parameters:

    • x (Integer)

      The new x-coordinate of the window.

    • y (Integer)

      The new y-coordinate of the window.



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'ext/opencv/window.cpp', line 195

VALUE
rb_move(int argc, VALUE *argv, VALUE self)
{
  int x = 0;
  int y = 0;
  switch (argc) {
  case 1: {
    CvPoint point = VALUE_TO_CVPOINT(argv[0]);
    x = point.x;
    y = point.y;
    break;
  }
  case 2:
    x = NUM2INT(argv[0]);
    y = NUM2INT(argv[1]);
    break;
  default:
    rb_raise(rb_eArgError, "wrong number of arguments (1 or 2)");
    break;
  }
  try {
    cvMoveWindow(GET_WINDOW_NAME(self), x, y);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return self;
}

#resize(size) ⇒ Object #resize(width, height) ⇒ Object

Resizes window to the specified size.

Overloads:

  • #resize(size) ⇒ Object

    Parameters:

    • size (CvSize)

      The new window size.

  • #resize(width, height) ⇒ Object

    Parameters:

    • width (Integer)

      The new window width.

    • height (Integer)

      The new window height.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'ext/opencv/window.cpp', line 156

VALUE
rb_resize(int argc, VALUE *argv, VALUE self)
{
  int width = 0;
  int height = 0;
  switch (argc) {
  case 1: {
    CvSize size = VALUE_TO_CVSIZE(argv[0]);
    width = size.width;
    height = size.height;
    break;
  }
  case 2:
    width = NUM2INT(argv[0]);
    height = NUM2INT(argv[1]);
    break;
  default:
    rb_raise(rb_eArgError, "wrong number of arguments (1 or 2)");
    break;
  }
  try {
    cvResizeWindow(GET_WINDOW_NAME(self), width, height);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return self;
}

#set_mouse_callback({ |mouse_event| ... }) {|mouse_event| ... } ⇒ Object Also known as: on_mouse

Sets mouse handler for the specified window.

Examples:

display mouse event on console

window = OpenCV::GUI::Window.new "sample window"
image = OpenCV::IplImage::load "sample.png"
window.show(image)
window.set_mouse_callback {|mouse|
  e = "#{mouse.x}, #{mouse.y} : #{mouse.event} : "
  e << "<L>" if mouse.left_button?
  e << "<R>" if mouse.right_button?
  e << "<M>" if mouse.middle_button?
  e << "[CTRL]" if mouse.ctrl_key?
  e << "[SHIFT]" if mouse.shift_key?
  e << "[ALT]" if mouse.alt_key?
  puts e
}
OpenCV::GUI::wait_key

Yields:

  • (mouse_event)

    Mouse callback.

Yield Parameters:



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'ext/opencv/window.cpp', line 321

VALUE
rb_set_mouse_callback(int argc, VALUE* argv, VALUE self)
{
  if (!rb_block_given_p()) {
    rb_raise(rb_eArgError, "block not given.");
  }

  VALUE block = Qnil;
  rb_scan_args(argc, argv, "0&", &block);
  try {
    cvSetMouseCallback(GET_WINDOW_NAME(self), on_mouse, (void*)block);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }

  rb_ary_push(WINDOW(self)->blocks, block);
  return block;
}

#set_trackbar(trackbar) ⇒ Object #set_trackbar(name, count, value = nil) {|value| ... } ⇒ Object

Creates or sets a trackbar and attaches it to the specified window.

Overloads:

  • #set_trackbar(trackbar) ⇒ Object

    Parameters:

    • trackbar (TrackBar)

      The trackbar to set.

  • #set_trackbar(name, count, value = nil) {|value| ... } ⇒ Object

    Parameters:

    • name (String)

      Name of the created trackbar.

    • count (Integer)

      Maximal position of the slider. The minimal position is always 0.

    • value (Integer) (defaults to: nil)

      Optional value to an integer variable whose value reflects the position of the slider. Upon creation, the slider position is defined by this variable.

    Yields:

    • (value)

      Function to be called every time the slider changes position.

    Yield Parameters:

    • value (Integer)

      The trackbar position.



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'ext/opencv/window.cpp', line 266

VALUE
rb_set_trackbar(int argc, VALUE *argv, VALUE self)
{
  VALUE trackbar;
  if (argc == 1) {
    trackbar = argv[0];
  }
  else {
    trackbar = cTrackbar::rb_initialize(argc, argv, cTrackbar::rb_allocate(cTrackbar::rb_class()));
  }
  Trackbar *trackbar_ptr = TRACKBAR_WITH_CHECK(trackbar);
  try {
    cv::createTrackbar(trackbar_ptr->name, GET_WINDOW_NAME(self), &(trackbar_ptr->val), trackbar_ptr->maxval,
		       (cv::TrackbarCallback)trackbar_callback, (void*)(trackbar_ptr->block));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  rb_ary_push(WINDOW(self)->trackbars, trackbar);
  
  return trackbar;
}

#show_image(image) ⇒ Object Also known as: show

Displays an image in the specified window.

Parameters:

  • image (CvMat)

    Image to be shown.



231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'ext/opencv/window.cpp', line 231

VALUE
rb_show_image(VALUE self, VALUE img)
{
  CvArr* image = CVARR_WITH_CHECK(img);
  WINDOW(self)->image = img;
  try {
    cvShowImage(GET_WINDOW_NAME(self), image);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return self;
}