OpenCV-Python is a Python link library designed to solve computer vision problems. The cv2.circle () method is used to draw a circle on any image.
Syntax: cv2.circle(image, center_coordinates, radius, color, thickness)
Parameters:
image: It is the image on which circle is to be drawn.
center_coordinates: It is the center coordinates of circle. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value).
radius: It is the radius of circle.
color: It is the color of border line of circle to be drawn. For BGR, we pass a tuple. eg: (255, 0, 0) for blue color.
thickness: It is the thickness of the circle border line in px. Thickness of -1 px will fill the circle shape by the specified color.
Return Value: It returns an image.
How to draw a circle over image OpenCV?
StackOverflow question
Im usign python and opencv to get a image from the webcam, and I want to know how to draw a circle over my image, just a simple green circle with transparent fill
my code:
import cv2
import numpy
import sys
if __name__ == ’__main__’:
#get current frame from webcam
cam = cv2.VideoCapture(0)
img = cam.read()
#how draw a circle????
cv2.imshow(’WebCam’, img)
cv2.waitKey()
Thanks in advance.
Answer
cv2.circle(img, center, radius, color, thickness=1, lineType=8, shift=0) ‚Üí None
Draws a circle.
Parameters:
img (CvArr) – Image where the circle is drawn
center (CvPoint) – Center of the circle
radius (int) – Radius of the circle
color (CvScalar) – Circle color
thickness (int) – Thickness of the circle outline if positive, otherwise this indicates that a filled circle is to be drawn
lineType (int) – Type of the circle boundary, see Line description
shift (int) – Number of fractional bits in the center coordinates and radius value
Use "thickness" parameter for only the border.
Example #1
def blob(x):
"""Given an Nx3 matrix of blob positions and size,
create N img_size x img_size images, each with a blob drawn on
them given by the value in each row of x
One row of x = [x,y,radius]."""
y = np.zeros((x.shape[0], img_size, img_size))
for i, particle in enumerate(x):
rr, cc = skimage.draw.circle(
particle[0], particle[1], max(particle[2], 1), shape=(img_size, img_size)
)
y[i, rr, cc] = 1
return y
#%%
# names (this is just for reference for the moment!)
Example #2
def update(self, radarData):
self.img = np.zeros((self.height, self.width, self.channels), np.uint8)
cv2.line(self.img, (10, 0), (self.width/2 - 5, self.height), (100, 255, 255))
cv2.line(self.img, (self.width - 10, 0), (self.width/2 + 5, self.height), (100, 255, 255))
for track_number in range(1, 65):
if str(track_number)+’_track_range’ in radarData:
track_range = radarData[str(track_number)+’_track_range’]
track_angle = (float(radarData[str(track_number)+’_track_angle’])+90.0)*math.pi/180
x_pos = math.cos(track_angle)*track_range*4
y_pos = math.sin(track_angle)*track_range*4
cv2.circle(self.img, (self.width/2 + int(x_pos), self.height - int(y_pos) - 10), 5, (255, 255, 255))
#cv2.putText(self.img, str(track_number),
# (self.width/2 + int(x_pos)-2, self.height - int(y_pos) - 10), self.font, 1, (255,255,255), 2)
cv2.imshow("Radar", self.img)
cv2.waitKey(2)
Example #3
def ProcessFrame(self, frame):
# segment arm region
segment = self.SegmentArm(frame)
# make a copy of the segmented image to draw on
draw = cv2.cvtColor(segment, cv2.COLOR_GRAY2RGB)
# draw some helpers for correctly placing hand
cv2.circle(draw,(self.imgWidth/2,self.imgHeight/2),3,[255,102,0],2)
cv2.rectangle(draw, (self.imgWidth/3,self.imgHeight/3), (self.imgWidth*2/3, self.imgHeight*2/3), [255,102,0],2)
# find the hull of the segmented area, and based on that find the
# convexity defects
[contours,defects] = self.FindHullDefects(segment)
# detect the number of fingers depending on the contours and convexity defects
# draw defects that belong to fingers green, others red
[nofingers,draw] = self.DetectNumberFingers(contours, defects, draw)
# print number of fingers on image
cv2.putText(draw, str(nofingers), (30,30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255))
return draw
Example #4
def mark_hand_center(frame_in,cont):
max_d=0
pt=(0,0)
x,y,w,h = cv2.boundingRect(cont)
for ind_y in xrange(int(y+0.3*h),int(y+0.8*h)): #around 0.25 to 0.6 region of height (Faster calculation with ok results)
for ind_x in xrange(int(x+0.3*w),int(x+0.6*w)): #around 0.3 to 0.6 region of width (Faster calculation with ok results)
dist= cv2.pointPolygonTest(cont,(ind_x,ind_y),True)
if(dist>max_d):
max_d=dist
pt=(ind_x,ind_y)
if(max_d>radius_thresh*frame_in.shape[1]):
thresh_score=True
cv2.circle(frame_in,pt,int(max_d),(255,0,0),2)
else:
thresh_score=False
return frame_in,pt,max_d,thresh_score
# 6. Find and display gesture
Example #5
def update(self, radarData):
self.img = np.zeros((self.height, self.width, self.channels), np.uint8)
cv2.line(self.img, (10, 0), (self.width/2 - 5, self.height), (100, 255, 255))
cv2.line(self.img, (self.width - 10, 0), (self.width/2 + 5, self.height), (100, 255, 255))
for track_number in range(1, 65):
if str(track_number)+’_track_range’ in radarData:
track_range = radarData[str(track_number)+’_track_range’]
track_angle = (float(radarData[str(track_number)+’_track_angle’])+90.0)*math.pi/180
x_pos = math.cos(track_angle)*track_range*4
y_pos = math.sin(track_angle)*track_range*4
cv2.circle(self.img, (self.width/2 + int(x_pos), self.height - int(y_pos) - 10), 5, (255, 255, 255))
#cv2.putText(self.img, str(track_number),
# (self.width/2 + int(x_pos)-2, self.height - int(y_pos) - 10), self.font, 1, (255,255,255), 2)
cv2.imshow("Radar", self.img)
cv2.waitKey(2)
Archived version
OpenCV-Python —¬†is a Python bindings library for solving computer vision problems.¬†
cv2.circle ()
is used to draw a circle on any image.
Syntax: cv2.circle (image, center_coordinates, radius, color , thickness)
Parameters:
image: It is the image on which circle is to be drawn.
center_coordinates: It is the center coordinates of circle. The coordinates are represented as tuples of two values ​​ie ( X coordinate value, Y coordinate value).
radius: It is the radius of circle.
color: It is the color of border line of circle to be drawn. For BGR , we pass a tuple. eg: (255, 0, 0) for blue color.
thickness: It is the thickness of the circle border line in px . Thickness of -1 px will fill the rectangle shape by the specified color.
Return Value: It returns an image.
The image is used for all examples below:
Example # 1:
# Python program to explain the cv2.circle () method
 
