Home>

### python opencv color index

Currently, Hough transform detects circles
Examine all pixels inside the detected circle
Color conditions

``````hsv = cv2.cvtColor (img, cv2.COLOR_BGR2HSV)
bin_img = cv2.inRange (hsv, (30, 40, 0), (90, 255, 255))``````

If 70% or more of one circle meets the conditions, draw a circle.
I created a program that does not draw if it is less than.

``````Traceback (most recent call last):
File "E: \ K \ pg \ test.py", line 58, in<module>
if bin_img [j, i] == 0:
IndexError: index 594 is out of bounds for axis 0 with size 594``````
Corresponding source code
``````hsv = cv2.cvtColor (img, cv2.COLOR_BGR2HSV)
bin_img = cv2.inRange (hsv, (30, 40, 0), (90, 255, 255))
count1 = 0
count2 = 0
# Draw the detection result.
if circles is not None:
circles = circles.squeeze (axis = 0) # (1, NumCircles, 3)->(NumCircles, 3)
for cx, cy, r in circles.astype (int):
Set #i and j to the range of the rectangle from end to end of the circle
for i, j in zip (range (cx-r, cx + r), range (cy-r, cy + r)):
a = (cx, cy)
b = (i, j)
Calculate the distance between # (i, j) and the center of the circle (cx, cy)
answer = distance.euclidean (a, b)
# When the distance answer is within r, count.
if answer<= r:
count1 + = 1
# Count when the color inside the circle meets the conditions.
if bin_img [j, i] == 0:
count2 + = 1
continue
#Draw a circle if 30% of the inside of the circle does not meet the conditions (70% of the inside of the circle meets the color conditions)
if (count2/count1) * 100<= 30:
# Draw a circle
# Draw the circumference of the circle.
cv2.circle (img, (cx, cy), r, (0, 255, 0), 2)``````
What i don't understand

Before tampering with the relevant source code (at the time of the previous question)

``if bin_img [j, i] == 0:``

I didn't get an error due to this, so I don't know why it happened.

Is there something wrong with the code in another part?