gpt4 book ai didi

python - OpenCV没有在图像上添加线和点

转载 作者:行者123 更新时间:2023-12-02 16:08:36 27 4
gpt4 key购买 nike

我正在使用 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

如您所见,我读取了角点并尝试将其(线和圆)添加到图像中,然后显示图像。角存在并且图像被显示,但没有一个单一的绿色角被可视化。他们都是黑...

结果如下:显示图像,跟踪工作,没有绿色角落和跟踪线可视化,即使它们存在。

not a corner

有什么建议?

P.S.:我确定代码有效,因为我已经用 .avi 测试过,一旦我把 .tiff 放在了问题上。 Tiff 只是灰度,所以它可能无法显示绿点。

最佳答案

正如您所提到的,您不能在灰度平面上绘制绿色。

解决方案是将图像从灰度转换为 BGR 格式,并在 BGR 图像上绘图。

例子:
在灰度上绘图会产生一个黑色圆圈:

import numpy as np
import cv2 as cv

color = (0, 255, 0)

# Read image as Grayscale
gray = cv.imread('chelsea.png', cv.IMREAD_GRAYSCALE)

rows, cols = gray.shape

gray = cv.circle(gray, (cols//2, rows//2), rows//4, color, thickness=8)

cv.imshow('gray', gray)
cv.waitKey(0)
cv.destroyAllWindows()

结果:
enter image description here

解决方案:
将表单灰度转换为 BGR(其中每个像素的 r=g=b),并在 BGR 图像上绘图:
gray = cv.imread('chelsea.png', cv.IMREAD_GRAYSCALE)    
rows, cols = gray.shape

# Convert from Grayscale format to BGR format, where r=g=b for each pixel
bgr = np.dstack((gray, gray, gray))

# Plot the circle on the BGR image:
bgr = cv.circle(bgr, (cols//2, rows//2), rows//4, color, thickness=8)

cv.imshow('bgr', bgr)
cv.waitKey(0)
cv.destroyAllWindows()

结果:
enter image description here

关于python - OpenCV没有在图像上添加线和点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60168041/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com