fftshift implementation for OpenCV
I think the best way to implement fftshift is like it is done in OpenCv itself:

//The body is inspired from OpenCV, the fct_fftshift is mine
void fct_fftshift(cv::Mat&amp; src)
{
    int cx = src.cols/2;
    int cy = src.rows/2;

    cv::Mat q0(src, cv::Rect(0, 0, cx, cy));   
    cv::Mat q1(src, cv::Rect(cx, 0, cx, cy));  
    cv::Mat q2(src, cv::Rect(0, cy, cx, cy));  
    cv::Mat q3(src, cv::Rect(cx, cy, cx, cy)); 

    cv::Mat tmp;                           
    q0.copyTo(tmp);
    q3.copyTo(q0);
    tmp.copyTo(q3);

    q1.copyTo(tmp);
    q2.copyTo(q1);
    tmp.copyTo(q2); 
}

int main(int argc, char* argv[])
{
  cv::Mat src=cv::imread(argv[1], CV_8UC1);
  fct_fftshift(src); //it will change your original image
}