#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include"TCHAR.H"
#include"5555.h"
#include <opencv2/legacy/legacy.hpp>
using namespace std;
using namespace cv;
Mat disp1,_Q;
CvStereoBMState *m_BMstate = cvCreateStereoBMState();
void stereo_match(int)
{
//m_BMstate->SADWindowSize =2*blockSize+5;
m_BMstate->SADWindowSize =21;
m_BMstate->minDisparity = 0;
//m_BMstate->uniquenessRatio = uniquenessRatio;
m_BMstate->uniquenessRatio = 17;
m_BMstate->preFilterCap = 31;
//m_BMstate->numberOfDisparities = numDisparities * 16 + 16;
m_BMstate->numberOfDisparities = 272;
m_BMstate->textureThreshold = 10;
m_BMstate->speckleWindowSize = 100;
m_BMstate->speckleRange = 32;
m_BMstate->disp12MaxDiff = -1;
}
static void onMouse(int event, int x, int y, int , void* )
{
if (event == CV_EVENT_LBUTTONDOWN)
{
pic_info[0] = x;
pic_info[1] = y;
cout << "x:" << pic_info[0] << "y:" << pic_info[1] << endl;
left_mouse = true;
CvScalar scal = cvGet2D(xyz1,y, x);
cout << "点(" << x << "," << y << ")的三维坐标为:" << endl << scal.val[0]*1.6 << " " << scal.val[1]*1.6 << " " << scal.val[2]*1.6<< endl;
cout << "in world coordinate: " << xyz.at<Vec3f>(pic_info[1], pic_info[0])<< endl;//获取三维坐标方式2
cout << "x: " << xyz.at<Vec3f>(pic_info[1], pic_info[0])[0] << endl;
cout << "y: " << xyz.at<Vec3f>(pic_info[1], pic_info[0])[1] << endl;
cout << "z: " << xyz.at<Vec3f>(pic_info[1], pic_info[0])[2] << endl;
}
else if (event == CV_EVENT_LBUTTONUP)
{
left_mouse = false;
}
else if ((event == CV_EVENT_MOUSEMOVE) && (left_mouse == true))
{
}
}
//int getPointClouds(cv::Mat& disparity, cv::Mat& pointClouds)
//{
// if (disparity.empty())
// {
// return 0;
// }
//
// //计算生成三维点云
// reprojectImageTo3D(disparity, pointClouds, Q, true);
//
// pointClouds *= 1.6;
// for (int y = 0; y < pointClouds.rows; ++y)
// {
// for (int x = 0; x < pointClouds.cols; ++x)
// {
// cv::Point3f point = pointClouds.at<cv::Point3f>(y, x);
// point.y = -point.y;
// pointClouds.at<cv::Point3f>(y, x) = point;
// }
// }
// pointClouds.copyTo(xyz); //获取三维坐标方式2
//
// split(pointClouds, xyzSet);//获取三维坐标方式1
// xyzSet[0].copyTo(depth0);
// xyzSet[1].copyTo(depth1);
// xyzSet[2].copyTo(depth2);
// return 1;
//}
int main(int argc, char* argv[])
{
//cout << "测试两个摄像头同时读取数据" << endl;
//读取内部参数
CvMat *Intrinsics_Camera_Left = (CvMat *)cvLoad("Intrinsics_Camera_Left.xml");
CvMat *Intrinsics_Camera_Right = (CvMat *)cvLoad("Intrinsics_Camera_Right.xml");
CvMat *Distortion_Camera_Left = (CvMat *)cvLoad("Distortion_Camera_Left.xml");
CvMat *Distortion_Camera_Right = (CvMat *)cvLoad("Distortion_Camera_Right.xml");
CvMat *Translation_matlab = (CvMat *)cvLoad("Translation.xml");
CvMat *RotRodrigues_matlab = (CvMat *)cvLoad("RotRodrigues.xml");
CvMat *R_opencv = cvCreateMat(3, 3, CV_64F);
cvRodrigues2(RotRodrigues_matlab, R_opencv);
CvCapture* cap_left;
CvCapture* cap_right;
IplImage *img0,*img1;
cap_left = cvCreateCameraCapture(0);
assert(cap_left != NULL);
cap_right = cvCreateCameraCapture(1);
assert(cap_right != NULL);
img0 = cvRetrieveFrame(cap_left);
//img1 = cvRetrieveFrame(cap_right);
IplImage *img_left_Change=cvCreateImage(cvGetSize(img0),img0->depth,1);
IplImage *img_right_Change=cvCreateImage(cvGetSize(img0),img0->depth,1);
xyz1=cvCreateMat(img0->height,img0->width, CV_32FC3);
CvSize imSize=cvSize(img0->width,img0->height );
//CvSize imSize=cvSize(640 ,480);
CvMat* mxl = cvCreateMat(imSize.height, imSize.width, CV_32F);
CvMat* myl = cvCreateMat(imSize.height, imSize.width, CV_32F);
CvMat* mxr = cvCreateMat(imSize.height, imSize.width, CV_32F);
CvMat* myr = cvCreateMat(imSize.height, imSize.width, CV_32F);
CvMat* img1r = cvCreateMat(imSize.height, imSize.width, CV_8U);
CvMat* img2r = cvCreateMat(imSize.height, imSize.width, CV_8U);
CvMat* disp = cvCreateMat(imSize.height, imSize.width, CV_16S);//视察映射
CvMat* vdisp = cvCreateMat(imSize.height, imSize.width, CV_8U);//归一化后的视差映射
CvMat *Rl = cvCreateMat(3, 3, CV_64F);
CvMat *Rr = cvCreateMat(3, 3, CV_64F);
CvMat *Pl = cvCreateMat(3, 4, CV_64F);
CvMat *Pr = cvCreateMat(3, 4, CV_64F);
CvMat* pair;//定义一个矩阵用于存放校正后的两张图片
CvMat part;//定义一个矩阵用于存放校正后的一张图片
pair = cvCreateMat(imSize.height, imSize.width * 2, CV_8UC3);
cvStereoRectify(Intrinsics_Camera_Left, Intrinsics_Camera_Right,
Distortion_Camera_Left, Distortion_Camera_Right,
imSize, R_opencv, Translation_matlab,
Rl, Rr, Pl, Pr,Q);
cvStereoRectify(Intrinsics_Camera_Left, Intrinsics_Camera_Right,
Distortion_Camera_Left, Distortion_Camera_Right,
imSize, R_opencv, Translation_matlab,
Rl, Rr, Pl, Pr, Q, 1024, 0);//增加图像缩放,去除死区
cvInitUndistortRectifyMap(Intrinsics_Camera_Left, Distortion_Camera_Left, Rl, Pl, mxl, myl);
cvInitUndistortRectifyMap(Intrinsics_Camera_Right, Distortion_Camera_Right, Rr, Pr,mxr, myr);
cvNamedWindow("disparity", CV_WINDOW_AUTOSIZE);
// 创建SAD窗口 Trackbar
cvCreateTrackbar("BlockSize:\n", "disparity",&blockSize, 8, stereo_match);
// 创建视差唯一性百分比窗口 Trackbar
cvCreateTrackbar("UniquenessRatio:\n", "disparity", &uniquenessRatio, 50, stereo_match);
// 创建视差窗口 Trackbar
cvCreateTrackbar("NumDisparities:\n", "disparity", &numDisparities, 50, stereo_match);
//鼠标响应函数setMouseCallback(窗口名称, 鼠标回调函数, 传给回调函数的参数,一般取0)
cvSetMouseCallback("disparity", onMouse, 0);
while(1)
{
int64 t = getTickCount();
cvGrabFrame(cap_left);
cvGrabFrame(cap_right);
img0 = cvRetrieveFrame(cap_left);
img1 = cvRetrieveFrame(cap_right);
if( !img0 || !img1)
{
cout << "camera0 error" << endl;
break;
}
cvCvtColor(img0,img_left_Change,CV_BGR2GRAY);
cvCvtColor(img1,img_right_Change,CV_BGR2GRAY);
cvRemap(img_left_Change, img1r, mxl, myl);
cvRemap(img_right_Change, img2r, mxr, myr);
//stereo_match(0);
cvFindStereoCorrespondenceBM(img1r, img2r, disp, m_BMstate);//计算视差映射
cvNormalize(disp, vdisp, 0, 256, CV_MINMAX);//归一化视差映射
cvReprojectImageTo3D(disp,xyz1,Q,1);//c实现输出坐标
disp1=Mat(disp,true);//C++实现输出坐标
_Q=Mat(Q,true);
reprojectImageTo3D(disp1,xyz,_Q,true);
xyz*=1.6;
//pair的左侧复制左相机立体校正后的照片
cvGetCols(pair, &part, 0, imSize.width);/*CvMat* cvGetCols( const CvArr* arr, CvMat* submat, int start_col, int end_col );
返回数组的一定跨度内的列
arr-输入数组
submat-指向结果子数组头指针.
start_col-跨度的开始列(包括该列)索引下标,该下标以0为基准。
end_col-跨度的结束列(不包括该列)索引下标,该下标以0为基准。*/
cvCvtColor(img1r, &part, CV_GRAY2BGR);//GRAY->RGB
//pair右侧复制右相机立体校正后的照片
cvGetCols(pair, &part, imSize.width, imSize.width * 2);
cvCvtColor(img2r, &part, CV_GRAY2BGR);//GRAY->RGB
//在一定距离间隔处绘制水平直线
for (int j = 0; j < imSize.height; j += 16)
cvLine(pair, cvPoint(0, j), cvPoint(imSize.width * 2, j), CV_RGB(0, 255, 0));
/
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
用C写的,但是其坐标输出有C和C++两个,鼠标点击输出三维坐标,其相机标定用的是MATLAB结果导入,用的VS2010,实际运行成功的,不出我需要修改的是相机标定的xml文件,标定过程参考前面的博文,还有就是修改左右相机的参数cap_left = cvCreateCameraCapture(0); 以及cap_right = cvCreateCameraCapture(1); 我的是0和1,有可能是-1,0或者1和2
资源推荐
资源详情
资源评论
收起资源包目录
555(导入MATLB结果显示视差并显示坐标).rar (99个子文件)
555(导入MATLB结果显示视差并显示坐标)
555.sln 876B
555.sdf 16.46MB
Debug
555.pdb 1.83MB
555.exe 72KB
555.ilk 587KB
ipch
555-aa2af674
555-872c9ee5.ipch 60.19MB
555
Debug
link.8992.read.1.tlog 2B
cl.command.1.tlog 3KB
link.25752.read.1.tlog 2B
link.14260-cvtres.write.1.tlog 2B
rc.command.1.tlog 2KB
CL.read.1.tlog 91KB
555.exe.embed.manifest 406B
vc100.idb 1.35MB
mt.read.1.tlog 2KB
link.11476.write.1.tlog 2B
link.9044.write.1.tlog 2B
link.8472.write.1.tlog 2B
link.9200-cvtres.read.1.tlog 2B
555.vcxprojResolveAssemblyReference.cache 713B
link.9044-cvtres.write.1.tlog 2B
link.14260.read.1.tlog 2B
link-cvtres.read.1.tlog 2B
link.read.1.tlog 26KB
link.12976-cvtres.read.1.tlog 2B
link.13688.write.1.tlog 2B
link.9044.read.1.tlog 2B
link.25752-cvtres.read.1.tlog 2B
link.12976.write.1.tlog 2B
rc.read.1.tlog 1KB
link.write.1.tlog 4KB
link.9044-cvtres.read.1.tlog 2B
link.9200.write.1.tlog 2B
CL.write.1.tlog 2KB
link.25752-cvtres.write.1.tlog 2B
link.9200.read.1.tlog 2B
555.exe.embed.manifest.res 472B
vc100.pdb 1.27MB
link.11476-cvtres.write.1.tlog 2B
link.13688.read.1.tlog 2B
link.8472-cvtres.read.1.tlog 2B
link.14260-cvtres.read.1.tlog 2B
link.11476-cvtres.read.1.tlog 2B
link.command.1.tlog 12KB
link.8992.write.1.tlog 2B
mt.write.1.tlog 374B
link.25752.write.1.tlog 2B
555.write.1.tlog 0B
link.8992-cvtres.read.1.tlog 2B
link.8992-cvtres.write.1.tlog 2B
link.9200-cvtres.write.1.tlog 2B
link.8472.read.1.tlog 2B
link.13688-cvtres.write.1.tlog 2B
555_manifest.rc 196B
link.12976.read.1.tlog 2B
link.13688-cvtres.read.1.tlog 2B
link.11476.read.1.tlog 2B
link.12976-cvtres.write.1.tlog 2B
555.log 990B
mt.command.1.tlog 2KB
5555.obj 214KB
link.8472-cvtres.write.1.tlog 2B
555.lastbuildstate 135B
rc.write.1.tlog 1KB
link-cvtres.write.1.tlog 2B
555.exe.intermediate.manifest 381B
link.14260.write.1.tlog 2B
555.vcxproj.filters 1KB
Intrinsics_Camera_Left.xml 360B
5555.h 421B
left.jpg 69KB
Distortion_Camera_Left.xml 276B
right.jpg 32KB
555.vcxproj 4KB
555.vcxproj.user 143B
Translation.xml 260B
Release
cl.command.1.tlog 730B
CL.read.1.tlog 16KB
mt.read.1.tlog 704B
link.read.1.tlog 5KB
link.write.1.tlog 618B
CL.write.1.tlog 434B
mt.read.2.tlog 1KB
vc100.pdb 1020KB
link.command.1.tlog 2KB
mt.write.1.tlog 332B
555.write.1.tlog 0B
555.log 3KB
mt.command.1.tlog 504B
5555.obj 1.78MB
555.lastbuildstate 137B
555.exe.intermediate.manifest 381B
RotRodrigues.xml 258B
Distortion_Camera_Right.xml 275B
Intrinsics_Camera_Right.xml 363B
5555.cpp 9KB
Release
555.pdb 1.17MB
555.exe 14KB
555.suo 20KB
共 99 条
- 1
资源评论
- marykeil2019-04-10移植到linux可用,但是使用opencv3以上的需要调整函数中一些参数。
dreamharding
- 粉丝: 26
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- c语言-leetcode题解之0084-largest-rectangle-in-histogram.zip
- c语言-leetcode题解之0083-remove-duplicates-from-sorted-list.zip
- c语言-leetcode题解之0082-remove-duplicates-from-sorted-list-ii.zip
- c语言-leetcode题解之0081-search-in-rotated-sorted-array-ii.zip
- tnsnames tnsnames tnsnames
- c语言-leetcode题解之0080-remove-duplicates-from-sorted-array-ii.zip
- c语言-leetcode题解之0079-word-search.zip
- c语言-leetcode题解之0078-subsets.zip
- c语言-leetcode题解之0077-combinations.zip
- c语言-leetcode题解之0076-minimum-window-substring.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功