void perceptron_code(Mat src, int w, int h, int* result)
{
src.convertTo(src, CV_32F, 1.0 / 255);
Mat grayDCT;
dct(src, grayDCT);
grayDCT = grayDCT.rowRange(0, h).colRange(0,w);
float meanV = 0.0;
for (int i = 0; i < h; i++)
{
float *pData = grayDCT.ptr<float>(i);
for (int j = 0; j < w; j++)
{
meanV += pData[j];
}
}
meanV /= (w * h);
for (int i = 0; i < h; i++)
{
for (int j = 0; j < grayDCT.cols; j++)
{
if (grayDCT.at<float>(i, j) >= meanV)
{
result[i * w + j] = 1;
}
else
{
result[i * w + j] = 0;
}
}
}
}
int main(int argc, char **argv)
{
Mat src1 = imread("./image1.jpg", 0);
if (src1.empty())
{
cout << "Can not load the image1.jpg..." << endl;
return -1;
}
Mat src2 = imread("./image2.jpg", 0);
if (src2.empty())
{
cout << "Can not load the image2.jpg..." << endl;
return -1;
}
const int resize_w = 8;
const int resize_h = 8;
int *result1 = new int[resize_w * resize_h];
int *result2 = new int[resize_w * resize_h];
perceptron_code(src1, resize_w, resize_h, result1);
perceptron_code(src2, resize_w, resize_h, result2);
int matchNum = 0;
for (int i = 0; i < resize_w * resize_h; i++)
{
if (result1[i] == result2[i])
{
matchNum++;
}
}
float correlation = 1.0 * matchNum / (resize_w * resize_h);
cout << "The two image correlation is " << correlation << endl;
delete(result1);
delete(result2);
return 0;
}
输出为:The two image correlation is 0.78125