Matt Stokes
Matt Stokes

Reputation: 4958

OpenCV pixel colours all the same

I need a second eye on my code that gets the centre of a circle colour. For some untold reason it's returning near the same values for every centre which is far from correct. Results for circles:

Center Colour: [126 126 126] Point x: 502 y: 440

Center Colour [124 124 124] Point x: 502 y: 516

Center Colour [133 133 133] Point x: 502 y: 596

Center Colour [116 116 116] Point x: 504 y: 306

Center Colour [119 119 119] Point x: 504 y: 366

The input image is seen below. Obviously there should be very different values as the black circles should have an average RBG much lower than 100 range.

input image

The image below shows that the code is finding the circles and the centre of the circles correctly (marking in green) it just doesn't find the correct centre colour values

output

Code below:

import cv2
import numpy as np
from math import sqrt

# Open
img = cv2.imread('test1.jpg',0)

# Process
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

# Find Interest
circles = cv2.HoughCircles(img,cv2.cv.CV_HOUGH_GRADIENT,1,20,
                                param1=50,param2=30,minRadius=1,maxRadius=20)
circles = np.uint16(np.around(circles))

# Post Process
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 Colour"
  print cimg[i[0], i[1]]
  print "Point"
  print "x: "+str(i[0]) + " y: " + str(i[1])

cv2.imwrite('output.png',cimg)

Upvotes: 1

Views: 651

Answers (1)

Robert Caspary
Robert Caspary

Reputation: 1634

From my understanding you should get the color of the inner circle, because you're drawing it before accessing the color value. But since the values returning aren't 0, 0, 255 the coordinates have to be swapped.

orig print is:

print "Center Colour (orig)", cimg[i[0], i[1]]

swapped print is:

print "Center Colour (swapped)", cimg[i[1], i[0]]

Output after drawing:

Center Colour (orig) [126 126 126]
Center Colour (orig) [116 116 116]
...
Center Colour (swapped) [  0   0 255]
Center Colour (swapped) [  0   0 255]
...

If you now use the swapped print before painting the inner circle, the output will look like:

Center Colour (swapped) [128 128 128]
Center Colour (swapped) [27 27 27]
...

Is that what you're looking for?

Upvotes: 2

Related Questions