2016-08-08 3 views
1

Я пытаюсь обнаружить ГОЛУБОЙ цветной КРУГ и его ЦЕНТР. Затем нарисуйте круг на обнаруженном круге и очень маленький круг на его центре. Но я получаю несколько ошибок. (Я использую OpenCV 3.1.0, Python 2.7 Anaconda 64 бита, PyCharm как IDE) (Пожалуйста, помогите мне с помощью питона кодов) я запускаю следующий код:Обнаружение цветного круга и его центра с помощью 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

Я тоже пробовал это, а затем получаю эту ошибку: ** Traceback (последний последний вызов): Файл« C: /Users/Meliodas/PycharmProjects/OpenCV_By_Examples/code_tester.py " , строка 21, в круги = np.uint16 (np.around (круги)) Файл «C: \ Users \ Meliodas \ Anaconda2 \ lib \ site-packages \ numpy \ core \ fromnumeric.py», строка 2774, вокруг return _wrapit (a, 'round', decimals, out) Файл «C: \ Users \ Meliodas \ Anaconda2 \ lib \ site-packages \ numpy \ core \ fromnumeric.py», строка 48, в _wrapit result = getattr (asarray (obj), метод) (* args, ** kwds) AttributeError: объект «NoneType» не имеет атрибута «rint» ** – Omee

+0

Он говорит, что атрибут AttributeError: объект «NoneType» не имеет атрибута «rint». Я думаю, что у вас есть туманный синтаксис 'print'. проверьте один раз. Попробуйте отредактировать код. –

+0

Я пробовал код, который вы отредактировал, он дает ошибку: ** Traceback (последний последний вызов): Файл «C: /Users/Meliodas/PycharmProjects/OpenCV_By_Examples/code_tester.py», строка 21, в круги = np.uint16 (np.around (круги)) Файл «C: \ Users \ Meliodas \ Anaconda2 \ lib \ site-packages \ numpy \ core \ fromnumeric.py», строка 2774, вокруг return _wrapit (a, 'round', decimals, out) Файл «C: \ Users \ Meliodas \ Anaconda2 \ lib \ site-packages \ numpy \ core \ fromnumeric.py», строка 48, в _wrapit result = getattr (asarray (obj), метод) (* args, ** kwds) AttributeError: объект «NoneType» не имеет атрибута «rint» ** – Omee