Opencv_contrib: Wrong answer(or wrongly use) for cv2.optflow.calcOpticalFlowSparseRLOF in opencv-python

Created on 9 Sep 2020  路  2Comments  路  Source: opencv/opencv_contrib

System information (version)
  • OpenCV => :4.4.0.42
  • Operating System / Platform => :window 10 64 bit
  • Compiler => :gcc
Detailed description

I'm trying to use cv2.optflow.calcOpticalFlowSparseRLOF() to tracker the sparse point in frame series. But in the experiment, I found that if I just input one point to cv2.optflow.calcOpticalFlowSparseRLOF(), it can work well. However, if I input more than one points to the function, it can't calc the optflow and return same numbers of the input points. And the same input can work well in cv2.calcOpticalFlowPyrLK().
I have checked the type and the dimension of the input points numpy array, it is same to the result of cv2.goodFeaturesToTrack().
I don't kown what happend, thanks for the help!

Steps to reproduce


```.py
from cv2 import cv2
import numpy as np
import glob

img_path = '../20200622/L'

images = glob.glob(img_path+'/*.'+'png')
images.sort()

feature_params = dict(maxCorners=100,
qualityLevel=0.3,
minDistance=7)
p0 = cv2.goodFeaturesToTrack(cv2.cvtColor(cv2.imread(images[0]), cv2.COLOR_BGR2GRAY), mask=None, **feature_params)

pts0 = np.array([[1092,534],[920,404]]).reshape(-1,1,2)
pts1 = np.zeros(np.shape(pts0))
pts0 = np.float32(pts0)
pts1 = np.float32(pts1)

for i in range(1,len(images)):
print('loop: {}'.format(i))
pimg = cv2.imread(images[i-1])
nimg = cv2.imread(images[i])
pimg_g = pimg
nimg_g = nimg

pts1, status, err = cv2.optflow.calcOpticalFlowSparseRLOF(pimg_g, nimg_g, pts0, pts1)
# lk_params = dict( winSize  = (15, 15), 
#               maxLevel = 2, 
#               criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))  
# pts1, status, err = cv2.calcOpticalFlowPyrLK(pimg_g, nimg_g, pts0, None) #, **lk_params)

pts0 = pts1

```

Issue submission checklist
  • [ ] I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues,
    answers.opencv.org, Stack Overflow, etc and have not found solution

  • I updated to latest OpenCV version and the issue is still there
  • [ ] There is reproducer code and related data files: videos, images, onnx, etc

  • bug optflow python bindings

    Most helpful comment

    Unfortunatly I can somehow recreate this.

    System information (version)

    • Opencv 4.4.0 installed in a venv from opencv-contrib-python (I know that this is not official)

      • Opencv 4.5.0 installed from arch linux packages

      • reproduced in both versions

      • Operating system: Arch Linux

    Detailed description
    When tracking feature points with cv.optflow.calcOpticalFlowSparseRLOF() there is some undefined behaviour. This is is somewhat mysterious, but here's the deal:

    import numpy as np
    import cv2 as cv
    
    pimg = cv.imread('/usr/share/opencv4/samples/data/basketball1.png', None)
    nimg = cv.imread('/usr/share/opencv4/samples/data/basketball2.png', None)
    
    p0 = np.array([[129, 122],[130, 111], [100, 100]], dtype=np.float32)
    
    p1, st, err = cv.optflow.calcOpticalFlowSparseRLOF(pimg, nimg, p0, None)
    # print(p1)
    p1, st, err = cv.optflow.calcOpticalFlowSparseRLOF(pimg, nimg, p0, None)
    print(p1)
    

    Images are from the opencv-samples, the basketball1.png and basketball2.png.

    • When I call cv.optflow.calcOpticalFlowSparseRLOF() twice for multiple feature points, I get reasonable results.
    • When I call cv.optflow.calcOpticalFlowSparseRLOF() twice for multiple feature points but uncomment the print(p1) in between the calls, I get undefined points.
    • When I call cv.optflow.calcOpticalFlowSparseRLOF() once for multiple feature points, I get undefined points.
    • When I call cv.optflow.calcOpticalFlowSparseRLOF() once for _one_ feature point, I get [[0. 0.]] as output.
    • When I call cv.optflow.calcOpticalFlowSparseRLOF() twice for _one_ feature point, I get reasonable results.

    I have no idea why the call has to be twice and, even more mysterious to me, how the print-statement can somehow change the results. :thinking:

    All 2 comments

    Please try to capture input of failed case and provide minimal complete reproducer (including input images in lossless .png format - see imwrite())

    I have checked the type and the dimension of the input points numpy array

    Consider adding dump from cv.utils.dumpInputArray(value).

    Unfortunatly I can somehow recreate this.

    System information (version)

    • Opencv 4.4.0 installed in a venv from opencv-contrib-python (I know that this is not official)

      • Opencv 4.5.0 installed from arch linux packages

      • reproduced in both versions

      • Operating system: Arch Linux

    Detailed description
    When tracking feature points with cv.optflow.calcOpticalFlowSparseRLOF() there is some undefined behaviour. This is is somewhat mysterious, but here's the deal:

    import numpy as np
    import cv2 as cv
    
    pimg = cv.imread('/usr/share/opencv4/samples/data/basketball1.png', None)
    nimg = cv.imread('/usr/share/opencv4/samples/data/basketball2.png', None)
    
    p0 = np.array([[129, 122],[130, 111], [100, 100]], dtype=np.float32)
    
    p1, st, err = cv.optflow.calcOpticalFlowSparseRLOF(pimg, nimg, p0, None)
    # print(p1)
    p1, st, err = cv.optflow.calcOpticalFlowSparseRLOF(pimg, nimg, p0, None)
    print(p1)
    

    Images are from the opencv-samples, the basketball1.png and basketball2.png.

    • When I call cv.optflow.calcOpticalFlowSparseRLOF() twice for multiple feature points, I get reasonable results.
    • When I call cv.optflow.calcOpticalFlowSparseRLOF() twice for multiple feature points but uncomment the print(p1) in between the calls, I get undefined points.
    • When I call cv.optflow.calcOpticalFlowSparseRLOF() once for multiple feature points, I get undefined points.
    • When I call cv.optflow.calcOpticalFlowSparseRLOF() once for _one_ feature point, I get [[0. 0.]] as output.
    • When I call cv.optflow.calcOpticalFlowSparseRLOF() twice for _one_ feature point, I get reasonable results.

    I have no idea why the call has to be twice and, even more mysterious to me, how the print-statement can somehow change the results. :thinking:

    Was this page helpful?
    0 / 5 - 0 ratings