# Asynchronous feature detection not running asynchronously

**URL:** <https://forum.opencv.org/t/asynchronous-feature-detection-not-running-asynchronously/10889>\
**Category:** C++\
**Tags:** feature-detection, orb, cuda\
**Created:** [November 9, 2022, 11:53am UTC](https://forum.opencv.org/t/asynchronous-feature-detection-not-running-asynchronously/10889 "2022-11-09T11:53:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChrisN](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/chrisn/32/5114_2.png) [@ChrisN](https://forum.opencv.org/u/ChrisN)\
**Post date:** [November 9, 2022, 11:53am UTC](https://forum.opencv.org/t/asynchronous-feature-detection-not-running-asynchronously/10889/1 "2022-11-09T11:53:20Z")

</div>

I have been experimenting with the asynchronous feature detection calls, but when I time them it seems they still block the CPU for a significant amount of time.

I tried to detect ORB features similar to the below:

```auto
void detectFeatures(cv::cuda::GpuMat& greyscaleImage)
{
    cv::Ptr<cv::cuda::ORB> orb = cv::cuda::ORB::create(300, 1.2f, 8, 31, 0, 2, 0, 31, 20, true);
    cv::cuda::Stream stream;
    cv::cuda::GpuMat keypoints, descriptors;
    m_rmatcher->getFeatureDetector()->detectAndComputeAsync(greyscaleImage, cv::noArray(), keypoints, descriptors, false, stream);
    stream.waitForCompletion();
}

```

I would expect the asynchronous call to defer most of the CPU time to `stream.waitForCompletion()` , however only around 2 ms is spent in that line, with `detectAndComputeAsync` still taking around 12 ms.

I tried separating the calls into detectAsync and computeAsync, and it looks like the blocking time is mostly spent in the detection part. I also tried FAST feature detection, and found a similar issue, with the majority of time being spent on the CPU. Turning off nonmaxsupression helped reduce the blocking time, but there doesn’t seem to be an option to modify this in the ORB detector and this might reduce the quality of the features.

I’ve tried various things such as running multiple times, changing various options and preallocating the memory for the keypoints and descriptors, however nothing seems to help.

Is there else I can look at in my setup, or is this expected and these functions are not really asynchronous?

---

<div class="post-metadata">

**Author:** ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)\
**Post date:** [November 9, 2022, 2:50pm UTC](https://forum.opencv.org/t/asynchronous-feature-detection-not-running-asynchronously/10889/2 "2022-11-09T14:50:39Z")

</div>

> [@ChrisN](#):
>
> Is there else I can look at in my setup, or is this expected and these functions are not really asynchronous?

I think the calling the function `detectAndComputeAsync` is a little misleading. I would guess it is because the CUDA feature detectors inherit from `Feature2DAsync` which have these method names or it is because at some point `Async` in the name meant that the function took CUDA streams. Anyway the function requires calls to `cudaStreamSynchronize` throughout which is causing the host side delay you are seeing.

---

<div class="post-metadata">

**Author:** ![ChrisN](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/chrisn/32/5114_2.png) [@ChrisN](https://forum.opencv.org/u/ChrisN)\
**Post date:** [November 10, 2022, 10:53am UTC](https://forum.opencv.org/t/asynchronous-feature-detection-not-running-asynchronously/10889/3 "2022-11-10T10:53:50Z")

</div>

Thanks for the explanation, good to know that this is expected.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [November 10, 2022, 8:57pm UTC](https://forum.opencv.org/t/asynchronous-feature-detection-not-running-asynchronously/10889/4 "2022-11-10T20:57:24Z")

</div>

related: [c++ - OpenCV asynchronous ORB feature detection function is blocking on the CPU - Stack Overflow](https://stackoverflow.com/questions/74316748/opencv-asynchronous-orb-feature-detection-function-is-blocking-on-the-cpu)
