opencv 实现SAD匹配,和计时

#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <iostream>
#include <string>
#include "windows.h"

using namespace std;
using namespace cv;

const int w = 384;
const int h = 288;
const int maxDisparity = 40;
const int RAD = 5;
void cpu_stereo(uchar* Limg,
                          uchar* Rimg,
                          uchar* Oimg)
{
    for( int y = 0 ; y< h; y++)
    {
        for(int x = 0 ; x< w; x++){
            unsigned int bestCost = 999999;
            unsigned int bestDisparity = 0;
            for( int  d = 0; d<= maxDisparity; d++)
            {
                unsigned int cost = 0;
                for( int i = -RAD; i<= RAD; i ++)
                {
                    for( int j = -RAD; j<=RAD; j++)
                    {
                        int yy,xx,xxd;
                        yy = y + i;
                        if( yy < 0 ) yy = 0;
                        if (yy>=h) yy = h-1;
                        xx = x + j;
                        if( xx < 0) xx = 0;
                        if ( xx>=w) xx = w -1;
                        xxd = xx - d ;
                        if ( xxd < 0 ) xxd = 0;
                        if ( xxd >=w ) xxd  = w-1;
                        cost += abs((int)(Limg[yy*w + xx] - Rimg[yy*w + xxd]));
                    }
                }
                if(cost < bestCost)
                {
                    bestCost = cost;
                    bestDisparity = 255  - d*6;
                }
                Oimg[y*w + x] = bestDisparity;
            }
        }
    }
}

int main()
{
    Mat imL, imR,imO;
    imL = imread("data\\imL.bmp",0);
    if ( imL.empty() )
    {
        cout<<"Could not find the imL."<< std::endl;
        return -1;
    }
    imR = imread("data\\imR.bmp",0);
    if ( imR.empty() )
    {
        cout<<"Could not find the imR."<< std::endl;
        return -1;
    }
    imO.create(imL.rows,imL.cols,imL.type());
    // 创建、初始化CPU计时
    LARGE_INTEGER freq;  
    LARGE_INTEGER start_t, stop_t;  
    double exe_time;  
    QueryPerformanceFrequency(&freq);
    QueryPerformanceCounter(&start_t);

    cpu_stereo(imL.data,imR.data,imO.data);

    QueryPerformanceCounter(&stop_t);  
    exe_time = 1e3*(stop_t.QuadPart-start_t.QuadPart)/freq.QuadPart;  
    cout<<"CPU costs"<<exe_time<<"ms"<<endl;

    namedWindow("left",WINDOW_AUTOSIZE);
    namedWindow("right",WINDOW_AUTOSIZE);
    namedWindow("Output",WINDOW_AUTOSIZE);
    imshow("Output", imO);
    imshow("left", imL);
    imshow("right",imR);
    waitKey(0);
     return 0;
}

输出图像的初始化由函数create(imL.rows,imL.cols,imL.type())实现。

cpu_stereo函数的接口是uchar*类型,调用时是imL.data。

计时函数需要包含头文件"windows.h",实现计时的具体步骤为

// 创建、初始化CPU计时
    LARGE_INTEGER freq;  
    LARGE_INTEGER start_t, stop_t;  
    double exe_time;  
    QueryPerformanceFrequency(&freq);
    QueryPerformanceCounter(&start_t);

    do some job
QueryPerformanceCounter(
&stop_t); exe_time = 1e3*(stop_t.QuadPart-start_t.QuadPart)/freq.QuadPart; cout<<"CPU costs"<<exe_time<<"ms"<<endl;

 

posted @ 2016-04-08 14:49  骏腾  阅读(849)  评论(0编辑  收藏  举报