2016-08-10 2 views
-1

이미지에서 가장 밝은 픽셀을 찾는 데 필요한 미리 작성된 코드가 있습니다. 코드에 그림이로드되는 명령이 있습니다. 필요한 것은 웹캠으로 만든 라이브 비디오에서 가장 밝은 픽셀을 찾는 것입니다. 그래서 지금해야 할 일은 그림을로드하려는 선을 삭제하고 카메라에 액세스하기위한 선을 추가하는 것입니다. 나는 지금 몇 시간 동안 그렇게하려고 노력해 왔지만, 항상 오류 메시지를 받는다. 아무도 그 문제를 어떻게 해결할 수 있을지 모른다. 나는 블록 '그레이 스케일로 #로드 이미지 및 변환'을 전체를 삭제하려면 다음과 같은 라인을 추가 할코드 편집 : 사진 대신 내 웹캠에 액세스하는 방법

# 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) 

을 :

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

을 이것은 내가 편집하는 데 필요한 코드입니다 아무도 내가 새 오류 메시지를받지 않고 코드를 편집 할 수있는 방법을 알고 있습니까?

답변

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(1)에 선 cv2.waitKey(0)을 변경할 수 있습니다)

관련 문제