# Own server for Java programms to use the NVidia GPU

**URL:** <https://forum.opencv.org/t/own-server-for-java-programms-to-use-the-nvidia-gpu/12978>\
**Category:** Android/Java\
**Created:** [April 27, 2023, 1:37pm UTC](https://forum.opencv.org/t/own-server-for-java-programms-to-use-the-nvidia-gpu/12978 "2023-04-27T13:37:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![trausch](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@trausch](https://forum.opencv.org/u/trausch)\
**Post date:** [April 27, 2023, 1:37pm UTC](https://forum.opencv.org/t/own-server-for-java-programms-to-use-the-nvidia-gpu/12978/1 "2023-04-27T13:37:29Z")

</div>

I use OpenCV under Java, I wanted to ask whether I can also use performance gains there if I compile OpenCV like this.

And the next question: Are all operations then shifted to the GPU, for example

> SURF detector = SURF.create(…

and

> detector.detectAndCompute(…

?

Are “normal” matrix operations also outsourced to the GPU?

And since the Tesla C2050 is no longer available, what do you buy for it today?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 28, 2023, 5:44am UTC](https://forum.opencv.org/t/own-server-for-java-programms-to-use-the-nvidia-gpu/12978/2 "2023-04-28T05:44:55Z")

</div>

> [@trausch](#):
>
> I use OpenCV under Java, I wanted to ask whether I can also use performance gains there if I compile OpenCV like this.

unfortunately, no (or, not so easy)  
you need to use special api’s / classes to use CUDA/OpenCL code, which are not exposed to the java api

IF you’re good with c++, you can still wrap your gpu pipeline into JNI code, and call that from java

> [@trausch](#):
>
> SURF detector = SURF.create(…

this is cpu only, to use the CUDA optimization, you need [a different api](https://docs.opencv.org/4.x/db/d06/classcv_1_1cuda_1_1SURF__CUDA.html) – but again, no java bindings ;(

---

<div class="post-metadata">

**Author:** ![trausch](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@trausch](https://forum.opencv.org/u/trausch)\
**Post date:** [April 28, 2023, 6:52am UTC](https://forum.opencv.org/t/own-server-for-java-programms-to-use-the-nvidia-gpu/12978/3 "2023-04-28T06:52:35Z")

</div>

> [@berak](#):
>
> you need to use special api’s / classes to use CUDA/OpenCL code, which are not exposed to the java api

I use the official API:

`// C++: void cv::Feature2D::detectAndCompute(Mat image, Mat mask, vector_KeyPoint& keypoints, Mat& descriptors, bool useProvidedKeypoints = false)`

So I have to write my own JNI so that I can access it via Java and then use these functions instead of the “official” ones?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 28, 2023, 7:08am UTC](https://forum.opencv.org/t/own-server-for-java-programms-to-use-the-nvidia-gpu/12978/4 "2023-04-28T07:08:03Z")

</div>

> [@trausch](#):
>
> I use the official API:
> 
> `// C++: void cv::Feature2D::detectAndCompute(Mat image, Mat mask, vector_KeyPoint& keypoints, Mat& descriptors, bool useProvidedKeypoints = false)`

that’s still the CPU API (you need GPUMat instead, look e.g. here:

> <https://github.com/opencv/opencv/blob/4.x/samples/gpu/surf_keypoint_matcher.cpp>

---

<div class="post-metadata">

**Author:** ![trausch](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@trausch](https://forum.opencv.org/u/trausch)\
**Post date:** [April 28, 2023, 7:09am UTC](https://forum.opencv.org/t/own-server-for-java-programms-to-use-the-nvidia-gpu/12978/5 "2023-04-28T07:09:04Z")

</div>

What do I have to consider specifically when I go NonFree?

Switch from  
cv::Feature2D Class Reference  
[2D Features Framework] » [Feature Detection and Description]

To:

cv::cuda::SURF\_CUDA Class Reference  
[Extra 2D Features Framework] » [Non-free 2D Features Algorithms]

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 28, 2023, 7:11am UTC](https://forum.opencv.org/t/own-server-for-java-programms-to-use-the-nvidia-gpu/12978/6 "2023-04-28T07:11:43Z")

</div>

> [@trausch](#):
>
> What do I have to consider specifically when I go NonFree?

you have to enable the nonfree code at build time of the libs:

```
cmake -DOPENCV_ENABLE_NONFREE=ON

```

(and ofc. need to build with the contrib repo, where all CUDA code was moved)

---

<div class="post-metadata">

**Author:** ![trausch](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@trausch](https://forum.opencv.org/u/trausch)\
**Post date:** [April 28, 2023, 7:12am UTC](https://forum.opencv.org/t/own-server-for-java-programms-to-use-the-nvidia-gpu/12978/7 "2023-04-28T07:12:19Z")

</div>

> [@berak](#):
>
> you need GPUMat instead, look e.g. here

Thanks for the tip, I will try it out in the next few weeks.

---

<div class="post-metadata">

**Author:** ![trausch](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@trausch](https://forum.opencv.org/u/trausch)\
**Post date:** [April 28, 2023, 7:14am UTC](https://forum.opencv.org/t/own-server-for-java-programms-to-use-the-nvidia-gpu/12978/8 "2023-04-28T07:14:50Z")

</div>

> [@berak](#):
>
> you have to enable the nonfree code at build time of the libs:

Ok, technically I know, I mean legally, if I don’t publish parts of my software but I want to use these non-public routines myself?
