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

<ul dir="auto">
<li>OpenCV =&gt; 4.1.2</li>
</ul>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="-- General configuration for OpenCV 4.1.2 =====================================
--   Version control:               unknown
-- 
--   Platform:
--     Timestamp:                   2019-10-27T07:15:40Z
--     Host:                        Linux 5.3.6-1-MANJARO x86_64
--     CMake:                       3.15.4
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               Release
-- "><pre class="notranslate"><code class="notranslate">-- General configuration for OpenCV 4.1.2 =====================================
--   Version control:               unknown
-- 
--   Platform:
--     Timestamp:                   2019-10-27T07:15:40Z
--     Host:                        Linux 5.3.6-1-MANJARO x86_64
--     CMake:                       3.15.4
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               Release
-- 
</code></pre></div>
<p dir="auto">Docker image running Alpine Linux Edge, gcc 9.2.0, cmake</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[ 39%] Linking CXX executable ../../bin/opencv_version
/usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/../../../../x86_64-alpine-linux-musl/bin/ld: ../../lib/libopencv_core.so.4.1.2: undefined reference to `cblas_sgemm'
/usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/../../../../x86_64-alpine-linux-musl/bin/ld: ../../lib/libopencv_core.so.4.1.2: undefined reference to `cblas_zgemm'
/usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/../../../../x86_64-alpine-linux-musl/bin/ld: ../../lib/libopencv_core.so.4.1.2: undefined reference to `cblas_cgemm'
/usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/../../../../x86_64-alpine-linux-musl/bin/ld: ../../lib/libopencv_core.so.4.1.2: undefined reference to `cblas_dgemm'"><pre class="notranslate"><code class="notranslate">[ 39%] Linking CXX executable ../../bin/opencv_version
/usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/../../../../x86_64-alpine-linux-musl/bin/ld: ../../lib/libopencv_core.so.4.1.2: undefined reference to `cblas_sgemm'
/usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/../../../../x86_64-alpine-linux-musl/bin/ld: ../../lib/libopencv_core.so.4.1.2: undefined reference to `cblas_zgemm'
/usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/../../../../x86_64-alpine-linux-musl/bin/ld: ../../lib/libopencv_core.so.4.1.2: undefined reference to `cblas_cgemm'
/usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/../../../../x86_64-alpine-linux-musl/bin/ld: ../../lib/libopencv_core.so.4.1.2: undefined reference to `cblas_dgemm'
</code></pre></div>
<p dir="auto">I think cmake should error or not compile that module if CBLAS is not found.</p>
<h5 dir="auto">System information (version)</h5>

<ul dir="auto">
<li>opencv-python version =&gt; 4.2.0.32</li>
<li>Operating System / Platform =&gt; Windows 64 Bit</li>
</ul>
<h5 dir="auto">Detailed description</h5>
<p dir="auto">I think there is a bug when using cv2.UMat with cv2.calcOpticalFlowPyrLK.</p>
<p dir="auto">When running the code taken from <a href="https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_video/py_lucas_kanade/py_lucas_kanade.html" rel="nofollow">https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_video/py_lucas_kanade/py_lucas_kanade.html</a> , it works as expected.</p>
<p dir="auto">However, after attempting to use cv2.UMat with it, the points are scaled smaller with each frame as seen in the pictures on the left below. The right side pictures are obtained without using cv2.UMat (points drawn in green).<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/48981455/76056157-c7408a80-5fb0-11ea-8d4b-5095d5dc1a6a.png"><img src="https://user-images.githubusercontent.com/48981455/76056157-c7408a80-5fb0-11ea-8d4b-5095d5dc1a6a.png" alt="Untitled" style="max-width: 100%;"></a></p>
<p dir="auto">Also, I noticed that changing maxLevel seems to affect the rate at which the points are scaled. Multiplying the new points by the value gives the correct position but after running for a while the points disappear.</p>
<ul dir="auto">
<li>maxLevel = 0, pts are 2 times smaller than expected</li>
<li>maxLevel = 1, pts are 4 times smaller than expected</li>
<li>maxLevel = 2, pts are 8 times smaller than expected</li>
</ul>
<p dir="auto">I've tried using other versions of opencv-python such as 4.0.0.21 or 3.4.9.21 which produced the same results.</p>
<h5 dir="auto">Steps to reproduce</h5>

