2016-08-08 6 views
1

푸른 색 원을 감지하려고합니다. 센터입니다. 그런 다음 감지 된 원과 그 중심에있는 매우 작은 원에 원을 그립니다. 하지만 몇 가지 오류가 발생합니다. (내가 OpenCV의 3.1.0를 사용하고, 파이썬 2.7 아나콘다 64 비트가 IDE로 PyCharm) (파이썬 코드를 사용하여 제발 도와주세요) 내가 다음 코드를 실행 : 나는를 실행할 때OpenCV를 사용하여 색이있는 동그라미와 그 중심을 감지합니다.

import cv2 
import numpy as np 

cap = cv2.VideoCapture(0) 
if cap.isOpened(): 
    while(True): 
     frame, _ = cap.read() 
     # blurring the frame that's captured 
     frame_gau_blur = cv2.GaussianBlur(frame, (3, 3), 0) 
     # converting BGR to HSV 
     hsv = cv2.cvtColor(frame_gau_blur, cv2.COLOR_BGR2HSV) 
     # the range of blue color in HSV 
     lower_blue = np.array([110, 50, 50]) 
     higher_blue = np.array([130, 255, 255]) 
     # getting the range of blue color in frame 
     blue_range = cv2.inRange(hsv, lower_blue, higher_blue) 
     # getting the V channel which is the gray channel 
     blue_s_gray = blue_range[::2] 
     # applying HoughCircles 
     circles = cv2.HoughCircles(blue_s_gray, cv2.HOUGH_GRADIENT, 1, 10, 100, 30, 5, 50) 
     circles = np.uint16(np.around(circles)) 
     for i in circles[0,:]: 
      # drawing on detected circle and its center 
      cv2.circle(frame,(i[0],i[1]),i[2],(0,255,0),2) 
      cv2.circle(frame,(i[0],i[1]),2,(0,0,255),3) 
     cv2.imshow('circles', frame) 
     k = cv2.waitKey(5) & 0xFF 
     if k == 27: 
      break 
    cv2.destroyAllWindows() 
else: 
    print "Can't find camera" 

내가 오류를 코드는 다음과 같습니다 :

OpenCV Error: Assertion failed (depth == CV_8U || depth == CV_16U || depth == CV_32F) in cv::cvtColor, file C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgproc\src\color.cpp, line 7935 Traceback (most recent call last): File "C:/Users/Meliodas/PycharmProjects/OpenCV_By_Examples/code_tester.py", line 11, in hsv = cv2.cvtColor(frame_gau_blur, cv2.COLOR_BGR2HSV) cv2.error: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgproc\src\color.cpp:7935: error: (-215) depth == CV_8U || depth == CV_16U || depth == CV_32F in function cv::cvtColor

미리 도움을 청하십시오!

+0

frame_gau_blur의 유형은 무엇입니까 :

코드는 아래와 같습니다? using frame_gau_blur.dtype –

+0

"frame_gau_blur"의 데이터 유형을 어떻게 확인합니까? 나는 매우 유감 스럽다, 나는 numpy와 python 및 컴퓨터 시각에 아주 새롭다. @AmitayNachmani – Omee

+0

print frame_gau_blur.dtype –

답변

1

에 나는 내 문제를 해결 한 온라인 오류의 의미를 찾는 후 (하나는 내가 가진 것을의), 나는 그들을 위해 따라서 해결책을 찾을 수 있었다 나는 그들을 풀 수 있었다. 아래의 코드를 실행하면 파란 원을 꽤 잘 감지 할 수 있습니다. 내 문제를 해결하는 데 도움을 준 사람들에게 감사드립니다.

import cv2 
import numpy as np 

