1

I am trying to detect BLUE colored CIRCLE and it's CENTER. Then draw a circle on the detected circle and a very small circle on it's center. But I get a few errors. (I am using OpenCV 3.1.0, Python 2.7 Anaconda 64 bits, PyCharm as an IDE) (Please help me using python codes) I run the following code:

  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"

The error I get when I run the code is:

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 Thanks a lot in advance for your help!

Muhammad Waleed
  • 2,517
  • 4
  • 27
  • 75
Omee
  • 33
  • 1
  • 3
  • 11
  • What is the type of frame_gau_blur? using frame_gau_blur.dtype – Amitay Nachmani Aug 08 '16 at 11:27
  • How do I check the data type of "frame_gau_blur"? I am very sorry, I am very new to numpy and python and computer vision too. @AmitayNachmani – Omee Aug 08 '16 at 11:37
  • print frame_gau_blur.dtype – Amitay Nachmani Aug 08 '16 at 11:48
  • it's data type "float 64" for frame_gau_blur.dtype @AmitayNachmani – Omee Aug 08 '16 at 11:55
  • You can see that the exception you get sais that you need one of the following: "depth == CV_8U || depth == CV_16U || depth == CV_32F" therefore try to do hsv = cv2.cvtColor(frame_gau_blur.astype(np.float32, cv2.COLOR_BGR2HSV) – Amitay Nachmani Aug 08 '16 at 12:10
  • I tried `hsv = cv2.cvtColor(frame_gau_blur.astype(np.float32), cv2.COLOR_BGR2HSV)` , then i get another error @AmitayNachmani – Omee Aug 08 '16 at 13:56

2 Answers2

1

I have solved the my problem and after looking up the meanings of the errors online (the one's that I got), I was able to find the solutions for them and hence I was able to solve them. If you run the following code given below you should be able to detect blue circles pretty well. Thanks a lot to the people who tried to help me to solve my problem.

The code is given below:

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'
Omee
  • 33
  • 1
  • 3
  • 11
  • 1
    What exactly was causing the AttributeError: 'NoneType' object has no attribute 'rint' error? Can you explain what it meant? – user391339 Apr 01 '18 at 07:29
0

Change frame, _ = cap.read() to 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()
  • 1
    I tried this too and then i get this error: **Traceback (most recent call last): File "C:/Users/Meliodas/PycharmProjects/OpenCV_By_Examples/code_tester.py", line 21, in circles = np.uint16(np.around(circles)) File "C:\Users\Meliodas\Anaconda2\lib\site-packages\numpy\core\fromnumeric.py", line 2774, in around return _wrapit(a, 'round', decimals, out) File "C:\Users\Meliodas\Anaconda2\lib\site-packages\numpy\core\fromnumeric.py", line 48, in _wrapit result = getattr(asarray(obj), method)(*args, **kwds) AttributeError: 'NoneType' object has no attribute 'rint'** – Omee Aug 08 '16 at 14:02
  • It says AttributeError: 'NoneType' object has no attribute 'rint'. I think you have mistyped `print` syntax. check once. Try the code i edited. – vijay singh Rajpurohit Aug 08 '16 at 14:06
  • I tried the code you edited it gives the error: **Traceback (most recent call last): File "C:/Users/Meliodas/PycharmProjects/OpenCV_By_Examples/code_tester.py", line 21, in circles = np.uint16(np.around(circles)) File "C:\Users\Meliodas\Anaconda2\lib\site-packages\numpy\core\fromnumeric.py", line 2774, in around return _wrapit(a, 'round', decimals, out) File "C:\Users\Meliodas\Anaconda2\lib\site-packages\numpy\core\fromnumeric.py", line 48, in _wrapit result = getattr(asarray(obj), method)(*args, **kwds) AttributeError: 'NoneType' object has no attribute 'rint'** – Omee Aug 08 '16 at 14:19
  • Not sure. Works fine here. May be put `circles = np.uint16(np.around(circles))` in if condition. `if(circles):circles = np.uint16(np.around(circles))` – vijay singh Rajpurohit Aug 08 '16 at 14:28
  • If i do that then i get this error: **Traceback (most recent call last): File "C:/Users/Meliodas/PycharmProjects/OpenCV_By_Examples/code_tester.py", line 20, in if(circles): ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() ** – Omee Aug 08 '16 at 14:38
  • how about this `if(len(circles)):circles = np.uint16(np.around(circles))` put the for loop also in the if condition – vijay singh Rajpurohit Aug 08 '16 at 15:02
  • Yep, I tried this too then i get the following error: **Traceback (most recent call last): File "C:/Users/Meliodas/PycharmProjects/OpenCV_By_Examples/code_tester_0.py", line 21, in if(len(circles)): TypeError: object of type 'NoneType' has no len()** – Omee Aug 08 '16 at 15:19
  • The point is camera cant find any circles in the webcam feed. Put some drawn circles in front of webcam and remove the if condition and other edited part except cap.read. – vijay singh Rajpurohit Aug 09 '16 at 06:14
  • I don't think that's the case, as i have already tried it. I still get the same error. I think there is a problem with this `circles = cv2.HoughCircles(blue_s_gray, cv2.HOUGH_GRADIENT, 1, 10, 100, 30, 5, 50)` of my code. What do you think? – Omee Aug 09 '16 at 13:50
  • I won't say that cause this exact piece of code works great on my system. – vijay singh Rajpurohit Aug 09 '16 at 15:00
  • Can you please sent me that code? Or can you edit your answer to the code? Please! It will be very helpful for me to understand what mistake I am making. – Omee Aug 09 '16 at 15:43
  • I also get this AttributeError: 'NoneType' object has no attribute 'rint' error w/ macos sierra python 3. In my case it does not like me to set the last parameter for cv2.HoughCircles (maxRadius=..) to anything but 0. I set it to 10 and get this error, but works fine with 0. – user391339 Apr 01 '18 at 07:23