透视矩阵homo是我上面没放出来的代码Mat homo=findHomography(imagePointsLeft,imagePointsRight,CV_RANSAC,5,inliersMask)求出的,不好意思没说清楚。这个主要是求出scene_corners。我用了这个代码跑了别的图片(三通道图,像素635x509),跑出来的效果很好,但是我换了另外两张图片(灰度图,像素1024x2112),经过透视变换后就变成黑影。想问问是不是和图片有关呢?
int minHessian=800;
SurfFeatureDetector detector( minHessian );
std::vector<KeyPoint>keyLeftPoint,keyRightPoint;
detector.detect(imageGrayLeft,keyLeftPoint);
detector.detect(imageGrayRight,keyRightPoint);
Mat imgkeyPoint1,imgkeyPoint2;
drawKeypoints(imageRgbLeft,keyLeftPoint,imgkeyPoint1,Scalar::all(-1),DrawMatchesFlags:EFAULT);
drawKeypoints(imageRgbRight,keyRightPoint,imgkeyPoint2,Scalar::all(-1),DrawMatchesFlags:EFAULT);
imshow("1",imgkeyPoint1);
imshow("2",imgkeyPoint2);
SurfDescriptorExtractor extractor;
Mat imageDescLeft,imageDescRight;
extractor.compute(imageGrayLeft,keyLeftPoint,imageDescLeft);
extractor.compute(imageGrayRight,keyRightPoint,imageDescRight);
C:/Users/apple/Pictures/imageTransformLeft.jpg
FlannBasedMatcher matcher;
std::vector<DMatch>matchePoints;
matcher.match(imageDescLeft,imageDescRight,matchePoints);
Mat firstmatches;
drawMatches(imageRgbLeft,keyLeftPoint,imageRgbRight,keyRightPoint,matchePoints,firstmatches,Scalar::all(-1),Scalar::all(-1),vector<char>(),DrawMatchesFlags:EFAULT);
imshow("firstmatches",firstmatches);
imwrite("C:/Users/apple/Pictures/firstmatches.jpg",firstmatches);
sort(matchePoints.begin(),matchePoints.end());
vector<oint2f>imagePointsLeft,imagePointsRight;
int j=0;
for(int i=0;i<matchePoints.size();i++)
{
j++;
imagePointsLeft.push_back(keyLeftPoint[matchePoints.queryIdx].pt);
imagePointsRight.push_back(keyRightPoint[matchePoints.trainIdx].pt);
if(j>20)
break;
}
vector<uchar>inliersMask(imagePointsLeft.size());
Mat homo=findHomography(imagePointsLeft,imagePointsRight,CV_RANSAC,5,inliersMask);
vector<DMatch>matches_ransac;
for(int i=0;i<inliersMask.size();i++)
{
cout<<(int)(inliersMask)<<endl;
if(inliersMask)
{
matches_ransac.push_back(matchePoints);
}
}
Mat secondmatches;
drawMatches(imageRgbLeft,keyLeftPoint,imageRgbRight,keyRightPoint,matches_ransac,secondmatches,Scalar::all(-1),Scalar::all(-1),vector<char>(),DrawMatchesFlags:EFAULT);
imshow("secondmatches",secondmatches);
imwrite("C:/Users/apple/Pictures/secondmatches.jpg",secondmatches);
vector<oint2f>obj_corners(4);
obj_corners[0]=Point(0,0);
obj_corners[1]=Point(imageRgbLeft.cols,0);
obj_corners[2]=Point(imageRgbLeft.cols,imageRgbLeft.rows);
obj_corners[3]=Point(0,imageRgbLeft.rows);
vector<oint2f>scene_corners(4);
perspectiveTransform(obj_corners,scene_corners,homo);
Mat adjustMat=(Mat_<double>(3,3)<<1.0,0,abs(scene_corners[0].x),0,1.0,0,0,0,1.0);
Mat adjustHomo=adjustMat*homo;
Mat imageTransformLeft;
warpPerspective(imageRgbLeft,imageTransformLeft,adjustHomo,Size(imageRgbRight.cols+abs(scene_corners[0].x),imageRgbRight.rows));
imshow("imageTransformLeft",imageTransformLeft);
imwrite("C:/Users/apple/Pictures/imageTransformLeft.jpg",imageTransformLeft);
|