cap = cv2.VideoCapture(0) 
if cap.isOpened(): 
    while(True): 
     ret, frame = cap.read() 
     # blurring the frame that's captured 
     frame_gau_blur = cv2.GaussianBlur(frame, (3, 3), 0) 
     # converting BGR to HSV 
     hsv = cv2.cvtColor(frame_gau_blur, cv2.COLOR_BGR2HSV) 
     # the range of blue color in HSV 
     lower_blue = np.array([110, 50, 50]) 
     higher_blue = np.array([130, 255, 255]) 
     # getting the range of blue color in frame 
     blue_range = cv2.inRange(hsv, lower_blue, higher_blue) 
     res_blue = cv2.bitwise_and(frame_gau_blur,frame_gau_blur, mask=blue_range) 
     blue_s_gray = cv2.cvtColor(res_blue, cv2.COLOR_BGR2GRAY) 
     canny_edge = cv2.Canny(blue_s_gray, 50, 240) 
     # applying HoughCircles 
     circles = cv2.HoughCircles(canny_edge, cv2.HOUGH_GRADIENT, dp=1, minDist=10, param1=10, param2=20, minRadius=100, maxRadius=120) 
     cir_cen = [] 
     if circles != None: 
      # circles = np.uint16(np.around(circles)) 
      for i in circles[0,:]: 
       # drawing on detected circle and its center 
       cv2.circle(frame,(i[0],i[1]),i[2],(0,255,0),2) 
       cv2.circle(frame,(i[0],i[1]),2,(0,0,255),3) 
       cir_cen.append((i[0],i[1])) 
     print cir_cen 
     cv2.imshow('circles', frame) 
     cv2.imshow('gray', blue_s_gray) 
     cv2.imshow('canny', canny_edge) 
     k = cv2.waitKey(5) & 0xFF 
     if k == 27: 
      break 
    cv2.destroyAllWindows() 
else: 
    print 'no cam' 
0

변경 frame, _ = cap.read()ret,frame = cap.read()

import cv2 
import numpy as np 

cap = cv2.VideoCapture(0) 
if cap.isOpened(): 
while(True): 
    ret,frame= cap.read() 
    # blurring the frame that's captured 
    frame_gau_blur = cv2.GaussianBlur(frame, (3, 3), 0) 
    # converting BGR to HSV 
    hsv = cv2.cvtColor(frame_gau_blur, cv2.COLOR_BGR2HSV) 
    # the range of blue color in HSV 
    lower_blue = np.array([110, 50, 50]) 
    higher_blue = np.array([130, 255, 255]) 
    # getting the range of blue color in frame 
    blue_range = cv2.inRange(hsv, lower_blue, higher_blue) 
    # getting the V channel which is the gray channel 
    blue_s_gray = blue_range[::2] 
    # applying HoughCircles 
    circles = cv2.HoughCircles(blue_s_gray, cv2.HOUGH_GRADIENT, 1, 10, 100, 30, 5, 50) 
    circles = np.uint16(np.around(circles)) 
    for i in circles[0,:]: 
     # drawing on detected circle and its center 
     cv2.circle(frame,(i[0],i[1]),i[2],(0,255,0),2) 
     cv2.circle(frame,(i[0],i[1]),2,(0,0,255),3) 
    cv2.imshow('circles', frame) 
    k = cv2.waitKey(5) & 0xFF 
    if k == 27: 
     break 
cv2.destroyAllWindows() 
+0

나는 이것을 시도한 다음이 오류가 발생합니다 : ** 추적 (가장 최근의 마지막 호출) : 파일 "C : /Users/Meliodas/PycharmProjects/OpenCV_By_Examples/code_tester.py" , 줄 21, circles = np.uint16 (np.around (circle)) 파일 "C : \ Users \ Meliodas \ Anaconda2 \ lib \ site-packages \ numpy \ core \ fromnumeric.py" ~ 안에 return _wrapit (a, 'round', decimals, out) 파일 "C : \ Users \ Meliodas \ Anaconda2 \ lib \ site-packages \ numpy \ core \ fromnumeric.py", 줄 48, _wrapit에 있음 결과 = getattr (* args, ** kwds) AttributeError : 'NoneType'객체에 'rint'속성이 없습니다 ** – Omee

+0

AttributeError : 'NoneType'객체에 'rint'속성이 없습니다. 나는 당신이'print' 구문을 잘못 입력했다고 생각합니다. 한 번 확인하십시오. 편집 한 코드를 사용해보십시오. –

+0

편집 한 코드에서 오류가 발생했습니다. ** Traceback (가장 최근의 마지막 호출) : 파일 "C : /Users/Meliodas/PycharmProjects/OpenCV_By_Examples/code_tester.py", 줄 21, circles = np.uint16 (np.around (circle)) 2774 줄에서 "C : \ Users \ Meliodas \ Anaconda2 \ lib \ site-packages \ numpy \ core \ fromnumeric.py"파일을 반환 _wrapit (a, 'round', decimals, outattr (result) getattr (asarray (obj), method) (* args, **) 파일 "C : \ Users \ Meliodas \ Anaconda2 \ lib \ site-packages \ numpy \ core \ fromnumeric.py" kwds) AttributeError : 'NoneType'객체에 'rint'속성이 없습니다. ** – Omee