imgsmlr

Image similarity with haar

Overview

PackageVersionCategoryLicenseLanguage
imgsmlr1.0FEATPostgreSQLC
IDExtensionBinLibLoadCreateTrustRelocSchema
2830imgsmlrNoYesNoYesNoYes-
Relatedage hll rum pg_graphql pg_jsonschema jsquery pg_hint_plan hypopg

breaks on el10

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY1.01817161514imgsmlr-
RPMPIGSTY1.01817161514imgsmlr_$v-
DEBPIGSTY1.01817161514postgresql-$v-imgsmlr-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64PIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISS
el10.aarch64PIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISS
d12.x86_64
d12.aarch64
d13.x86_64PIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISS
d13.aarch64PIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY 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

Build

You can build the RPM / DEB packages for imgsmlr using pig build:

pig build pkg imgsmlr         # build RPM / DEB packages

Install

You can install imgsmlr directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

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

Install the extension using pig or apt/yum/dnf:

pig install imgsmlr;          # Install for current active PG version
pig ext install -y imgsmlr -v 18  # PG 18
pig ext install -y imgsmlr -v 17  # PG 17
pig ext install -y imgsmlr -v 16  # PG 16
pig ext install -y imgsmlr -v 15  # PG 15
pig ext install -y imgsmlr -v 14  # PG 14
dnf install -y imgsmlr_18       # PG 18
dnf install -y imgsmlr_17       # PG 17
dnf install -y imgsmlr_16       # PG 16
dnf install -y imgsmlr_15       # PG 15
dnf install -y imgsmlr_14       # PG 14
apt install -y postgresql-18-imgsmlr   # PG 18
apt install -y postgresql-17-imgsmlr   # PG 17
apt install -y postgresql-16-imgsmlr   # PG 16
apt install -y postgresql-15-imgsmlr   # PG 15
apt install -y postgresql-14-imgsmlr   # PG 14

Create Extension:

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 Modified 2026-03-12: add pg extension catalog (95749bf)