opemcv txt 转MAT,vector到TXT

来源:互联网 发布:企业版软件报盘 编辑:程序博客网 时间:2024/04/30 19:32
void LoadDatav(string fileName, const vector<KeyPoint>& keypoints)
{
int retVal = 0;


// 打开文件  
ifstream inFile(fileName.c_str(), ios_base::in);
if (!inFile.is_open())
{
cout << "读取文件失败" << endl;
retVal = -1;


}




// 载入数据  
istream_iterator<float> begin(inFile);    //按 float 格式取文件数据流的起始指针  
istream_iterator<float> end;          //取文件流的终止位置  
vector<KeyPoint> keypoints(begin, end);      //将文件数据保存至 std::vector 中  






}


void LoadData(string fileName, cv::Mat& matData, int matRows = 0, int matCols = 0, int matChns = 0)
{
int retVal = 0;


// 打开文件  
ifstream inFile(fileName.c_str(), ios_base::in);
if (!inFile.is_open())
{
cout << "读取文件失败" << endl;
retVal = -1;

}


// 载入数据  
istream_iterator<float> begin(inFile);    //按 float 格式取文件数据流的起始指针  
istream_iterator<float> end;          //取文件流的终止位置  
vector<float> inData(begin, end);      //将文件数据保存至 std::vector 中  
cv::Mat tmpMat = cv::Mat(inData);       //将数据由 std::vector 转换为 cv::Mat  


// 输出到命令行窗口  
//copy(vec.begin(),vec.end(),ostream_iterator<double>(cout,"\t"));   


// 检查设定的矩阵尺寸和通道数  
size_t dataLength = inData.size();
//1.通道数  
if (matChns == 0)
{
matChns = 1;
}
//2.行列数  
if (matRows != 0 && matCols == 0)
{
matCols = dataLength / matChns / matRows;
}
else if (matCols != 0 && matRows == 0)
{
matRows = dataLength / matChns / matCols;
}
else if (matCols == 0 && matRows == 0)
{
matRows = dataLength / matChns;
matCols = 1;
}
//3.数据总长度  
if (dataLength != (matRows * matCols * matChns))
{
cout << "读入的数据长度 不满足 设定的矩阵尺寸与通道数要求,将按默认方式输出矩阵!" << endl;
retVal = 1;
matChns = 1;
matRows = dataLength;
}


// 将文件数据保存至输出矩阵  
matData = tmpMat.reshape(matChns, matRows).clone();



}


 Mat shiyan1, shiyan2;
KeyPoint dian1, dian2;

        Point2f pot1, pot2;
int shu1 = 0, shu2 = 0;
ofstream outfile1("data1.txt", ios::out);
for (int k = 0; k<keypoints_1.size(); k++)
{
dian1 = keypoints_1[k];
pot1 = dian1.pt;
outfile1 << pot1.x << " , " << pot1.y << endl;
//cout << "[" << pot1.x << " , " << pot1.y << "]" << endl;
++shu1;
}
cout << shu1 << " keypoints in the first image." << endl;


int WriteData(string fileName, cv::Mat& matData)  
{  
    int retVal = 0;  
  
    // 打开文件  
    ofstream outFile(fileName.c_str(), ios_base::out);  //按新建或覆盖方式写入  
    if (!outFile.is_open())  
    {  
        cout << "打开文件失败" << endl;   
        retVal = -1;  
        return (retVal);  
    }  
  
    // 检查矩阵是否为空  
    if (matData.empty())  
    {  
        cout << "矩阵为空" << endl;   
        retVal = 1;  
        return (retVal);  
    }  
  
    // 写入数据  
    for (int r = 0; r < matData.rows; r++)  
    {  
        for (int c = 0; c < matData.cols; c++)  
        {  
            uchar data = matData.at<uchar>(r,c);  //读取数据,at<type> - type 是矩阵元素的具体数据格式  
            outFile << data << "\t" ;   //每列数据用 tab 隔开  
        }  
        outFile << endl;  //换行  
    }  
  
    return (retVal);  
}  

原创粉丝点击