0

该功能缝合两个图像...效果很好...

功能:

for( int i = 1; i<(4); i++ )
{
test=Bildpaar(test,image1[2+z]);
z=z+1;
}
imwrite("../superrechts.jpg",test);

但第二次迭代失败,因为全景图像中有黑色部分,我只能看到第三张图像......

我的功能

Mat Bildpaar(Mat Bild1,Mat Bild2)

{


Mat img_keypoints_1;
std::vector<KeyPoint> keypoints_1,keypoints_2;//anzahl der keypoint vektoren


int minHessian = 50;


  FastFeatureDetector  detector( minHessian); 

  detector.detect( Bild1, keypoints_1 );
  detector.detect( Bild2, keypoints_2 );

  //-- Draw keypoints

  drawKeypoints( Bild1, keypoints_1, img_keypoints_1, Scalar::all(-1), DrawMatchesFlags::DEFAULT );



 //%%%%%%%%%%%%%%%%%%%%%%%%&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
 //Discriptor
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 SiftDescriptorExtractor extractor; //Zeit
 Mat descriptors_1,descriptors_2;

extractor.compute( Bild1, keypoints_1, descriptors_1 );
extractor.compute( Bild2, keypoints_2, descriptors_2 );

//  waitKey(0);
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Matcher
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//-- Step 3: Matching descriptor vectors using FLANN matcher

std::vector< DMatch > matches;

 FlannBasedMatcher matcher;
 matcher.match( descriptors_1, descriptors_2, matches );

double max_dist = 0; double min_dist = 100;



Mat img_matches;
drawMatches(Bild1, keypoints_1, Bild2, keypoints_2, matches, img_matches);
//namedWindow( filename, CV_WINDOW_NORMAL );// Create a window for display.  
//imshow(filename, img_matches[i]);



std::vector< DMatch > good_matches;



 for( int i = 0; i < descriptors_1.rows; i++ )
  { double dist = matches[i].distance;
    if( dist < min_dist ) min_dist = dist;
    if( dist > max_dist ) max_dist = dist;
  }



  for( int i = 0; i < descriptors_1.rows; i++ )
  { if( matches[i].distance < 2*min_dist )
    { good_matches.push_back( matches[i]); 
  }

  }

  Mat img_matches1;

  drawMatches( Bild1, keypoints_1, Bild2, keypoints_2,
               good_matches, img_matches1, Scalar::all(-1), Scalar::all(-1),
               vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );

  //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  //bis hierhin geht es
  //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 std::vector< Point2f > obj1;
 std::vector< Point2f > scene1;


for( int i = 0; i < good_matches.size(); i++ )
 {
 //-- Get the keypoints from the good matches
 obj1.push_back( keypoints_1[ good_matches[i].queryIdx ].pt );
 scene1.push_back( keypoints_2[ good_matches[i].trainIdx ].pt );
 }

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  // Find the Homography Matrix
  //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
Mat H;


 H= findHomography( scene1, obj1, CV_RANSAC,5 );

 std::vector<Point2f> obj_corners(4);
 obj_corners[0] = cvPoint(0,0); obj_corners[1] = cvPoint( Bild1.cols, 0 );
 obj_corners[2] = cvPoint( Bild1.cols, Bild1.rows ); obj_corners[3] = cvPoint( 0, Bild1.rows );
  std::vector<Point2f> scene_corners(4);

  perspectiveTransform( obj_corners, scene_corners, H);
  //-- Show detected matches
  //-- Draw lines between the corners (the mapped object in the scene - image_2 )
  line( img_matches1, scene_corners[0] + Point2f( Bild1.cols, 0), scene_corners[1] + Point2f( Bild1.cols, 0), Scalar(0, 255, 0), 4 );
  line( img_matches1, scene_corners[1] + Point2f( Bild1.cols, 0), scene_corners[2] + Point2f( Bild1.cols, 0), Scalar( 0, 255, 0), 4 );
  line( img_matches1, scene_corners[2] + Point2f( Bild1.cols, 0), scene_corners[3] + Point2f( Bild1.cols, 0), Scalar( 0, 255, 0), 4 );
  line( img_matches1, scene_corners[3] + Point2f( Bild1.cols, 0), scene_corners[0] + Point2f( Bild1.cols, 0), Scalar( 0, 255, 0), 4 );
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Warp images
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Mat result;

warpPerspective(Bild2, result, H, Size(Bild2.cols*2, Bild2.rows), INTER_CUBIC);
Mat final(Size(Bild2.cols + Bild1.cols, Bild2.rows),CV_8UC3);//richtig //testen ob Spaltenanzahl auswirkungen auf das ergebnis hat



//velikost img1
Mat roi1(final, Rect(0, 0,  Bild1.cols, Bild1.rows));

Mat roi2(final, Rect(0, 0,  result.cols, result.rows));
result.copyTo(roi2);
Bild1.copyTo(roi1);



return final;


}

在我看来,我在投资回报率方面犯了一些错误......

我该怎么办?也许我将一个 roi 复制到另一个我尝试更改顺序

result.copyTo(roi2);
Bild1.copyTo(roi1);

在一种情况下,我看到了左图,而在另一种情况下,我看到了右图……我怎样才能同时看到两者?

此致

我找到了一种方法(但这不是一个好方法)。

首先,您缝合了 2 个图像(称为 final1)其次,当您缝合 final1 和图像 3 时,您必须混合 rois,然后它才能工作......而且您必须使用 alpha 进行环绕......

Mat blendo;
double alpha=0.3;
double beta = ( 1.0 - alpha );
addWeighted( roi1, alpha, roi2, beta, 0.0, blendo);
4

0 回答 0