Skip to content

Instantly share code, notes, and snippets.

@Mainvooid
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));
//声明存放Hash结果数据的矩阵
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);
//PHash
//获取pHash类的智能指针并调用类的create()方法初始化
cv::Ptr<cv::img_hash::PHash> pHashPtr(cv::img_hash::PHash::create());
//计算pHash结果数组
pHashPtr->compute(imgUMat, imgPHashMat);
pHashPtr->compute(templUMat, templPHashMat);
//计算汉明距离
double pHashCompareResult = pHashPtr->compare(imgPHashMat, templPHashMat);
//ColorMomentHash
cv::Ptr<cv::img_hash::ColorMomentHash> colorMomentHashPtr(
cv::img_hash::ColorMomentHash::create());
colorMomentHashPtr->compute(imgUMat, imgColorMomentHashMat);
colorMomentHashPtr->compute(templUMat, templColorMomentHashMat);
//计算欧几里得距离
double colorMomentHashCompareResult = colorMomentHashPtr->compare(imgColorMomentHashMat,
templColorMomentHashMat);
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