Ask Your Question

Revision history [back]

No wonder it is slow, if every time you need to remove a contour you need to find ALL contours and verify which should be removed, it will be really slow.

Probably, somewhere in your code you already calculated the contours, there is no sense that a function for such a simple operations needs to define ALL contours of the image. So, the arguments that this function take in should be the already calculated contours that you are probably using somewhere else in the code.

If this is in fact the only operation where you need to use findContours, then there is small space for optimization. Still, there are things that can be done:

  1. Do not use .clone(). In that case it doesn't matter if findContours changes the original image, since you do not use it for anything else. Creating a hard copy takes longer that using the one that already exists.
  2. Instead of using drawContours() to remove the item, see if you can instead draw a rectangle on the contour's bounding box. Drawing a rectangle is many times faster that drawing a contour, and for many applications it wont cause any problems in the functionality.

Cheers.

No wonder it is slow, if every time you need to remove a contour you need to find ALL contours and verify which should be removed, it will be really slow.

Probably, somewhere in your code you already calculated the contours, there is no sense that a function for such a simple operations needs to define ALL contours of the image. So, the arguments that this function take in should be the already calculated contours that you are probably using somewhere else in the code.

If this is in fact the only operation where you need to use findContours, then there is small space for optimization. Still, there are things that can be done:

  1. Do not use .clone(). In that case it doesn't matter if findContours changes the original image, since you do not use it for anything else. Creating a hard copy takes longer that using the one that already exists.
  2. Instead of using drawContours() to remove the item, see if you can instead draw a rectangle on the contour's bounding box. Drawing a rectangle is many times faster that drawing a contour, and for many applications it wont cause any problems in the functionality.

Replace the line drawContours(output, contours, i, color, -1, 8); with

rectangle(output, cv::boundingRect(contours[i], color, -1, 8);

Cheers.

No wonder it is slow, if every time you need to remove a contour you need to find ALL contours and verify which should be removed, it will be really slow.

Probably, somewhere in your code you already calculated the contours, there is contours,it makes no sense that a function for such a simple operations operation needs to define find ALL contours of the image. So, the arguments that this function take in should be the already calculated contours that you are probably using somewhere else in the code.

If this is in fact the only operation where you need to use findContours, then there is small space for optimization. Still, there are things that can be done:

  1. Do not use .clone(). In that case it doesn't matter if findContours changes the original image, since you do not use it for anything else. Creating a hard copy takes longer that using the one that already exists.
  2. Instead of using drawContours() to remove the item, see if you can instead draw a rectangle on the contour's bounding box. Drawing a rectangle is many times faster that drawing a contour, and for many applications it wont cause any problems in the functionality.

Replace the line drawContours(output, contours, i, color, -1, 8); with

rectangle(output, cv::boundingRect(contours[i], color, -1, 8);

Cheers.

No wonder it is slow, if every time you need to remove a contour you need to find ALL contours and verify which should be removed, it will be really slow.

Probably, somewhere in your code you already calculated the contours,it makes no sense that a function for such a simple operation needs to find ALL contours of the image. So, the arguments that this function take in should be the already calculated contours that you are probably using somewhere else in the code.

If this is in fact the only operation where you need to use findContours, then there is small space for optimization. Still, there are things that can be done:

  1. Do not use .clone(). In that case it doesn't matter if findContours changes the original image, since you do not use it for anything else. Creating a hard copy takes longer that using the one that already exists.
  2. Instead of using drawContours() to remove the item, see if you can instead draw a rectangle on the contour's bounding box. Drawing a rectangle is many times faster that drawing a contour, and for many applications it wont cause any problems in the functionality.

Replace the line drawContours(output, contours, i, color, -1, 8); with

rectangle(output, cv::boundingRect(contours[i], cv::boundingRect(contours[i]), color, -1, 8);

Cheers.