我正在编写一个C程序,它计算94000个图像区域的度量标准并将值写入文件中。我可以成功地编写51069个区域的指标,但之后C程序输出了一个分段错误。代码非常简单:
int main(int argc, char** argv)
{
Image *cimg=NULL;
Image *mask=NULL;
Image *gt=NULL;
Image *rmask=NULL;
Image *nwcimg=NULL;
Image *nwrmask=NULL;
Histogram *hist=NULL;
FILE* output;
int x, r, j, y, maxR, minR, index, hsize, value, relevance;
int min_x, min_y, max_x, max_y;
//just checking the parameters
if (argc != 5)
{
fprintf(stderr,"usage: generatemetric<image> <region mask> <ground truth> <feature_file> \n");
exit(-1);
}
//reading the input image
cimg = ReadImage(argv[1]);
//reading the image segmentation
mask = ReadImage(argv[2]);
//reading the ground truth which i will use to label the image regions as +1 and -1
gt = ReadImage(argv[3]);
//maximum and minimum region value
maxR = MaximumValue(mask);
minR = MinimumValue(mask);
//i will iterate through all the image regions
for(r=minR; r <= maxR; r++)
{
//this is done because I want to know 2 points in the image region
//to create a bounding box on it
min_x = mask->ncols-1;
min_y = mask->nrows-1;
max_x = 0;
max_y = 0;
for(y=0; y < mask->nrows; y++)
{
for(x=0; x < mask->ncols;x++)
{
index = y*(mask->ncols)+x;
if(mask->val[index] == r)
{
if(x < min_x)
min_x = x;
if(y < min_y)
min_y = y;
if(x > max_x)
max_x = x;
if(y > max_y)
max_y = y;
}
}
}
nwcimg = CreateImage(max_x-min_x,max_y-min_y);
nwrmask = CreateImage(max_x-min_x,max_y-min_y);
//creating ROIS
CreateROI(cimg, nwcimg, min_x, min_y, max_x, max_y);
CreateROI(mask, nwrmask, min_x, min_y, max_x, max_y);
//calculating region class
relevance = isRelevant(gt, mask, r);
//makes the region of interest white in the bounding box
make_binary(nwrmask,r);
//calculates the metric only in the bounding box around the region of interest (a vector that is a kind of histogram)
hist=metric(nwcimg,nwrmask);
//starting to write
//histogram size
hsize = hist->n;
//open the file to append the values
//the problem starts here after 51069 iterations
output = fopen(argv[4], "a+");
//record the region class in the file
fprintf(output, "%d, ", relevance);
//record each value of the histogram as comma separated values
for(j = 0; j < hsize; j++)
{
value = hist->v[j];
fprintf(output, "%d, ", value);
}
//recod the region id
fprintf(output,"%d \n", r+1);
fclose(output);
DestroyHistogram(&hist);
DestroyImage(&nwcimg);
DestroyImage(&nwrmask);
}
DestroyImage(&rmask);
DestroyImage(>);
DestroyImage(&cimg);
DestroyImage(&mask);
return(0);
}
换句话说,在94000次迭代中的每次迭代中,程序打开文件,写入值并关闭它。我想也许我在程序运行时有很多I / O.我对吗?还是其他问题?这里有人遇到过这个问题吗?怎么解决?
答案 0 :(得分:0)
通过将文件从循环中取出并在循环之后按照用户4815162342的建议进行解析来解决。我认为问题是很多i / o,而且我的大学集群中也有一些并发的过程。该程序现在正在运行。谢谢大家的关注和帮助。