Class: OpenCV::CvHaarClassifierCascade

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

Overview

Haar Feature-based Cascade Classifier for Object Detection

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load(filename) ⇒ CvHaarClassifierCascade

Load trained cascade of haar classifers from file.

Parameters:

  • filename (String)

    Haar classifer file name

Returns:


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'ext/opencv/cvhaarclassifiercascade.cpp', line 51

VALUE
rb_load(VALUE klass, VALUE path)
{
  CvHaarClassifierCascade *cascade = NULL;
  try {
    cascade = (CvHaarClassifierCascade*)cvLoad(StringValueCStr(path), 0, 0, 0);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  if (!CV_IS_HAAR_CLASSIFIER(cascade))
    rb_raise(rb_eArgError, "invalid format haar classifier cascade file.");
  return Data_Wrap_Struct(klass, 0, cvhaarclassifiercascade_free, cascade);
}

Instance Method Details

#detect_objects(image, options = nil) ⇒ CvSeq<CvAvgComp>

Detects objects of different sizes in the input image.

Parameters:

  • image (CvMat, IplImage)

    Matrix of the type CV_8U containing an image where objects are detected.

  • options (Hash) (defaults to: nil)

    Options

Options Hash (options):

  • :scale_factor (Number)

    Parameter specifying how much the image size is reduced at each image scale.

  • :storage (Number)

    Memory storage to store the resultant sequence of the object candidate rectangles

  • :min_neighbors (Number)

    Parameter specifying how many neighbors each candidate rectangle should have to retain it.

  • :min_size (CvSize)

    Minimum possible object size. Objects smaller than that are ignored.

  • :max_size (CvSize)

    Maximum possible object size. Objects larger than that are ignored.

Returns:


85
86
87
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
124
125
126
127
# File 'ext/opencv/cvhaarclassifiercascade.cpp', line 85

VALUE
rb_detect_objects(int argc, VALUE *argv, VALUE self)
{ 
  VALUE image, options;
  rb_scan_args(argc, argv, "11", &image, &options);

  double scale_factor;
  int flags, min_neighbors;
  CvSize min_size, max_size;
  VALUE storage_val;
  if (NIL_P(options)) {
    scale_factor = 1.1;
    flags = 0;
    min_neighbors = 3;
    min_size = max_size = cvSize(0, 0);
    storage_val = cCvMemStorage::new_object();
  }
  else {
    scale_factor = IF_DBL(LOOKUP_HASH(options, "scale_factor"), 1.1);
    flags = IF_INT(LOOKUP_HASH(options, "flags"), 0);
    min_neighbors = IF_INT(LOOKUP_HASH(options, "min_neighbors"), 3);
    VALUE min_size_val = LOOKUP_HASH(options, "min_size");
    min_size = NIL_P(min_size_val) ? cvSize(0, 0) : VALUE_TO_CVSIZE(min_size_val);
    VALUE max_size_val = LOOKUP_HASH(options, "max_size");
    max_size = NIL_P(max_size_val) ? cvSize(0, 0) : VALUE_TO_CVSIZE(max_size_val);
    storage_val = CHECK_CVMEMSTORAGE(LOOKUP_HASH(options, "storage"));
  }

  VALUE result = Qnil;
  try {
    CvSeq *seq = cvHaarDetectObjects(CVARR_WITH_CHECK(image), CVHAARCLASSIFIERCASCADE(self), CVMEMSTORAGE(storage_val),
            scale_factor, min_neighbors, flags, min_size, max_size);
    result = cCvSeq::new_sequence(cCvSeq::rb_class(), seq, cCvAvgComp::rb_class(), storage_val);
    if (rb_block_given_p()) {
      for(int i = 0; i < seq->total; ++i)
  rb_yield(REFER_OBJECT(cCvAvgComp::rb_class(), cvGetSeqElem(seq, i), storage_val));
    }
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return result;
}