这是我的代码 我需要在灰色视频流上绘制一条颜色线和一个矩形。 在我的代码中有一些错误,因为我的线条和矩形是黑色的,但不是。
import cv2
cap = cv2.VideoCapture(0)
if (cap.isOpened() == False):
print("Unable to read camera feed")
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
out = cv2.VideoWriter('output.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame_width,frame_height),0)
while(cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# draw line
start_point = (0, 0)
end_point = (250, 250)
color = (0, 255, 0)
thickness = 5
gray = cv2.line(img=gray, pt1=start_point, pt2=end_point, color=color, thickness=thickness, lineType=8, shift=0)
# draw rectangle
x1,y1 = 200, 200
x2,y2 = 250, 250
gray = cv2.rectangle(gray,(x1, y1), (x2, y2),color, 2)
cv2.imshow('webcam(1)', gray)
out.write(gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
答案 0 :(得分:1)
要绘制颜色元素,您必须将图像转换回BGR
gray_BGR = cv2.cvtColor(gray cv2.COLOR_GRAY2BGR)
转换为灰色不仅可以将颜色转换为灰色,还可以将每个像素从三个值(B,G,R)减少为只能保留灰色的单个值。
如果选中frame.shape
和gray.shape
,则会发现差异。
第一个只有(height, width, 3)
,第二个只有(height, width)
,这意味着(height, width, 1)