Przekształcenia geometryczne

Translacja (przesunięcie)

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;
int main(){
  Mat img = imread("./LENA_512.jpg", 1 ); 
  Mat result_img;
  // Initialise to the identity matrix:
  Mat affine_matrix = Mat::eye( 2, 3, CV_32FC1 );
  // affine_matrix[0][2]:
  *(((float*) (affine_matrix.data))+2) = 40;
  // affine_matrix [1][2]:
  *(((float*) (affine_matrix.data))+5) = 80;
  cout<<affine_matrix<<endl;
  // Apply transformation
  warpAffine( img,result_img,affine_matrix,img.size() );
  imshow( "Image", img);
  imshow( "Result", result_img);
  waitKey(0);
  return 0;  
}  

Przeskalowanie (zmniejszenie/rozszerzenie)

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;
int main(){
  Mat img = imread("./LENA_512.jpg", 1 ); 
  Mat result_img;
  // Initialise to the identity matrix:
  Mat affine_matrix = Mat::eye( 2, 3, CV_32FC1 );
  // affine_matrix[0][0] Skewing:
  *(((float*) (affine_matrix.data))+0) = 0.5;
  // affine_matrix [1][1] Translate to the left:
  *(((float*) (affine_matrix.data))+4) = 1.5;cout<
  cout<<affine_matrix<<endl;
  // Apply transformation
  warpAffine( img,result_img,affine_matrix,img.size() );
  imshow( "Image", img);
  imshow( "Result", result_img);
  waitKey(0);
  return 0;  
}  

Obroty

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

#define PI 3.14159265
using namespace std;
using namespace cv;
int main(){
  Mat img = imread("./LENA_512.jpg", 1 ); 
  Mat result_img;
  double phi = PI/4;
  // Initialise to the identity matrix:
  Mat affine_matrix = Mat::eye( 2, 3, CV_32FC1 );
  // affine_matrix[0][0] Skewing:
  *(((float*) (affine_matrix.data))+0) = cos(phi);
  // affine_matrix[0][1] Skewing:
  *(((float*) (affine_matrix.data))+1) = sin(phi);
  // affine_matrix [1][0] Translate to the left:
  *(((float*) (affine_matrix.data))+3) = -sin(phi);
  cout<<affine_matrix<<endl;
  // Apply transformation
  warpAffine( img,result_img,affine_matrix,img.size() );
  imshow( "Image", img);
  imshow( "Result", result_img);
  waitKey(0);
  return 0;  
}  

Pochylanie

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

#define PI 3.14159265
using namespace std;
using namespace cv;
int main(){
  Mat img = imread("./LENA_512.jpg", 1 ); 
  Mat result_img;
  double phi = PI/8;
  // Initialise to the identity matrix:
  Mat affine_matrix = Mat::eye( 2, 3, CV_32FC1 );
  // affine_matrix[0][1] Skewing:
  *(((float*) (affine_matrix.data))+1) = tan(phi);
  cout<<affine_matrix<<endl;
  // Apply transformation
  warpAffine( img,result_img,affine_matrix,img.size() );
  imshow( "Image", img);
  imshow( "Result", result_img);
  waitKey(0);
  return 0;  
}  

Przykład

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

#define PI 3.14159265
using namespace std;
using namespace cv;
int main(){
  Mat img = imread("./car_registration.png", 1 ); 
  Mat result_img_1;
  Mat result_img_2;
  Mat result_img_3;
  double phi_1 = -15;
  cv:Size s_1 = Size((int) (1.4*img.cols), img.rows);

  Point center = Point( img.cols/2, img.rows/2 );
  Mat rot_mat = getRotationMatrix2D( center, phi_1, 1 );
  cout<<rot_mat<<endl;
  warpAffine( img,result_img_1,rot_mat, s_1);
  imshow( "Result 1", result_img_1);

  double phi_2 = PI/12;
  Mat affine_matrix = Mat::eye( 2, 3, CV_32FC1 );
  *(((float*) (affine_matrix.data))+1) = tan(phi_2);
  cout<<affine_matrix<<endl;
  warpAffine( result_img_1, result_img_2, affine_matrix, s_1 );
  imshow( "Result 2", result_img_2);

  affine_matrix = Mat::eye( 2, 3, CV_32FC1 );
  *(((float*) (affine_matrix.data))+0) = 1.3;
  cout<<affine_matrix<<endl;
  warpAffine( result_img_2,result_img_3, affine_matrix, s_1 );
  imshow( "Result 3", result_img_3);

  imshow( "Image", img);
  waitKey(0);
  return 0;  
}  

Znajdowanie nieznanego przekształcenia afinicznego

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;

int main(){
   Point2f srcTri[3];
   Point2f dstTri[3];

   Mat rot_mat( 2, 3, CV_32FC1 );
   Mat warp_mat( 2, 3, CV_32FC1 );
   Mat img, warp_dst, warp_rotate_dst;
   img = imread("./car_registration.png", 1 );   
   srcTri[0] = Point2f( 37, 116);
   srcTri[1] = Point2f( 37, 256 );
   srcTri[2] = Point2f( 454, 1 );

   dstTri[0] = Point2f( 0, 0 );
   dstTri[1] = Point2f( 0, img.rows - 1 );
   dstTri[2] = Point2f( img.cols - 1, 0 );

   // Get the Affine Transform
   warp_mat = getAffineTransform( srcTri, dstTri );
   cout<<warp_mat<<endl;
   // Apply the Affine Transform just found to the img image
   warpAffine( img, warp_dst, warp_mat, warp_dst.size() );
   // Show what you got
   namedWindow( "Source window", CV_WINDOW_AUTOSIZE );
   imshow( "Source window", img );
   namedWindow( "Warp Window", CV_WINDOW_AUTOSIZE );
   imshow( "Warp Window", warp_dst );
   waitKey(0);
   return 0;
  }

Transformacja perspektywiczna

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

#define PI 3.14159265
using namespace std;
using namespace cv;
int main(){
  Point2f srcTri[3];
  Point2f dstTri[3];
  Mat warp_mat( 2, 3, CV_32FC1 );
  Mat img, img_per;
  Mat img = imread("./erspective.jpg", 1 ); 
  srcTri[0] = Point2f( 20, 20);
  srcTri[1] = Point2f( 20, 260 );
  srcTri[2] = Point2f( 345, 105 );
  srcTri[3] = Point2f( 345, 140 );
   
  dstTri[0] = Point2f( 0, 0 );
  dstTri[1] = Point2f( 0, img.rows - 1 );
  dstTri[2] = Point2f( img.cols - 1, 0 );
  dstTri[3] = Point2f( img.cols - 1, img.rows - 1 );

  // Get the Persoective Transform
  warp_mat = getPerspectiveTransform( srcTri, dstTri );
  cout<<warp_mat<<endl;
  // Apply the Persoective Transform just found to the src image
  warpPerspective(img, img_per, warp_mat, img_per.size() );
  // Show what you got
  namedWindow( "Source window", CV_WINDOW_AUTOSIZE );
  imshow( "Source window", img );
  namedWindow( "Warp Window", CV_WINDOW_AUTOSIZE );
  imshow( "Warp Window", img_per );

  waitKey(0);
  return 0;
}