<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
import cv2

cap = cv2.VideoCapture('traffic_video.mp4')

# params for ShiTomasi corner detection
feature_params = dict( maxCorners=100,
                       qualityLevel=0.03,
                       minDistance=7,
                       blockSize=7)

# Parameters for lucas kanade optical flow
lk_params = dict( winSize=(15,15),
                  maxLevel=0,
                  criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))

# Take first frame and find corners in it
ret, old_frame = cap.read()
old_frame = cv2.UMat(old_frame)
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
p0 = cv2.goodFeaturesToTrack(old_gray, mask=None, **feature_params)
p0 = cv2.UMat(np.array(sorted(p0.get(), key=lambda p: tuple(p[0]))))

while(1):

    ret,frame = cap.read()
    frame = cv2.UMat(frame)
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Calculate optical flow
    p1, st, err = list(map(lambda umat: umat.get(), cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)))

    for i in p1:
        x, y = i.ravel()
        cv2.circle(frame, (x, y), 4, (0, 0, 255), -1)
    good_new = p1[st == 1]
    p0 = cv2.UMat(good_new.reshape(-1, 1, 2))
    old_gray = frame_gray
    cv2.imshow('frame',frame)
    k = cv2.waitKey(0) &amp; 0xff
    if k == 27:
        break

cv2.destroyAllWindows()
cap.release()"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">import</span> <span class="pl-s1">cv2</span>

<span class="pl-s1">cap</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-v">VideoCapture</span>(<span class="pl-s">'traffic_video.mp4'</span>)

<span class="pl-c"># params for ShiTomasi corner detection</span>
<span class="pl-s1">feature_params</span> <span class="pl-c1">=</span> <span class="pl-en">dict</span>( <span class="pl-s1">maxCorners</span><span class="pl-c1">=</span><span class="pl-c1">100</span>,
                       <span class="pl-s1">qualityLevel</span><span class="pl-c1">=</span><span class="pl-c1">0.03</span>,
                       <span class="pl-s1">minDistance</span><span class="pl-c1">=</span><span class="pl-c1">7</span>,
                       <span class="pl-s1">blockSize</span><span class="pl-c1">=</span><span class="pl-c1">7</span>)

<span class="pl-c"># Parameters for lucas kanade optical flow</span>
<span class="pl-s1">lk_params</span> <span class="pl-c1">=</span> <span class="pl-en">dict</span>( <span class="pl-s1">winSize</span><span class="pl-c1">=</span>(<span class="pl-c1">15</span>,<span class="pl-c1">15</span>),
                  <span class="pl-s1">maxLevel</span><span class="pl-c1">=</span><span class="pl-c1">0</span>,
                  <span class="pl-s1">criteria</span><span class="pl-c1">=</span>(<span class="pl-s1">cv2</span>.<span class="pl-v">TERM_CRITERIA_EPS</span> <span class="pl-c1">|</span> <span class="pl-s1">cv2</span>.<span class="pl-v">TERM_CRITERIA_COUNT</span>, <span class="pl-c1">10</span>, <span class="pl-c1">0.03</span>))

