Advertisement
Guest User

OpenCV Complex transform code

a guest
Mar 11th, 2013
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <opencv2/core/core.hpp>
  4. #include <opencv2/imgproc/imgproc.hpp>
  5. #include <opencv2/highgui/highgui.hpp>
  6. #include <math.h>
  7. #include <complex>
  8.  
  9. using namespace std;
  10. using namespace cv;
  11.  
  12. void wipe(Mat &image)
  13. {
  14.     int i = image.cols;
  15.     int j = image.rows; int k; int l;
  16.     for(k=0;k<j;k++)
  17.     {
  18.         for(l=0;l<i;l++)
  19.         {
  20.             image.at<Vec3b>(k,l)[0] = 0;//image1.at<Vec3b>(k,l)[0] + image2.at<Vec3b>(k,l)[0];
  21.             image.at<Vec3b>(k,l)[1] = 0;//image1.at<Vec3b>(k,l)[1] - image2.at<Vec3b>(k,l)[1];
  22.             image.at<Vec3b>(k,l)[2] = 0;//image1.at<Vec3b>(k,l)[2] + image2.at<Vec3b>(k,l)[2];
  23.         }
  24.     }
  25. }
  26.  
  27. void move_to_centre(Mat &input_image, Mat &target_image, int* row_start, int* col_start)
  28. {
  29.     //Declaration of variables
  30.     int input_x = input_image.cols;
  31.     int input_y = input_image.rows;
  32.     int target_x = target_image.cols;
  33.     int target_y = target_image.rows;
  34.     int input_row  = 0; int input_col=0;
  35.     int target_row = 0; int target_col = (target_y/2) - input_y;
  36.  
  37.     //The next two lines of code return the starting position of the
  38.     //image to be transformed by our complex function.
  39.     *row_start = target_y/2 - input_y;
  40.     *col_start = target_x/2;
  41.     cout<<"From inside the function: \n";
  42.     cout<<"The row_start value is: " << target_y/2 - input_y<<" \n ";
  43.     cout<<"The col_start value is: " << target_x/2 << "\n";
  44.  
  45.     //Loop that moves the image to be transformed to the centre of a temporary image of the same dimensions as
  46.     //the result image. This makes it easier to perform the transformation.
  47.     for(input_row=0, target_row=target_y/2 - input_y; input_row<input_y; input_row++, target_row++ )
  48.     {
  49.         for(input_col=0, target_col=target_x/2; input_col<input_x; input_col++, target_col++)
  50.         {
  51.             target_image.at<Vec3b>(target_row,target_col)[0] = input_image.at<Vec3b>(input_row,input_col)[0];
  52.             target_image.at<Vec3b>(target_row,target_col)[1] = input_image.at<Vec3b>(input_row,input_col)[1];
  53.             target_image.at<Vec3b>(target_row,target_col)[2] = input_image.at<Vec3b>(input_row,input_col)[2];
  54.         }
  55.     }
  56. }
  57.  
  58.  
  59. int main(int argc, char** argv)
  60. {
  61.     Mat image1 = imread(argv[1], 1);
  62.     Mat image2 = imread(argv[2], 1);
  63.     Mat result;
  64.     image1.copyTo(result);
  65.     //int i = image2.cols;
  66.     //int j = image2.rows;
  67.     int k; int l;
  68.     complex<double> one(1,0);
  69.     complex<double> rot(0.4,0);
  70.     complex<double> trans(400, 0);
  71.     complex<double> coord_transform(-(image1.cols)/2, (image1.rows)/2); //This is the coordinate transformation that superimposes axes at the centre of the image.
  72.     complex<double> dom1(0,0); //First domain
  73.     complex<double> dom2(0,0); //After coord_stransform
  74.     complex<int> rang(0,0);    // Final image coordinates
  75.     complex<double> rang_inter(0,0); //image coords in the transformed system.
  76.     int row_start=0; int col_start=0; //Starting positions of image
  77.     wipe(result);
  78.     wipe(image1);
  79.     //This function moves the image to be transformed to the centre of a temporary image of same dimensions as the result.
  80.     move_to_centre(image2, image1, &row_start, &col_start);
  81.  
  82.     cout<<"\n\nFrom outside the function: \n";
  83.     cout<<"The row_start value is: " << row_start <<" \n ";
  84.     cout<<"The col_start value is: " << col_start << "\n";
  85.     k = 28; l = 960;
  86.     cout<<"\n";
  87.     imag(dom1) = -k; real(dom2) = l;
  88.     cout<<"Step 1: "<<dom1<<"\n";
  89.     dom2 = dom1 + coord_transform;
  90.     cout<<"Step 2:"<<dom2<<"\n";
  91.     rang_inter = dom2;
  92.     cout<<"Step 3: "<<rang_inter<<"\n";
  93.     rang = rang_inter - coord_transform;
  94.     cout<<"Step 3: "<< rang<<"\n";
  95.     imag(rang) = -1*imag(rang);
  96.     cout<<"Step 4:"<<rang<<"\n";
  97.  
  98.     for(k = row_start; k < row_start + image2.rows; k++)
  99.     {
  100.         for(l = col_start; l < col_start + image2.cols; l++)
  101.         {
  102.             imag(dom1) = -k; real(dom1) = l;
  103.             dom2 = dom1 + coord_transform;
  104.  
  105.             //Complex transforms. Uncomment the one you want to apply transform.
  106.             rang_inter = (1/abs(dom2))*dom2*dom2;        //w=z**2 transform
  107.             //rang_inter = (1/exp(real(dom2)))*exp(dom2);//exponential
  108.             //rang_inter = (one/dom2)*abs(dom2);         //Joujouwski transform
  109.  
  110.             rang = rang_inter - coord_transform;
  111.             imag(rang) = -1*imag(rang);
  112.             //cout<<rang;
  113.             if((imag(rang)<result.rows)&&(real(rang)<result.cols)&&(imag(rang)>0)&&(real(rang)>0))
  114.             {
  115.                 result.at<Vec3b>(imag(rang),real(rang))[0] = image1.at<Vec3b>(k, l)[0];//image1.at<Vec3b>(k,l)[0] + image2.at<Vec3b>(k,l)[0];
  116.                 result.at<Vec3b>(imag(rang),real(rang))[1] = image1.at<Vec3b>(k, l)[1];//image1.at<Vec3b>(k,l)[1] - image2.at<Vec3b>(k,l)[1];
  117.                 result.at<Vec3b>(imag(rang),real(rang))[2] = image1.at<Vec3b>(k, l)[2];//image1.at<Vec3b>(k,l)[2] + image2.at<Vec3b>(k,l)[2];
  118.             }
  119.  
  120.         }
  121.     }
  122.  
  123.     namedWindow(argv[1]);
  124.     namedWindow(argv[2]);
  125.     namedWindow("Result");
  126.  
  127.     imshow(argv[1], image1);
  128.     imshow(argv[2], image2);
  129.     imshow("Result", result);
  130.     imwrite("Result.png",result);
  131.  
  132.     waitKey(0);
  133.  
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement