Class: OpenCV::CvHistogram

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

Overview

Multi-dimensional histogram.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(dims, sizes, type, ranges = nil, uniform = true) ⇒ CvHistogram

Creates a histogram

OpenCV function:

  • cvCreateHist

OpenCV function:

  • cvCreateHist



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'ext/opencv/cvhistogram.cpp', line 88

VALUE
rb_initialize(int argc, VALUE *argv, VALUE self)
{
  VALUE _dims, _sizes, _type, _ranges, _uniform;
  int uniform;
  int* sizes;
  float** ranges = NULL;

  rb_scan_args(argc, argv, "32", &_dims, &_sizes, &_type, &_ranges, &_uniform);
  int sizes_len = RARRAY_LEN(_sizes);
  sizes = RB_ALLOC_N(int, sizes_len);

  if (NIL_P(_ranges)) {
    sizes = ary2intptr(_sizes, sizes);
    ranges = NULL;
  }
  else {
    ranges = RB_ALLOC_N(float*, sizes_len);
    VALUE* range_ptr = RARRAY_PTR(_ranges);
    int i;
    for (i = 0; i < sizes_len; i++) {
      sizes[i] = NUM2INT(RARRAY_PTR(_sizes)[i]);
      ranges[i] = ary2fltptr(range_ptr[i], RB_ALLOC_N(float, 2));
    }
  }
  uniform = TRUE_OR_FALSE(_uniform, 1);

  try {
    DATA_PTR(self) = cvCreateHist(NUM2INT(_dims), sizes, NUM2INT(_type), ranges, uniform);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }

  return self;
}

Class Method Details

.calc_prob_density(hist1, hist2, scale = 255) ⇒ CvHistogram

Divides one histogram by another.

OpenCV function:

  • cvCalcProbDensity

OpenCV function:

  • cvCalcProbDensity



673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
# File 'ext/opencv/cvhistogram.cpp', line 673

VALUE
rb_calc_prob_density(int argc, VALUE* argv, VALUE self)
{
  VALUE hist1, hist2, scale;
  rb_scan_args(argc, argv, "21", &hist1, &hist2, &scale);
  double s = NIL_P(scale) ? 255 : NUM2DBL(scale);

  CvHistogram* hist1_ptr = CVHISTOGRAM_WITH_CHECK(hist1);
  VALUE dst_hist = rb_allocate(rb_klass);
  try {
    cvCopyHist(hist1_ptr, (CvHistogram**)&(DATA_PTR(dst_hist)));
    cvCalcProbDensity(hist1_ptr, CVHISTOGRAM_WITH_CHECK(hist2), CVHISTOGRAM(dst_hist), s);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }

  return dst_hist;
}

.compare_hist(hist1, hist2, method) ⇒ Number

Compares two histograms.

OpenCV function:

  • cvCompareHist

OpenCV function:

  • cvCompareHist



649
650
651
652
653
654
655
656
657
658
659
660
661
662
# File 'ext/opencv/cvhistogram.cpp', line 649

VALUE
rb_compare_hist(VALUE self, VALUE hist1, VALUE hist2, VALUE method)
{
  double result = 0;
  try {
    result = cvCompareHist(CVHISTOGRAM_WITH_CHECK(hist1), CVHISTOGRAM_WITH_CHECK(hist2),
         NUM2INT(method));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }

  return rb_float_new(result);
}

Instance Method Details

#[](idx0) ⇒ Number #[](idx0, idx1) ⇒ Number #[](idx0, idx1, idx2) ⇒ Number #[](idx0, idx1, idx2, idx3, ...) ⇒ Number Also known as: query_hist_value

Queries the value of the histogram bin.

OpenCV function:

  • cvQueryHistValue_1D

  • cvQueryHistValue_2D

  • cvQueryHistValue_3D

  • cvQueryHistValue_nD

OpenCV function:

  • cvQueryHistValue_1D

  • cvQueryHistValue_2D

  • cvQueryHistValue_3D

  • cvQueryHistValue_nD



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'ext/opencv/cvhistogram.cpp', line 225

