Repository: opencv
Issue: cv::cuda::remap is not working correctly and there may be an error？
Labels: bug,category: gpu/cuda (contrib)


##### System information (version)
win10 vcpkg build

- General configuration for OpenCV 4.5.1 =====================================
--   Version control:               unknown
-- 
--   Extra modules:
--     Location (extra):            D:/vcpkg/buildtrees/opencv4/src/4.5.1-0a6ce0443d.clean/modules
--     Version control (extra):     unknown
-- 
--   Platform:
--     Timestamp:                   2021-03-10T15:05:17Z
--     Host:                        Windows 10.0.19041 AMD64
--     CMake:                       3.19.4
--     CMake generator:             Ninja
--     CMake build tool:            C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/Ninja/ninja.exe
--     MSVC:                        1928
--     Configuration:               Debug
-- 
--   CPU/HW features:
--     Baseline:                    SSE SSE2 SSE3
--       requested:                 SSE3
--     Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
--       requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
--       SSE4_1 (14 files):         + SSSE3 SSE4_1
--       SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
--       FP16 (0 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
--       AVX (4 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
--       AVX2 (28 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
--       AVX512_SKX (4 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
-- 
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++ standard:                11
--     C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe  (ver 19.28.29910.0)
--     C++ flags (Release):         /nologo /DWIN32 /D_WINDOWS /W4 /utf-8 /GR /MP   /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise /FS     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 -openmp  /MD /O2 /Oi /Gy /DNDEBUG /Z7  
--     C++ flags (Debug):           /nologo /DWIN32 /D_WINDOWS /W4 /utf-8 /GR /MP   /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise /FS     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 -openmp  /D_DEBUG /MDd /Z7 /Ob0 /Od /RTC1  
--     C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe
--     C flags (Release):           /nologo /DWIN32 /D_WINDOWS /W3 /utf-8 /MP   /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise /FS     -openmp   /MD /O2 /Oi /Gy /DNDEBUG /Z7  
--     C flags (Debug):             /nologo /DWIN32 /D_WINDOWS /W3 /utf-8 /MP   /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise /FS     -openmp /D_DEBUG /MDd /Z7 /Ob0 /Od /RTC1  
--     Linker flags (Release):      /machine:x64  /nologo /DEBUG /INCREMENTAL:NO /OPT:REF /OPT:ICF    /debug
--     Linker flags (Debug):        /machine:x64  /nologo    /debug /INCREMENTAL 
--     ccache:                      NO
--     Precompiled headers:         NO
--     Extra dependencies:          cudart_static.lib nppc.lib nppial.lib nppicc.lib nppicom.lib nppidei.lib nppif.lib nppig.lib nppim.lib nppist.lib nppisu.lib nppitc.lib npps.lib cublas.lib cudnn.lib cufft.lib -LIBPATH:"C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/lib/x64" -LIBPATH:"D:/vcpkg/installed/x64-windows/lib"
--     3rdparty dependencies:
-- 
--   OpenCV modules:
--     To be built:                 alphamat aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann fuzzy hdf hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot quality rapid reg saliency shape stereo stitching structured_light superres surface_matching text tracking video videoio videostab viz xfeatures2d ximgproc xobjdetect xphoto
--     Disabled:                    rgbd world
--     Disabled by dependency:      -
--     Unavailable:                 cnn_3dobj cvv freetype gapi java julia matlab ovis python2 python3 sfm ts
--     Applications:                -
--     Documentation:               NO
--     Non-free algorithms:         YES
-- 
--   Windows RT support:            NO
-- 
--   GUI: 
--     Win32 UI:                    YES
--     VTK support:                 YES (ver 9.0.1)
-- 
--   Media I/O: 
--     ZLib:                        optimized D:/vcpkg/installed/x64-windows/lib/zlib.lib debug D:/vcpkg/installed/x64-windows/debug/lib/zlibd.lib (ver 1.2.11)
--     JPEG:                        optimized D:/vcpkg/installed/x64-windows/lib/jpeg.lib debug D:/vcpkg/installed/x64-windows/debug/lib/jpegd.lib (ver 62)
--     WEBP:                        optimized D:/vcpkg/installed/x64-windows/lib/webp.lib debug D:/vcpkg/installed/x64-windows/debug/lib/webpd.lib (ver 1.1.0)
--     PNG:                         optimized D:/vcpkg/installed/x64-windows/lib/libpng16.lib debug D:/vcpkg/installed/x64-windows/debug/lib/libpng16d.lib (ver 1.6.37)
--     TIFF:                        optimized D:/vcpkg/installed/x64-windows/lib/tiff.lib debug D:/vcpkg/installed/x64-windows/debug/lib/tiffd.lib optimized D:/vcpkg/installed/x64-windows/lib/lzma.lib debug D:/vcpkg/installed/x64-windows/debug/lib/lzmad.lib optimized D:/vcpkg/installed/x64-windows/lib/jpeg.lib debug D:/vcpkg/installed/x64-windows/debug/lib/jpegd.lib optimized D:/vcpkg/installed/x64-windows/lib/zlib.lib debug D:/vcpkg/installed/x64-windows/debug/lib/zlibd.lib optimized D:/vcpkg/installed/x64-windows/lib/lzma.lib debug D:/vcpkg/installed/x64-windows/debug/lib/lzmad.lib optimized D:/vcpkg/installed/x64-windows/lib/jpeg.lib debug D:/vcpkg/installed/x64-windows/debug/lib/jpegd.lib optimized D:/vcpkg/installed/x64-windows/lib/zlib.lib debug D:/vcpkg/installed/x64-windows/debug/lib/zlibd.lib (ver 42 / 4.1.0)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
-- 
--   Video I/O:
--     DC1394:                      NO
--     FFMPEG:                      NO (find_package)
--       avcodec:                   YES (4.3.1)
--       avformat:                  YES (4.3.1)
--       avutil:                    YES (4.3.1)
--       swscale:                   YES (4.3.1)
--       avresample:                YES (4.3.1)
--     GStreamer:                   NO
--     DirectShow:                  YES
--     Media Foundation:            YES
--       DXVA:                      YES
-- 
--   Parallel framework:            TBB (ver 2020.3 interface 11103)
-- 
--   Trace:                         YES (built-in)
-- 
--   Other third-party libraries:
--     Intel IPP:                   2020.0.0 Gold [2020.0.0]
--            at:                   D:/vcpkg/buildtrees/opencv4/x64-windows-dbg/3rdparty/ippicv/ippicv_win/icv
--     Intel IPP IW:                NO
--     Eigen:                       YES (ver 3.3.9)
--     Custom HAL:                  NO
--     Protobuf:                    D:/vcpkg/installed/x64-windows/lib/libprotobuf.lib (3.14.0)
-- 
--   NVIDIA CUDA:                   YES (ver 10.1, CUFFT CUBLAS)
--     NVIDIA GPU arch:             30 35 37 50 52 60 61 70 75
--     NVIDIA PTX archs:
-- 
--   cuDNN:                         YES (ver 7.6.0)
-- 
--   OpenCL:                        YES (NVD3D11)
--     Include path:                D:/vcpkg/buildtrees/opencv4/src/4.5.1-e92f755db3.clean/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python (for build):            NO
-- 
--   Install to:                    D:/vcpkg/packages/opencv4_x64-windows/debug
-- -----------------------------------------------------------------

*** CUDA Device Query (Runtime API) version (CUDART static linking) ***

Device count: 1

Device 0: "GeForce GTX 1650 with Max-Q Design"
  CUDA Driver Version / Runtime Version          11.0 / 10.10
  CUDA Capability Major/Minor version number:    7.5
  Total amount of global memory:                 4096 MBytes (4294967296 bytes)
  GPU Clock Speed:                               1.35 GHz
  Max Texture Dimension Size (x,y,z)             1D=(131072), 2D=(131072,65536), 3D=(16384,16384,16384)
  Max Layered Texture Size (dim) x layers        1D=(32768) x 2048, 2D=(32768,32768) x 2048
  Total amount of constant memory:               65536 bytes
  Total amount of shared memory per block:       49152 bytes
  Total number of registers available per block: 65536
  Warp size:                                     32
  Maximum number of threads per block:           1024
  Maximum sizes of each dimension of a block:    1024 x 1024 x 64
  Maximum sizes of each dimension of a grid:     2147483647 x 65535 x 65535
  Maximum memory pitch:                          2147483647 bytes
  Texture alignment:                             512 bytes
  Concurrent copy and execution:                 Yes with 6 copy engine(s)
  Run time limit on kernels:                     Yes
  Integrated GPU sharing Host Memory:            No
  Support host page-locked memory mapping:       Yes
  Concurrent kernel execution:                   Yes
  Alignment requirement for Surfaces:            Yes
  Device has ECC support enabled:                No
  Device is using TCC driver mode:               No
  Device supports Unified Addressing (UVA):      Yes
  Device PCI Bus ID / PCI location ID:           1 / 0
  Compute Mode:
      Default (multiple host threads can use ::cudaSetDevice() with device simultaneously)

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version  = 11.0, CUDA Runtime Version = 10.10, NumDevs = 1

##### Detailed description

I used cv::remap and cv::cuda::remap to keep the images undistorted. I found that cv::remap returned a good undistorted image, but  cv::cuda::remap did not return the correct image. Is this a bug？

##### Steps to reproduce

 ```.cpp
#include <iostream>
#include <string>
#include <opencv2/opencv.hpp>
#include <opencv2/core/persistence.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/cudawarping.hpp>
using namespace std;
using namespace cv;

Mat remap(const cv::Mat& img, const cv::Mat& map1, const cv::Mat& map2)
{
	Mat res;
	cv::remap(img, res, map1, map2, INTER_LINEAR);
	return res;
}

Mat remapGpuMat(const cv::Mat& img, const cv::cuda::GpuMat& xmap, const cv::cuda::GpuMat& ymap)
{
	Mat res;
	cuda::GpuMat gimg(img);
	cuda::remap(gimg, gimg, xmap, ymap, INTER_LINEAR);
	gimg.download(res);
	return res;
}

int main()
{
	//cuda::printCudaDeviceInfo(cuda::getDevice());
	VideoCapture cap(R"(D:\192.168.0.62_01_20210115105415939.mp4)");
	string paramFile = R"(D:\code\C++\RemapTest\test\distort_192_168_0_62.yml)";
	FileStorage fs(paramFile, FileStorage::READ);
	Mat cameraMatrix = fs["camera_matrix"].mat();
	Mat distCoeffs = fs["distortion_coefficients"].mat();
	Size imageSize = { 1920,1080 };
	Mat newCameraMatrix = getOptimalNewCameraMatrix(cameraMatrix,distCoeffs,
		imageSize, 0, imageSize, nullptr, true);
	Mat map1, map2;
	initUndistortRectifyMap(cameraMatrix,distCoeffs,
		Mat(), newCameraMatrix,imageSize,
		CV_32FC1,map1, map2);
	cuda::GpuMat gmap1(map1);
	cuda::GpuMat gmap2(map2);
	while (true) {
		Mat frame;
		cap >> frame;
		if (frame.empty())
			break;
		imwrite("raw.jpg", frame);
		Mat reframe = remap(frame, map1, map2);
		imwrite("cpu_ramap.jpg", reframe);
		Mat gpuFrame = remapGpuMat(frame, gmap1, gmap2);
		imwrite("gpu_ramap.jpg", gpuFrame);
		//remap(frame, map1, map2);
		resize(frame, frame, { 1280,720 });
		imshow("camera", frame);
		if (waitKey(10) == 27)
			break;
		break;
	}
	return 0;
}
```
### raw image
![raw](https://user-images.githubusercontent.com/17083562/110726494-73682180-8254-11eb-90cc-aca81adf1800.jpg)
### cv::remap image
![cpu_ramap](https://user-images.githubusercontent.com/17083562/110726541-84189780-8254-11eb-8dc9-5dfa700567f7.jpg)
### cv::cuda::remap image
![gpu_ramap](https://user-images.githubusercontent.com/17083562/110726559-909cf000-8254-11eb-8977-1beb7bf014c0.jpg)
### camera param file
[distort_192_168_0_62.zip](https://github.com/opencv/opencv/files/6120046/distort_192_168_0_62.zip)