digoal
2016-11-26
postgresql , haar wavelet , 圖像搜尋 , 圖檔去重 , 視訊去重
圖像處理的業務場景比較多,例如 圖像搜尋、視訊去重、人臉識别、美圖、圖檔去重 等。
比如,視訊去重,一些使用者上傳了較多的視訊,同一部電影可能有不同的版本,分辨率不一樣,音軌不一樣,壓縮比不一樣。這種情況會導緻服務端重複存儲大量的視訊。
又比如甄别黃色視訊或黃色圖檔,鑒黃師的職業要消失了。
有什麼方法可以得到重複的視訊呢? 如何鑒别黃色視訊和圖檔呢? 本文将給你揭曉。
另一方面,圖檔搜尋是繼文字搜尋後又一個比較常用的搜尋引擎。
市面上常見的搜尋引擎有谷歌、百度、搜狗等圖檔搜尋引擎。
<a href="http://image.baidu.com/">http://image.baidu.com/</a>
<a href="http://images.google.com.hk/">http://images.google.com.hk</a>
例如在搜尋引擎提供的接口中上層了一張雪人的圖檔,搜出來一堆和雪人近似的圖檔。
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5SMwAzXjlGcfFDMfZjMxEjNxAjMvwVMxYTMwIzLcJXZ0NXYt9CX3Fmcvw1ZvxmYvwFbh92ZpR2Lc12bj5iY1hGdpd2Lc9CX6MHc0RHaiojIsJye.png)
圖檔搜尋是怎麼做到的呢?
萬能的postgresql絕不落下這麼好玩的東東,通過pg萬能的api,可以擴充它的圖檔搜尋功能。
如果你對postgresql擴充開發感興趣,可以參考我寫的文章
<a href="https://yq.aliyun.com/articles/55981">《找對業務g點, 體驗酸爽 - postgresql核心擴充指南》</a>
postgresql的圖像搜尋插件使用了非常主流的haar wavelet技術對圖像進行變換後存儲,可以參考wiki和一篇關于hw的文獻。
<a href="https://en.wikipedia.org/wiki/haar_wavelet">https://en.wikipedia.org/wiki/haar_wavelet</a>
<a href="http://www.cs.toronto.edu/~kyros/courses/320/lectures.2013s/lecture.2013s.10.pdf">http://www.cs.toronto.edu/~kyros/courses/320/lectures.2013s/lecture.2013s.10.pdf</a>
<a href="https://wiki.postgresql.org/images/4/43/pgcon_2013_similar_images.pdf">https://wiki.postgresql.org/images/4/43/pgcon_2013_similar_images.pdf</a>
截取幾頁,注意燒腦。
依賴gd.h
下載下傳安裝imgsmlr
安裝插件
imgsmlr新增了兩個資料類型
datatype
storage length
description
pattern
16388 bytes
result of haar wavelet transform on the image
signature
64 bytes
short representation of pattern for fast search using gist indexes
gist 索引方法(支援pattern和signature類型), 以及knn操作符,可以用于搜尋相似度
operator
left type
right type
return type
<->
float8
eucledian distance between two patterns
eucledian distance between two signatures
新增了幾個函數
将圖像的二進制轉換為pattern類型,将pattern中存儲的資料轉換為signature類型
function
jpeg2pattern(bytea)
convert jpeg image into pattern
png2pattern(bytea)
convert png image into pattern
gif2pattern(bytea)
convert gif image into pattern
pattern2signature(pattern)
create signature from pattern
shuffle_pattern(pattern)
shuffle pattern for less sensitivity to image shift
導入一些圖檔,例如(越多越好)
建立圖檔表
導入圖檔到資料庫
将圖檔轉換成 patten 和 signature
建立索引
近似度查詢,例如查詢與id = :id的圖像相似的圖像,按相似度排行,取出前10條
這裡可以用到knn索引,快速按相似度排行輸出結果。
例子
視訊去重,可以抽取視訊中的關鍵幀,自關聯産生笛卡爾積,計算不同視訊的任意兩張圖檔的相似度,相似度達到一定門檻值,可以認為是相同視訊。
1. postgresql是一個非常強大的資料庫,功能高度可定制。而且不需要動到postgresql的核心。 安全可靠。
2. 使用圖像搜尋的技術就是postgresql功能擴充的例子,速度杠杠的,還記得我以前給出的關于地理位置近鄰查詢的性能名額嗎。
<a href="https://yq.aliyun.com/articles/2999">《postgresql 百億地理位置資料 近鄰查詢毫秒級回報》</a>
3. 如果你對postgresql擴充開發感興趣,可以參考我寫的文章