Search code examples
pythonopencvimage-processingcomputer-visionhough-transform

Error while trying to detect color at center of circle


I am trying to detect the color at the center of the detected circle from HoughCircles. The way I am doing this is as follows:

print("Center of the circle: ", i[0]," ", i[1])
print(ci[i[0]][i[1]][0]," blue")
print(ci[i[0]][i[1]][1]," green")
print(ci[i[0]][i[1]][2]," red")

Here ci is the opencv image array and i[0] and i[1] represent the center coordinates of the circle as given by HoughCircles in the code given below.

But as I do this, I get an error saying.

IndexError: index 1034 is out of bounds for axis 0 with size 600

I could not understand the reason for this. I am trying to detect the color at the center of the circle.

    import cv2
    import numpy as np
    import sys
    import math


    img = cv2.imread("images/diffc.jpeg", 0)
    ci = cv2.imread("images/diffc.jpeg")

    cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)



    minDist = 150
    param1 = 120
    param2 = 37

    minRadius = 120
    maxRadius = 140


    circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,minDist,
                                param1=param1,param2=param2,minRadius=minRadius,maxRadius=maxRadius)


    if circles is None:
            print("No circles detected!")
            sys.exit(-1)


    circles = np.uint16(np.around(circles))

    for i in circles[0,:]:

        # draw the outer circle
        cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
        # draw the center of the circle
        cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
        print("Center of the circle: ", i[0]," ", i[1])
        # STATEMENTS THAT THROW ERROR
        print(ci[i[0]][i[1]][0]," blue")
        print(ci[i[0]][i[1]][1]," green")
        print(ci[i[0]][i[1]][2]," red")

    cv2.imshow('detected circles',cimg)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

Here is the image: Image


Solution

  • Here You need to know that HoughCircles method returns the center of the circle in the form of width x height and numpy find the image with rows x columns.

    So you need to pass first columns and then rows in the ci.

    So to detect blue color: ci[i[1]][i[0]][0].

    Your final code is:

    import cv2
    import numpy as np
    import sys
    import math
    
    
    img = cv2.imread("images/diffc.jpeg", 0)
    ci = cv2.imread("images/diffc.jpeg")
    
    cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
    
    
    
    minDist = 150
    param1 = 120
    param2 = 37
    
    minRadius = 120
    maxRadius = 140
    
    
    circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,minDist,
                              param1=param1,param2=param2,minRadius=minRadius,maxRadius=maxRadius)
    
    
    if circles is None:
          print("No circles detected!")
          sys.exit(-1)
    
    
    circles = np.uint16(np.around(circles))
    
    for i in circles[0,:]:
    
      # draw the outer circle
      cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
      # draw the center of the circle
      cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
      print("Center of the circle: ", i[0]," ", i[1])
      # STATEMENTS THAT THROW ERROR
      print(ci[i[1]][i[0]][0]," blue")
      print(ci[i[1]][i[0]][1]," green")
      print(ci[i[1]][i[0]][2]," red")
    
    cv2.imshow('detected circles',cimg)
    cv2.waitKey(0)
    cv2.destroyAllWindows()