编辑代码:如何访问我的网页而不是照片



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

# 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 and convert it to grayscale'块,并添加以下行:

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

有人知道我如何编辑代码而不得到新的错误信息吗?

在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),因为第一行将永远保持图像在屏幕上,而第二行将保持它直到显示下一张图像。)

相关内容

  • 没有找到相关文章

最新更新