0% found this document useful (0 votes)
20 views

Circle and Line

Computer Vision Circle and Line

Uploaded by

Jana S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Circle and Line

Computer Vision Circle and Line

Uploaded by

Jana S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Circle Detection

Circle detection finds a variety of uses in biomedical applications, ranging from iris
detection to white blood cell segmentation.

Basics of Circle Detection


A circle can be described by the following equation:

To detect circles, we may fix a point (x, y). Now, we are required to find 3 parameters: a,
b and r. Therefore, the problem is in a 3-dimensional search space. To find possible
circles, the algorithm uses a 3-D matrix called the “Accumulator Matrix” to store
potential a, b and r values.
The HoughCircles function in OpenCV has the following parameters which can be
altered according to the image.

circles = cv2.HoughCircles(image, method, dp, minDist[, param1[, param2[, minRadius[,


maxRadius]]]]])

Below are the parameters explained in detail

 image: 8-bit, single-channel, grayscale input image


 method: HOUGH_GRADIENT and HOUGH_GRADIENT_ALT
 dp: The inverse ratio of accumulator resolution and image resolution
 minDist: Minimum distance between the centers of the detected circles. All the
candidates below this distance are neglected as explained above
 param1: it is the higher threshold of the two passed to the Canny edge detector (the
lower canny threshold is twice smaller)
 param2: it is the accumulator threshold for the circle centers at the detection stage
as discussed above.
 minRadius: minimum radius that you expect. If unknown, put zero as default.
 maxRadius: if -ve, only circle centers are returned without radius search. If
unknown, put zero as default.

Coding
import cv2

# Read image
img = cv2.imread(“image1.png”)
# Smooth it
img = cv2.medianBlur(img,3)
img_copy = img.copy()
# Convert to greyscale
img_gray = cv2.cvtColor(img_copy,cv2.COLOR_BGR2GRAY)
# Apply Hough transform to greyscale image
circles = cv2.HoughCircles(img_gray,cv2.HOUGH_GRADIENT,1,20,
param1=60,param2=40,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))
# Draw the circles
for (x,y,r ) in circles[0,:]:
# draw the outer circle
cv2.circle(img,(x,y),r,(0,255,0),3)
# draw the center of the circle
cv2.circle(img,(x,y),2,(0,255,0),3)
cv2.imshow('detected circles',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:
Line Detection

The Hough Transform is a method that is used in image processing to detect any shape,
if that shape can be represented in mathematical form. It can detect the shape even if it
is broken or distorted a little bit.

Basics of Houghline Method


A line can be represented as y = mx + c or in parametric form, as r = xcosθ + ysinθ
where r is the perpendicular distance from origin to the line, and θ is the angle formed
by this perpendicular line and horizontal axis measured in counter-clockwise

Python Program
import cv2
import numpy as np
image = cv2.imread(“lines.jpg”)
gray=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
canimg = cv2.Canny(gray,50,200)
lines = cv2.HoughLines(canimg, 1, np.pi/180, 120, np.array([]))
for line in lines:
Rho,theta = line[0]
a=np.cos(theta)
b=np.sin(theta)
x0=a*rho
y0=b*rho
x1=int(x0+1000*(-b))
y1=int(y0+1000*(a))

x2=int(x0-1000*(-b))
y2=int(y0-1000*(a))
cv2.line(image,(x1,y1),(x2,y2),(0,0,255),2)
cv2.imshow(image)
cv2.imshow(canimage)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy