imgsmlr

imgsmlr

imgsmlr : Image similarity with haar

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
2830
imgsmlr
imgsmlr
1.0
FEAT
PostgreSQL
C
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d-r
No
Yes
No
Yes
yes
no
Relationships
See Also
age
hll
rum
pg_graphql
pg_jsonschema
jsquery
pg_hint_plan
hypopg

breaks on el10

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXT
PIGSTY
1.0
18
17
16
15
14
imgsmlr-
RPM
PIGSTY
1.0
18
17
16
15
14
imgsmlr_$v-
DEB
PIGSTY
1.0
18
17
16
15
14
postgresql-$v-imgsmlr-
Linux / PGPG18PG17PG16PG15PG14
el8.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
el8.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
el9.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
el9.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
el10.x86_64
MISS
MISS
MISS
MISS
MISS
el10.aarch64
MISS
MISS
MISS
MISS
MISS
d12.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
d12.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
d13.x86_64
MISS
MISS
MISS
MISS
MISS
d13.aarch64
MISS
MISS
MISS
MISS
MISS
u22.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u22.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u24.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u24.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0

Source

pig build pkg imgsmlr;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

pig repo add pgsql -u   # add both repo and update cache

Install this extension with pig:

pig install imgsmlr;		# install via package name, for the active PG version

pig install imgsmlr -v 18;   # install for PG 18
pig install imgsmlr -v 17;   # install for PG 17
pig install imgsmlr -v 16;   # install for PG 16
pig install imgsmlr -v 15;   # install for PG 15
pig install imgsmlr -v 14;   # install for PG 14

Create this extension with:

CREATE EXTENSION imgsmlr;

Usage

imgsmlr: similar images search for PostgreSQL using Haar wavelet transform

The imgsmlr extension implements similar image searching functionality based on Haar wavelet transforms. It provides two data types and functions for converting images into searchable signatures.

CREATE EXTENSION imgsmlr;

Data Types

DatatypeStorage LengthDescription
pattern16388 bytesResult of Haar wavelet transform on the image
signature64 bytesShort representation of pattern for fast GiST index searches

Functions

FunctionReturn TypeDescription
jpeg2pattern(bytea)patternConvert JPEG image data into pattern
png2pattern(bytea)patternConvert PNG image data into pattern
gif2pattern(bytea)patternConvert GIF image data into pattern
pattern2signature(pattern)signatureCreate signature from pattern
shuffle_pattern(pattern)patternShuffle pattern for less sensitivity to image shift

Operators

OperatorLeftRightReturnDescription
<->patternpatternfloat8Euclidean distance between two patterns
<->signaturesignaturefloat8Euclidean distance between two signatures

The signature type supports GiST indexing with KNN on the <-> operator.

Example

Create a table of patterns and signatures from JPEG images:

CREATE TABLE pat AS (
    SELECT
        id,
        shuffle_pattern(pattern) AS pattern,
        pattern2signature(pattern) AS signature
    FROM (
        SELECT id, jpeg2pattern(data) AS pattern
        FROM image
    ) x
);

ALTER TABLE pat ADD PRIMARY KEY (id);
CREATE INDEX pat_signature_idx ON pat USING gist (signature);

Search for the top 10 similar images to a given image:

SELECT id, smlr
FROM (
    SELECT
        id,
        pattern <-> (SELECT pattern FROM pat WHERE id = :id) AS smlr
    FROM pat
    WHERE id <> :id
    ORDER BY signature <-> (SELECT signature FROM pat WHERE id = :id)
    LIMIT 100
) x
ORDER BY x.smlr ASC
LIMIT 10;

The inner query selects the top 100 candidates by signature using the GiST index. The outer query refines to the top 10 by pattern distance.

Last updated on