在Python 2.7中使用OpenCV www.example.com将图像叠加到摄像头源上2.4.7.0

z18hc3ub  于 2022-12-30  发布在  Python
关注(0)|答案(1)|浏览(161)

我尝试在python中将一张图片叠加到一个摄像头源上,我可以将一张图片叠加到另一张图片上,但是当我将同样的操作应用到我的摄像头源上时,它就不工作了,下面是我目前为止的代码:

#!/usr/bin/python

import cv2
import time

cv2.cv.NamedWindow("Hawk Eye", 1)

capture = cv2.cv.CaptureFromCAM(0)
cv2.cv.SetCaptureProperty(capture, cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 800)
cv2.cv.SetCaptureProperty(capture, cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 600)

x_offset=y_offset=50
arrows = cv2.imread("arrows.png")

while True:
    webcam=cv2.cv.QueryFrame(capture)
    #webcam[y_offset:y_offset+arrows.shape[0], x_offset:x_offset+arrows.shape[1]]=arrows
    cv2.cv.ShowImage("Hawk Eye", webcam)
    if cv2.cv.WaitKey(10) == 27:
        break
cv2.cv.DestroyAllWindows()

如果我取消评论:

img[y_offset:y_offset+arrows.shape[0], x_offset:x_offset+arrows.shape[1]]=arrows

添加图像的那一行,它只显示了相机的输入,但是当我把它添加到循环中时,它就停止工作了。谢谢!

z0qdvdin

z0qdvdin1#

使用cv2 API可以正常工作:

import cv2
import time

cv2.namedWindow("Hawk Eye", 1)

capture = cv2.VideoCapture(0)
capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 800)
capture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 600)

x_offset=y_offset=50
arrows = cv2.imread("hawk.png")

while True:
    ret, webcam = capture.read()
    if ret:
        webcam[y_offset:y_offset+arrows.shape[0], x_offset:x_offset+arrows.shape[1]]=arrows
        cv2.imshow("Hawk Eye", webcam)
        if cv2.waitKey(10) == 27:
            break
cv2.destroyAllWindows()

相关问题