我正在尝试迭代许多图像,并将像素值与相应的地面实况图像进行比较。然而,当这样做时,我的计算机的内存使用量增加得非常快,最终程序崩溃了,我得到了这个错误:
OpenCV Error: Insufficient memory (Failed to allocate 259200 bytes) in unknown function, file C:\OpenCV2.3\modules\core\src\alloc.cpp, line 52
我尝试的主要方法如下。我错过了导致这种情况发生的事情吗?有没有更有效的方法呢?
方法1:
void calculateMetrics(cv::Mat gt_frame, cv::Mat result_frame){
rows = result_frame.rows;
cols = result_frame.cols;
int gt;
int res;
for(i = 0; i < rows; i++){
for(j = 0; j < cols; j++){
gt = gt_frame.at<uchar>(i, j);
res = result_frame.at<uchar>(i, j);
if((gt == 255) && (res == 255)) // True positive
tp++;
else if((gt == 0) && (res == 0)) // True negative
tn++;
else if((gt == 0) && (res == 255)) // False positive
fp++;
else if((gt == 255) && (res == 0)) // False negative
fn++;
}
}
}
方法2:
void calculateMetrics(cv::Mat gt_frame, cv::Mat result_frame){
rows = result_frame.rows;
cols = result_frame.cols;
uchar *gt;
uchar *res;
for(i = 0; i < rows; ++i){
gt = gt_frame.ptr(i);
res = result_frame.ptr(i);
for(j = 0; j < cols; ++j){
*gt++;
*res++;
if((*gt == 255) && (*res == 255)) // True positive
tp++;
else if((*gt == 0) && (*res == 0)) // True negative
tn++;
else if((*gt == 0) && (*res == 255)) // False positive
fp++;
else if((*gt == 255) && (*res == 0)) // False negative
fn++;
}
}
}
致电代码:
void EvaluationFramework::processResults(int video_num){
num_frames = getNumFrames(video_num);
for(j = 0; j < num_frames; j++){
gt_frame = getGroundTruthFrame(video_num, j);
result_frame = getResultFrame(video_num, j);
calculateMetrics(gt_frame, result_frame);
}
std::cout << "TP: " << tp << std::endl;
std::cout << "TN: " << tn << std::endl;
std::cout << "FP: " << fp << std::endl;
std::cout << "FN: " << fn << std::endl;
precision = getPrecision((double)tp, (double)fp);
recall = getRecall((double)tp, (double)fn);
fmeasure = getFmeasure(precision, recall);
std::cout << "Precision: " << precision << std::endl;
std::cout << "Recall: " << recall << std::endl;
std::cout << "F-Measure: " << fmeasure << std::endl;
insertResults(0, video_num, precision, recall, fmeasure);
}
getGroundTruthFrame和getResultFrame函数只是从文件夹读取相关图像,getPrecision,getRecall和getFmeasure执行相当简单的计算,insertResults将计算值插入数据库。