对于一项大学任务,我目前正在用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实现配合得很好
优胜美地序列上给出的视差图与示例中给出的视差图完全不匹配
预期参数为:
但看起来是这样的:
下面是中间步骤的可视化:
目前没有回答
相关问题 更多 >
编程相关推荐