File: Seek.pm

package info (click to toggle)
libimage-seek-perl 0.06-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 320 kB
  • sloc: cpp: 464; perl: 94; pascal: 34; makefile: 6
file content (286 lines) | stat: -rw-r--r-- 7,529 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
128
129
130
131
132
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package Image::Seek;

use 5.006;
use strict;
use warnings;
use Carp;

require Exporter;
use AutoLoader;

our @ISA = qw(Exporter);
our %EXPORT_TAGS = ( 'all' => [ qw( add_image query_id loaddb savedb cleardb
    add_image_imager add_image_imlib2 remove_id) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw( );

our $VERSION = '0.06';

require XSLoader;
XSLoader::load('Image::Seek', $VERSION);



sub add_image {
    my ($image, $id) = @_;
    if (UNIVERSAL::isa($image, "Imager"))        { goto &add_image_imager }
    if (UNIVERSAL::isa($image, "Image::Imlib2")) { goto &add_image_imlib2 }
    if (UNIVERSAL::isa($image, "GD::Image"))     { goto &add_image_gd }
    if (UNIVERSAL::isa($image, "Image::Magick")) { goto &add_image_magick }
    croak "Don't know what sort of image $image is";
}

sub add_image_magick {
    my ($img, $id) = @_;
    #my ($reds, $blues, $greens);

    my ($width,$height) = $img->Get('width', 'height');
    my $thumb;

    if ($width == 128 && $height == 128) {
      $thumb = $img;
    }
    else {
      $thumb = $img->Clone();
      $thumb->Scale('128x128!');
    }

    my @red = $thumb->GetPixels('map' => 'R', 'height' => 128, 'width' =>128, 'normalize' => 1);
    my $reds = join('',(map { chr($_); } @red));
    my @blue = $thumb->GetPixels('map' => 'B', 'height' => 128, 'width' =>128, 'normalize' => 1);
    my $blues = join('',(map { chr($_); } @blue));
    my @green = $thumb->GetPixels('map' => 'G', 'height' => 128, 'width' =>128, 'normalize' => 1);
    my $greens = join('',(map { chr($_); } @green));
    addImage($id, $reds, $greens, $blues);
}

sub add_image_gd {
    my ($img, $id) = @_;
    my ($reds, $blues, $greens);

    my $thumb = new GD::Image(128,128,1);
    $thumb->copyResized($img,0,0,0,0,128,128,$img->width ,$img->height);

    for my $y (0..127) {
        for my $x (0..127) {
            my ($r, $g, $b) = $thumb->rgb($thumb->getPixel($x,$y));
            $reds .= chr($r); $blues .= chr($b); $greens .= chr($g);
        }
    }
    addImage($id, $reds, $greens, $blues);
}

sub add_image_imager {
    my ($img, $id) = @_;
    my ($reds, $blues, $greens);

    my $thumb = $img->scaleX(pixels => 128)->scaleY(pixels => 128);
    for my $y (0..127) {
        my @cols = $thumb->getscanline('y' => $y);
        for (@cols) {
            my ($r, $g, $b) = $_->rgba;
            $reds .= chr($r); $blues .= chr($b); $greens .= chr($g);
        }
    }
    addImage($id, $reds, $greens, $blues);
}

sub add_image_imlib2 {
    my ($img, $id) = @_;
    my ($reds, $blues, $greens);

    my $thumb = $img->create_scaled_image(128,128);
    for my $y (0..127) {
        for my $x (0..127) {
            my ($r, $g, $b,$a) = $thumb->query_pixel($x,$y);
            $reds .= chr($r); $blues .= chr($b); $greens .= chr($g);
        }
    }
    addImage($id, $reds, $greens, $blues);
}

sub query_id {
    my $id = shift;
    my $results = shift || 10;
    queryImgID($id, $results);
    my @r = results();
    my @rv;
    unshift @rv, [shift @r, shift @r] while @r;
    @rv;
}

sub remove_id {
    my $id = shift;
    removeID($id);
}

1;
__END__

=head1 NAME

Image::Seek - A port of ImgSeek to Perl

=begin html

<a href="https://travis-ci.org/wollmers/Image-Seek"><img src="https://travis-ci.org/wollmers/LCS.png" alt="Image-Seek"></a>
<a href='https://coveralls.io/r/wollmers/Image-Seek?branch=master'><img src='https://coveralls.io/repos/wollmers/Image-Seek/badge.png?branch=master' alt='Coverage Status' /></a>
<a href='http://cpants.cpanauthors.org/dist/Image-Seek'><img src='http://cpants.cpanauthors.org/dist/Image-Seek.png' alt='Kwalitee Score' /></a>
<a href="http://badge.fury.io/pl/Image-Seek"><img src="https://badge.fury.io/pl/Image-Seek.svg" alt="CPAN version" height="18"></a>

=end html

=head1 SYNOPSIS

    use Image::Seek qw(loaddb add_image query_id savedb);

    loaddb("haar.db");

    # EITHER
    my $img = GD::Image->newFromJpeg("photo-216.jpg", 1);

    # OR
    my $img = Imager->new();
    $img->open(file => "photo-216.jpg");

    # OR
    my $img = Image::Imlib2->load("photo-216.jpg");

    # Then...
    add_image($img, 216);
    savedb("haar.db");

    my @results = query_id(216); # What looks like this photo?

    remove_id(216); # Just remove id from database.

=head1 DESCRIPTION

ImgSeek (http://www.imgseek.net/) is an implementation of Haar wavelet
decomposition techniques to find similar pictures in a library. This
module is port of the ImgSeek library to Perl's XS. It can deal with
image objects produced by the C<Imager>, C<Image::Imlib2> and C<GD> libraries.

=head1 EXPORT

None by default, but the following functions are available:

=head2 savedb($file)

Dumps the state of the norms and image buckets to the file C<$file>.

=head2 loaddb($file)

Loads a database of image norms produced by savedb

=head2 cleardb

Clears the internal database. Note that C<loaddb> will load into memory
a bunch of data that you may already have - it will duplicate rather
than replace this data, so results will be skewed if you load a database
multiple times without clearing it in between.

=head2 add_image($image, $id)

Adds the image object to the database, keyed against the numeric id
C<$id>. This will compute the Haar transformation for a 128x128
thumbnail of the image, and then store its norms into a database in
memory.

=head2 remove_id($id)

remove id from database, and you should C<savedb> to save the changed database.

=head2 query_id($id[, $results))

This queries the internal database for pictures which are "like" number
C<$id>. It returns a list of C<$results> results (by default, 10);
a result is an array reference. The first element is the ID of a
picture, the second is a score. So for example:

    query_id(2481, 5)

returns, in a shoot I have, the following:

          [ 2481, -38.3800003528595 ],
          [ 2480, -37.5519620793145 ],
          [ 2478, -37.39896965962   ],
          [ 2479, -37.2777427507208 ],
          [ 2584, -10.0803730081134 ],
          [ 2795, -7.89326129961427 ]

Notice that the scores go the opposite way to what you might imagine:
lower is better. The results come out sorted, and the first result is
the thing you queried for.

=head2 addImage($id, $reds, $greens, $blues)

Internally used.

=head2 add_image_magick($image, $id)

Internally used.

=head2 add_image_gd($image, $id)

Internally used.

=head2 add_image_imager($image, $id)

Internally used.

=head2 add_image_imlib2($image, $id)

Internally used.

=head2 constant

Internally used.

=head2 queryImgID

Internally used.

=head2 removeID

Internally used.

=head2 results

Internally used.

=head1 SEE ALSO

http://www.imgseek.net/

=head1 SOURCE REPOSITORY

L<http://github.com/wollmers/Image-Seek>

=head1 MAINTAINER

Helmut Wollmersdorfer E<lt>helmut.wollmersdorfer@gmail.comE<gt>

=begin html

<a href='http://cpants.cpanauthors.org/author/wollmers'><img src='http://cpants.cpanauthors.org/author/wollmers.png' alt='Kwalitee Score' /></a>

=end html

=head1 AUTHOR

Simon Cozens, E<lt>simon@cpan.org<gt>
Lilo Huang, E<lt>kenwu@cpan.orgE<gt>
Helmut Wollmersdorfer, E<lt>helmut.wollmersdorfer@gmail.comE<gt>

All the clever bits were written by Ricardo Niederberger Cabral; Simon Cozens just
mangled them to wrap Perl around them.

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2005 by Simon Cozens, 2008 by Lilo Huang, 2015 Helmut Wollmersdorfer

This library is free software; as it is a derivative work of imgseek,
this library is distributed under the same terms (GPL) as imgseek.

=cut