编辑代码:如何访问我的网络摄像头而不是照片

时间:2016-08-10 13:57:10

标签: opencv simplecv

所以我有一个预先编写的代码,用于查找图像中最亮的像素 - 代码中包含加载图片的命令。我需要的是找到用我的网络摄像头制作的实时视频中最亮的像素。所以我现在需要做的是删除想要加载图片的行,并添加行来访问相机。我一直试图这么做几个小时,但我总是得到错误信息,有人知道如何解决这个问题吗? 这是我需要编辑的代码:

# import the necessary packages
import numpy as np
import argparse
import cv2

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", help = "path to the image file")
ap.add_argument("-r", "--radius", type = int,
    help = "radius of Gaussian blur; must be odd")
args = vars(ap.parse_args())

# load the image and convert it to grayscale
image = cv2.imread(args["image"])
orig = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# perform a naive attempt to find the (x, y) coordinates of
# the area of the image with the largest intensity value
(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
cv2.circle(image, maxLoc, 5, (255, 0, 0), 2)

# display the results of the naive attempt
cv2.imshow("Naive", image)

# apply a Gaussian blur to the image then find the brightest
# region
gray = cv2.GaussianBlur(gray, (args["radius"], args["radius"]), 0)
(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
image = orig.copy()
cv2.circle(image, maxLoc, args["radius"], (255, 0, 0), 2)

# display the results of our newly improved method
cv2.imshow("Robust", image)
cv2.waitKey(0)

我想删除整个'#load the image并将其转换为灰度'块并希望添加以下行:

Import SimpleCV
cam = SimpleCV.Camera()
img = cam.getImage().flipHorizontal().toGray()
img.show()

有没有人知道如何编辑代码而不会收到新的错误消息?

1 个答案:

答案 0 :(得分:0)

在opencv中访问网络摄像头流非常简单。

要执行此操作,请编写类似cap = cv2.VideoCapture(XXX)的内容,其中XXX是带视频的文件路径,ip摄像机的IP地址或默认计算机网络摄像头的0。

获取此流后,您可以像这样迭代图像:

while(True):
    didReturnImage, image = cap.read()
    if not didReturnImage:
        #The VideoCapture did not provide an image. 
        #Assuming this to mean that there are no more left 
        #in the video, we leave the loop.
        break
    else:
        #image is now available for use as a regular image

只需将代码置于此循环中,从上述循环中的orig = image.copy()开始。

(注意:您可能希望将行cv2.waitKey(0)更改为cv2.waitKey(1),因为第一行会将图像永久保留在屏幕上,而第二行会将图像保持打开,直到显示下一张图像。)