我正在编写一个 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。我对吗?还是其他类型的问题?这里有人遇到过这个问题吗?如何解决?