如何通过最快的方法

时间:2018-02-07 13:09:45

标签: c++ opencv image-processing

交叉发布here

假设我有一个这样的二进制图像

Mat img(1000, 2000, CV_8UC1);
randu(img, Scalar(0), Scalar(255));
inRange(img, Scalar(160), Scalar(200), img);

我将使用connectedComponentsWithStats标记二进制img

Mat labels, stats, centroids;
int counts_img = connectedComponentsWithStats(img, labels, stats, centroids);

我将指定一些要从labels中删除的标签。

vector<int> drop_label(counts_img - 3);
iota(drop_label.begin(), drop_label.end(), 1);

当然我可以用以下方法做到:

Mat img(1000, 2000, CV_8UC1);
randu(img, Scalar(0), Scalar(255));
inRange(img, Scalar(160), Scalar(200), img);

Mat labels, stats, centroids;
int counts_img = connectedComponentsWithStats(img, labels, stats, centroids);

vector<int> drop_label(counts_img - 3);
iota(drop_label.begin(), drop_label.end(), 1);

//start to count the time.
double start_time = (double)getTickCount();

Mat select = img.clone() = 0;
int img_height = img.rows, img_width = img.cols;
for (int i = 0; i < img_height; i++) {
    int*plabels = labels.ptr<int>(i);
    uchar*pselect = select.ptr<uchar>(i);
    for (int j = 0; j < img_width; j++) {
        if (find(drop_label.begin(), drop_label.end(), plabels[j]) != drop_label.end())
            pselect[j] = 255;
    }
}

Mat result = img - select;

//total time
double total_time = ((double)getTickCount() - start_time) / getTickFrequency();
cout << "total time: " << total_time << "s" << endl;
  

total time: 96.8676s

如你所见,我确实可以做到,而据我所知,.ptr是最快的方法。但我不得不说我无法忍受find这个功能花费了我很多时间。任何人都可以告诉我一个最快的方法吗?

1 个答案:

答案 0 :(得分:1)

正如CrisLuengo的评论here,我已将99s增加到0.004s现在

Mat img(1000, 2000, CV_8UC1);
randu(img, Scalar(0), Scalar(255));
inRange(img, Scalar(160), Scalar(200), img);

Mat labels, stats, centroids;
int counts_img = connectedComponentsWithStats(img, labels, stats, centroids);

vector<int> drop_label(counts_img - 3);
iota(drop_label.begin(), drop_label.end(), 1);


vector<int> replace_table(counts_img);
iota(replace_table.begin(), replace_table.end(), 0);
for (int i : drop_label)
    replace_table[i] = 0;

//start to count the time.
double start_time = (double)getTickCount();

Mat select = img.clone() = 0;
int img_height = img.rows, img_width = img.cols;
for (int i = 0; i < img_height; i++) {
    int*plabels = labels.ptr<int>(i);
    uchar*pselect = select.ptr<uchar>(i);
    for (int j = 0; j < img_width; j++) {
        if (replace_table[plabels[j]] != 0)
            pselect[j] = 255;
    }
}

Mat result = img - select;

//total time
double total_time = ((double)getTickCount() - start_time) / getTickFrequency();
cout << "total time: " << total_time << "s" << endl;