Skip to content

Commit a4a8b84

Browse files
stereomatchingkissmshabunin
authored andcommitted
Implement image hash modules (#688)
* first commit * first commit * adjust code layout * round mean value * add missed header * remove useless header * remove useless header * first commit * first commit * first commit * Encapsule function averageHash by class * remove export macro * encapsulate phash algo by class * first commit * fix bugs of createHash and fillBlcoks * 1 : add create function 2 : add overload functions * implement get set functions * fix bug--destination depth should be CV_32F * first commit * first commit * 1 : fix bug--forgot '"' 2 : forgot to include iostream * fix warnings * remove tab * remove trailing white space * remove trailing white space * remove trailing white space * remove trailing white space * remove trailing white space * remove trailing white space * first commit * remove trailing white space * remove trailing white space * remove trailing white space * reduce redundance operation * add explanation of img_hash * remove useless comments * remove trailing space * first commit * fix missed symbol * add new defgroup and change all defgroup to ihash * fix namespace confliction * change namespace from ihash to img_hash * change ihash to img_hash * change include guard * forbid implicit conversion * first commit * 1 : declare function findFeatureVector 2 : forward declare test class--RavialVarHashTester as friend * first commit * replace auto with explicit type * export some symbols, for initialization and testing * remove trailing white space * add namespace cv * fix type cast warning and define default constrcutor/destructor * declare and define RadialVarHashTester in namespace * remove default constructor/destructor * exports functions findFeatures and destructor * remove trailing white space * fix bug--wrong definition of destructor * remove trailing white space * implement findFeatureVector * add test case for findFeatureVector * 1 : fix bug--forgot to allocate space for input 2 : fix bug--compare the results of pixPerLine with wrong matrix * remove trailing space * implement hashCalculate * add test case for hashCalculate * remove trailing white space * avoid hiding parameter * refine codes and keep the original range * adjust expected hash value since the range of hash change * add comment * reduce scope * remove trailing white space * adjust format * add new function compare * implement compute functions * use array as buffer of cv::Mat, avoid memory allocation * remove trailing whitespace * 1 : implement cross-correlation rather than using matchTemplate since the results of matchTemplate are weird 2 : remove gamma param, although the paper said PHash apply gamma correction on the image, but the codes do not do that(I think it is a bug of PHash) 3 : create function can specify sigma and numOfAngleLine 4 : Use blurImg to replace normalizeImg 5 : remove useless parameters which related to gamma correction * add example of radial variance hash * use buffer to avoid memory allocation and use enum to specify hash size * remove useless header * fix bug--constructor only accept two params * add comments * transpose projection matrix, friendlier for cache hit * use pointer to access projection value * function able to specify sigma and numOfAngleLine * add get/set functions * implement image hash algo--block mean hash * include block mean hash and add comments * remove trailing whitespace * fix warning--compare with sign and unsigned value * implement destructor and change mode type to size_t * add example of block mean hash * compress the bits of hash * function--blockMeanHash able to set mode * fix type cast warning and style * change expected result to bool * compress the hash value from 16 bytes to 8 bytes * update comments * compress hash from 16 bytes to 8 bytes * use peak value of cross correlation as comparison result * add limit header * first commit * add group and header file of color moment hash * should not use auto, it is c++11 feature * support python binding * implement destructor of AverageHash * support python binding * support python binding * support python binding * change type to inputArray and outputArray, easier for python binding * all algorithms support input type CV_8UC4,CV_8UC3 and CV_8UC1 * Provide better instructions * Make it more pleasant to read * Add information of speed comparsion * remove trailing white space * remove useless blank line * refine comments of example * fix link error * refine title * 1 : implement function "getDefaultName" 2 : adjust style * Update README.md 1 : Fix wrong branch 2 : Add another solution to download img_hash * img_hash: refactored interfaces to use pImpl * remove trailing white space * img_hash: use type-safe pImpl * change name, easier to find the source file * 1 : narrow scope of ImgHashImpl 2 : use static cast to replace dynamic cast because class hierarchy of img_hash is very straightforward * should not declare ImgHashImpl api in the header file of ImgHashBase, this increase chance of breaking ABI * should not declare ImgHashImpl api in the header file of ImgHashBase, this increase chance of breaking ABI * fix warning, because unelaborated friend declaration is a C++11 extension * first commit * fix bug, except of windows, other platforms cannot access private member. pimpl only accessable by the class itself, therefore I think declare everything as public is quite safe * first commit comparison and computation charts * update chart link
1 parent 8ef2f71 commit a4a8b84

30 files changed

+2139
-0
lines changed

modules/img_hash/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
set(the_description "Image hash algorithms")
2+
set(OPENCV_MODULE_IS_PART_OF_WORLD OFF)
3+
ocv_define_module(img_hash opencv_imgproc opencv_core WRAP java python)

modules/img_hash/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Image Hashing algorithms
2+
========================
3+
4+
This module is intended to port the algorithms from PHash library and implement other image hash
5+
algorithm do not exist in PHash library yet.
110 KB
Loading
43.8 KB
Loading
45.9 KB
Loading

modules/img_hash/doc/img_hash.bib

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@misc{lookslikeit,
2+
author={Krawetz, Neal},
3+
title={Looks Like It},
4+
url={http://www.hackerfactor.com/blog/?/archives/432-Looks-Like-It.html}
5+
}
6+
7+
@article{tang2012perceptual,
8+
title={Perceptual hashing for color images using invariant moments},
9+
author={Tang, Zhenjun and Dai, Yumin and Zhang, Xianquan},
10+
journal={Appl. Math},
11+
volume={6},
12+
number={2S},
13+
pages={643S--650S},
14+
year={2012},
15+
url={http://www.phash.org/docs/pubs/thesis_zauner.pdf}
16+
}
17+
18+
@article{zauner2010implementation,
19+
title={Implementation and benchmarking of perceptual image hash functions},
20+
author={Zauner, Christoph},
21+
year={2010},
22+
publisher={na}
23+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html.
4+
5+
#ifndef OPENCV_IMG_HASH_H
6+
#define OPENCV_IMG_HASH_H
7+
8+
#include "opencv2/img_hash/average_hash.hpp"
9+
#include "opencv2/img_hash/block_mean_hash.hpp"
10+
#include "opencv2/img_hash/color_moment_hash.hpp"
11+
#include "opencv2/img_hash/marr_hildreth_hash.hpp"
12+
#include "opencv2/img_hash/phash.hpp"
13+
#include "opencv2/img_hash/radial_variance_hash.hpp"
14+
15+
/**
16+
@defgroup img_hash The module brings implementations of different image hashing algorithms.
17+
18+
Provide algorithms to extract the hash of images and fast way to figure out most similar images in
19+
huge data set.
20+
21+
Namespace for all functions is cv::img_hash.
22+
23+
### Supported Algorithms
24+
25+
- Average hash (also called Different hash)
26+
- PHash (also called Perceptual hash)
27+
- Marr Hildreth Hash
28+
- Radial Variance Hash
29+
- Block Mean Hash (modes 0 and 1)
30+
- Color Moment Hash (this is the one and only hash algorithm resist to rotation attack(-90~90 degree))
31+
32+
You can study more about image hashing from following paper and websites:
33+
34+
- "Implementation and benchmarking of perceptual image hash functions" @cite zauner2010implementation
35+
- "Looks Like It" @cite lookslikeit
36+
37+
### Code Example
38+
39+
@include samples/hash_samples.cpp
40+
41+
### Performance under different attacks
42+
43+
![Performance chart](img_hash/doc/attack_performance.JPG)
44+
45+
### Speed comparison with PHash library (100 images from ukbench)
46+
47+
![Hash Computation chart](img_hash/doc/hash_computation_chart.JPG)
48+
![Hash comparison chart](img_hash/doc/hash_comparison_chart.JPG)
49+
50+
As you can see, hash computation speed of img_hash module outperform [PHash library](http://www.phash.org/) a lot.
51+
52+
PS : I do not list out the comparison of Average hash, PHash and Color Moment hash, because I cannot
53+
find them in PHash.
54+
55+
### Motivation
56+
57+
Collects useful image hash algorithms into opencv, so we do not need to rewrite them by ourselves
58+
again and again or rely on another 3rd party library(ex : PHash library). BOVW or correlation
59+
matching are good and robust, but they are very slow compare with image hash, if you need to deal
60+
with large scale CBIR(content based image retrieval) problem, image hash is a more reasonable
61+
solution.
62+
63+
### More info
64+
65+
You can learn more about img_hash modules from following links, these links show you how to find
66+
similar image from ukbench dataset, provide thorough benchmark of different attacks(contrast, blur,
67+
noise(gaussion,pepper and salt), jpeg compression, watermark, resize).
68+
69+
* [Introduction to image hash module of opencv](http://qtandopencv.blogspot.my/2016/06/introduction-to-image-hash-module-of.html)
70+
* [Speed up image hashing of opencv(img_hash) and introduce color moment hash](http://qtandopencv.blogspot.my/2016/06/speed-up-image-hashing-of-opencvimghash.html)
71+
72+
### Contributors
73+
74+
Tham Ngap Wei, [email protected]
75+
76+
*/
77+
78+
#endif // OPENCV_IMG_HASH_H
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html.
4+
5+
#ifndef OPENCV_AVERAGE_HASH_HPP
6+
#define OPENCV_AVERAGE_HASH_HPP
7+
8+
#include "img_hash_base.hpp"
9+
10+
namespace cv {
11+
namespace img_hash {
12+
13+
//! @addtogroup img_hash
14+
//! @{
15+
16+
/** @brief Computes average hash value of the input image
17+
18+
This is a fast image hashing algorithm, but only work on simple case. For more details, please
19+
refer to @cite lookslikeit
20+
*/
21+
class CV_EXPORTS_W AverageHash : public ImgHashBase
22+
{
23+
public:
24+
CV_WRAP static Ptr<AverageHash> create();
25+
protected:
26+
AverageHash() {}
27+
};
28+
29+
/** @brief Calculates img_hash::AverageHash in one call
30+
@param inputArr input image want to compute hash value, type should be CV_8UC4, CV_8UC3 or CV_8UC1.
31+
@param outputArr Hash value of input, it will contain 16 hex decimal number, return type is CV_8U
32+
*/
33+
CV_EXPORTS_W void averageHash(cv::InputArray inputArr, cv::OutputArray outputArr);
34+
35+
//! @}
36+
37+
}} // cv::img_hash::
38+
39+
#endif // OPENCV_AVERAGE_HASH_HPP
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html.
4+
5+
#ifndef OPENCV_BLOCK_MEAN_HASH_HPP
6+
#define OPENCV_BLOCK_MEAN_HASH_HPP
7+
8+
#include "img_hash_base.hpp"
9+
10+
namespace cv {
11+
namespace img_hash {
12+
13+
//! @addtogroup img_hash
14+
//! @{
15+
16+
enum BlockMeanHashMode
17+
{
18+
BLOCK_MEAN_HASH_MODE_0 = 0, //!< use fewer block and generate 16*16/8 uchar hash value
19+
BLOCK_MEAN_HASH_MODE_1 = 1, //!< use block blocks(step sizes/2), generate 31*31/8 + 1 uchar hash value
20+
};
21+
22+
/** @brief Image hash based on block mean.
23+
24+
See @cite zauner2010implementation for details.
25+
*/
26+
class CV_EXPORTS_W BlockMeanHash : public ImgHashBase
27+
{
28+
public:
29+
/** @brief Create BlockMeanHash object
30+
@param mode
31+
*/
32+
CV_WRAP void setMode(int mode);
33+
CV_WRAP std::vector<double> getMean() const;
34+
CV_WRAP static Ptr<BlockMeanHash> create(int mode = BLOCK_MEAN_HASH_MODE_0);
35+
protected:
36+
BlockMeanHash() {}
37+
};
38+
39+
/** @brief Computes block mean hash of the input image
40+
@param inputArr input image want to compute hash value, type should be CV_8UC4, CV_8UC3 or CV_8UC1.
41+
@param outputArr Hash value of input, it will contain 16 hex decimal number, return type is CV_8U
42+
@param mode
43+
*/
44+
CV_EXPORTS_W void blockMeanHash(cv::InputArray inputArr,
45+
cv::OutputArray outputArr,
46+
int mode = BLOCK_MEAN_HASH_MODE_0);
47+
48+
//! @}
49+
50+
}} // cv::img_hash::
51+
52+
#endif // OPENCV_BLOCK_MEAN_HASH_HPP
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html.
4+
5+
#ifndef OPENCV_COLOR_MOMENT_HASH_HPP
6+
#define OPENCV_COLOR_MOMENT_HASH_HPP
7+
8+
#include "img_hash_base.hpp"
9+
10+
namespace cv {
11+
namespace img_hash {
12+
13+
//! @addtogroup img_hash
14+
//! @{
15+
16+
/** @brief Image hash based on color moments.
17+
18+
See @cite tang2012perceptual for details.
19+
*/
20+
class CV_EXPORTS_W ColorMomentHash : public ImgHashBase
21+
{
22+
public:
23+
CV_WRAP static Ptr<ColorMomentHash> create();
24+
protected:
25+
ColorMomentHash() {}
26+
};
27+
28+
/** @brief Computes color moment hash of the input, the algorithm
29+
is come from the paper "Perceptual Hashing for Color Images
30+
Using Invariant Moments"
31+
@param inputArr input image want to compute hash value,
32+
type should be CV_8UC4, CV_8UC3 or CV_8UC1.
33+
@param outputArr 42 hash values with type CV_64F(double)
34+
*/
35+
CV_EXPORTS_W void colorMomentHash(cv::InputArray inputArr, cv::OutputArray outputArr);
36+
37+
//! @}
38+
39+
}} // cv::img_hash::
40+
41+
#endif // OPENCV_COLOR_MOMENT_HASH_HPP

0 commit comments

Comments
 (0)