Class: OpenCV::CvScalar

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

Overview

Element-value of one pixel. OpenCV supports the image of 4-channels in the maximum. Therefore, CvScalar has 4-values.

C structure is here, very simple.

typdef struct CvScalar {
  double val[4];
} CvScalar;

If obtain CvScalar-object from the method of CvMat(or IplImage), the channel outside the range is obtained as all 0.

image = IplImage::load("opencv.jpg")  #=> 3-channel 8bit-depth BGR image
pixel = image[10, 20]                 #=> Get pixel value of (10, 20) of image. pixel is CvScalar-object.
blue, green, red = pixel[0], pixel[1], pixel[2]
# pixel[3] always 0.

CvColor is alias of CvScalar.

Constant Summary collapse

Black =
cCvScalar::new_object(cvScalar(0x0,0x0,0x0))
Silver =
cCvScalar::new_object(cvScalar(0x0c,0x0c,0x0c))
Gray =
cCvScalar::new_object(cvScalar(0x80,0x80,0x80))
White =
cCvScalar::new_object(cvScalar(0xff,0xff,0xff))
Maroon =
cCvScalar::new_object(cvScalar(0x0,0x0,0x80))
Red =
cCvScalar::new_object(cvScalar(0x0,0x0,0xff))
Purple =
cCvScalar::new_object(cvScalar(0x80,0x0,0x80))
Fuchsia =
cCvScalar::new_object(cvScalar(0xff,0x0,0xff))
Green =
cCvScalar::new_object(cvScalar(0x0,0x80,0x0))
Lime =
cCvScalar::new_object(cvScalar(0x0,0xff,0x0))
Olive =
cCvScalar::new_object(cvScalar(0x0,0x80,0x80))
Yellow =
cCvScalar::new_object(cvScalar(0x0,0xff,0xff))
cCvScalar::new_object(cvScalar(0x80,0x0,0x0))
Blue =
cCvScalar::new_object(cvScalar(0xff,0x0,0x0))
Teal =
cCvScalar::new_object(cvScalar(0x80,0x80,0x0))
Aqua =
cCvScalar::new_object(cvScalar(0xff,0xff,0x0))

Instance Method Summary collapse

Constructor Details

#new([d1][,d2][,d3][,d4]) ⇒ Object

Create new Scalar. Argument should be Fixnum (or nil as 0).



58
59
60
61
62
63
64
65
66
67
68
# File 'ext/opencv/cvscalar.cpp', line 58

VALUE
rb_initialize(int argc, VALUE *argv, VALUE self)
{
  VALUE val[4];
  rb_scan_args(argc, argv, "04", &val[0], &val[1], &val[2], &val[3]);
  CvScalar* self_ptr = CVSCALAR(self);
  for (int i = 0; i < 4; ++i) {
    self_ptr->val[i] = NIL_P(val[i]) ? 0 : NUM2DBL(val[i]);
  }
  return self;
}

Instance Method Details

#[](index) ⇒ Object

Return value of index dimension.



76
77
78
79
80
81
82
83
84
# File 'ext/opencv/cvscalar.cpp', line 76

VALUE
rb_aref(VALUE self, VALUE index)
{
  int idx = NUM2INT(index);
  if (idx < 0 || idx >= 4) {
    rb_raise(rb_eIndexError, "scalar index should be 0...4");
  }
  return rb_float_new(CVSCALAR(self)->val[idx]);
}

#[]=(index, value) ⇒ Object

Set value of index dimension to value



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

VALUE
rb_aset(VALUE self, VALUE index, VALUE value)
{
  int idx = NUM2INT(index);
  if (idx < 0 || idx >= 4) {
    rb_raise(rb_eIndexError, "scalar index should be 0...4");
  }
  CVSCALAR(self)->val[idx] = NUM2DBL(value);
  return self;
}

#sub(val[,mask]) ⇒ Object Also known as: -

Return new CvScalar if val is CvScalar or compatible object.

self[I] - val[I]

Or return new CvMat if val is CvMat or subclass.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'ext/opencv/cvscalar.cpp', line 111

VALUE
rb_sub(int argc, VALUE *argv, VALUE self)
{
  VALUE val, mask;
  rb_scan_args(argc, argv, "11", &val, &mask);
  if (rb_obj_is_kind_of(val, cCvMat::rb_class())) {
    CvArr *val_ptr = CVARR(val);
    VALUE dest = Qnil;
    try {
      dest = cCvMat::new_object(cvGetSize(val_ptr), cvGetElemType(val_ptr));
      cvSubRS(val_ptr, *CVSCALAR(self), CVARR(dest), MASK(mask));
    }
    catch (cv::Exception& e) {
      raise_cverror(e);
    }
    return dest;
  }
  else {
    CvScalar *src = CVSCALAR(self);
    CvScalar scl = VALUE_TO_CVSCALAR(val);
    return new_object(cvScalar(src->val[0] - scl.val[0],
                               src->val[1] - scl.val[1],
                               src->val[2] - scl.val[2],
                               src->val[3] - scl.val[3]));
  }
}

#to_aryArray Also known as: to_a

Return values by Array.

Returns:

  • (Array)


164
165
166
167
168
169
170
171
172
# File 'ext/opencv/cvscalar.cpp', line 164

VALUE
rb_to_ary(VALUE self)
{
  return rb_ary_new3(4,
		     rb_aref(self, INT2FIX(0)),
		     rb_aref(self, INT2FIX(1)),
		     rb_aref(self, INT2FIX(2)),
		     rb_aref(self, INT2FIX(3)));
}

#to_s"<OpeCV::CvScalar:#selff[0]}

Return values by String.

Returns:

  • ("<OpeCV::CvScalar:#selff[0]})


144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'ext/opencv/cvscalar.cpp', line 144

VALUE
rb_to_s(VALUE self)
{
  const int i = 6;
  VALUE str[i];
  str[0] = rb_str_new2("<%s:%g,%g,%g,%g>");
  str[1] = rb_str_new2(rb_class2name(CLASS_OF(self)));
  str[2] = rb_aref(self, INT2FIX(0));
  str[3] = rb_aref(self, INT2FIX(1));
  str[4] = rb_aref(self, INT2FIX(2));
  str[5] = rb_aref(self, INT2FIX(3));
  return rb_f_sprintf(i, str);
}