实现视差图和三维重构

根据两个不同视角的图片来获得视差图:


#include "cv.h"
#include "cvaux.h"
#include "cxcore.h"
#include "highgui.h"

int main1(int argc, char** argv)
{
    IplImage * cv_left_rectified;
    IplImage * cv_right_rectified;

    //note the sequence of the stereo pairs
    cv_left_rectified = cvLoadImage("D://a//left1.jpg", CV_LOAD_IMAGE_GRAYSCALE);
    cv_right_rectified = cvLoadImage("D://a//right1.jpg", CV_LOAD_IMAGE_GRAYSCALE);
    CvSize size = cvGetSize(cv_left_rectified);
    //the disparity map is an array h*w, with 16bit signed elements.
    CvMat* disparity_left = cvCreateMat(size.height, size.width, CV_16S);
    CvMat* disparity_right = cvCreateMat(size.height, size.width, CV_16S);
    CvStereoGCState* state = cvCreateStereoGCState(16, 2);
    cvFindStereoCorrespondenceGC(cv_left_rectified,
        cv_right_rectified,
        disparity_left,
        disparity_right,
        state,
        0);
    cvReleaseStereoGCState(&state);
    //post-progressing the result
    CvMat* disparity_left_visual = cvCreateMat(size.height, size.width, CV_8U);
    cvConvertScale(disparity_left, disparity_left_visual, -16);
    cvNamedWindow("disparity", 1);
    cvShowImage("disparity", disparity_left_visual);
    cvSaveImage("dst.jpg", disparity_left_visual);
    cvWaitKey(0);
    cvDestroyWindow("disparity");
    return 0;
}

视差图的每个像素代表该对象的移动距离。
我使用的两张图片为:


实现视差图和三维重构_第1张图片

经过运算得到的视差图:


实现视差图和三维重构_第2张图片

根据该视差图进行三维建模:

#include "stdafx.h"

#include 
#include 
#include "opencv2/calib3d/calib3d.hpp"  
#include "opencv2/imgproc/imgproc.hpp"  
#include "opencv2/highgui/highgui.hpp"  
#include "opencv2/contrib/contrib.hpp" 

#include
#include 
#include   
#include 
using namespace cv;

using namespace std;

#define MAX_SIZE 1024

float imgdata[MAX_SIZE][MAX_SIZE];

int w = 0;
int h = 0;
float scalar = 50;//scalar of converting pixel color to float coordinates

void renderScene(void)
{

    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();               // Reset the coordinate system before modifying
    gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    glRotatef(-30, 0.0, 1.0, 0.0); //rotate about the x axis
    glRotatef(-180, 0.0, 0.0, 1.0); //rotate about the z axis
    glRotatef(-180, 0.0, 1.0, 0.0); //rotate about the y axis

    float imageCenterX = w*.5;
    float imageCenterY = h*.5;
    float x, y, z;
    glPointSize(1.0);
    glBegin(GL_POINTS);//GL_POINTS
    for (int i = 0; iwidth;
    h = imgGrey->height;

    displayDisparity(imgGrey);
    cvNamedWindow("original", CV_WINDOW_AUTOSIZE);
    cvShowImage("original", imgGrey);

    //------------------OpenGL-------------------------
    glutInit(&argc, (char**)argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(500, 500);
    glutCreateWindow("3D disparity image");
    glutDisplayFunc(renderScene);
    glutReshapeFunc(reshape);
    glutMainLoop();
    cvWaitKey(0);
    //release opencv stuff.
    cvReleaseImage(&imgGrey);
    cvDestroyWindow("Original");

    return 0;
}

重构结果:


实现视差图和三维重构_第3张图片

你可能感兴趣的:(实现视差图和三维重构)