<span class="pl-c"># Take first frame and find corners in it</span>
<span class="pl-s1">ret</span>, <span class="pl-s1">old_frame</span> <span class="pl-c1">=</span> <span class="pl-s1">cap</span>.<span class="pl-en">read</span>()
<span class="pl-s1">old_frame</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-v">UMat</span>(<span class="pl-s1">old_frame</span>)
<span class="pl-s1">old_gray</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-en">cvtColor</span>(<span class="pl-s1">old_frame</span>, <span class="pl-s1">cv2</span>.<span class="pl-v">COLOR_BGR2GRAY</span>)
<span class="pl-s1">p0</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-en">goodFeaturesToTrack</span>(<span class="pl-s1">old_gray</span>, <span class="pl-s1">mask</span><span class="pl-c1">=</span><span class="pl-c1">None</span>, <span class="pl-c1">**</span><span class="pl-s1">feature_params</span>)
<span class="pl-s1">p0</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-v">UMat</span>(<span class="pl-s1">np</span>.<span class="pl-en">array</span>(<span class="pl-en">sorted</span>(<span class="pl-s1">p0</span>.<span class="pl-en">get</span>(), <span class="pl-s1">key</span><span class="pl-c1">=</span><span class="pl-k">lambda</span> <span class="pl-s1">p</span>: <span class="pl-en">tuple</span>(<span class="pl-s1">p</span>[<span class="pl-c1">0</span>]))))

<span class="pl-k">while</span>(<span class="pl-c1">1</span>):

    <span class="pl-s1">ret</span>,<span class="pl-s1">frame</span> <span class="pl-c1">=</span> <span class="pl-s1">cap</span>.<span class="pl-en">read</span>()
    <span class="pl-s1">frame</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-v">UMat</span>(<span class="pl-s1">frame</span>)
    <span class="pl-s1">frame_gray</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-en">cvtColor</span>(<span class="pl-s1">frame</span>, <span class="pl-s1">cv2</span>.<span class="pl-v">COLOR_BGR2GRAY</span>)

    <span class="pl-c"># Calculate optical flow</span>
    <span class="pl-s1">p1</span>, <span class="pl-s1">st</span>, <span class="pl-s1">err</span> <span class="pl-c1">=</span> <span class="pl-en">list</span>(<span class="pl-en">map</span>(<span class="pl-k">lambda</span> <span class="pl-s1">umat</span>: <span class="pl-s1">umat</span>.<span class="pl-en">get</span>(), <span class="pl-s1">cv2</span>.<span class="pl-en">calcOpticalFlowPyrLK</span>(<span class="pl-s1">old_gray</span>, <span class="pl-s1">frame_gray</span>, <span class="pl-s1">p0</span>, <span class="pl-c1">None</span>, <span class="pl-c1">**</span><span class="pl-s1">lk_params</span>)))

    <span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-s1">p1</span>:
        <span class="pl-s1">x</span>, <span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-s1">i</span>.<span class="pl-en">ravel</span>()
        <span class="pl-s1">cv2</span>.<span class="pl-en">circle</span>(<span class="pl-s1">frame</span>, (<span class="pl-s1">x</span>, <span class="pl-s1">y</span>), <span class="pl-c1">4</span>, (<span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">255</span>), <span class="pl-c1">-</span><span class="pl-c1">1</span>)
    <span class="pl-s1">good_new</span> <span class="pl-c1">=</span> <span class="pl-s1">p1</span>[<span class="pl-s1">st</span> <span class="pl-c1">==</span> <span class="pl-c1">1</span>]
    <span class="pl-s1">p0</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-v">UMat</span>(<span class="pl-s1">good_new</span>.<span class="pl-en">reshape</span>(<span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">2</span>))
    <span class="pl-s1">old_gray</span> <span class="pl-c1">=</span> <span class="pl-s1">frame_gray</span>
    <span class="pl-s1">cv2</span>.<span class="pl-en">imshow</span>(<span class="pl-s">'frame'</span>,<span class="pl-s1">frame</span>)
    <span class="pl-s1">k</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-en">waitKey</span>(<span class="pl-c1">0</span>) <span class="pl-c1">&amp;</span> <span class="pl-c1">0xff</span>
    <span class="pl-k">if</span> <span class="pl-s1">k</span> <span class="pl-c1">==</span> <span class="pl-c1">27</span>:
        <span class="pl-k">break</span>

<span class="pl-s1">cv2</span>.<span class="pl-en">destroyAllWindows</span>()
<span class="pl-s1">cap</span>.<span class="pl-en">release</span>()</pre></div>