您的位置:首页 > 运维架构 > 网站架构

立体匹配:关于用OpenCV彩色化middlebury网站给定的视差

2016-03-17 13:42 471 查看
#include "XYZ.h"

void readPFM(Mat_<float> &disp, float &scale, string path)
{    //关于将Mat见我的另外一篇博客:
ifstream in(path, ios_base::binary);

int cols;
int rows;
char tmp[10];
in.get(tmp, 10, '\n'); in.get();//文件头
in.get(tmp, 10, ' '); in.get(); cols = atoi(tmp);//列数
in.get(tmp, 10, '\n'); in.get(); rows = atoi(tmp); //行数
in.get(tmp, 10, '\n'); in.get(); scale = atof(tmp);//缩放因子

disp.create(rows, cols);
for (int i = disp.rows - 1; i >= 0; i--)//因为存储是从最后行开始存储的
for (int j = 0; j < disp.cols; j++)
in.read((char*)(&disp(i, j)), sizeof(float));

in.close();
}

void dyeDisp_core(Mat_<Vec3b> &colordisp, Mat_<float> &disp, float mindisp, float maxdisp)
{
float scale = 1.0 / (maxdisp - mindisp);
for (int i = 0; i < disp.rows; i++)
for (int j = 0; j < disp.cols; j++)
{
Vec3b *xxx = &colordisp(i, j);
float x = disp(i, j);
if ((x != INFINITY) && (x != -INFINITY))
{
x = scale * (x - mindisp);
x = x / 1.15 + 0.1; // use slightly asymmetric range to avoid darkest shades of blue.
(*xxx)[2] = __max(0, __min(255, (int)(round(255 * (1.5 - 4 * fabs(x - .75))))));
(*xxx)[1] = __max(0, __min(255, (int)(round(255 * (1.5 - 4 * fabs(x - .5))))));
(*xxx)[0] = __max(0, __min(255, (int)(round(255 * (1.5 - 4 * fabs(x - .25))))));
}
else
(*xxx) = Vec3b(0, 0, 0);
}
}

void dyeDisp_one(string dir)
{
string calibpath = dir + "/calib.txt";
string disppath = dir + "/disp0GT.pfm";
string colordisppath = dir + "/disp0GT.png";

//1.读取极值视差
float dmin = 0;
float dmax = 0;
char line[1000];
float f;
FILE *fp = fopen(calibpath.c_str(), "r");
while (fgets(line, sizeof line, fp) != NULL)
{
if (sscanf(line, " vmin= %f", &f) == 1) dmin = f;
if (sscanf(line, " vmax= %f", &f) == 1) dmax = f;
}
fclose(fp);

//2.读取视差图像
float scale;
Mat_<float> disp;
readPFM(disp, scale, disppath);
//FileStorage fs(disppath, FileStorage::READ);
//fs["mat"] >> disp;

//3.计算彩色视差
Mat_<Vec3b> colordisp(disp.rows, disp.cols);
dyeDisp_core(colordisp, disp, dmin, dmax);

//4.保存结果
imwrite(colordisppath, colordisp);
}

void dyeDisp_dir()
{
vector<string> dirs = cv_GetListFolders("./../TestData");
for (int i = 0; i < dirs.size(); i++)
dyeDisp_one(dirs[i]);
}

void main()
{
dyeDisp_dir();
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: