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

Parameters:

  • dims (Integer)

    Number of histogram dimensions

  • sizes (Array<Integer>)

    Array of the histogram dimension sizes

  • type (Integer)

    Histogram representation format. CV_HIST_ARRAY means that the histogram data is represented as a multi-dimensional dense array CvMatND. CV_HIST_SPARSE means that histogram data is represented as a multi-dimensional sparse array CvSparseMat.

  • ranges (Array<Integer>)

    Array of ranges for the histogram bins. Its meaning depends on the uniform parameter value. The ranges are used when the histogram is calculated or backprojected to determine which histogram bin corresponds to which value/tuple of values from the input image(s).

  • uniform (Boolean)

    Uniformity flag.



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.

Parameters:

  • hist1 (CvHistogram)

    First histogram (the divisor).

  • hist2 (CvHistogram)

    Second histogram.

  • scale (Number)

    Scale factor for the destination histogram.

Returns:



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.

Parameters:

  • hist1 (CvHistogram)

    First compared histogram.

  • hist2 (CvHistogram)

    Second compared histogram of the same size as hist1.

  • method (Integer)

    Comparison method that could be one of the following:

    • CV_COMP_CORREL: Correlation

    • CV_COMP_CHISQR: Chi-Square

    • CV_COMP_INTERSECT: Intersection

    • CV_COMP_BHATTACHARYYA: Bhattacharyya distance

    • CV_COMP_HELLINGER: Synonym for CV_COMP_BHATTACHARYYA

Returns:

  • (Number)

    Distance of the two histograms.



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.

Parameters:

  • idx* (Integer)

    *-th index

Returns:

  • (Number)

    The value of the specified bin of the 1D, 2D, 3D, or N-D histogram.



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.

Parameters:

  • images (Array<IplImage>)

    Source arrays. They all should have the same depth, CV_8U or CV_32F, and the same size. Each of them can have an arbitrary number of channels.

Returns:

  • (CvMat, IplImage)

    Destination back projection array that is a single-channel array of the same size and depth as the first element of images



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.

Parameters:

  • images (Array<IplImage>)

    Source arrays.

  • pach_size (CvSize)

    Size of the patch slid though the source image.

  • method (Integer)

    Comparison method that could be one of the following:

    • CV_COMP_CORREL: Correlation

    • CV_COMP_CHISQR: Chi-Square

    • CV_COMP_INTERSECT: Intersection

    • CV_COMP_BHATTACHARYYA: Bhattacharyya distance

    • CV_COMP_HELLINGER: Synonym for CV_COMP_BHATTACHARYYA

  • factor (Number)

    Normalization factor for histograms that affects the normalization scale of the destination image. Pass 1 if not sure.

Returns:



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.

Parameters:

  • images (Array<IplImage>)

    Source arrays. They all should have the same depth, CV_8U or CV_32F, and the same size. Each of them can have an arbitrary number of channels.

  • accumulate (Boolean)

    Accumulation flag. If it is set, the histogram is not cleared in the beginning when it is allocated. This feature enables you to compute a single histogram from several sets of arrays, or to update the histogram in time.

  • mask (CvMat)

    Optional mask. If the matrix is not empty, it must be an 8-bit array of the same size as images. The non-zero mask elements mark the array elements counted in the histogram.

Returns:



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.

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.

Returns:



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.

Returns:

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

Returns:



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

Parameters:

  • idx* (Integer)

    *-th index

Returns:

  • (Array<Integer, Array<Integer>>)

    [dims, sizes]: Number of array dimensions and its sizes.

    • dims (Integer): Number of array dimensions

    • sizes (Array<Integer>): Vector of the array dimension sizes. For 2D arrays the number of rows (height) goes first, number of columns (width) next.



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

Returns:

  • (Boolean)

    Has range or not



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

Returns:

  • (Boolean)

    Sparse or not



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

Returns:

  • (Boolean)

    Uniform or not



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.

Returns:

  • (Array)

    [min_value, max_value, min_idx, max_idx]: Array of the minimum / maximum value of the histogram and their coordinates.

    • min_value: The minimum value of the histogram.

    • max_value: The maximum value of the histogram.

    • min_idx: The array of coordinates for the minimum.

    • max_idx: The array of coordinates for the maximum.



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.

Parameters:

  • factor (Number)

    Normalization factor. The sum of the bins becomes equal to this value.

Returns:



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.

Parameters:

  • factor (Number)

    Normalization factor. The sum of the bins becomes equal to this value.

Returns:

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.

Parameters:

  • ranges (Array<Number>)

    Array of ranges for the histogram bins. Its meaning depends on the uniform parameter value. The ranges are used when the histogram is calculated or backprojected to determine which histogram bin corresponds to which value/tuple of values from the input image(s).

  • uniform (Boolean)

    Uniformity flag.

Returns:



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.

Parameters:

  • ranges (Array<Number>)

    Array of ranges for the histogram bins. Its meaning depends on the uniform parameter value. The ranges are used when the histogram is calculated or backprojected to determine which histogram bin corresponds to which value/tuple of values from the input image(s).

  • uniform (Boolean)

    Uniformity flag.

Returns:

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.

Parameters:

  • threshold (Number)

    Threshold value

Returns:



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.

Parameters:

  • threshold (Number)

    Threshold value

Returns:

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