OpenCV  5.0.0alpha
Open Source Computer Vision
Loading...
Searching...
No Matches
The module brings implementations of different image hashing algorithms.

Detailed Description

Provide algorithms to extract the hash of images and fast way to figure out most similar images in huge data set.

Namespace for all functions is cv::img_hash.

Supported Algorithms

You can study more about image hashing from following paper and websites:

Code Example

#include "opencv2/core.hpp"
#include <iostream>
using namespace cv;
using namespace cv::img_hash;
using namespace std;
template <typename T>
inline void test_one(const std::string &title, const Mat &a, const Mat &b)
{
cout << "=== " << title << " ===" << endl;
TickMeter tick;
Mat hashA, hashB;
func = T::create();
tick.reset(); tick.start();
func->compute(a, hashA);
tick.stop();
cout << "compute1: " << tick.getTimeMilli() << " ms" << endl;
tick.reset(); tick.start();
func->compute(b, hashB);
tick.stop();
cout << "compute2: " << tick.getTimeMilli() << " ms" << endl;
cout << "compare: " << func->compare(hashA, hashB) << endl << endl;;
}
int main(int argc, char **argv)
{
if (argc != 3)
{
cerr << "must input the path of input image and target image. ex : hash_samples lena.jpg lena2.jpg" << endl;
return -1;
}
ocl::setUseOpenCL(false);
Mat input = imread(argv[1]);
Mat target = imread(argv[2]);
test_one<AverageHash>("AverageHash", input, target);
test_one<PHash>("PHash", input, target);
test_one<MarrHildrethHash>("MarrHildrethHash", input, target);
test_one<RadialVarianceHash>("RadialVarianceHash", input, target);
test_one<BlockMeanHash>("BlockMeanHash", input, target);
return 0;
}
n-dimensional dense array class
Definition mat.hpp:951
a Class to measure passing time.
Definition utility.hpp:326
void start()
starts counting ticks.
Definition utility.hpp:335
void stop()
stops counting ticks.
Definition utility.hpp:341
void reset()
resets internal values.
Definition utility.hpp:430
double getTimeMilli() const
returns passed time in milliseconds.
Definition utility.hpp:365
std::shared_ptr< _Tp > Ptr
Definition cvstd_wrapper.hpp:23
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
Loads an image from a file.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
Definition average_hash.hpp:11
Definition core.hpp:107
STL namespace.

Performance under different attacks

Performance chart

Speed comparison with PHash library (100 images from ukbench)

As you can see, hash computation speed of img_hash module outperform PHash library a lot.

PS : I do not list out the comparison of Average hash, PHash and Color Moment hash, because I cannot find them in PHash.

Motivation

Collects useful image hash algorithms into opencv, so we do not need to rewrite them by ourselves again and again or rely on another 3rd party library(ex : PHash library). BOVW or correlation matching are good and robust, but they are very slow compare with image hash, if you need to deal with large scale CBIR(content based image retrieval) problem, image hash is a more reasonable solution.

More info

You can learn more about img_hash modules from following links, these links show you how to find similar image from ukbench dataset, provide thorough benchmark of different attacks(contrast, blur, noise(gaussion,pepper and salt), jpeg compression, watermark, resize).

Introduction to image hash module of opencv Speed up image hashing of opencv(img_hash) and introduce color moment hash

Contributors

Tham Ngap Wei, thamn.nosp@m.gapw.nosp@m.ei@gm.nosp@m.ail..nosp@m.com

Classes

class  cv::img_hash::AverageHash
 Computes average hash value of the input image. More...
 
class  cv::img_hash::BlockMeanHash
 Image hash based on block mean. More...
 
class  cv::img_hash::ColorMomentHash
 Image hash based on color moments. More...
 
class  cv::img_hash::ImgHashBase
 The base class for image hash algorithms. More...
 
class  cv::img_hash::MarrHildrethHash
 Marr-Hildreth Operator Based Hash, slowest but more discriminative. More...
 
class  cv::img_hash::PHash
 pHash More...
 
class  cv::img_hash::RadialVarianceHash
 Image hash based on Radon transform. More...
 

Enumerations

enum  cv::img_hash::BlockMeanHashMode {
  cv::img_hash::BLOCK_MEAN_HASH_MODE_0 = 0 ,
  cv::img_hash::BLOCK_MEAN_HASH_MODE_1 = 1
}
 

Functions

void cv::img_hash::averageHash (cv::InputArray inputArr, cv::OutputArray outputArr)
 Calculates img_hash::AverageHash in one call.
 
void cv::img_hash::blockMeanHash (cv::InputArray inputArr, cv::OutputArray outputArr, int mode=BLOCK_MEAN_HASH_MODE_0)
 Computes block mean hash of the input image.
 
void cv::img_hash::colorMomentHash (cv::InputArray inputArr, cv::OutputArray outputArr)
 Computes color moment hash of the input, the algorithm is come from the paper "Perceptual Hashing for Color Images Using Invariant Moments".
 
