Last active
November 14, 2016 08:45
-
-
Save msly/bb0bff63914d54fed88f to your computer and use it in GitHub Desktop.
图片相似算法phash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rect(1, 1, 8, 8) // important