OpenCV中文网站

 找回密码
 立即注册
搜索
热搜: 安装 配置
查看: 3415|回复: 2

图像拼接:透视变换后整个图黑了

[复制链接]
发表于 2019-3-21 12:40:45 | 显示全部楼层 |阅读模式
在做两张图像的拼接,但待拼接图像(是灰度图)经过透视变换后整个黑了,初学opencv,实在不知道是什么原因。希望大神帮帮忙!谢谢!


vector<Point2f>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<Point2f>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);
这是透视变换部分的代码,imageRgbLeft是待拼接图像。


回复

使用道具 举报

发表于 2019-3-22 21:11:12 | 显示全部楼层
vector<Point2f>scene_corners(4);
perspectiveTransform(obj_corners,scene_corners,homo);

这句中scene_corners是空的,你透视变换矩阵肯定是计算错误的。
请首先搞明白透视变化,参考资料《opencv透视变换》https://www.cnblogs.com/jsxyhelu/p/4219564.html
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-24 21:25:08 | 显示全部楼层
jsxyheu2014 发表于 2019-3-22 21:11
vectorscene_corners(4);
perspectiveTransform(obj_corners,scene_corners,homo);

透视矩阵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);
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|OpenCV中文网站

GMT+8, 2024-4-20 11:05 , Processed in 0.010465 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表