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

#!=(compare_to) ⇒ Object



117
118
119
120
121
122
123
# File 'ext/opencv/cvscalar.cpp', line 117

VALUE
rb_check_inequality(VALUE self, VALUE compare_to) {
	CvScalar compare = VALUE_TO_CVSCALAR(compare_to);
  CvScalar* self_ptr = CVSCALAR(self);

  return (self_ptr->val[0] != compare.val[0] || self_ptr->val[1] != compare.val[1] || self_ptr->val[2] != compare.val[2] || self_ptr->val[3] != compare.val[3]) ? Qtrue : Qfalse;
}

#rb_check_equality(val[,mask]) ⇒ Object

Return true CvScalar if has same values as we do



109
110
111
112
113
114
115
# File 'ext/opencv/cvscalar.cpp', line 109

VALUE
rb_check_equality(VALUE self, VALUE compare_to) {
	CvScalar compare = VALUE_TO_CVSCALAR(compare_to);
  CvScalar* self_ptr = CVSCALAR(self);

  return (self_ptr->val[0] == compare.val[0] && self_ptr->val[1] == compare.val[1] && self_ptr->val[2] == compare.val[2] && self_ptr->val[3] == compare.val[3]) ? Qtrue : Qfalse;
}

#[](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;
}

#add(val[,mask]) ⇒ Object Also known as: +

Return new CvScalar self + val

Or return new CvMat if val is CvMat or subclass.



168
169
170
171
172
173
174
175
176
177
178
179
# File 'ext/opencv/cvscalar.cpp', line 168

VALUE
rb_add(int argc, VALUE *argv, VALUE self)
{
  VALUE val, mask;
  rb_scan_args(argc, argv, "11", &val, &mask);
	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]));
}

#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.



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

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)


218
219
220
221
222
223
224
225
226
# File 'ext/opencv/cvscalar.cpp', line 218

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]})


198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'ext/opencv/cvscalar.cpp', line 198

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);
}

#zero?false

Returns:

  • (false)


185
186
187
188
189
190
# File 'ext/opencv/cvscalar.cpp', line 185

VALUE
rb_zero_q(VALUE self)
{
  CvScalar* self_ptr = CVSCALAR(self);
  return (self_ptr->val[0] == 0 && self_ptr->val[1] == 0 && self_ptr->val[2] == 0 && self_ptr->val[3] == 0) ? Qtrue : Qfalse;
}