Skip to content

Instantly share code, notes, and snippets.

Last active April 11, 2021 17:07
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Mainvooid/a0f76ac05aa4119c2d12e7c8919fdada to your computer and use it in GitHub Desktop.
基于Hash的结合纹理和颜色的图像相似度算法 #OpenCV #C++
bool isHashSimilar(cv::UMat imageUMat, cv::UMat templUMat, cv::Point matchLoc, int threshold) {
cv::UMat imgUMat = imageUMat(cv::Range(matchLoc.y, matchLoc.y + templUMat.rows),
cv::Range(matchLoc.x, matchLoc.x + templUMat.cols));
cv::UMat imgPHashMat = cv::UMat(1, 8, CV_8U);
cv::UMat templPHashMat = cv::UMat(1, 8, CV_8U);
cv::UMat imgColorMomentHashMat = cv::UMat(1, 42, CV_64F);
cv::UMat templColorMomentHashMat = cv::UMat(1, 42, CV_64F);
cv::Ptr<cv::img_hash::PHash> pHashPtr(cv::img_hash::PHash::create());
pHashPtr->compute(imgUMat, imgPHashMat);
pHashPtr->compute(templUMat, templPHashMat);
double pHashCompareResult = pHashPtr->compare(imgPHashMat, templPHashMat);
cv::Ptr<cv::img_hash::ColorMomentHash> colorMomentHashPtr(
colorMomentHashPtr->compute(imgUMat, imgColorMomentHashMat);
colorMomentHashPtr->compute(templUMat, templColorMomentHashMat);
double colorMomentHashCompareResult = colorMomentHashPtr->compare(imgColorMomentHashMat,
if (pHashCompareResult <= threshold && colorMomentHashCompareResult <= threshold) {
return 1;
} else {
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment