001 #include <opencv2/opencv.hpp> 002 #include <opencv2/opencv_lib.hpp> 003 004 using namespace cv; 005 006 void myGrayscaleImage(InputArray _matSrc, OutputArray _matDst) { 007 Mat matSrc = _matSrc.getMat(); 008 Mat matDst = _matDst.getMat(); 009 010 for(int sy = 0; sy < matSrc.rows; sy++) { 011 Vec3b* srcData = matSrc.ptr<Vec3b>(sy); 012 double* dstData = matDst.ptr<double>(sy); 013 for(int sx = 0; sx < matSrc.cols; sx++) { 014 double b = srcData[sx][0]; 015 double g = srcData[sx][1]; 016 double r = srcData[sx][2]; 017 dstData[sx] = (0.299 * r + 0.587 * g + 0.114 * b) / 255.0; 018 } 019 } 020 021 return; 022 } 023 024 int main(int argc, char* argv[]) { 025 if(argc != 3) { 026 printf("Usage: %s <ImageFile0> <ImageFile1>\n", argv[0]); 027 exit(-1); 028 } 029 030 Mat imgSrc0 = imread(argv[1], CV_LOAD_IMAGE_ANYCOLOR); 031 Mat imgSrc1 = imread(argv[2], CV_LOAD_IMAGE_ANYCOLOR); 032 033 if(imgSrc0.empty() || imgSrc1.empty()) { 034 fprintf(stderr, "Error: Can not open ImageFile(s)\n"); 035 exit(-1); 036 } 037 038 Mat imgGray0(imgSrc0.rows, imgSrc0.cols, CV_64FC1); 039 Mat imgGray1(imgSrc1.rows, imgSrc1.cols, CV_64FC1); 040 041 myGrayscaleImage(imgSrc0, imgGray0); 042 myGrayscaleImage(imgSrc1, imgGray1); 043 044 Mat matHann; 045 createHanningWindow(matHann, imgSrc0.size(), CV_64F); 046 047 double response; 048 Point2d shift = phaseCorrelateRes(imgGray0, imgGray1, matHann, &response); 049 050 printf("Shift X: %g\nShift Y: %g\nResponse: %g\n", shift.x, shift.y, response); 051 052 imshow("imgSrc0", imgSrc0); 053 imshow("imgSrc1", imgSrc1); 054 055 waitKey(0); 056 057 return(0); 058 }