我知道问题的标题可能会使它看起来像是一个重复的问题,但事实并非如此。我已经在stackoverflow上尝试了几乎所有解决方案.. A way to read data out of a file at compile time to put it somewhere in application image files to initialize an array。
我的问题是我使用的是C ++代码,我也在使用一些开放的cv库和函数。我有一个主要功能OsiMain.cpp,其代码为
我的代码:
int main (int argc, char * argv[])
{
try
{
OsiManager osi ;
osi.loadConfiguration(argv[1]) ;
osi.showConfiguration() ;
osi.run() ;
}
catch ( std::exception & e )
{
std::cout << e.what() << std::endl ;
}
return 0 ;
OsiManager是另一个类。该类管理所有文件,配置,保存和加载选项。它使用OsiEye类来执行技术处理。
在OsiManager构造函数中,我们初始化代码所需的一些参数的值。 OsiManager代码的构造函数部分在下面给出
// 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 ;
}
这些值在构造函数中初始化。在函数 initconfiguration 中,我们可以看到两个文件路径mfilenameGaborFilter和mfilenameApplication points。这些给出了两个文件名的路径。这些文件基本上有9×15,9×25的巨大矩阵。这些矩阵被读入变量mGaborFilters和mpApplicationPoints,它们分别是vectorCvMat *和CvMat *类型。用于将这些文件读入变量的函数如下所示分别为loadGaborFilters和loadApplicationPoints。
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
以上两个函数是OsiManager类的成员函数loadConfiguration的一部分,它在main函数中被调用。
我的问题是我希望这些文件在编译时将Gabor过滤器和应用程序点读入变量,因为我们不希望将文件与可执行文件或构建一起上传到集群。
答案 0 :(得分:1)
您无法在编译时执行运行时文件IO,但由于源数据只是文本,您可以使用这些文件的内容初始化const char*
字符串(使用C ++ 11原始字符串文字并复制和粘贴很容易并使用您的字符串文字初始化ifstream
替换初始函数中的std::stringstream
。