# CUDA Treshold does not seem to work

**URL:** <https://forum.opencv.org/t/cuda-treshold-does-not-seem-to-work/17480>\
**Category:** Python\
**Tags:** cuda\
**Created:** [April 30, 2024, 3:18pm UTC](https://forum.opencv.org/t/cuda-treshold-does-not-seem-to-work/17480 "2024-04-30T15:18:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zadkiel](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/zadkiel/32/10375_2.png) [@Zadkiel](https://forum.opencv.org/u/Zadkiel)\
**Post date:** [April 30, 2024, 3:18pm UTC](https://forum.opencv.org/t/cuda-treshold-does-not-seem-to-work/17480/1 "2024-04-30T15:18:35Z")

</div>

Hi, i’m trying to get tresholding to work with CUDA. I’ve tried a few different approaches and conversions to different types, but nothing seems to solve it.  
I’m working off this definition

```auto
double cv::cuda::threshold (InputArray src, OutputArray dst, double thresh, double maxval, int type, Stream &stream=Stream::Null())
 	Applies a fixed-level threshold to each array element.

```

Test Code

```auto
import cv2

frame = cv2.imread("001.jpg")

frame_gpu = cv2.cuda_GpuMat()
frame_gpu.upload(frame)
frame_gpu = frame_gpu.convertTo(cv2.CV_8UC1)

dst_gpu = cv2.cuda_GpuMat(frame_gpu.size(), cv2.CV_8UC1)

dst_gpu = cv2.cuda.threshold(frame_gpu, dst_gpu, 128, 255, cv2.THRESH_BINARY)
print(dst_gpu)

```

Produces this error

```auto
cv2.error: OpenCV(4.9.0) :-1: error: (-5:Bad argument) in function 'threshold'
> Overload resolution failed:
> - src is not a numpy array, neither a scalar
> - Expected Ptr<cv::cuda::GpuMat> for argument 'dst'
> - Expected Ptr<cv::UMat> for argument 'src'

```

 ![001](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/e/ee913fde7d6911497fd6b31ccc874181bbfb167d.jpeg)

I’m probably missing something obvious haha but i’d greatly appreciate any help.  
The code i’ve submitted is just the minimal code needed to reproduce, the idea was using the tresholding for harris corner detection, if anyone has any tricks for that too i’d greatly appreciate it.

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [April 30, 2024, 3:39pm UTC](https://forum.opencv.org/t/cuda-treshold-does-not-seem-to-work/17480/2 "2024-04-30T15:39:32Z")

</div>

I don’t know where is doc but this line does not give an error

dst\_gpu = cv.cuda.threshold(frame\_gpu,128, 255, cv.THRESH\_BINARY)

May be you must use [doc threshold](https://docs.opencv.org/4.x/d7/d1b/group __imgproc__ misc.html#gae8a4a146d1ca78c626a53577199e9c57) doc ?

---

<div class="post-metadata">

**Author:** ![Zadkiel](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/zadkiel/32/10375_2.png) [@Zadkiel](https://forum.opencv.org/u/Zadkiel)\
**Post date:** [April 30, 2024, 4:16pm UTC](https://forum.opencv.org/t/cuda-treshold-does-not-seem-to-work/17480/3 "2024-04-30T16:16:04Z")

</div>

Thank you, that solved it!

---

<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:** [May 1, 2024, 6:47am UTC](https://forum.opencv.org/t/cuda-treshold-does-not-seem-to-work/17480/4 "2024-05-01T06:47:05Z")

</div>

@Zadkiel When you know the function you want to use, check the help docs for the function signature so you know which arguments it expects

```auto
>>> help(cv.cuda.threshold)
Help on built-in function threshold:

threshold(...)
    threshold(src, thresh, maxval, type[, dst[, stream]]) -> retval, dst
    . @brief Applies a fixed-level threshold to each array element.
    .
    . @param src Source array (single-channel).
    . @param dst Destination array with the same size and type as src .
    . @param thresh Threshold value.
    . @param maxval Maximum value to use with THRESH_BINARY and THRESH_BINARY_INV threshold types.
    . @param type Threshold type. For details, see threshold . The THRESH_OTSU and THRESH_TRIANGLE
    . threshold types are not supported.
    . @param stream Stream for the asynchronous version.
    .
    . @sa threshold

```

In your case both

```auto
1)
ret, dst_gpu = cv.cuda.threshold(frame_gpu,128, 255, cv.THRESH_BINARY)

```

and

```auto
2)
dst_gpu = cv2.cuda_GpuMat(frame_gpu.size(), cv2.CV_8UC1)
cv.cuda.threshold(frame_gpu,128, 255, cv.THRESH_BINARY, dst = dst_gpu )

```

will work. The only difference is that the first approach will allocate a new `dst_gpu` on the GPU each time it is called resulting in a slight performance hit.
