Skip to content

Instantly share code, notes, and snippets.

@msly
Last active November 14, 2016 08:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msly/bb0bff63914d54fed88f to your computer and use it in GitHub Desktop.
Save msly/bb0bff63914d54fed88f to your computer and use it in GitHub Desktop.
图片相似算法phash
string pHashValue(Mat &src) {
// gray
assert(src.channels() == 1);
string hash(64, '\0');
Mat img, dst;
resize(src, img, Size(32, 32));
img = Mat_<double>(img);
dct(img, dst);
dst = dst( Rect(1, 1, 8, 8) );
Scalar imageMean = mean(dst);
Mat mask = (dst >= imageMean[0]);
for (int i = 0; i<mask.rows; i++) {
for (int j = 0; j<mask.cols; j++) {
mask.at<uchar>(i, j) == 0 ? (hash[i*mask.cols + j] = '0')
: (hash[i*mask.cols + j] = '1');
}
}
return hash;
}
@msly
Copy link
Author

msly commented Dec 12, 2015

Rect(1, 1, 8, 8) // important

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment