我正在使用SparseOptFlow算法。我想跟踪一些角点,并在图像上实时显示它们
这对.avi视频非常有效,现在我正在使用tiff序列。 发生的事情是,它不想在图像上显示跟踪的绿色角点,即使它有角点并且代码是正确的
代码如下:
color = (0, 255, 0) # Corner colors (green)
[.....]
while(totAnalyzedFrame[nucleo]<totFrame):
# ret = a boolean return value from getting the frame, frame = the current frame being projected in the video
try:
frame = VideoToSOF[totAnalyzedFrame[nucleo]]
except Exception as e:
print("Frame finished...Exception:")
print(e)
# Converts each frame to grayscale - we previously only converted the first frame to grayscale (cv.cvtColor(frame, cv.COLOR_BGR2GRAY), tiff already in grayscale)
gray = frame
# Calculates sparse optical flow by Lucas-Kanade method
# https://docs.opencv.org/3.0-beta/modules/video/doc/motion_analysis_and_object_tracking.html#calcopticalflowpyrlk
next, status, error = cv.calcOpticalFlowPyrLK(prev_gray, gray, prev, None, **lk_params)
#Save the information of the corners
for i in range(len(next)):
cornerPosition[nucleo][totAnalyzedFrame[nucleo]][i][0] = next[i][0][0] # X pos of i_th corner
cornerPosition[nucleo][totAnalyzedFrame[nucleo]][i][1] = next[i][0][1] # Y pos of i_th corner
if next[i][0][0] <= 0 or next[i][0][1] <= 0:
printf("Got a '0': frame = %d, X = %d, Y = %d " % (i,next[i][0][0],next[i][0][1]))
# Selects good feature points for previous position
good_old = prev[status == 1]
# Selects good feature points for next position
good_new = next[status == 1]
# Draws the optical flow tracks
for i, (new, old) in enumerate(zip(good_new, good_old)):
# Returns a contiguous flattened array as (x, y) coordinates for new point
a, b = new.ravel()
# Returns a contiguous flattened array as (x, y) coordinates for old point
c, d = old.ravel()
# Draws line between new and old position with green color and 1 thickness
mask = cv.line(mask, (a, b), (c, d), color, 1)
# Draws filled circle (thickness of -1) at new position with green color and radius of 2
frame = cv.circle(frame, (a, b), 2, color, -1)
# Overlays the optical flow tracks on the original frame
output = cv.add(frame, mask)
# Updates previous frame
prev_gray = gray.copy()
# Updates previous good feature points
prev = good_new.reshape(-1, 1, 2)
# Opens a new window and displays the output frame
cv.imshow("sparse optical flow", output)
# Frames are read by intervals of 10 milliseconds. The programs breaks out of the while loop when the user presses the 'q' key
if cv.waitKey(1) & 0xFF == ord('q'):
np.delete(prev, [])
break
totAnalyzedFrame[nucleo] = totAnalyzedFrame[nucleo] + 1
print("SOF working... Frame = %d/%d\t\t\t[press 'q' to quit]" % (totAnalyzedFrame[nucleo],totFrame), end='\r')
else:
print("No corner found @ nucleo %d" % nucleo+1)
pass
正如你所看到的,我阅读了角点,并尝试将其(线和圆)添加到图像中,然后显示图像。角点存在并显示图像,但未显示任何绿色角点。它们都是黑色的
结果如下:显示图像,跟踪工作,即使存在绿色角点和跟踪线,也不显示绿色角点和跟踪线
有什么建议吗
注:我确信代码是有效的,因为我已经用.avi对它进行了测试,一旦我把.tiff放进去,它就是从问题开始的。Tiff仅为灰度,因此可能无法显示绿点
如前所述,不能在灰度平面上绘制绿色
解决方案是将图像从灰度转换为BGR格式,并在BGR图像上打印
示例:
在灰度结果上打印黑色圆圈:
结果:
![enter image description here](https://i.stack.imgur.com/AWlze.png)
解决方案:
将灰度转换为BGR(其中每个像素的r=g=b),并在BGR图像上打印:
结果:
![enter image description here](https://i.stack.imgur.com/tjPH0.png)
相关问题 更多 >
编程相关推荐