Class: Similie

Inherits:
Object
  • Object
show all
Defined in:
ext/similie.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Object



129
130
131
132
133
134
135
136
# File 'ext/similie.c', line 129

VALUE rb_image_initialize(VALUE self, VALUE file) {
    IplImage *img = cvLoadImage(CSTRING(file), -1);
    if (!img)
        rb_raise(rb_eArgError, "Invalid image or unsupported format: %s", CSTRING(file));

    DATA_PTR(self) = img;
    return self;
}

Class Method Details

.distance(file1, file2) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'ext/similie.c', line 145

VALUE rb_image_distance_func(VALUE self, VALUE file1, VALUE file2) {
    IplImage *img1, *img2;
    img1 = cvLoadImage(CSTRING(file1), -1);
    if (!img1)
        rb_raise(rb_eArgError, "Invalid image or unsupported format: %s", CSTRING(file1));

    img2 = cvLoadImage(CSTRING(file2), -1);
    if (!img2) {
        cvReleaseImage(&img1);
        rb_raise(rb_eArgError, "Invalid image or unsupported format: %s", CSTRING(file2));
    }

    int dist = popcount(image_fingerprint(img1) ^ image_fingerprint(img2));

    cvReleaseImage(&img1);
    cvReleaseImage(&img2);

    return INT2NUM(dist);
}

.fingerprint(file) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
# File 'ext/similie.c', line 172

VALUE rb_image_fingerprint_func(VALUE self, VALUE file) {
    IplImage *img;
    img = cvLoadImage(CSTRING(file), -1);
    if (!img)
        rb_raise(rb_eArgError, "Invalid image or unsupported format: %s", CSTRING(file));

    uint64_t phash = image_fingerprint(img);
    cvReleaseImage(&img);

    return SIZET2NUM(phash);
}

.popcount(value) ⇒ Object



165
166
167
168
169
# File 'ext/similie.c', line 165

VALUE rb_popcount_func(VALUE self, VALUE value) {
    if (TYPE(value) != T_BIGNUM && TYPE(value) != T_FIXNUM)
        rb_raise(rb_eArgError, "value needs to be a number");
    return INT2NUM(popcount(NUM2INT(value)));
}

Instance Method Details

#distance(other) ⇒ Object



138
139
140
141
142
143
# File 'ext/similie.c', line 138

VALUE rb_image_distance(VALUE self, VALUE other) {
    VALUE hash1 = rb_image_fingerprint(self);
    VALUE hash2 = rb_image_fingerprint(other);
    int dist    = popcount(NUM2SIZET(hash1) ^ NUM2SIZET(hash2));
    return INT2NUM(dist);
}

#fingerprintObject



120
121
122
123
124
125
126
127
# File 'ext/similie.c', line 120

VALUE rb_image_fingerprint(VALUE self) {
    VALUE hash = rb_iv_get(self, "@fingerprint");
    if (NIL_P(hash)) {
        hash = SIZET2NUM(image_fingerprint(rb_image_handle(self)));
        rb_iv_set(self, "@fingerprint", hash);
    }
    return hash;
}