操作步骤:
1. 载入图像(灰度图或者彩色图),并使其大小一致;
2. 若为彩色图,增进行颜色空间变换,从RGB转换到HSV,若为灰度图则无需变换;
3. 若为灰度图,直接计算其直方图,并进行直方图归一化;
4. 若为彩色图,则计算其彩色直方图,并进行彩色直方图归一化;
5. 使用相似度公式,如相关系数、卡方、相交或巴氏距离,计算出相似度值。
string strSrcImageName = "src.jpg"; cv::Mat matSrc, matSrc1, matSrc2; matSrc = cv::imread(strSrcImageName, CV_LOAD_IMAGE_UNCHANGED); cv::resize(matSrc, matSrc1, cv::Size(357, 419), 0, 0, cv::INTER_NEAREST); cv::resize(matSrc, matSrc2, cv::Size(2177, 3233), 0, 0, cv::INTER_LANCZOS4); cv::Mat matDst1, matDst2; cv::Size sizeImage = cv::Size(500, 500); cv::resize(matSrc1, matDst1, sizeImage, 0, 0, cv::INTER_CUBIC); //cv::flip(matDst1, matDst1, 1); cv::resize(matSrc2, matDst2, sizeImage, 0, 0, cv::INTER_CUBIC); if (matSrc.channels() == 1) { int histSize = 256; float range[] = {0, 256}; const float* histRange = {range}; bool uniform = true; bool accumulate = false; cv::Mat hist1, hist2; cv::calcHist(&matDst1, 1, 0, cv::Mat(), hist1, 1, &histSize, &histRange, uniform, accumulate); cv::normalize(hist1, hist1, 0, 1, cv::NORM_MINMAX, -1, cv::Mat()); cv::calcHist(&matDst2, 1, 0, cv::Mat(), hist2, 1, &histSize, &histRange, uniform, accumulate); cv::normalize(hist2, hist2, 0, 1, cv::NORM_MINMAX, -1, cv::Mat()); double dSimilarity = cv::compareHist(hist1, hist2, CV_COMP_CORREL);//,CV_COMP_CHISQR,CV_COMP_INTERSECT,CV_COMP_BHATTACHARYYA cout<<"similarity = "<<