您好,登錄后才能下訂單哦!
這篇“C++如何實現(xiàn)雙目立體匹配Census算法”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“C++如何實現(xiàn)雙目立體匹配Census算法”文章吧。
Census原理:
在視圖中選取任一點,以該點為中心劃出一個例如3 × 3 的矩形,矩形中除中心點之外的每一點都與中心點進行比較,灰度值小于中心點記為1,灰度大于中心點的則記為0,以所得長度為 8 的只有 0 和 1 的序列作為該中心點的 census 序列,即中心像素的灰度值被census 序列替換。經(jīng)過census變換后的圖像使用漢明距離計算相似度,所謂圖像匹配就是在匹配圖像中找出與參考像素點相似度最高的點,而漢明距正是匹配圖像像素與參考像素相似度的度量。具體而言,對于欲求取視差的左右視圖,要比較兩個視圖中兩點的相似度,可將此兩點的census值逐位進行異或運算,然后計算結果為1 的個數(shù),記為此兩點之間的漢明值,漢明值是兩點間相似度的一種體現(xiàn),漢明值愈小,兩點相似度愈大實現(xiàn)算法時先異或再統(tǒng)計1的個數(shù)即可,漢明距越小即相似度越高。
下面的代碼是自己根據(jù)原理寫的,實現(xiàn)的結果并沒有很好,以后繼續(xù)優(yōu)化代碼。
具體代碼如下:
//*************************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的個數(shù) //-------------------定義Census處理圖像函數(shù)--------------------- int hWind = 1;//定義窗口大小為(2*hWind+1) Mat ProcessImg(Mat &Img);//將矩形內(nèi)的像素與中心像素相比較,將結果存于中心像素中 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)) { //--------- 將二進制數(shù)存在變量中----- 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; }
經(jīng)過處理后的左圖census圖像
經(jīng)過處理后的右圖census圖像
disparity圖像
處理后的disparity圖像
以上就是關于“C++如何實現(xiàn)雙目立體匹配Census算法”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關的知識內(nèi)容,請關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。