Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Lucas和Kanade的结构张量_Python_Opencv_Image Processing_Opticalflow - Fatal编程技术网

Python Lucas和Kanade的结构张量

Python Lucas和Kanade的结构张量,python,opencv,image-processing,opticalflow,Python,Opencv,Image Processing,Opticalflow,对于一项大学任务,我目前正在用Python在OpenCV中实现Lucas和Canade,但结果并不像预期的那样: import numpy as np import cv2 as cv #begin func def calculate_lc_displacement(img1,img2,imgsmooting,p): #smooth image if imgsmooting > 0: img1 = cv.GaussianBlur(img1, (0,0)

对于一项大学任务,我目前正在用Python在OpenCV中实现Lucas和Canade,但结果并不像预期的那样:

import numpy as np
import cv2 as cv

#begin func
def calculate_lc_displacement(img1,img2,imgsmooting,p):
    #smooth image

    if imgsmooting > 0:
        img1 = cv.GaussianBlur(img1, (0,0), imgsmooting)
        img2 = cv.GaussianBlur(img2, (0,0), imgsmooting)

    #calc gradients
    dx = cv.Sobel(img1, cv.CV_64F, 1, 0, ksize = 3, scale = 1.0/3.0)
    dy = cv.Sobel(img1, cv.CV_64F, 0, 1, ksize = 3, scale = 1.0/3.0)

    #calc temporal gradient
    dt = img2-img1

    #compute structure tensor components
    axx = cv.GaussianBlur(dx * dx, (0,0), p)
    ayy = cv.GaussianBlur(dy * dy, (0,0), p)
    axy = cv.GaussianBlur(dx * dy, (0,0), p)

    
    bx = cv.GaussianBlur(dx * dt, (0,0), p)
    by = cv.GaussianBlur(dy * dt, (0,0), p)

    #compute u and v according to lecture
    norm = (axx*ayy) - (axy*axy)
    u = ( (bx * ayy) - (axy * by)) / norm
    v = ( (by* axx) - (axy * bx) ) / norm
    return u,v
#endfuc

#get imgs
I1g = cv.imread(r"C:\Users\Tobias\Desktop\pyhonws\lucaskanada\yos1.pgm", cv.IMREAD_GRAYSCALE)
I2g = cv.imread(r"C:\Users\Tobias\Desktop\pyhonws\lucaskanada\yos2.pgm", cv.IMREAD_GRAYSCALE)
#calc displacement
u,v = calculate_lc_displacement(I1g,I2g,1.4,6.4)

# Use Hue, Saturation, Value colour model 
img3 = cv.imread(r"C:\Users\Tobias\Desktop\pyhonws\lucaskanada\yos1.pgm", cv.IMREAD_COLOR) #for 3 dimensions
hsv = np.zeros(img3.shape,dtype=np.uint8)
mag, ang = cv.cartToPolar(u,v)
hsv[..., 1] = 255
hsv[..., 0] = ang * 180 / np.pi / 2
hsv[..., 2] = cv.normalize(mag, None, 0, 255, cv.NORM_MINMAX)
bgr = cv.cvtColor(hsv, cv.COLOR_HSV2BGR)
cv.imshow("colored flow", bgr)
cv.waitKey(0)
cv.destroyAllWindows()
有人看到代码有什么问题吗? 显示位移的函数实际上是正确的,它与Lucas和Canade的openCV实现配合得很好

优胜美地序列上给出的视差图与示例中给出的视差图完全不匹配

预期参数为:

但看起来是这样的:

下面是中间步骤的可视化:


为什么您认为代码有问题?乍一看,这似乎是正确的。请包括您遇到的问题的完整描述。
但结果不符合预期
如何?你的预期结果是什么?你得到了什么?所以我编辑了完整答案的代码。也许会显示你的输出和预期输出?仅仅基于“完全不匹配”就很难诊断问题。到目前为止,我发现的唯一问题是向量b缺少一个减号。哦,是的,我已经尝试了减号,但它并没有发生太大的变化