# cv2 import
import cv2 
 
# path
path = r ’C: UsersRajnishDesktoppythonengineeringgeeks.png’
   
# Read image in default mode
image = cv2.imread (path)
 
# Name of the window in which the image is displayed
window_name = ’Image’
   
# Center coordinates
center_coordinates = ( 120 , 50 )
 
# Circle radius
radius = 20
   
# Blue color in BGR
color = ( 255 , 0 , 0 )
 
# Line width 2 px
thickness = 2
 
# Using the cv2.circle () method
# Draw a circle with a 2 px blue border
image = cv2.circle (image, center_coordinates, radius, color, thickness)
 
# Display image
cv2.imshow (window_name, image)  |
Output:
Example # 2:
Using -1 px thickness to fill the rectangle with red.
# Python program to explain the cv2.circle () method
 
# cv2 import
import cv2 
 
# path
path = r ’C: UsersRajnishDesktoppythonengineeringgeeks.png’
 
# Read image in default mode
image = cv2.imread (path)
 
# Name of the window in which the image is displayed
window_name = ’Image’
   
# Center coordinates
center_coordinates = ( 120 , 100 )
   
# Circle radius
radius = 30
   
# Red in BGR
color = ( 0 , 0 , 255 )
 
# Line thickness -1 px
thickness = - 1
 
# Using the cv2.circle () method
# Draw a -1 px red circle
image = cv2.circle (image, center_coordinates, radius, color, thickness)
< code class = "undefined spaces">  
# Displaying an image
cv2.imshow (window_name, image ) 
  |
Output: