<h5 dir="auto">System information (version)</h5>

<h5 dir="auto">Detailed description</h5>
<p dir="auto">Actually, I have read the official documentation <a href="https://github.com/opencv/opencv_contrib/blob/master/modules/reg/samples/map_test.cpp">here</a> about class <code class="notranslate">Map</code> in opencv to try to use the module <code class="notranslate">reg</code>. And This is my test image:</p>
<p dir="auto"><a href="https://i.stack.imgur.com/RuesS.jpg" rel="nofollow"><img src="https://camo.githubusercontent.com/d51a75ea182b0018c617422801bd14d5fbe4ee2e91fa342882d1a0c1ef4a9b81/68747470733a2f2f692e737461636b2e696d6775722e636f6d2f52756573532e6a7067" alt="enter image description here" data-canonical-src="https://i.stack.imgur.com/RuesS.jpg" style="max-width: 100%;"></a></p>
<p dir="auto">This is my code:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="#include&lt;opencv.hpp&gt;
#include &quot;opencv2/reg/mapshift.hpp&quot;
#include &quot;opencv2/reg/mappergradshift.hpp&quot;
#include &quot;opencv2/reg/mapperpyramid.hpp&quot;

using namespace cv;
using namespace std;
using namespace cv::reg;

Mat highlight1(const Mat src, const Mat t_mask) {
	Mat srcImg = src.clone(), mask = t_mask.clone();
	threshold(mask, mask, 0, 255, THRESH_BINARY_INV + THRESH_OTSU);
	cvtColor(mask, mask, COLOR_GRAY2BGR);
	cvtColor(srcImg, srcImg, COLOR_GRAY2BGR);
	dilate(mask - Scalar(0, 0, 255), mask, Mat(), Point(-1, -1), 1);
	return srcImg - mask;
}

int main() {
	Mat img1 = imread(&quot;img.jpg&quot;, 0);
	Mat img2;

	// Warp original image
	Vec&lt;double, 2&gt; shift(5., 5.);
	MapShift mapTest(shift);
	mapTest.warp(img1, img2);

	// Register
	Ptr&lt;MapperGradShift&gt; mapper = makePtr&lt;MapperGradShift&gt;();
	MapperPyramid mappPyr(mapper);
	Ptr&lt;Map&gt; mapPtr = mappPyr.calculate(img1, img2);
	MapShift* mapShift = dynamic_cast&lt;MapShift*&gt;(mapPtr.get());

	// Display registration result
	Mat result;
	mapShift-&gt;inverseWarp(img2, result);
	Mat registration_before = highlight1(img1, img2);
	Mat registration_after = highlight1(img1, result);
	return 0;
}"><pre class="notranslate"><code class="notranslate">#include&lt;opencv.hpp&gt;
#include "opencv2/reg/mapshift.hpp"
#include "opencv2/reg/mappergradshift.hpp"
#include "opencv2/reg/mapperpyramid.hpp"

using namespace cv;
using namespace std;
using namespace cv::reg;

Mat highlight1(const Mat src, const Mat t_mask) {
	Mat srcImg = src.clone(), mask = t_mask.clone();
	threshold(mask, mask, 0, 255, THRESH_BINARY_INV + THRESH_OTSU);
	cvtColor(mask, mask, COLOR_GRAY2BGR);
	cvtColor(srcImg, srcImg, COLOR_GRAY2BGR);
	dilate(mask - Scalar(0, 0, 255), mask, Mat(), Point(-1, -1), 1);
	return srcImg - mask;
}

int main() {
	Mat img1 = imread("img.jpg", 0);
	Mat img2;

	// Warp original image
	Vec&lt;double, 2&gt; shift(5., 5.);
	MapShift mapTest(shift);
	mapTest.warp(img1, img2);

	// Register
	Ptr&lt;MapperGradShift&gt; mapper = makePtr&lt;MapperGradShift&gt;();
	MapperPyramid mappPyr(mapper);
	Ptr&lt;Map&gt; mapPtr = mappPyr.calculate(img1, img2);
	MapShift* mapShift = dynamic_cast&lt;MapShift*&gt;(mapPtr.get());

	// Display registration result
	Mat result;
	mapShift-&gt;inverseWarp(img2, result);
	Mat registration_before = highlight1(img1, img2);
	Mat registration_after = highlight1(img1, result);
	return 0;
}
</code></pre></div>
<p dir="auto">But as we see, the <code class="notranslate">registration_after</code> is even worse than <code class="notranslate">registration_before</code>. What's I have missed?</p>
<p dir="auto">This is <code class="notranslate">registration_before</code>:</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/9b6fd67d00a793c519a7c3056c330d979ce60b4dabf1fea19d94198a7d9987c8/68747470733a2f2f692e737461636b2e696d6775722e636f6d2f626b6e30662e706e67"><img src="https://camo.githubusercontent.com/9b6fd67d00a793c519a7c3056c330d979ce60b4dabf1fea19d94198a7d9987c8/68747470733a2f2f692e737461636b2e696d6775722e636f6d2f626b6e30662e706e67" alt="" data-canonical-src="https://i.stack.imgur.com/bkn0f.png" style="max-width: 100%;"></a></p>
<p dir="auto">This is <code class="notranslate">registration_after</code>:</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/f014ce7fbf9bf827b2712a718bfe15099f30cab931bc80c6cc013df2b45cc8c8/68747470733a2f2f692e737461636b2e696d6775722e636f6d2f684f5742662e706e67"><img src="https://camo.githubusercontent.com/f014ce7fbf9bf827b2712a718bfe15099f30cab931bc80c6cc013df2b45cc8c8/68747470733a2f2f692e737461636b2e696d6775722e636f6d2f684f5742662e706e67" alt="" data-canonical-src="https://i.stack.imgur.com/hOWBf.png" style="max-width: 100%;"></a></p>
<p dir="auto">Why I get a worse result? Is it a bug in this module of OpenCV?</p>
<h5 dir="auto">System information (version)</h5>

<ul dir="auto">
<li>OpenCV =&gt; 4.2.0-dev</li>
<li>Operating System / Platform =&gt; Ubuntu 18.04</li>
<li>Compiler =&gt; GNU G++ 7.4.0</li>
<li>Python =&gt; 3.6.9</li>
</ul>
<h5 dir="auto">Detailed description</h5>
<p dir="auto">When I run <strong>self.frame_device.upload(self.frame, stream)</strong> I'm getting an extra <em>cudaStreamCreate</em> and <em>cudaStreamDestroy</em> right before the <em>cudaMemcpy2DAsync</em>. (See attached)<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/50021634/74594782-014ceb00-5008-11ea-9b89-a381021ecde5.png"><img src="https://user-images.githubusercontent.com/50021634/74594782-014ceb00-5008-11ea-9b89-a381021ecde5.png" alt="extraStream" style="max-width: 100%;"></a></p>
<h5 dir="auto">Steps to reproduce</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import os
import sys
import glob
import numpy as np
import time
import cv2
import cudasift

class PreCuda:
    def __init__(self, size):
        print(&quot;__init__&quot;)
        self.rows = size
        self.cols = size
        self.stream = cv2.cuda_Stream( )
        self.frame = np.zeros((size,size,3),np.uint8)
        cv2.cuda.registerPageLocked(self.frame)
        self.frame_device = cv2.cuda_GpuMat(size,size,cv2.CV_8UC3)

    def __del__(self):
        print(&quot;unregisterPageLocked&quot;)
        cv2.cuda.unregisterPageLocked(self.frame)

    def ProcessFrame(self):
        print(&quot;ProcessFrame&quot;)
        self.frame_device.upload(self.frame, self.stream) # &lt;-- issue

    def Frame(self, img):
        print(&quot;Frame&quot;)
        img = cv2.imread(image_name)
        img.resize(self.rows, self.cols, 3)
        np.copyto(self.frame,img)

image_names = glob.glob('images/*')


size = 512
image_data = PreCuda(size)

for image_name in image_names:
    image_data.Frame(image_name)
    image_data.ProcessFrame()"><pre class="notranslate"><code class="notranslate">import os
import sys
import glob
import numpy as np
import time
import cv2
import cudasift

class PreCuda:
    def __init__(self, size):
        print("__init__")
        self.rows = size
        self.cols = size
        self.stream = cv2.cuda_Stream( )
        self.frame = np.zeros((size,size,3),np.uint8)
        cv2.cuda.registerPageLocked(self.frame)
        self.frame_device = cv2.cuda_GpuMat(size,size,cv2.CV_8UC3)

    def __del__(self):
        print("unregisterPageLocked")
        cv2.cuda.unregisterPageLocked(self.frame)

    def ProcessFrame(self):
        print("ProcessFrame")
        self.frame_device.upload(self.frame, self.stream) # &lt;-- issue

    def Frame(self, img):
        print("Frame")
        img = cv2.imread(image_name)
        img.resize(self.rows, self.cols, 3)
        np.copyto(self.frame,img)

image_names = glob.glob('images/*')


size = 512
image_data = PreCuda(size)

for image_name in image_names:
    image_data.Frame(image_name)
    image_data.ProcessFrame()
</code></pre></div>
<p dir="auto">--&gt;</p>