# GPU Affine Warp

**URL:** <https://forum.opencv.org/t/gpu-affine-warp/7443>\
**Category:** C++\
**Tags:** cuda, imgproc\
**Created:** [February 4, 2022, 3:04pm UTC](https://forum.opencv.org/t/gpu-affine-warp/7443 "2022-02-04T15:04:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BIOS\_The\_Zerg](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/bios_the_zerg/32/2098_2.png) [@BIOS\_The\_Zerg](https://forum.opencv.org/u/BIOS_The_Zerg)\
**Post date:** [February 4, 2022, 3:04pm UTC](https://forum.opencv.org/t/gpu-affine-warp/7443/1 "2022-02-04T15:04:04Z")

</div>

Heya! I’m trying to write some code using `cv::cuda::warpAffine` and although it seems to be working fine, I’d like to have some guarantees that I understand correctly what is happening.

For one, in most tutorials online (e.g. [OpenCV: Affine Transformations](https://docs.opencv.org/3.4/d4/d61/tutorial_warp_affine.html)) I find people setting the `size` parameter just to the size of the `dst` image. I’m very happy to do that, but can I be in such a case certain that it’s not going to reallocate the underlying data? I already have the data allocated (the `Mat` object is just a shallow wrapper on top of it) and want to have the result in the memory where I expect it; is that going to always happen?

Second question is about in-place safety. If my `src` and `dst` are the same image, will OpenCV sort any possible issues for me, or do I need to deal with that explicitly in my wrapper function? The CPU-based warp ([OpenCV: Geometric Image Transformations](https://docs.opencv.org/4.x/da/d54/group __imgproc__ transform.html#ga0203d9ee5fcd28d40dbc4a1ea4451983)) can NOT operate in-place, can the GPU variant?

Many thanks!

---

<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:** [February 4, 2022, 3:19pm UTC](https://forum.opencv.org/t/gpu-affine-warp/7443/2 "2022-02-04T15:19:22Z")

</div>

seing your current code (snippet) would be helpful, esp. this part:

> [@BIOS\_The\_Zerg](#):
>
> I already have the data allocated (the `Mat` object is just a shallow wrapper on top of it)

---

<div class="post-metadata">

**Author:** ![BIOS\_The\_Zerg](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/bios_the_zerg/32/2098_2.png) [@BIOS\_The\_Zerg](https://forum.opencv.org/u/BIOS_The_Zerg)\
**Post date:** [February 4, 2022, 3:38pm UTC](https://forum.opencv.org/t/gpu-affine-warp/7443/3 "2022-02-04T15:38:33Z")

</div>

Oh, that one is nothing fancy just a little:

```auto
cv::cuda::GpuMat dMat(
      dImg.height(),
      dImg.width(),
      CV_MAKETYPE(cv::traits::Depth<float>::value, 3),
      static_cast<void*>(dImg.deviceData()),
      dImg.pitch()
);

```

where `dImg` is of my GpuImage class.  
(it’s in a templated function, so the `float` and `3` are not usually fixed, they’re just here for sake of simplicity!)

P.S. it should be a `reinterpret_cast`, shouldn’t it? 😛
