1. 程式人生 > >【OpenCV】透視變換 Perspective Transformation

【OpenCV】透視變換 Perspective Transformation

透視變換的原理和矩陣求解請參見前一篇《透視變換 Perspective Transformation》。在OpenCV中也實現了透視變換的公式求解和變換函式。

求解變換公式的函式:

Mat getPerspectiveTransform(const Point2f src[], const Point2f dst[])
    
輸入原始影象和變換之後的影象的對應4個點,便可以得到變換矩陣。之後用求解得到的矩陣輸入perspectiveTransform便可以對一組點進行變換:

void perspectiveTransform
(InputArray src, OutputArray dst, InputArray m)
注意這裡src和dst的輸入並不是影象,而是影象對應的座標。應用前一篇的例子,做個相反的變換:


    
  1. int main( )
  2. {
  3. Mat img=imread( "boy.png"
    );
  4. int img_height = img.rows;
  5. int img_width = img.cols;
  6. vector<Point2f> corners( 4);
  7. corners[ 0] = Point2f( 0, 0);
  8. corners[ 1] = Point2f(img_width -1, 0);
  9. corners[ 2] = Point2f( 0,img_height -1);
  10. corners[ 3] = Point2f(img_width -1,img_height -1);
  11. vector<Point2f> corners_trans( 4);
  12. corners_trans[ 0] = Point2f( 150, 250);
  13. corners_trans[ 1] = Point2f( 771, 0);
  14. corners_trans[ 2] = Point2f( 0,img_height -1);
  15. corners_trans[ 3] = Point2f( 650,img_height -1);
  16. Mat transform = getPerspectiveTransform(corners,corners_trans);
  17. cout<<transform<< endl;
  18. vector<Point2f> ponits, points_trans;
  19. for( int i= 0;i<img_height;i++){
  20. for( int j= 0;j<img_width;j++){
  21. ponits.push_back(Point2f(j,i));
  22. }
  23. }
  24. perspectiveTransform( ponits, points_trans, transform);
  25. Mat img_trans = Mat::zeros(img_height,img_width,CV_8UC3);
  26. int count = 0;
  27. for( int i= 0;i<img_height;i++){
  28. uchar* p = img.ptr<uchar>(i);
  29. for( int j= 0;j<img_width;j++){
  30. int y = points_trans[count].y;
  31. int x = points_trans[count].x;
  32. uchar* t = img_trans.ptr<uchar>(y);
  33. t[x* 3] = p[j* 3];
  34. t[x* 3+ 1] = p[j* 3+ 1];
  35. t[x* 3+ 2] = p[j* 3+ 2];
  36. count++;
  37. }
  38. }
  39. imwrite( "boy_trans.png",img_trans);
  40. return 0;
  41. }

得到變換之後的圖片:


注意這種將原圖變換到對應影象上的方式會有一些沒有被填充的點,也就是右圖中黑色的小點。解決這種問題一是用差值的方式,再一種比較簡單就是不用原圖的點變換後對應找新圖的座標,而是直接在新圖上找反向變換原圖的點。說起來有點繞口,具體見前一篇《透視變換 Perspective Transformation》的程式碼應該就能懂啦。

