我有大约20个图像已被彩色编码。我想扫描每个图像并将像素匹配到与该颜色关联的标签。我已经编写了下面的代码,但是它需要大约30分钟才能完成这个看起来很简单的任务。这些图像具有960×720C++:OpenCV性能问题扫描图像
我的代码的分辨率:
void go_through_pixels(path &image_dir, string& ground_truth_suffix, string image_format, unordered_map<RGB, string> colors_for_labels){
if(!exists(image_dir)){
cerr << image_dir << " does not exist, prematurely returning" << endl;
exit(-1);
}
unordered_map<string, set<path> > label_to_files_map;
//initialise label_to_files_map
for(unordered_map<RGB, string>::iterator it = colors_for_labels.begin(); it != colors_for_labels.end(); it++){
label_to_files_map[it->second] = set<path>();
}
directory_iterator end_itr; //default construction provides an end reference
for(directory_iterator itr(image_dir); itr != end_itr; itr++){
path file = itr->path();
string filename = file.filename().string();
RGB rgb(0,0,0); //default rgb struct, values will be changed in the loop
if(extension(file) == image_format && filename.find(ground_truth_suffix) != string::npos){
//ground truth file
Mat img = imread(file.string(), CV_LOAD_IMAGE_COLOR);
for(int y = 0; y < img.rows; y++){
for(int x = 0; x < img.cols; x++){
//gives data as bgr instead of rgb
Point3_<uchar>* pixel = img.ptr<Point3_<uchar> >(y,x);
rgb.red = (int)pixel->z;
rgb.green = (int)pixel->y;
rgb.blue =(int)pixel->x;
string label = colors_for_labels[rgb];
label_to_files_map[label].insert(file);
cout << label << endl;
}
}
}
}
}
我将与此数据之后做多,但简化了我的代码下来只是为了尝试和发现性能问题。
我发现label_to_files_map[label].insert(file)
造成了大部分的延迟,因为当它被移走大约需要3分钟才能扫描图像。我仍然认为这太长了,但可能是错误的?
此外,由于集insert
需要很长时间(因为它必须在插入之前检查重复)任何人都可以提出一个更好的数据结构在这里使用?
本质上的图片可以让我们说相当于100个像素的建筑物,100对应于汽车等,所以我只是想在地图label_to_files_map
记录,这个文件(当前图像扫描)具有建设它(在这种情况下用特定的rgb值表示)。
那么,unordered_map ** [] **运算符的复杂性,在最坏的情况下,[容器大小的线性...](http://www.cplusplus.com/参考/ STL /图/)。设置的容器插入操作是对数的。因此,在最坏的情况下,整个批次将是上述复杂度的960x720x20像素倍。 –