vs2015与opencv3.1实现视差图特征点检测
时间: 2023-11-20 11:08:34 浏览: 89
实现视差图特征点检测需要以下步骤:
1. 安装OpenCV 3.1和Visual Studio 2015,并将OpenCV添加到VS项目中。
2. 加载左右图像,将它们转换为灰度图像并将它们存储在Mat对象中。
```
Mat imgL = imread("left.jpg", 0);
Mat imgR = imread("right.jpg", 0);
```
3. 创建一个StereoSGBM对象并设置参数,如最小视差、最大视差和块大小等。
```
Ptr<StereoSGBM> sgbm = StereoSGBM::create(0, 16, 3);
sgbm->setBlockSize(11);
sgbm->setMinDisparity(0);
sgbm->setNumDisparities(16*4);
sgbm->setPreFilterCap(63);
sgbm->setMode(StereoSGBM::MODE_SGBM);
```
4. 计算视差图。
```
Mat disp;
sgbm->compute(imgL, imgR, disp);
```
5. 使用OpenCV中的函数(如cv::goodFeaturesToTrack)检测视差图中的特征点。
```
vector<Point2f> points;
cv::goodFeaturesToTrack(disp, points, 500, 0.01, 10);
```
6. 可选地,可以使用cv::drawKeypoints函数将检测到的特征点绘制在原始图像上。
```
Mat imgL_keypoints;
cv::drawKeypoints(imgL, points, imgL_keypoints, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_OVER_OUTIMG);
```
完整代码示例:
```
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat imgL = imread("left.jpg", 0);
Mat imgR = imread("right.jpg", 0);
Ptr<StereoSGBM> sgbm = StereoSGBM::create(0, 16, 3);
sgbm->setBlockSize(11);
sgbm->setMinDisparity(0);
sgbm->setNumDisparities(16*4);
sgbm->setPreFilterCap(63);
sgbm->setMode(StereoSGBM::MODE_SGBM);
Mat disp;
sgbm->compute(imgL, imgR, disp);
vector<Point2f> points;
cv::goodFeaturesToTrack(disp, points, 500, 0.01, 10);
Mat imgL_keypoints;
cv::drawKeypoints(imgL, points, imgL_keypoints, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_OVER_OUTIMG);
imshow("Left Image", imgL);
imshow("Disparity Map", disp);
imshow("Left Image Keypoints", imgL_keypoints);
waitKey(0);
return 0;
}
```
阅读全文