You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| pattern | 16388 bytes | Result of Haar wavelet transform on the image |
56
57
| signature | 64 bytes | Short representation of pattern for fast search using GiST indexes |
57
58
59
+
There is set of functions *2pattern(bytea) which converts bynary data in given format into pattern. Convertion into pattern consists of following steps.
60
+
61
+
* Decompress image.
62
+
* Make image black&white.
63
+
* Resize image to 64x64 pixels.
64
+
* Apply Haar wavelet transform to the image.
65
+
66
+
Pattern could be converted into signature and shuffled for less sensitivity to image shift.
| <-> | pattern | pattern | float8 | Eucledian distance between two patterns |
69
81
| <-> | signature | signature | float8 | Eucledian distance between two signatures |
70
82
83
+
The idea is to find top N similar images by signature using GiST index. Then find top n (n < N) similar images by pattern from top N similar images by signature.
84
+
71
85
Example
72
86
-------
73
87
88
+
Let 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.
89
+
74
90
```sql
75
91
CREATETABLEpatAS (
76
92
SELECT
@@ -85,10 +101,17 @@ CREATE TABLE pat AS (
85
101
image
86
102
) x
87
103
);
104
+
```
105
+
106
+
Then let's create primary key for `pat` table and GiST index for signatures.
107
+
108
+
```sql
109
+
ALTERTABLE pat ADD PRIMARY KEY (id);
88
110
CREATEINDEXpat_signature_idxON pat USING gist (signature);
89
-
CREATEINDEXpat_id_idxON pat(id);
90
111
```
91
112
113
+
Prelimimary work is done. Now we can search for top 10 similar images to given image with specified id using following query.
114
+
92
115
```sql
93
116
SELECT
94
117
id,
@@ -107,3 +130,5 @@ FROM
107
130
ORDER BYx.smlrASC
108
131
LIMIT10
109
132
```
133
+
134
+
Inner 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.
0 commit comments