除了getPerspectiveTransform()函式,OpenCV還提供了findHomography()的函式,不是用點來找,而是直接用透視平面來找變換公式。這個函式在特徵匹配的經典例子中有用到,也非常直觀:


    
  1. int main( int argc, char** argv )
  2. {
  3. Mat img_object = imread( argv[ 1], IMREAD_GRAYSCALE );
  4. Mat img_scene = imread( argv[ 2], IMREAD_GRAYSCALE );
  5. if( !img_object.data || !img_scene.data )
  6. { std:: cout<< " --(!) Error reading images " << std:: endl; return -1; }
  7. //-- Step 1: Detect the keypoints using SURF Detector
  8. int minHessian = 400;
  9. SurfFeatureDetector detector( minHessian );
  10. std:: vector<KeyPoint> keypoints_object, keypoints_scene;
  11. detector.detect( img_object, keypoints_object );
  12. detector.detect( img_scene, keypoints_scene );
  13. //-- Step 2: Calculate descriptors (feature vectors)
  14. SurfDescriptorExtractor extractor;
  15. Mat descriptors_object, descriptors_scene;
  16. extractor.compute( img_object, keypoints_object, descriptors_object );
  17. extractor.compute( img_scene, keypoints_scene, descriptors_scene );
  18. //-- Step 3: Matching descriptor vectors using FLANN matcher
  19. FlannBasedMatcher matcher;
  20. std:: vector< DMatch > matches;
  21. matcher.match( descriptors_object, descriptors_scene, matches );
  22. double max_dist = 0; double min_dist = 100;
  23. //-- Quick calculation of max and min distances between keypoints
  24. for( int i = 0; i < descriptors_object.rows; i++ )
  25. { double dist = matches[i].distance;
  26. if( dist < min_dist ) min_dist = dist;
  27. if( dist > max_dist ) max_dist = dist;
  28. }
  29. printf( "-- Max dist : %f \n", max_dist );
  30. printf( "-- Min dist : %f \n", min_dist );
  31. //-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
  32. std:: vector< DMatch > good_matches;
  33. for( int i = 0; i < descriptors_object.rows; i++ )
  34. { if( matches[i].distance < 3*min_dist )
  35. { good_matches.push_back( matches[i]); }
  36. }
  37. Mat img_matches;
  38. drawMatches( img_object, keypoints_object, img_scene, keypoints_scene,
  39. good_matches, img_matches, Scalar::all( -1), Scalar::all( -1),
  40. vector< char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
  41. //-- Localize the object from img_1 in img_2
  42. std:: vector<Point2f> obj;
  43. std:: vector<Point2f> scene;
  44. for( size_t i = 0; i < good_matches.size(); i++ )
  45. {
  46. //-- Get the keypoints from the good matches
  47. obj.push_back( keypoints_object[ good_matches[i].queryIdx ].pt );
  48. scene.push_back( keypoints_scene[ good_matches[i].trainIdx ].pt );
  49. }
  50. Mat H = findHomography( obj, scene, RANSAC );
  51. //-- Get the corners from the image_1 ( the object to be "detected" )
  52. std:: vector<Point2f> obj_corners( 4);
  53. obj_corners[ 0] = Point( 0, 0); obj_corners[ 1] = Point( img_object.cols, 0 );
  54. obj_corners[ 2] = Point( img_object.cols, img_object.rows ); obj_corners[ 3] = Point( 0, img_object.rows );
  55. std:: vector<Point2f> scene_corners( 4);
  56. perspectiveTransform( obj_corners, scene_corners, H);
  57. //-- Draw lines between the corners (the mapped object in the scene - image_2 )
  58. Point2f offset( (float)img_object.cols, 0);
  59. line( img_matches, scene_corners[ 0] + offset, scene_corners[ 1] + offset, Scalar( 0, 255, 0), 4 );
  60. line( img_matches, scene_corners[ 1] + offset, scene_corners[ 2] + offset, Scalar( 0, 255, 0), 4 );
  61. line( img_matches, scene_corners[ 2] + offset, scene_corners[ 3] + offset, Scalar( 0, 255, 0), 4 );
  62. line( img_matches, scene_corners[ 3] + offset, scene_corners[ 0] + offset, Scalar( 0, 255, 0), 4 );
  63. //-- Show detected matches
  64. imshow( "Good Matches & Object detection", img_matches );
  65. waitKey( 0);
  66. return 0;
  67. }

程式碼執行效果:



findHomography()函式直接通過兩個平面上相匹配的特徵點求出變換公式,之後程式碼又對原圖的四個邊緣點進行變換,在右圖上畫出對應的矩形。這個圖也很好地解釋了所謂透視變換的“Viewing Plane”。


(轉載請註明作者和出處:http://blog.csdn.net/xiaowei_cqu 未經允許請勿用於商業用途)



透視變換的原理和矩陣求解請參見前一篇《透視變換 Perspective Transformation》。在OpenCV中也實現了透視變換的公式求解和變換函式。

求解變換公式的函式:

Mat getPerspectiveTransform(const Point2f src[], const Point2f dst[])
  
輸入原始影象和變換之後的影象的對應4個點,便可以得到變換矩陣。之後用求解得到的矩陣輸入perspectiveTransform便可以對一組點進行變換:

void perspectiveTransform(InputArray src, OutputArray dst, InputArray m)
  
注意這裡src和dst的輸入並不是影象,而是影象對應的座標。應用前一篇的例子,做個相反的變換:


  
  1. int main( )
  2. {
  3. Mat img=imread( "boy.png");
  4. int img_height = img.rows;
  5. int img_width = img.cols;
  6. vector<Point2f> corners( 4);
  7. corners[ 0] = Point2f( 0, 0);
  8. corners[ 1] = Point2f(img_width -1, 0);
  9. corners[ 2] = Point2f( 0,img_height -1);
  10. corners[ 3] = Point2f(img_width -1,img_height -1);
  11. vector<Point2f> corners_trans( 4);
  12. corners_trans[ 0] = Point2f( 150, 250);
  13. corners_trans[ 1] = Point2f( 771, 0);
  14. corners_trans[ 2] = Point2f( 0,img_height -1);
  15. corners_trans[ 3] = Point2f( 650,img_height -1);
  16. Mat transform = getPerspectiveTransform(corners,corners_trans);
  17. cout<<transform<< endl;
  18. vector<Point2f> ponits, points_trans;
  19. for( int i= 0;i<img_height;i++){
  20. for( int j= 0;j<img_width;j++){
  21. ponits.push_back(Point2f(j,i));
  22. }
  23. }
  24. perspectiveTransform( ponits, points_trans, transform);
  25. Mat img_trans = Mat::zeros(img_height,img_width,CV_8UC3);
  26. int count = 0;
  27. for( int i= 0;i<img_height;i++){
  28. uchar* p = img.ptr<uchar>(i);
  29. for( int j= 0;j<img_width;j++){
  30. int y = points_trans[count].y;
  31. int x = points_trans[count].x;
  32. uchar* t = img_trans.ptr<uchar>(y);
  33. t[x* 3] = p[j* 3];
  34. t[x* 3+ 1] = p[j* 3+ 1];
  35. t[x* 3+ 2] = p[j* 3+ 2];
  36. count++;
  37. }
  38. }
  39. imwrite( "boy_trans.png",img_trans);
  40. return 0;
  41. }

得到變換之後的圖片:


注意這種將原圖變換到對應影象上的方式會有一些沒有被填充的點,也就是右圖中黑色的小點。解決這種問題一是用差值的方式,再一種比較簡單就是不用原圖的點變換後對應找新圖的座標,而是直接在新圖上找反向變換原圖的點。說起來有點繞口,具體見前一篇《透視變換 Perspective Transformation》的程式碼應該就能懂啦。

除了getPerspectiveTransform()函式,OpenCV還提供了findHomography()的函式,不是用點來找,而是直接用透視平面來找變換公式。這個函式在特徵匹配的經典例子中有用到,也非常直觀:


  
  1. int main( int argc, char** argv )
  2. {
  3. Mat img_object = imread( argv[ 1], IMREAD_GRAYSCALE );
  4. Mat img_scene = imread( argv[ 2], IMREAD_GRAYSCALE );
  5. if( !img_object.data || !img_scene.data )
  6. { std:: cout<< " --(!) Error reading images " << std:: endl; return -1; }
  7. //-- Step 1: Detect the keypoints using SURF Detector
  8. int minHessian = 400;
  9. SurfFeatureDetector detector( minHessian );
  10. std:: vector<KeyPoint> keypoints_object, keypoints_scene;
  11. detector.detect( img_object, keypoints_object );
  12. detector.detect( img_scene, keypoints_scene );
  13. //-- Step 2: Calculate descriptors (feature vectors)
  14. SurfDescriptorExtractor extractor;
  15. Mat descriptors_object, descriptors_scene;
  16. extractor.compute( img_object, keypoints_object, descriptors_object );
  17. extractor.compute( img_scene, keypoints_scene, descriptors_scene );
  18. //-- Step 3: Matching descriptor vectors using FLANN matcher
  19. FlannBasedMatcher matcher;
  20. std:: vector< DMatch > matches;
  21. matcher.match( descriptors_object, descriptors_scene, matches );
  22. double max_dist = 0; double min_dist = 100;
  23. //-- Quick calculation of max and min distances between keypoints
  24. for( int i = 0; i < descriptors_object.rows; i++ )
  25. { double dist = matches[i].distance;
  26. if( dist < min_dist ) min_dist = dist;