C++實現雙目立體匹配Census算法的示例代碼

上一篇介紹瞭雙目立體匹配SAD算法,這一篇介紹Census算法。

Census原理:

在視圖中選取任一點,以該點為中心劃出一個例如3 × 3 的矩形,矩形中除中心點之外的每一點都與中心點進行比較,灰度值小於中心點記為1,灰度大於中心點的則記為0,以所得長度為 8 的隻有 0 和 1 的序列作為該中心點的 census 序列,即中心像素的灰度值被census 序列替換。經過census變換後的圖像使用漢明距離計算相似度,所謂圖像匹配就是在匹配圖像中找出與參考像素點相似度最高的點,而漢明距正是匹配圖像像素與參考像素相似度的度量。具體而言,對於欲求取視差的左右視圖,要比較兩個視圖中兩點的相似度,可將此兩點的census值逐位進行異或運算,然後計算結果為1 的個數,記為此兩點之間的漢明值,漢明值是兩點間相似度的一種體現,漢明值愈小,兩點相似度愈大實現算法時先異或再統計1的個數即可,漢明距越小即相似度越高。

下面的代碼是自己根據原理寫的,實現的結果並沒有很好,以後繼續優化代碼。

具體代碼如下:

//*************************Census*********************
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

using namespace std;
using namespace cv;

//-------------------定義漢明距離----------------------------
int disparity;
int GetHammingWeight(uchar value);//求1的個數

//-------------------定義Census處理圖像函數---------------------
int hWind = 1;//定義窗口大小為(2*hWind+1)
Mat ProcessImg(Mat &Img);//將矩形內的像素與中心像素相比較,將結果存於中心像素中
Mat Img_census, Left_census, Right_census;

//--------------------得到Disparity圖像------------------------
Mat getDisparity(Mat &left, Mat &right);

//--------------------處理Disparity圖像-----------------------
Mat ProcessDisparity(Mat &disImg);

int ImgHeight, ImgWidth;

//int num = 0;//異或得到的海明距離
Mat LeftImg, RightImg;
Mat DisparityImg(ImgHeight, ImgWidth, CV_8UC1, Scalar::all(0));
Mat DisparityImg_Processed(ImgHeight, ImgWidth, CV_8UC1, Scalar::all(0));
Mat DisparityImg_Processed_2(ImgHeight, ImgWidth, CV_8UC1);
//定義讀取圖片的路徑
string file_dir="C:\\Program Files\\FLIR Integrated Imaging Solutions\\Triclops Stereo Vision SDK\\stereomatching\\Grab_Stereo\\pictures\\";
//定義存儲圖片的路徑
string save_dir= "C:\\Program Files\\FLIR Integrated Imaging Solutions\\Triclops Stereo Vision SDK\\stereomatching\\Grab_Stereo\\Census\\";

int main()
{
    LeftImg = imread(file_dir + "renwu_left.png", 0);
    RightImg = imread(file_dir + "renwu_right.png", 0);
    namedWindow("renwu_left", 1);
    namedWindow("renwu_right", 1);
    imshow("renwu_left", LeftImg);
    waitKey(5);
    imshow("renwu_right", RightImg);
    waitKey(5);
    ImgHeight = LeftImg.rows;
    ImgWidth = LeftImg.cols;

    Left_census= ProcessImg(LeftImg);//處理左圖,得到左圖的CENSUS圖像 Left_census
    namedWindow("Left_census", 1);
    imshow("Left_census", Left_census);
    waitKey(5);
//  imwrite(save_dir + "renwu_left.jpg", Left_census);

    Right_census= ProcessImg(RightImg);
    namedWindow("Right_census", 1);
    imshow("Right_census", Right_census);
    waitKey(5);
//  imwrite(save_dir  + "renwu_right.jpg", Right_census);

    DisparityImg= getDisparity(Left_census, Right_census);
    namedWindow("Disparity", 1);
    imshow("Disparity", DisparityImg);
//  imwrite(save_dir  + "disparity.jpg", DisparityImg);
    waitKey(5);

    DisparityImg_Processed = ProcessDisparity(DisparityImg);
    namedWindow("DisparityImg_Processed", 1);
    imshow("DisparityImg_Processed", DisparityImg_Processed);
//  imwrite(save_dir + "disparity_processed.jpg", DisparityImg_Processed);
    waitKey(0);
    return 0;
}


//-----------------------對圖像進行census編碼---------------
Mat ProcessImg(Mat &Img)
{
    int64 start, end;
    start = getTickCount();

    Mat Img_census = Mat(Img.rows, Img.cols, CV_8UC1, Scalar::all(0));
    uchar center = 0;

    for (int i = 0; i < ImgHeight - hWind; i++)
    {
        for (int j = 0; j < ImgWidth - hWind; j++)
        {
            center = Img.at<uchar>(i + hWind, j + hWind);
            uchar census = 0;
            uchar neighbor = 0;
            for (int p = i; p <= i + 2 * hWind; p++)//行
            {
                for (int q = j; q <= j + 2 * hWind; q++)//列
                {
                    if (p >= 0 && p <ImgHeight  && q >= 0 && q < ImgWidth)
                    {

                        if (!(p == i + hWind && q == j + hWind))
                        {
                            //--------- 將二進制數存在變量中-----
                            neighbor = Img.at<uchar>(p, q);

                            if (neighbor > center)
                            {
                                census = census * 2;//向左移一位,相當於在二進制後面增添0
                            }
                            else
                            {
                                census = census * 2 + 1;//向左移一位並加一,相當於在二進制後面增添1
                            }
                            //cout << "census = " << static_cast<int>(census) << endl;
                        }
                    }
                }

            }
            Img_census.at<uchar>(i + hWind, j + hWind) = census;
        }
    }
    /*end = getTickCount();
    cout << "time is = " << end - start << " ms" << endl;*/
    return Img_census;
}

//------------得到漢明距離---------------
int GetHammingWeight( uchar value)
{
    int num = 0;
    if (value == 0)
        return 0;
    while (value)
    {
        ++num;
        value = (value - 1)&value;
    }
    return num;
}

//--------------------得到視差圖像--------------
Mat getDisparity(Mat &left, Mat &right)
{
    int DSR =16;//視差搜索范圍
    Mat disparity(ImgHeight,ImgWidth,CV_8UC1);

    cout << "ImgHeight = " << ImgHeight << "   " << "ImgWidth = " << ImgWidth << endl;
    for (int i = 0; i < ImgHeight; i++)
    {
        for (int j = 0; j < ImgWidth; j++)
        {
            uchar L;
            uchar R;
            uchar diff;

            L = left.at<uchar>(i, j);
            Mat Dif(1, DSR, CV_8UC1);
//          Mat Dif(1, DSR, CV_32F);

            for (int k = 0; k < DSR; k++)
            {
                //cout << "k = " << k << endl;
                int y = j - k;
                if (y < 0)
                {
                    Dif.at<uchar>(k) = 0;
                }
                if (y >= 0)
                {
                    R = right.at<uchar>(i,y);
                    //bitwise_xor(L, R, );
                    diff = L^R;
                    diff = GetHammingWeight(diff);
                    Dif.at<uchar>(k) = diff;
//                  Dif.at<float>(k) = diff;
                }
            }
            //---------------尋找最佳匹配點--------------
            Point minLoc;
            minMaxLoc(Dif, NULL, NULL, &minLoc, NULL);
            int loc = minLoc.x;
            //cout << "loc..... = " << loc << endl;
            disparity.at<uchar>(i,j)=loc*16;
        }
    }
    return disparity;
}

//-------------對得到的視差圖進行處理-------------------
Mat ProcessDisparity(Mat &disImg)
{
    Mat ProcessDisImg(ImgHeight,ImgWidth,CV_8UC1);//存儲處理後視差圖
    for (int i = 0; i < ImgHeight; i++)
    {
        for (int j = 0; j < ImgWidth; j++)
        {
            uchar pixel = disImg.at<uchar>(i, j);
            if (pixel < 100)
                pixel = 0;
            ProcessDisImg.at<uchar>(i, j) = pixel;
        }
    }
    return ProcessDisImg;
}

經過處理後的左圖census圖像

經過處理後的右圖census圖像

disparity圖像

處理後的disparity圖像

以上就是C++實現雙目立體匹配Census算法的示例代碼的詳細內容,更多關於C++雙目立體匹配Census算法的資料請關註WalkonNet其它相關文章!

推薦閱讀: