C++ 在这种情况下,如何在编译时从文本文件中读取数据?
我知道问题的标题可能会让它看起来像是一个重复的问题,但事实并非如此。我已经尝试了这里给出的关于stackoverflow的几乎所有解决方案 <>强>我的问题是我使用C++代码,我也使用一些开放的CV库和函数。我有一个主函数osiman.cpp,其代码为 我的代码: } 这些值正在构造函数中初始化。在函数initconfiguration中,我们可以看到两个文件路径mfilenameGaborFilter和mfilenameApplication点。它们给出了两个文件名的路径。这些文件基本上都有巨大的矩阵,比如9乘15,9乘25。这些矩阵被读入变量mGaborFilters和mpApplicationPoints,分别为vectorCvMat*和CvMat*类型。用于将这些文件读入变量的函数分别作为loadGaborFilters和loadApplicationPoints给出C++ 在这种情况下,如何在编译时从文本文件中读取数据?,c++,opencv,compilation,C++,Opencv,Compilation,我知道问题的标题可能会让它看起来像是一个重复的问题,但事实并非如此。我已经尝试了这里给出的关于stackoverflow的几乎所有解决方案 强>我的问题是我使用C++代码,我也使用一些开放的CV库和函数。我有一个主函数osiman.cpp,其代码为 我的代码: } 这些值正在构造函数中初始化。在函数initconfiguration中,我们可以看到两个文件路径mfilenameGaborFilter和mfilenameApplication点。它们给出了两个文件名的路径。这些文件基本上都有巨大的
void OsiManager::loadGaborFilters( )
{
// Open text file containing the filters
ifstream file(mFilenameGaborFilters.c_str(),ios::in) ;
if ( ! file )
{
throw runtime_error("Cannot load Gabor filters in file " + mFilenameGaborFilters) ;
}
// Get the number of filters
int n_filters ;
file >> n_filters ;
mGaborFilters.resize(n_filters) ;
// Size of filter
int rows , cols ;
// Loop on each filter
for ( int f = 0 ; f < n_filters ; f++ )
{
// Get the size of the filter
file >> rows ;
file >> cols ;
// Temporary filter. Will be destroyed at the end of loop
mGaborFilters[f] = cvCreateMat(rows,cols,CV_32FC1) ;
// Set the value at coordinates r,c
for ( int r = 0 ; r < rows ; r++ )
{
for ( int c = 0 ; c < cols ; c++ )
{
file >> mGaborFilters[f]->data.fl[r*cols+c] ;
}
}
} // Loop on each filter
// Close the file
file.close() ;
} // end of function
// Load the application points (build a binary matrix) from a textfile
void OsiManager::loadApplicationPoints ( )
{
// Open text file containing the filters
ifstream file(mFilenameApplicationPoints.c_str(),ios::in) ;
if ( ! file )
{
throw runtime_error("Cannot load the application points in " + mFilenameApplicationPoints) ;
}
// Get the number of points
int n_points = 0 ;
file >> n_points ;
// Allocate memory for the matrix containing the points
mpApplicationPoints = cvCreateMat(mHeightOfNormalizedIris,mWidthOfNormalizedIris,CV_8UC1) ;
// Initialize all pixels to "off"
cvSet(mpApplicationPoints,cvScalar(0)) ;
// Local variables
int i , j ;
// Loop on each point
for ( int p = 0 ; p < n_points ; p++ )
{
// Get the coordinates
file >> i ; file >> j ;
// Set pixel to "on"
if ( i < 0 || i > mpApplicationPoints->rows-1 || j < 0 || j > mpApplicationPoints->cols-1 )
{
cout << "Point (" << i << "," << j << ") " ;
cout << "exceeds size of normalized image : " ;
cout << mpApplicationPoints->rows << "x" << mpApplicationPoints->cols ;
cout << " while loading application points" << endl ;
}
else
{
mpApplicationPoints->data.ptr[(i)*mpApplicationPoints->cols+j] = 255 ;
}
}
// Close the file
file.close() ;
} // end of function
void OsiManager::loadGaborFilters()
{
//打开包含过滤器的文本文件
ifstream文件(mFilenameGaborFilters.c_str(),ios::in);
如果(!文件)
{
抛出运行时错误(“无法在文件中加载Gabor筛选器”+mFilenameGaborFilters);
}
//获取过滤器的数量
int n_滤波器;
文件>>n_过滤器;
mGaborFilters.resize(n_filters);
//过滤器尺寸
int行,cols;
//在每个过滤器上循环
对于(int f=0;f>行;
文件>>cols;
//临时过滤器。将在循环结束时销毁
mGaborFilters[f]=cvCreateMat(行、列、CV_32FC1);
//在坐标r,c处设置值
对于(int r=0;r>mGaborFilters[f]->data.fl[r*cols+c];
}
}
}//在每个过滤器上循环
//关闭文件
file.close();
}//函数结束
//从文本文件加载应用程序点(构建二进制矩阵)
void OsiManager::loadApplicationPoints()
{
//打开包含过滤器的文本文件
ifstream文件(mFilenameApplicationPoints.c_str(),ios::in);
如果(!文件)
{
抛出运行时错误(“无法加载“+mFilenameApplicationPoints”中的应用程序点);
}
//获取点数
int n_点=0;
文件>>n_点;
//为包含点的矩阵分配内存
mpApplicationPoints=cvCreateMat(mHeightOfNormalizedIris,mWidthOfNormalizedIris,CV_8UC1);
//将所有像素初始化为“关闭”
cvSet(mpApplicationPoints,cvScalar(0));
//局部变量
int i,j;
//在每个点上循环
对于(int p=0;p>i;文件>>j;
//将像素设置为“开”
如果(i<0 | | i>mpApplicationPoints->rows-1 | | j<0 | | j>mpApplicationPoints->cols-1)
{
cout您不能在编译时执行运行时文件IO,但由于源数据只是文本,您可以使用这些文件的内容初始化const char*
字符串(使用C++11原始字符串文本和复制粘贴很容易)并将init函数中的ifstream
替换为用字符串文字初始化的std::stringstream
。尤其不必用无关的代码混淆所有人。在编译时不可能读取任何附加的初始化数据,除非它是以可以有效插入w的格式给出的使用#include
语句。链接到的问题是关于二进制数据的,不允许我的解决方案,即将文本配置数据放入静态初始化的std::istringstream
对象的初始化字符串中。此技术可能满足您的需要:@Galik我想知道是否可以o让预处理器将文本注入原始字符串文字中(利用其他杂注的优点,如#ifdef
等)。使用字符串文字延续或类似的技巧?@πάνταῥεῖ 我刚刚管理好了它,但我不得不将原始字符串分隔符R“~(
和)~”
在外部文本文件中。谢谢你的回答,但是我如何用这些文件的内容初始化const char*字符串。我只知道文件的位置。我想这是我的问题。我如何在编译时复制这些内容并用这些内容初始化变量我很抱歉我在这些方面太幼稚了C++的概念。你能举一个例子吗?复制和粘贴……在某些情况下,你可以<代码> >包含文件的内容>代码>但是我不认为这样可以将文本文件转换成字符串。如果你想自动读取文件,你可以使用一些简单的脚本通过你的构建系统来设置它。ing或类似于xxd的工具。事实上,我确实尝试过xxd,并试图#包括xxd生成的头文件,如mGaborFIlters={#include“filters.h”}……但我不确定它是否给了我很多编译时错误,我认为我做得不对
// Default constructor
OsiManager::OsiManager ( )
{
mMapInt["Minimum diameter for pupil"] = &mMinPupilDiameter ;
mMapInt["Maximum diameter for pupil"] = &mMaxPupilDiameter ;
mMapInt["Minimum diameter for iris"] = &mMinIrisDiameter ;
mMapInt["Maximum diameter for iris"] = &mMaxIrisDiameter ;
mMapInt["Width of normalized image"] = &mWidthOfNormalizedIris ;
mMapInt["Height of normalized image"] = &mHeightOfNormalizedIris ;
mMapString["Gabor filters"] = &mFilenameGaborFilters ;
mMapString["Application points"] = &mFilenameApplicationPoints ;
// Initialize all parameters
initConfiguration() ;
}
void OsiManager::initConfiguration ( )
{
mMinPupilDiameter = 50;
mMaxPupilDiameter = 160;
mMinIrisDiameter = 160;
mMaxIrisDiameter = 280;
mWidthOfNormalizedIris = 512;
mHeightOfNormalizedIris = 64;
mFilenameGaborFilters = "/home/Iris_Osiris_v4.1/OsirisParam/filters.txt" ;
mFilenameApplicationPoints = "/home/Iris_Osiris_v4.1/OsirisParam/points.txt" ;
mGaborFilters.clear();
mpApplicationPoints = 0 ;
void OsiManager::loadGaborFilters( )
{
// Open text file containing the filters
ifstream file(mFilenameGaborFilters.c_str(),ios::in) ;
if ( ! file )
{
throw runtime_error("Cannot load Gabor filters in file " + mFilenameGaborFilters) ;
}
// Get the number of filters
int n_filters ;
file >> n_filters ;
mGaborFilters.resize(n_filters) ;
// Size of filter
int rows , cols ;
// Loop on each filter
for ( int f = 0 ; f < n_filters ; f++ )
{
// Get the size of the filter
file >> rows ;
file >> cols ;
// Temporary filter. Will be destroyed at the end of loop
mGaborFilters[f] = cvCreateMat(rows,cols,CV_32FC1) ;
// Set the value at coordinates r,c
for ( int r = 0 ; r < rows ; r++ )
{
for ( int c = 0 ; c < cols ; c++ )
{
file >> mGaborFilters[f]->data.fl[r*cols+c] ;
}
}
} // Loop on each filter
// Close the file
file.close() ;
} // end of function
// Load the application points (build a binary matrix) from a textfile
void OsiManager::loadApplicationPoints ( )
{
// Open text file containing the filters
ifstream file(mFilenameApplicationPoints.c_str(),ios::in) ;
if ( ! file )
{
throw runtime_error("Cannot load the application points in " + mFilenameApplicationPoints) ;
}
// Get the number of points
int n_points = 0 ;
file >> n_points ;
// Allocate memory for the matrix containing the points
mpApplicationPoints = cvCreateMat(mHeightOfNormalizedIris,mWidthOfNormalizedIris,CV_8UC1) ;
// Initialize all pixels to "off"
cvSet(mpApplicationPoints,cvScalar(0)) ;
// Local variables
int i , j ;
// Loop on each point
for ( int p = 0 ; p < n_points ; p++ )
{
// Get the coordinates
file >> i ; file >> j ;
// Set pixel to "on"
if ( i < 0 || i > mpApplicationPoints->rows-1 || j < 0 || j > mpApplicationPoints->cols-1 )
{
cout << "Point (" << i << "," << j << ") " ;
cout << "exceeds size of normalized image : " ;
cout << mpApplicationPoints->rows << "x" << mpApplicationPoints->cols ;
cout << " while loading application points" << endl ;
}
else
{
mpApplicationPoints->data.ptr[(i)*mpApplicationPoints->cols+j] = 255 ;
}
}
// Close the file
file.close() ;
} // end of function