Skip to content

Instantly share code, notes, and snippets.

@harshkn
Created April 5, 2017 07:01
Show Gist options
  • Save harshkn/51dfe7c114bb9491958f664a7915a721 to your computer and use it in GitHub Desktop.
Save harshkn/51dfe7c114bb9491958f664a7915a721 to your computer and use it in GitHub Desktop.
Find circle centre
import cv2 as cv2
import numpy as np
fn = '200px-Traffic_lights_dark_red-yellow.svg.png'
# OpenCV reads image with BGR format
img = cv2.imread(fn)
# Convert to HSV format
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# lower mask (0-10)
lower_red = np.array([0, 50, 50])
upper_red = np.array([10, 255, 255])
mask = cv2.inRange(img_hsv, lower_red, upper_red)
# Bitwise-AND mask and original image
masked_red = cv2.bitwise_and(img, img, mask=mask)
# Check for circles using HoughCircles on opencv
circles = cv2.HoughCircles(mask, cv2.cv.CV_HOUGH_GRADIENT, 1, 20, param1=30, param2=15, minRadius=0, maxRadius=0)
print 'Radius ' + 'x = ' + str(circles[0][0][0]) + ' y = ' + str(circles[0][0][1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment