OPencv用C++做投影代码

#include <iostream>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv.hpp>
using namespace std;
using namespace cv;
Mat I;
Mat pI;
Point2f IPoint,pIPoint;
int i=0,j=0;
Point2f src[4];
Point2f dst[4];
void mouse_I(int event,int x,int y,int flags,void *param)
{
    switch(event)
    {
    case CV_EVENT_LBUTTONDOWN:
        IPoint=Point2f(x,y);
        break;
    case CV_EVENT_LBUTTONUP:
        src[i]=IPoint;
        circle(I,src[i],7,Scalar(0),3);
        i+=1;
        break;
    default:
        break;
    }
}
void mouse_pI(int event,int x,int y,int flags,void *param)
{
    switch(event)
    {
    case CV_EVENT_LBUTTONDOWN:
        IPoint=Point2f(x,y);
        break;
    case CV_EVENT_LBUTTONUP:
        dst[j]=pIPoint;
        circle(pI,dst[i],7,Scalar(0),3);
        j+=1;
        break;
    default:
        break;
    }
}
int main()
{
    I=imread("/home/xiaomingming/profile/xmm.jpg",IMREAD_COLOR);
    pI=255*Mat::ones(I.size(),CV_8UC1);
    namedWindow("I",1);
    setMouseCallback("I",mouse_I,NULL);
    namedWindow("pI",1);
    setMouseCallback("pI",mouse_pI,NULL);
    imshow("I",I);
    imshow("pI",pI);
    while(!(i==4 && j==4))
    {
        imshow("I",I);
        imshow("pI",pI);
        if(waitKey(50)=='q')
            break;
    }
    imshow("I",I);
    imshow("pI",pI);
    setMouseCallback("I",NULL,NULL);
    setMouseCallback("pI",NULL,NULL);
    Mat p=getPerspectiveTransform(src,dst);
    Mat result;
    warpPerspective(I,result,p,pI.size());
    imshow("投影后的效果",result);
    waitKey(0);
    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的C++代码实现OpenCV三维重建和投影的示例: ``` #include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main() { // 读取图像 Mat img1 = imread("image1.jpg", IMREAD_GRAYSCALE); Mat img2 = imread("image2.jpg", IMREAD_GRAYSCALE); // 特征点检测与匹配 Ptr<FeatureDetector> detector = SIFT::create(); vector<KeyPoint> keypoints1, keypoints2; detector->detect(img1, keypoints1); detector->detect(img2, keypoints2); Ptr<DescriptorExtractor> extractor = SIFT::create(); Mat descriptors1, descriptors2; extractor->compute(img1, keypoints1, descriptors1); extractor->compute(img2, keypoints2, descriptors2); Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased"); vector<DMatch> matches; matcher->match(descriptors1, descriptors2, matches); // 三维重建 vector<Point3f> points3d; vector<Point2f> points1, points2; for (int i = 0; i < matches.size(); i++) { int queryIdx = matches[i].queryIdx; int trainIdx = matches[i].trainIdx; points1.push_back(keypoints1[queryIdx].pt); points2.push_back(keypoints2[trainIdx].pt); } Mat R, t; Matx33f K(1000, 0, img1.cols / 2, 0, 1000, img1.rows / 2, 0, 0, 1); Mat E = findEssentialMat(points1, points2, K, RANSAC, 0.999, 1.0); recoverPose(E, points1, points2, K, R, t); Matx34f P1(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0); Matx34f P2(R.at<double>(0, 0), R.at<double>(0, 1), R.at<double>(0, 2), t.at<double>(0), R.at<double>(1, 0), R.at<double>(1, 1), R.at<double>(1, 2), t.at<double>(1), R.at<double>(2, 0), R.at<double>(2, 1), R.at<double>(2, 2), t.at<double>(2)); triangulatePoints(P1, P2, points1, points2, points3d); // 投影 vector<Point2f> projected_points; Matx33f K1(1000, 0, img1.cols / 2, 0, 1000, img1.rows / 2, 0, 0, 1); Matx33f K2(1000, 0, img2.cols / 2, 0, 1000, img2.rows / 2, 0, 0, 1); projectPoints(points3d, Matx31f(0, 0, 0), Matx31f(0, 0, 0), K1, Mat(), projected_points); // 显示结果 Mat result; hconcat(img1, img2, result); for (int i = 0; i < projected_points.size(); i++) { line(result, points1[i], projected_points[i] + Point2f(img1.cols, 0), Scalar(0, 255, 0)); } imshow("Result", result); waitKey(0); return 0; } ``` 以上代码实现了从两张图像中提取特征点,计算相机的位姿,并进行三维重建和投影。其中,使用了SIFT算法进行特征点提取和匹配,使用了findEssentialMat()和recoverPose()函数计算相机的位姿,使用了triangulatePoints()函数进行三维重建,使用了projectPoints()函数进行投影。最终,显示出了图像中特征点和重建出的三维点云的对应关系。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员阿明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值