VALUE
rb_aref(VALUE self, VALUE args)
{
  int num_idx = RARRAY_LEN(args);
  int* idx = RB_ALLOC_N(int, num_idx);
  VALUE* args_ptr = RARRAY_PTR(args);
  for (int i = 0; i < num_idx; i++) {
    idx[i] = NUM2INT(args_ptr[i]);
  }
  
  float value = 0.0;
  CvHistogram* self_ptr = CVHISTOGRAM(self);
  try {
    switch (num_idx) {
    case 1:
      value = cvQueryHistValue_1D(self_ptr, idx[0]);
      break;
    case 2:
      value = cvQueryHistValue_2D(self_ptr, idx[0], idx[1]);
      break;
    case 3:
      value = cvQueryHistValue_3D(self_ptr, idx[0], idx[1], idx[2]);
      break;
    default:
      value = cvQueryHistValue_nD(self_ptr, idx);
      break;
    }
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  
  return rb_float_new((double)value);
}

#at(x) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'ext/opencv/cvhistogram.cpp', line 260

VALUE
rb_at(VALUE self, VALUE x)
{
  float value = 0.0;
  CvHistogram* self_ptr = CVHISTOGRAM(self);
  try {
    value = cvQueryHistValue_1D(self_ptr, NUM2INT(x));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  
  return rb_float_new((double)value);
}

#at_3d(x, y, z) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'ext/opencv/cvhistogram.cpp', line 275

VALUE
rb_at_3d(VALUE self, VALUE x, VALUE y, VALUE z)
{
  float value = 0.0;
  CvHistogram* self_ptr = CVHISTOGRAM(self);
  try {
    value = cvQueryHistValue_3D(self_ptr, NUM2INT(x), NUM2INT(y), NUM2INT(z));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  
  return rb_float_new((double)value);
}

#calc_back_project(images) ⇒ CvMat, IplImage

Calculates the back projection of a histogram.

OpenCV function:

  • cvCalcBackProject

OpenCV function:

  • cvCalcBackProject



554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
# File 'ext/opencv/cvhistogram.cpp', line 554

VALUE
rb_calc_back_project(VALUE self, VALUE image)
{
  Check_Type(image, T_ARRAY);
  int num_images = RARRAY_LEN(image);
  if (num_images == 0) {
    return Qnil;
  }
  
  IplImage** img = RB_ALLOC_N(IplImage*, num_images);
  VALUE* image_ptr = RARRAY_PTR(image);
  for (int i = 0; i < num_images; ++i) {
    img[i] = IPLIMAGE_WITH_CHECK(image_ptr[i]);
  }
  
  CvSize size;
  size.width = img[0]->width;
  size.height = img[0]->height;
  VALUE back_project = cCvMat::new_mat_kind_object(size, image_ptr[0]);
  try {
    cvCalcBackProject(img, CVARR(back_project), CVHISTOGRAM(self));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  
  return back_project;
}

#calc_back_project_patch(images, patch_size, method, factor) ⇒ CvMat, IplImage

Locates a template within an image by using a histogram comparison.

OpenCV function:

  • cvCalcBackProject

OpenCV function:

  • cvCalcBackProject



601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# File 'ext/opencv/cvhistogram.cpp', line 601

VALUE
rb_calc_back_project_patch(VALUE self, VALUE image, VALUE patch_size, VALUE method, VALUE factor)
{
  Check_Type(image, T_ARRAY);
  int num_images = RARRAY_LEN(image);
  if (num_images == 0) {
    return Qnil;
  }
  
  IplImage** img = RB_ALLOC_N(IplImage*, num_images);
  VALUE* image_ptr = RARRAY_PTR(image);
  for (int i = 0; i < num_images; ++i) {
    img[i] = IPLIMAGE_WITH_CHECK(image_ptr[i]);
  }

  CvSize patchsize = VALUE_TO_CVSIZE(patch_size);
  CvSize dst_size;
  dst_size.width = img[0]->width - patchsize.width + 1;
  dst_size.height = img[0]->height - patchsize.height + 1;

  VALUE dst = cCvMat::new_mat_kind_object(dst_size, image_ptr[0], CV_32F, 1);
  try {
    cvCalcBackProjectPatch(img, CVARR(dst), patchsize, CVHISTOGRAM(self),
         NUM2INT(method), NUM2DBL(factor));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  
  return dst;
}

#calc_hist(images, accumulate = nil, mask = nil) ⇒ CvHistogram

Calculates a histogram of a set of arrays.

OpenCV function:

  • cvCalcHist

OpenCV function:

  • cvCalcHist



177
178
179
180
181
# File 'ext/opencv/cvhistogram.cpp', line 177

VALUE
rb_calc_hist(int argc, VALUE* argv, VALUE self)
{
  return rb_calc_hist_bang(argc, argv, rb_copy_hist(self));
}

#calc_hist!(images, accumulate = nil, mask = nil) ⇒ Object

Calculates a histogram of a set of arrays.

OpenCV function:

  • cvCalcHist

OpenCV function:

  • cvCalcHist

See Also:



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'ext/opencv/cvhistogram.cpp', line 189

VALUE
rb_calc_hist_bang(int argc, VALUE* argv, VALUE self)
{
  VALUE images, accumulate, mask;
  rb_scan_args(argc, argv, "12", &images, &accumulate, &mask);
  Check_Type(images, T_ARRAY);
  int num_images = RARRAY_LEN(images);
  IplImage** img = RB_ALLOC_N(IplImage*, num_images);
  VALUE* images_ptr = RARRAY_PTR(images);
  for (int i = 0; i < num_images; i++) {
    img[i] = IPLIMAGE_WITH_CHECK(images_ptr[i]);
  }
  CvMat* m = NIL_P(mask) ? NULL : CVMAT_WITH_CHECK(mask);
  try {
    cvCalcHist(img, CVHISTOGRAM(self), TRUE_OR_FALSE(accumulate, 0), m);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  
  return self;
}

#clear_histCvHistogram Also known as: clear

Sets all histogram bins to 0 in case of dense histogram and removes all histogram bins in case of sparse array.

OpenCV function:

  • cvClearHist

OpenCV function:

  • cvClearHist



388
389
390
391
392
# File 'ext/opencv/cvhistogram.cpp', line 388

VALUE
rb_clear_hist(VALUE self)
{
  return rb_clear_hist_bang(rb_copy_hist(self));
}

#clear_hist!CvHistogram Also known as: clear!

Sets all histogram bins to 0 in case of dense histogram and removes all histogram bins in case of sparse array. This method changes self.

OpenCV function:

  • cvClearHist

OpenCV function:

  • cvClearHist

See Also:



403
404
405
406
407
408
409
410
411
412
413
# File 'ext/opencv/cvhistogram.cpp', line 403

VALUE
rb_clear_hist_bang(VALUE self)
{
  try {
    cvClearHist(CVHISTOGRAM(self));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return self;
}

#copy_histCvHistogram

Clones histogram

OpenCV function:

  • cvCopyHist

OpenCV function:

  • cvCopyHist



368
369
370
371
372
373
374
375
376
377
378
379
# File 'ext/opencv/cvhistogram.cpp', line 368

VALUE
rb_copy_hist(VALUE self)
{
  CvHistogram* hist = NULL;
  try {
    cvCopyHist(CVHISTOGRAM(self), &hist);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return Data_Wrap_Struct(rb_klass, 0, release_hist, hist);
}

#[](idx0, idx1, ...) ⇒ Array<Integer, Array<Integer>>

Returns number of array dimensions

OpenCV function:

  • cvGetDims

OpenCV function:

  • cvGetDims



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'ext/opencv/cvhistogram.cpp', line 343

VALUE
rb_dims(VALUE self)
{
  VALUE _sizes = Qnil;
  int size[CV_MAX_DIM];
  int dims = 0;
  try {
    dims = cvGetDims(CVHISTOGRAM(self)->bins, size);  
    _sizes = rb_ary_new2(dims);
    for (int i = 0; i < dims; ++i) {
      rb_ary_store(_sizes, i, INT2NUM(size[i]));
    }
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return rb_assoc_new(INT2NUM(dims), _sizes);
}

#has_range?Boolean

Returns self has range or not

OpenCV function:

  • CV_HIST_HAS_RANGES

OpenCV function:

  • CV_HIST_HAS_RANGES



155
156
157
158
159
# File 'ext/opencv/cvhistogram.cpp', line 155

VALUE
rb_has_range(VALUE self)
{
  return CV_HIST_HAS_RANGES(CVHISTOGRAM(self)) ? Qtrue : Qfalse;
}

#is_sparse?Boolean

Returns self is sparse histogram or not

OpenCV function:

  • CV_IS_SPARSE_HIST

OpenCV function:

  • CV_IS_SPARSE_HIST



143
144
145
146
147
# File 'ext/opencv/cvhistogram.cpp', line 143

VALUE
rb_is_sparse(VALUE self)
{
  return CV_IS_SPARSE_HIST(CVHISTOGRAM(self)) ? Qtrue : Qfalse;
}

#is_uniform?Boolean

Returns self is uniform histogram or not

OpenCV function:

  • CV_IS_UNIFORM_HIST

OpenCV function:

  • CV_IS_UNIFORM_HIST



131
132
133
134
135
# File 'ext/opencv/cvhistogram.cpp', line 131

VALUE
rb_is_uniform(VALUE self)
{
  return CV_IS_UNIFORM_HIST(CVHISTOGRAM(self)) ? Qtrue : Qfalse;
}

#min_max_valueArray

Finds the minimum and maximum histogram bins.

OpenCV function:

  • cvGetMinMaxHistValue

OpenCV function:

  • cvGetMinMaxHistValue



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'ext/opencv/cvhistogram.cpp', line 302

VALUE
rb_min_max_value(VALUE self)
{
  CvHistogram* self_ptr = CVHISTOGRAM(self);
  int dims = 0;
  float min_value = 0.0, max_value = 0.0;
  int *min_idx = NULL;
  int *max_idx = NULL;
  try {
    dims = cvGetDims(self_ptr->bins, NULL);
    min_idx = RB_ALLOC_N(int, dims);
    max_idx = RB_ALLOC_N(int, dims);
    cvGetMinMaxHistValue(CVHISTOGRAM(self), &min_value, &max_value, min_idx, max_idx);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }

  VALUE _min_idx = rb_ary_new2(dims);
  VALUE _max_idx = rb_ary_new2(dims);
  for (int i = 0; i < dims; i++) {
    rb_ary_store(_min_idx, i, INT2NUM(min_idx[i]));
    rb_ary_store(_max_idx, i, INT2NUM(max_idx[i]));
  }

  return rb_ary_new3(4, rb_float_new((double)min_value), rb_float_new((double)max_value),
           _min_idx, _max_idx);
}

#normalize(factor) ⇒ CvHistogram Also known as: normalize

Returns normalized the histogram bins by scaling them, such that the sum of the bins becomes equal to factor.

OpenCV function:

  • cvNormalizeHist

OpenCV function:

  • cvNormalizeHist



423
424
425
426
427
# File 'ext/opencv/cvhistogram.cpp', line 423

VALUE
rb_normalize_hist(VALUE self, VALUE factor)
{
  return rb_normalize_hist_bang(rb_copy_hist(self), factor);
}

#normalize!(factor) ⇒ CvHistogram Also known as: normalize!

Returns normalized the histogram bins by scaling them, such that the sum of the bins becomes equal to factor. This method changes self.

OpenCV function:

  • cvNormalizeHist

OpenCV function:

  • cvNormalizeHist

See Also:



439
440
441
442
443
444
445
446
447
448
449
# File 'ext/opencv/cvhistogram.cpp', line 439

VALUE
rb_normalize_hist_bang(VALUE self, VALUE factor)
{
  try {
    cvNormalizeHist(CVHISTOGRAM(self), NUM2DBL(factor));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return self;
}

#set_hist_bin_ranges(ranges, uniform = true) ⇒ CvHistogram

Sets the bounds of the histogram bins.

OpenCV function:

  • cvSetHistBinRanges

OpenCV function:

  • cvSetHistBinRanges



498
499
500
501
502
# File 'ext/opencv/cvhistogram.cpp', line 498

VALUE
rb_set_hist_bin_ranges(int argc, VALUE* argv, VALUE self)
{
  return rb_set_hist_bin_ranges_bang(argc, argv, rb_copy_hist(self));
}

#set_hist_bin_ranges!(ranges, uniform = true) ⇒ CvHistogram

Sets the bounds of the histogram bins. This method changes self.

OpenCV function:

  • cvSetHistBinRanges

OpenCV function:

  • cvSetHistBinRanges

See Also:



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'ext/opencv/cvhistogram.cpp', line 518

VALUE
rb_set_hist_bin_ranges_bang(int argc, VALUE* argv, VALUE self)
{
  VALUE _ranges, _uniform;
  rb_scan_args(argc, argv, "11", &_ranges, &_uniform);
  Check_Type(_ranges, T_ARRAY);

  int ranges_size = RARRAY_LEN(_ranges);
  float** ranges = RB_ALLOC_N(float*, ranges_size);
  VALUE* range_ptr = RARRAY_PTR(_ranges);
  for (int i = 0; i < ranges_size; ++i) {
    ranges[i] = ary2fltptr(range_ptr[i], RB_ALLOC_N(float, 2));
  }
  int uniform = TRUE_OR_FALSE(_uniform, 1);

  try {
    cvSetHistBinRanges(CVHISTOGRAM(self), ranges, uniform);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }

  return self;
}

#thresh_hist(threshold) ⇒ CvHistogram Also known as: thresh

Returns cleared histogram bins that are below the specified threshold.

OpenCV function:

  • cvThreshHist

OpenCV function:

  • cvThreshHist



458
459
460
461
462
# File 'ext/opencv/cvhistogram.cpp', line 458

VALUE
rb_thresh_hist(VALUE self, VALUE threshold)
{
  return rb_thresh_hist_bang(rb_copy_hist(self), threshold);
}

#thresh_hist!(threshold) ⇒ CvHistogram Also known as: thresh!

Cleares histogram bins that are below the specified threshold. This method changes self.

OpenCV function:

  • cvThreshHist

OpenCV function:

  • cvThreshHist

See Also:



473
474
475
476
477
478
479
480
481
482
483
# File 'ext/opencv/cvhistogram.cpp', line 473

VALUE
rb_thresh_hist_bang(VALUE self, VALUE threshold)
{
  try {
    cvThreshHist(CVHISTOGRAM(self), NUM2DBL(threshold));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return self;
}