map<string,rad_err> linemod_match::match_final(string path){
srand((unsigned) time(NULL));//diff color
MIPP_test();
time_t begin,end;
double duration;
begin=clock();
map<string,double> match_radius;
map<string,double> match_error;
map<string,rad_err> map_match;
cout << "开始计算时间 .... " << endl;
cv::FileStorage fsall(path, cv::FileStorage::READ);
if (!fsall.isOpened()) {
fprintf(stderr, "%s:%d:loadParams falied. 'config.yaml' does not exist\n", __FILE__, __LINE__);
exit(1);
}
int NUM_CAMS = (int)fsall["NUM_CAMS"];
string train_root;
string roi_root;
string test_img_root;
string center_json;
string all_mode;
string xyz_json;
fsall["train_root"]>>train_root;
fsall["roi_root"]>>roi_root;
fsall["test_img_root"]>>test_img_root;
fsall["center_json"]>>center_json;
fsall["all_mode"]>>all_mode;
fsall["xyz_json"]>>xyz_json;
cout<<"cam:"<<NUM_CAMS<<endl;
cout<<"center_json"<<center_json<<endl;
fsall.release();
cout<<"***************"<<endl;
//读取最终的三坐标中心点json
ifstream xyz_json_file(xyz_json);
nlohmann::json xyz_d_set;
xyz_json_file >> xyz_d_set;
//配置文件读取完毕
//gzr
ofstream out0("path_map_0.txt");
ofstream out1("path_map_1.txt");
ofstream out2("path_map_2.txt");
ofstream out3("path_map_3.txt");
int comm_size,rank;
MPI_Init(NULL,NULL);
MPI_Comm_size(MPI_COMM_WORLD,&comm_size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
cout<<"MPI strat\n";
cout<<comm_size<<": comm_size \n";
//4 threads
for (int i=rank;i<NUM_CAMS;i+=comm_size){ //相机台数 NUM_CAMS 24
// 删除txt文件
string txt_dir=prefix+"txt/"+"camera-CCD"+to_string(i+1)+"/";
fstream _file_rmtxt;
//c_str():生成一个const char*指针,指向以空字符终止的数组。
const char *txt_path=(txt_dir).c_str();
_file_rmtxt.open(txt_dir, ios::in); //if result dir not exist, mkdir
if(_file_rmtxt) //如果存在 /txt/" 就删除,使得ofstream可以重新创建
{
auto namelist=file_search2(txt_dir,"*.txt");
if (namelist.size()!=0){
//删除txt文件
for(int k=0;k<namelist.size();k++){
string cur_txt=txt_dir+namelist[k];
fstream _file_rmtxt;
const char *txt_path=(cur_txt).c_str();
_file_rmtxt.open(txt_path, ios::in); //if result dir exist
if(_file_rmtxt) //如果存在 result_path+"result.txt" 就删除,使得ofstream可以重新创建
{
remove(txt_path);
}
_file_rmtxt.close();
}
}
}
_file_rmtxt.close();
// if(i+1==19 ||i+1==20){ //19 20模糊的两组用来测平面度的直接跳过
// continue;
// }
string train_img_dir = train_root+"camera-CCD"+to_string(i+1)+"/";
string current_roi=roi_root+"camera-CCD"+to_string(i+1)+".json"; //每个相机的roi json文件,于洋给出
string test_img_path=test_img_root+"CCD"+to_string(i+1)+".bmp";
//获取圆数目
char *cstr = ¤t_roi[0];
char* json = getfileAll(cstr);
Json::Reader reader;
Json::Value resp;
if (!reader.parse(json, resp, false))
{
printf("bad json format!\n");
exit(1);
}
Json::Value roi = resp["roi"];
int circle_per_cam=roi.size();
cout<<"circle_per_cam:"<<circle_per_cam<<endl;
cout<<"************ finish reading config ************"<<endl;
cout<<endl;
if(all_mode=="train"){
auto td_train=read_config("train",train_img_dir,test_img_path,current_roi,center_json,xyz_json,i+1); //center_json 名义值 xyz_json测量值
for (int j=0;j<td_train.size();j++){
//6边型
string c_name=td_train[j].objid;//孔编号
auto rad=angle_test(td_train[j]);
}
}
else{
auto td_test=read_config("test",train_img_dir,test_img_path,current_roi,center_json,xyz_json,i+1); //center_json 名义值 xyz_json测量值
for (int j=0;j<td_test.size();j++){
string c_name=td_test[j].objid;
auto rad=angle_test(td_test[j]);
td_test[j].test_img.release();
// nlohmann::json xyz_name= xyz_d_set[c_name];
// if(xyz_name==nullptr) //不存在
// continue;
nlohmann::json D= xyz_d_set[c_name]["D"];
rad.flag= false; //flag为false代表有测量半径
// if(D== nullptr)
// continue;
if(D == nullptr){
D=rad.radius*2;
rad.flag= true; //无测量半径,但是要测位置度
}
double r_real=double(D)/2;
rad.error=abs(rad.radius-r_real);
rad.cam=i+1;
if(rad.flag==false){ //只返回有半径的,对于无半径的,图像上的圆心已经写入txt了
if(rank==0){
out0<<c_name<<" "<<rad.radius<<" "<<rad.error<<" "<<endl;
}
if(rank==1){
out1<<c_name<<" "<<rad.radius<<" "<<rad.error<<" "<<endl;
}
if(rank==2){
out2<<c_name<<" "<<rad.radius<<" "<<rad.error<<" "<<endl;
}
if(rank==3){
out3<<c_name<<" "<<rad.radius<<" "<<rad.error<<" "<<endl;
}
}
}
}
}
//4 threads
MPI_Barrier(MPI_COMM_WORLD);
if(rank!=0){
MPI_Finalize();
exit(0);
}
MPI_Finalize();
end=clock();
cout<<"****** end over ******"<<endl;
duration=double(end-begin)/CLOCKS_PER_SEC;
cout<<"runtime: "<<duration<<" s"<<endl;
//便利合并map
ifstream in0("path_map_0.txt");ifstream in1("path_map_1.txt");ifstream in2("path_map_2.txt");ifstream in3("path_map_3.txt");
string c_name;
double radius,error;
while(!in0.eof()){
in0>>c_name>>radius>>error;
if(map_match.count(c_name)==0 &&radius!=100) //不存在 直接插入
{
map_match.insert(pair<string , rad_err >(c_name,rad_err{error,radius,0,0}));
}
if(map_match.count(c_name)>0) //存在
{
if (map_match[c_name].error > error) //如果map_error里的圆孔误差比当前大,更新误差
{
map_match[c_name].radius = radius;
map_match[c_name].error = error;
//rad.cam=i+1;
}
}
}
while(!in1.eof()){
in1>>c_name>>radius>>error;
if(map_match.count(c_name)==0 &&radius!=100) //不存在 直接插入
{
map_match.insert(pair<string , rad_err >(c_name,rad_err{error,radius,0,0}));
}
if(map_match.count(c_name)>0) //存在
{
if (map_match[c_name].error > error) //如果map_error里的圆孔误差比当前大,更新误差
{
map_match[c_name].radius = radius;
map_match[c_name].error = error;
//rad.cam=i+1;
}
}
}
while(!in2.eof()){
in2>>c_name>>radius>>error;
if(map_match.count(c_name)==0 &&radius!=100) //不存在 直接插入
{
map_match.insert(pair<string , rad_err >(c_name,rad_err{error,radius,0,0}));
}
if(map_match.count(c_name)>0) //存在
{
if (map_match[c_name].error > error) //如果map_error里的圆孔误差比当前大,更新误差
{
map_match[c_name].radius = radius;
map_match[c_name].error = error;
//rad.cam=i+1;
}
}
}
while(!in3.eof()){
in3>>c_name>>radius>>error;
if(map_match.count(c_name)==0 &&radius!=100) //不存在 直接插入
{
map_match.insert(pair<string , rad_err >(c_name,rad_err{error,radius,0,0}));
}
if(map_match.count(c_name)>0) //存在
{
if (map_match[c_name].error > error) //如果map_error里的圆孔误差比当前大,更新误差
{
map_match[c_name].radius = radius;
map_match[c_name].error = error;
//rad.cam=i+1;
}
}
}
in0.close();in1.close();in2.close();in3.close();
//have a look
cout<<"map_match size:"<<map_match.size()<<endl;
auto inter=map_match.begin();
while(inter!=map_match.end()){
cout<<inter->first<<" "<<inter->second.radius<<" "<<inter->second.error<<endl;
inter++;
}
return map_match;
}
linux for windows
最新推荐文章于 2023-09-03 08:15:00 发布