Btc Sources
Btc Sources

Reputation: 2061

Erode is too slow - Opencv

I'm applying an erode operation to a Mat, but it's being extremely slow.

Since in OpenCV you can apply it by two different ways (using morphologyEx and using erode).

I'm measuring (approximately) how fast does it runs, but both them are being extremely slow!

The code for this part is:

    int morph_size = 20;
    Mat element = getStructuringElement( MORPH_ELLIPSE, cv::Size( 2*morph_size + 1 , 2*morph_size +1), cv::Point( morph_size, morph_size ) );

    // morphologyEx(imgMascara, imgMascara, MORPH_ERODE, element );
finish_time5 = clock();

    erode( imgMascara, imgMascara, element );

finish_time6 = clock();

Where imgMascara is a Mat. Then, When I'm measuring the time since the start to finish_time5 and finish_time6, I'm getting:

0.0198s
0.887s

Any idea about why does it takes too much time? (It happen for both the functions, morphologyEx and erode).

Thank you in advance

Upvotes: 2

Views: 4623

Answers (1)

Btc Sources
Btc Sources

Reputation: 2061

I've found the reason of the lentitude of the erode. There're three different reasons:

  1. As @Micka said, the kernel size is big. The more I reduce it, the more faster it becomes.

However, I can't do this, because I need this kernel size. So I continued looking for another reason, and found out:

  1. The kernel type (MORPH_ELLIPSE). If I change it to MORPH_RECT or MORPH_CROSS, it becomes drastically faster.

Since my app works great with all them, I just changed it to MORP_RECT.

  1. Debug -> Release: As I as told by @drescherjm, I've configured my project to run in release mode (plus depencencies from debug libs to release ones), and it helped the performance too. Not so much as the second reason, but a nice shot.

Thank you all for your comments that helped me to find out these

Upvotes: 11

Related Questions