void cv::img_hash::marrHildrethHash (cv::InputArray inputArr, cv::OutputArray outputArr, float alpha=2.0f, float scale=1.0f)
 Computes average hash value of the input image.
 
void cv::img_hash::pHash (cv::InputArray inputArr, cv::OutputArray outputArr)
 Computes pHash value of the input image.
 
void cv::img_hash::radialVarianceHash (cv::InputArray inputArr, cv::OutputArray outputArr, double sigma=1, int numOfAngleLine=180)
 Computes radial variance hash of the input image.
 

Enumeration Type Documentation

◆ BlockMeanHashMode

#include <opencv2/img_hash/block_mean_hash.hpp>

Enumerator
BLOCK_MEAN_HASH_MODE_0 
Python: cv.img_hash.BLOCK_MEAN_HASH_MODE_0

use fewer block and generate 16*16/8 uchar hash value

BLOCK_MEAN_HASH_MODE_1 
Python: cv.img_hash.BLOCK_MEAN_HASH_MODE_1

use block blocks(step sizes/2), generate 31*31/8 + 1 uchar hash value

Function Documentation

◆ averageHash()

void cv::img_hash::averageHash ( cv::InputArray inputArr,
cv::OutputArray outputArr )
Python:
cv.img_hash.averageHash(inputArr[, outputArr]) -> outputArr

#include <opencv2/img_hash/average_hash.hpp>

Calculates img_hash::AverageHash in one call.

Parameters
inputArrinput image want to compute hash value, type should be CV_8UC4, CV_8UC3 or CV_8UC1.
outputArrHash value of input, it will contain 16 hex decimal number, return type is CV_8U

◆ blockMeanHash()

void cv::img_hash::blockMeanHash ( cv::InputArray inputArr,
cv::OutputArray outputArr,
int mode = BLOCK_MEAN_HASH_MODE_0 )
Python:
cv.img_hash.blockMeanHash(inputArr[, outputArr[, mode]]) -> outputArr

#include <opencv2/img_hash/block_mean_hash.hpp>

Computes block mean hash of the input image.

Parameters
inputArrinput image want to compute hash value, type should be CV_8UC4, CV_8UC3 or CV_8UC1.
outputArrHash value of input, it will contain 16 hex decimal number, return type is CV_8U
modethe mode

◆ colorMomentHash()

void cv::img_hash::colorMomentHash ( cv::InputArray inputArr,
cv::OutputArray outputArr )
Python:
cv.img_hash.colorMomentHash(inputArr[, outputArr]) -> outputArr

#include <opencv2/img_hash/color_moment_hash.hpp>

Computes color moment hash of the input, the algorithm is come from the paper "Perceptual Hashing for Color Images Using Invariant Moments".

Parameters
inputArrinput image want to compute hash value, type should be CV_8UC4, CV_8UC3 or CV_8UC1.
outputArr42 hash values with type CV_64F(double)

◆ marrHildrethHash()

void cv::img_hash::marrHildrethHash ( cv::InputArray inputArr,
cv::OutputArray outputArr,
float alpha = 2.0f,
float scale = 1.0f )
Python:
cv.img_hash.marrHildrethHash(inputArr[, outputArr[, alpha[, scale]]]) -> outputArr

#include <opencv2/img_hash/marr_hildreth_hash.hpp>

Computes average hash value of the input image.

Parameters
inputArrinput image want to compute hash value, type should be CV_8UC4, CV_8UC3, CV_8UC1.
outputArrHash value of input, it will contain 16 hex decimal number, return type is CV_8U
alphaint scale factor for marr wavelet (default=2).
scaleint level of scale factor (default = 1)

◆ pHash()

void cv::img_hash::pHash ( cv::InputArray inputArr,
cv::OutputArray outputArr )
Python:
cv.img_hash.pHash(inputArr[, outputArr]) -> outputArr

#include <opencv2/img_hash/phash.hpp>

Computes pHash value of the input image.

Parameters
inputArrinput image want to compute hash value, type should be CV_8UC4, CV_8UC3, CV_8UC1.
outputArrHash value of input, it will contain 8 uchar value

◆ radialVarianceHash()

void cv::img_hash::radialVarianceHash ( cv::InputArray inputArr,
cv::OutputArray outputArr,
double sigma = 1,
int numOfAngleLine = 180 )
Python:
cv.img_hash.radialVarianceHash(inputArr[, outputArr[, sigma[, numOfAngleLine]]]) -> outputArr

#include <opencv2/img_hash/radial_variance_hash.hpp>

Computes radial variance hash of the input image.

Parameters
inputArrinput image want to compute hash value, type should be CV_8UC4, CV_8UC3, CV_8UC1.
outputArrHash value of input
sigmaGaussian kernel standard deviation
numOfAngleLineThe number of angles to consider