终于等到你

众里寻他千百度,蓦然回首,那人却在灯火阑珊处。

OpenCV 图像和ROI去畸变

void calibration_distort(string cur_photo_path) {
    cv::Mat cameraMatrix[70];
    cv::Mat distortionCoeff[70];
    std::vector<cv::Mat> undistortImg(70);
    std::string datapath = "./data";
    for (int i = 0; i <NUM_CAMS; i++)
    {
        cout << "undistorting cam: " << i + 1 <<" "<< cur_photo_path<<endl;
        int camera_id = i;
        loadcamerapara(cameraMatrix[i], distortionCoeff[i], camera_id, datapath);
        undistortImgandjson(cameraMatrix[i], distortionCoeff[i], camera_id, undistortImg[i], datapath, cur_photo_path);
        cv::imwrite("./1230data/undistortImg/CCD" + to_string(i + 1) + ".bmp", undistortImg[i]);
    }
    cout << "图像去畸变finished!" << endl;
}


void loadcamerapara(cv::Mat & cameraMatrix, cv::Mat & distortionCoeff, int camera_id, std::string datapath)
{
    double fx,cx,fy,cy,kc1,kc2,kc3,kc4;
    double temp;
    char buffer[256];
    std::ifstream in(datapath+"/camera_para/CCD"+std::to_string(camera_id+1)+".cal");
  // std::ifstream in("C:/Users/gzr2018/Desktop/xuening'data/11-16/11-16/waicanCCD"+ std::to_string(camera_id + 1)+".txt");
    int idd=0;
    for(int i=0;i<5;i++)//原来是 5
            in.getline (buffer,100);
    in>>fx;in>>temp;in>>cx;
    in>>temp;in>>fy;in>>cy;
    for(int i=0;i<2;i++)
            in.getline (buffer,100);
 /*   std::ifstream in2("C:/Users/gzr2018/Desktop/xuening'data/1115/waicanCCD" + std::to_string(camera_id + 1) + ".txt");
    for (int i = 0; i < 8; i++)
        in2.getline(buffer, 100);*/
    in>>kc1;in>>kc2;in>>kc3;in>>kc4;
    cameraMatrix = ( cv::Mat_<double> ( 3,3 ) << fx,0.000000,cx,
                        0.000000,fy,cy,
                        0.000000,0.000000,1.000000  );
    distortionCoeff = ( cv::Mat_<double> ( 1,4 ) << kc1, kc2, kc3, kc4 );

    //std::cout<<cameraMatrix<<std::endl;
    //std::cout<<distortionCoeff<<std::endl;
}
void undistortImgandjson(cv::Mat cameraMatrix, cv::Mat distortionCoeff, int camera_id, cv::Mat& undistortImg, 
std::string datapath, std::string photopath)
{
    cv::Mat srcMat, histMat;
    if (photopath == "") {
        srcMat = cv::imread(datapath + "/rawImg/CCD" + std::to_string(camera_id + 1) + ".bmp",0);
    }
    else {
        srcMat = cv::imread(photopath + "/CCD" + std::to_string(camera_id + 1) + ".bmp",0);
    }

    //histMat = cv::imread(photopath + "/HistMatch/CCD" + std::to_string(camera_id + 1) + ".bmp");
    
    //srcMat = MyHistMatch(histMat, srcMat);

    cv::Mat dstMat,new_matrix;
    undistort(srcMat, dstMat, cameraMatrix, distortionCoeff, new_matrix);

    //dstMat = MyHistMatch(histMat, dstMat);
    
        //boost::filesystem::path dir((datapath+"/undistortImg").c_str());
        //boost::filesystem::create_directory(dir);
 	    //mkdir((datapath+"/undistortImg").c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 
        //imwrite(datapath+"/undistortImg/CCD"+std::to_string(camera_id+1)+".bmp", dstMat);  
    
    undistortImg = dstMat;
    //return;
    std::string jsonpath = datapath+"/rawImg_json/camera-CCD"+std::to_string(camera_id+1)+".json";
    std::ifstream jsonfile(jsonpath);
    nlohmann::json roifile;
    jsonfile >> roifile;

    std::string camera_no = roifile["camera"];
    //std::cout<<camera_no<<":"<<std::endl;
    nlohmann::json roi = roifile["roi"];

    nlohmann::json root;
    root["camera"]=camera_no;
    nlohmann::json jsonroi[max_roi_size];
    //std::cout<<roi.size()<<std::endl;
    for (int i=0; i<roi.size(); i++)
    {
        cv::Mat cut_image;
        cv::Rect m_select;
        std::string name = roi[i]["name"];
        int lx = roi[i]["lx"];
        int ly = roi[i]["ly"];
        int rx = roi[i]["rx"];
        int ry = roi[i]["ry"];
        checkroi(lx,ly,rx,ry);
        jsonroi[i]["name"]=name;
        std::vector<cv::Point2d> src;
        std::vector<cv::Point2d> tmp;
        std::vector<cv::Point2d> dst;
        cv::Point2d p1(lx,ly);
        src.push_back(p1);
        myUndistortPoints(src, dst, cameraMatrix, distortionCoeff);
        //std::cout<<name<<" "<<lx<<" "<<ly<<" "<<rx<<" "<<ry<<std::endl;	
        //std::cout<<dst[0]<<std::endl;
        jsonroi[i]["lx"]=dst[0].x;    
        jsonroi[i]["ly"]=dst[0].y;  
        if (dst[0].x > 5472 || dst[0].y > 3648)
            continue;
        //box tempbox;
        //tempbox.lx = dst[0].x;   
        //tempbox.ly = dst[0].y;   
        src.clear();
        tmp.clear();
        dst.clear();
        cv::Point2d p2(rx,ry);
        src.push_back(p2);
        myUndistortPoints(src, dst, cameraMatrix, distortionCoeff);
        //std::cout<<dst[0]<<std::endl;
        jsonroi[i]["rx"]=dst[0].x;    
        jsonroi[i]["ry"]=dst[0].y;  
        if (dst[0].x < 0 || dst[0].y < 0)
            continue;
        //tempbox.rx = dst[0].x;   
        //tempbox.ry = dst[0].y;  
        src.clear();
        tmp.clear();
        dst.clear();
        root["roi"].push_back(jsonroi[i]);
    }
    //std::cout<<"read json complete."<<std::endl;
    jsonfile.close();
	//mkdir((datapath+"/undistortImg_json").c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); 
    boost::filesystem::path dir((datapath+"/undistortImg_json").c_str());
    boost::filesystem::create_directory(dir);
    std::ofstream os(datapath+"/undistortImg_json/camera-CCD"+std::to_string(camera_id+1)+".json");
    os << root.dump(4) << std::endl;
    os.close();
}
posted @ 2021-01-18 16:54  gzr2018  阅读(267)  评论(0编辑  收藏  举报