{"id":19458490,"url":"https://github.com/postgrespro/imgsmlr","last_synced_at":"2025-04-09T12:06:53.544Z","repository":{"id":21919989,"uuid":"25244215","full_name":"postgrespro/imgsmlr","owner":"postgrespro","description":"Similar images search for PostgreSQL","archived":false,"fork":false,"pushed_at":"2024-02-22T10:31:05.000Z","size":250,"stargazers_count":259,"open_issues_count":8,"forks_count":41,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-04-05T22:03:30.682Z","etag":null,"topics":["gist","image-processing","postgres","postgresql","similarity-search"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/postgrespro.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-10-15T07:44:47.000Z","updated_at":"2025-01-24T07:44:55.000Z","dependencies_parsed_at":"2024-11-11T16:15:23.499Z","dependency_job_id":null,"html_url":"https://github.com/postgrespro/imgsmlr","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/postgrespro%2Fimgsmlr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/postgrespro%2Fimgsmlr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/postgrespro%2Fimgsmlr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/postgrespro%2Fimgsmlr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/postgrespro","download_url":"https://codeload.github.com/postgrespro/imgsmlr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248036063,"owners_count":21037092,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gist","image-processing","postgres","postgresql","similarity-search"],"created_at":"2024-11-10T17:27:18.299Z","updated_at":"2025-04-09T12:06:53.515Z","avatar_url":"https://github.com/postgrespro.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/postgrespro/imgsmlr.svg?branch=master)](https://travis-ci.org/postgrespro/imgsmlr)\n[![codecov](https://codecov.io/gh/postgrespro/imgsmlr/branch/master/graph/badge.svg)](https://codecov.io/gh/postgrespro/imgsmlr)\n[![GitHub license](https://img.shields.io/badge/license-PostgreSQL-blue.svg)](https://raw.githubusercontent.com/postgrespro/imgsmlr/master/LICENSE)\n\nImgSmlr – similar images search for PostgreSQL\n==============================================\n\nIntroduction\n------------\n\nImgSmlr – is a PostgreSQL extension which implements similar images searching\nfunctionality.\n\nImgSmlr method is based on Haar wavelet transform. The goal of ImgSmlr is not\nto provide most advanced state of art similar images searching methods. ImgSmlr\nwas written as sample extension which illustrate how PostgreSQL extendability\ncould cover such untypical tasks for RDBMS as similar images search.\n\nAuthors\n-------\n\n * Alexander Korotkov \u003caekorotkov@gmail.com\u003e, Postgres Professional, Moscow, Russia\n\nAvailability\n------------\n\nImgSmlr is released as an extension and not available in default PostgreSQL\ninstallation. It is available from\n[github](https://github.com/postgrespro/imgsmlr)\nunder the same license as\n[PostgreSQL](http://www.postgresql.org/about/licence/)\nand supports PostgreSQL 9.1+.\n\nInstallation\n------------\n\nBefore build and install ImgSmlr you should ensure following:\n    \n * PostgreSQL version is 9.1 or higher.\n * You have development package of PostgreSQL installed or you built\n   PostgreSQL from source.\n * You have gd2 library installed on your system.\n * Your PATH variable is configured so that pg\\_config command available.\n    \nTypical installation procedure may look like this:\n    \n    $ git clone https://github.com/postgrespro/imgsmlr.git\n    $ cd imgsmlr\n    $ make USE_PGXS=1\n    $ sudo make USE_PGXS=1 install\n    $ make USE_PGXS=1 installcheck\n    $ psql DB -c \"CREATE EXTENSION imgsmlr;\"\n\nUsage\n-----\n\nImgSmlr offers two datatypes: pattern and signature.\n\n| Datatype  | Storage length |                              Description                           |\n| --------- |--------------: | ------------------------------------------------------------------ |\n| pattern   | 16388 bytes    | Result of Haar wavelet transform on the image                      |\n| signature | 64 bytes       | Short representation of pattern for fast search using GiST indexes |\n\nThere is set of functions *2pattern(bytea) which converts bynary data in given format into pattern. Convertion into pattern consists of following steps.\n\n * Decompress image.\n * Make image black\u0026white.\n * Resize image to 64x64 pixels.\n * Apply Haar wavelet transform to the image.\n\nPattern could be converted into signature and shuffled for less sensitivity to image shift.\n\n|          Function          | Return type |                      Description                    |\n| -------------------------- |-------------| --------------------------------------------------- |\n| jpeg2pattern(bytea)        | pattern     | Convert jpeg image into pattern                     |\n| png2pattern(bytea)         | pattern     | Convert png image into pattern                      |\n| gif2pattern(bytea)         | pattern     | Convert gif image into pattern                      |\n| pattern2signature(pattern) | signature   | Create signature from pattern                       |\n| shuffle_pattern(pattern)   | pattern     | Shuffle pattern for less sensitivity to image shift |\n\nBoth pattern and signature datatypes supports `\u003c-\u003e` operator for eucledian distance. Signature also supports GiST indexing with KNN on `\u003c-\u003e` operator.\n\n| Operator | Left type | Right type | Return type |                Description                |\n| -------- |-----------| ---------- | ----------- | ----------------------------------------- |\n| \u003c-\u003e      | pattern   | pattern    | float8      | Eucledian distance between two patterns   |\n| \u003c-\u003e      | signature | signature  | float8      | Eucledian distance between two signatures |\n\nThe idea is to find top N similar images by signature using GiST index. Then find top n (n \u003c N) similar images by pattern from top N similar images by signature.\n\nExample\n-------\n\nLet us assume we have an `image` table with columns `id` and `data` where `data` column contains binary jpeg data. We can create `pat` table with patterns and signatures of given images using following query.\n\n```sql\nCREATE TABLE pat AS (\n\tSELECT\n\t\tid,\n\t\tshuffle_pattern(pattern) AS pattern, \n\t\tpattern2signature(pattern) AS signature \n\tFROM (\n\t\tSELECT \n\t\t\tid, \n\t\t\tjpeg2pattern(data) AS pattern \n\t\tFROM \n\t\t\timage\n\t) x \n);\n```\n\nThen let's create primary key for `pat` table and GiST index for signatures.\n\n```sql\nALTER TABLE pat ADD PRIMARY KEY (id);\nCREATE INDEX pat_signature_idx ON pat USING gist (signature);\n```\n\nPrelimimary work is done. Now we can search for top 10  similar images to given image with specified id using following query.\n\n```sql\nSELECT\n\tid,\n\tsmlr\nFROM\n(\n\tSELECT\n\t\tid,\n\t\tpattern \u003c-\u003e (SELECT pattern FROM pat WHERE id = :id) AS smlr\n\tFROM pat\n\tWHERE id \u003c\u003e :id\n\tORDER BY\n\t\tsignature \u003c-\u003e (SELECT signature FROM pat WHERE id = :id)\n\tLIMIT 100\n) x\nORDER BY x.smlr ASC \nLIMIT 10\n```\n\nInner query selects top 100 images by signature using GiST index. Outer query search for top 10 images by pattern from images found by inner query. You can adjust both of number to achieve better search results on your images collection.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpostgrespro%2Fimgsmlr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpostgrespro%2Fimgsmlr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpostgrespro%2Fimgsmlr/lists"}