├── Hough_Output.PNG ├── Sample_Input.jpg ├── Randomized_Approach_Output.PNG ├── An Efficient Randomized Algorithm for Detecting Circles.pdf ├── Readme.md ├── Hough_Transform.py └── Randomized_Circle_Detection.py /Hough_Output.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranavgadekar/circle-detection-hough-transform/HEAD/Hough_Output.PNG -------------------------------------------------------------------------------- /Sample_Input.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranavgadekar/circle-detection-hough-transform/HEAD/Sample_Input.jpg -------------------------------------------------------------------------------- /Randomized_Approach_Output.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranavgadekar/circle-detection-hough-transform/HEAD/Randomized_Approach_Output.PNG -------------------------------------------------------------------------------- /An Efficient Randomized Algorithm for Detecting Circles.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranavgadekar/circle-detection-hough-transform/HEAD/An Efficient Randomized Algorithm for Detecting Circles.pdf -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 1. Hough_Transform.py 2 | 3 | This file has the code for detecting circles in a given image using Hough Transform. 4 | The Radius range can be changed and adjusted as per need in order to improve the performance of the program 5 | 6 | 2. Randomized_Circle_Detection.py 7 | 8 | This file is basically the implementation of the Algorithm given in the Reference paper given in the repository. 9 | Please note that not all the thresholds given in the reference paper have been used to detect the circle. 10 | 11 | Feel free to contribute to codes given. 12 | 13 | Happy coding! :) 14 | -------------------------------------------------------------------------------- /Hough_Transform.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | import cv2 3 | import numpy as np 4 | import time 5 | 6 | original_image = cv2.imread('Sample_Input.jpg',1) 7 | #gray_image = cv2.imread('Sample_Input.jpg',0) 8 | cv2.imshow('Original Image',original_image) 9 | 10 | output = original_image.copy() 11 | 12 | #Gaussian Blurring of Gray Image 13 | blur_image = cv2.GaussianBlur(original_image,(3,3),0) 14 | cv2.imshow('Gaussian Blurred Image',blur_image) 15 | 16 | #Using OpenCV Canny Edge detector to detect edges 17 | edged_image = cv2.Canny(blur_image,75,150) 18 | cv2.imshow('Edged Image', edged_image) 19 | 20 | height,width = edged_image.shape 21 | radii = 100 22 | 23 | acc_array = np.zeros(((height,width,radii))) 24 | 25 | filter3D = np.zeros((30,30,radii)) 26 | filter3D[:,:,:]=1 27 | 28 | start_time = time.time() 29 | 30 | def fill_acc_array(x0,y0,radius): 31 | x = radius 32 | y=0 33 | decision = 1-x 34 | 35 | while(y90): 80 | cv2.circle(output,(b,a),c,(0,255,0),2) 81 | j=j+30 82 | filter3D[:,:,:]=1 83 | j=0 84 | i=i+30 85 | 86 | 87 | cv2.imshow('Detected circle',output) 88 | end_time = time.time() 89 | time_taken = end_time - start_time 90 | print 'Time taken for execution',time_taken 91 | 92 | cv2.waitKey(0) 93 | cv2.destroyAllWindows() -------------------------------------------------------------------------------- /Randomized_Circle_Detection.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | import cv2 3 | import numpy as np 4 | import random 5 | import math 6 | import time 7 | 8 | 9 | original_image = cv2.imread('Sample_Input.jpg',1) 10 | #cv2.imshow('Gray Image',original_image) 11 | 12 | #Gaussian Blurring of Gray Image 13 | blur_image = cv2.GaussianBlur(original_image,(3,3),0) 14 | #cv2.imshow('Gaussian Blurred Image',blur_image) 15 | 16 | #Using OpenCV Canny Edge detector to detect edges 17 | edged_image_one = cv2.Canny(original_image,75,150) 18 | edged_image_two = cv2.Canny(original_image,75,150) 19 | #cv2.imshow('Edged Image One', edged_image_one) 20 | 21 | height,width = edged_image_two.shape 22 | print height,width 23 | #finding contours from a image 24 | contours, hierarchy = cv2.findContours(edged_image_one,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) 25 | 26 | V = np.zeros((height,width)) 27 | V_list = [] #list used to update set of pixels after each 4 pixel iteration 28 | listV = [] #list to remove all points of true circle from V 29 | f=0 #failure counter 30 | Tf=10 #number of failures 31 | Tmin=60 #minumum number of pixels left in V 32 | Ta=3 #minimum distance between 2 pixels in V 33 | Td=1 #distance threshold for 4th pizel 34 | Tr=60/100 #ratio pixel 35 | 36 | edge_pixels = np.where(edged_image_two == 255) 37 | for i in xrange(0,len(edge_pixels[0])): 38 | x=edge_pixels[0][i] 39 | y=edge_pixels[1][i] 40 | V[x][y]=1 41 | 42 | 43 | def main(): 44 | for i in xrange(0,len(contours)): 45 | f=0 46 | circle_detected=0 47 | if(len(contours[i])>100): 48 | while (f<=Tf): 49 | f+=1 50 | random_pixels=random.sample(contours[i],4) 51 | x1 = random_pixels[0][0][1] 52 | y1 = random_pixels[0][0][0] 53 | x2 = random_pixels[1][0][1] 54 | y2 = random_pixels[1][0][0] 55 | x3 = random_pixels[2][0][1] 56 | y3 = random_pixels[2][0][0] 57 | x4 = random_pixels[3][0][1] 58 | y4 = random_pixels[3][0][0] 59 | 60 | V[x1][y1] = 0 61 | V[x2][y2] = 0 62 | V[x3][y3] = 0 63 | V[x4][y4] = 0 64 | 65 | colinearity = np.absolute(((x2-x1)*(y3-y1))-((x3-x1)*(y2-y1))) 66 | pixel_dist = check_pixel_distance(x1,y1,x2,y2,x3,y3) 67 | 68 | if(pixel_dist==1 and colinearity!=0): 69 | circle_detected=determine_possible_circle(x1,y1,x2,y2,x3,y3,x4,y4,colinearity) 70 | if(circle_detected==1): 71 | break 72 | 73 | 74 | 75 | 76 | 77 | #function to determine possible circle 78 | def determine_possible_circle(x1,y1,x2,y2,x3,y3,x4,y4,colinearity): 79 | var1 = x2**2+y2**2 - (x1**2+y1**2) 80 | var2 = x3**2+y3**2 - (x1**2+y1**2) 81 | X_center = int(((var1*2*(y3-y1)) - (var2*2*(y2-y1)))/(4*colinearity)) 82 | Y_center = int(((var2*2*(x2-x1)) - (var1*2*(x3-x1)))/(4*colinearity)) 83 | #print X_center,Y_center 84 | if(X_center>0 and Y_center>0 and X_center20): 87 | #print radius 88 | d4 = int(math.sqrt((x4-X_center)**2 + (y4-Y_center)**2))-radius 89 | if(d4<=Td): 90 | cv2.circle(original_image,(Y_center,X_center),radius,(255,0,0),2) 91 | return 1 92 | else: 93 | return 0 94 | 95 | 96 | 97 | #function to determine if points are closer than min threshold Ta 98 | def check_pixel_distance(x1,y1,x2,y2,x3,y3): 99 | d1 = math.sqrt(((x2-x1)**2)+((y2-y1)**2)) 100 | d2 = math.sqrt(((x3-x2)**2)+((y3-y2)**2)) 101 | d3 = math.sqrt(((x3-x1)**2)+((y3-y1)**2)) 102 | 103 | if(d1>Ta and d2>Ta and d3>Ta): 104 | return 1 105 | else: 106 | return 0 107 | 108 | 109 | 110 | 111 | start_time = time.time() 112 | main() 113 | cv2.imshow('Detected Circle',original_image) 114 | end_time = time.time() 115 | time_taken = end_time-start_time 116 | print 'Time taken for execution',time_taken 117 | cv2.waitKey(0) 118 | cv2.destroyAllWindows() --------------------------------------------------------------------------------