首頁 > 軟體

C++實現雙目立體匹配Census演演算法的範例程式碼

2022-08-05 22:00:04

上一篇介紹了雙目立體匹配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演演算法的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com