Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to remove those specified label component by fastest method

Suppose I have an such binary image

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

I will use connectedComponentsWithStats to label the img

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

And I will specify some labels to remove from labels.

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

Of course I can do it will following method:

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;

As you see, I can do it indeed, and as I know, the .ptr is the fastest methd. but I have to say I cannot bear the function find cost my so many time. Any body can tell me a fastest method to do this?

How to remove those specified label component by fastest method

Cross post here


Suppose I have an such binary image

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

I will use connectedComponentsWithStats to label the img

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

And I will specify some labels to remove from labels.

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

Of course I can do it will with following method:

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;

As you see, I can do it indeed, and as I know, the .ptr is the fastest methd. but I have to say I cannot bear the function find cost my so many time. Any body can tell me a fastest method to do this? this?

How to remove those specified label component by fastest method

Cross post here


Suppose I have an such binary image

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

I will use connectedComponentsWithStats to label the img

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

And I will specify some labels to remove from labels.

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

Of course I can do it with following method:

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

As you see, I can do it indeed, and as I know, the .ptr is the fastest methd. but I have to say I cannot bear the function find cost my so many time. Any body can tell me a fastest method to do this?