Cara menggunakan circle python

Membuat Lingkaran

Modul turtle memiliki satu fungsi yang bisa digunakan untuk membuat gambar lingkaran yaitu fungsi circle().

Contoh penggunaan fungsi circle() pada turtle

Cara menggunakan circle python

Tampilan:

Cara menggunakan circle python

Contoh di atas memperlihatkan penggunaan fungsi circle() untuk membuat lingkaran pada kanvas. Perintah x.circle(100) digunakan untuk membuat lingkaran dengan jari-jari 100 pixel. Untuk menuliskan suatu teks pada kanvas digunakan fungsi write(). Pada contoh di atas x.write(“r = 100”) digunakan untuk menuliskan teks “r  = 100” seperti yang terlihat pada gambar di atas.

Contoh Fraktal sederhana menggunakan turtle.

Cara menggunakan circle python

Tampilan:

Cara menggunakan circle python

Contoh di atas memperlihatkan bagaimana penggunaan turtle untuk membuat fraktal garis sederhana. Selain prajtal garis, Anda juga bisa membuat fraktal lingkaran dengan menggunakan fungsi circle() yang ada pada turtle.

This post will be helpful in learning OpenCV using Python programming. Here I will show how to implement OpenCV functions and apply them in various aspects using some great examples. Then the output will be visualized along with the comparisons.

Table of Contents

  • Requirements:
  • Documentation:
  • Example Code:
  • Using cv2.circle()
  • Using cv2.ellipse()
  • How do you draw a circle in Python?
  • How do I draw a circle in Python matplotlib?
  • How do I draw a circle in Numpy?
  • How do you draw a circle in OpenCV?

We will also discuss the basic of image processing and provide the detail explanation related to the OpenCV functions.

Requirements:

  • OpenCV 3.4+
  • Python 3.6+
  • Numpy
  • Image, Webcam or Video input
  • Documentation Source: OpenCV Official Documentation

First, you need to setup your Python Environment with OpenCV. You can easily do it by following Life2Coding’s tutorial on YouTube: Linking OpenCV with Python 3

Goals:

The goal is to make you understand how to draw a circle on image using Python OpenCV. We’re going to discuss how to draw opencv shape on images.

Documentation:

circle()

img=cv.circle(img, center, radius, color[, thickness[, lineType[, shift]]])

Draws a circle.

Parameters
img Image where the circle is drawn.
center Center of the circle.
radius Radius of the circle.
color Circle color.
thickness Thickness of the circle outline, if positive. Negative values, like FILLED, mean that a filled circle is to be drawn.
lineType Type of the circle boundary. See LineTypes
shift Number of fractional bits in the coordinates of the center and in the radius value.

imshow()

None=cv.imshow(winname, mat)

Displays an image in the specified window.

Parameters
winname Name of the window.
mat Image to be shown.

waitKey()

retval=cv.waitKey([, delay])

Waits for a pressed key.

Parameters
delay Delay in milliseconds. 0 is the special value that means “forever”.

destroyAllWindows()

None=cv.destroyAllWindows()

Destroys all of the HighGUI windows.

Steps:

  • First we will create a image array using np.zeros()
  • After that we will create a circle using cv2.circle()
  • Then display the image using cv2.imshow()
  • Wait for keyboard button press using cv2.waitKey()
  • Exit window and destroy all windows using cv2.destroyAllWindows()

Example Code:

import numpy as np
import cv2
#create a 512x512 black image
img=np.zeros((512,512,3),np.uint8)

#non filled circle
img1 = cv2.circle(img,(256,256),63, (0,255,0), 8)
#filled circle
img1 = cv2.circle(img,(256,256),63, (0,0,255), -1)
#now use a frame to show it just as displaying a image
cv2.imshow("Circle",img1)
cv2.waitKey(0)
cv2.destroyAllWindows()


Output:

  • Author
  • Recent Posts

Feel free to contact us for your any kind of technical problems. We are here to help you.

In this tutorial, we will learn how to draw a circle on an image using OpenCV Python. This can be useful when we want to detect an object in an image. We will discuss the techniques used to draw a circle. So let’s begin the tutorial.

For using the following methods, make sure that you have installed all the libraries for OpenCV.

Using cv2.circle()

This method is majorly used to draw circles. This method takes 7 arguments. Five of them are used.

image: The input image on which the circle should be drawn.

center_coordinates: Values for x and y must be specified in a tuple notation.

radius: Radius of the circle to be drawn.

color: Color of the circle to be drawn. Values are specified in the form of a three-tuple notation in the form of BGR.

thickness: Thickness of the circle. Negative values are used to fill the circle.

lineType: Type of circle boundary.

shift: Count of fractional bits in coordinates and radius value.

My file name is 1.jpeg

Example 1

Use the given code to draw a circle

import cv2
img = cv2.imread('1.jpeg')
circleim = cv2.circle(img,(250,200),110,(0,0,0),5)
cv2.imshow('image',circleim)
cv2.waitKey()

Here, img is the input image.

The coordinate values are (250,200).

Radius is 110.

The color is black.

The thickness is 5.

Using cv2.ellipse()

We can draw a circle using this method also. It has 11 arguments. Eight of them are used.

image: The input image on which the circle should be drawn.

center_coordinates: Values for x and y must be specified in a tuple notation.

axes: Values for major and minor axes.

angle: Rotation angle value in degrees.

startAngle: Value of start angle in degrees.

endAngle: Value of end angle in degrees.

box: Draws ellipse inscribed in a rotated rectangle.

color: Color of the circle to be drawn. Values are specified in the form of a three tuple notation in the form of BGR.

thickness: Thickness of the circle. Negative values are used to fill the circle.

lineType: Type of circle boundary.

shift: Count of fractional bits in coordinates and radius value.

Example 2

Consider the same image as above.

Use the given code to draw a circle.

import cv2
img = cv2.imread('1.jpeg')
imgg = cv2.ellipse(img,(245,200),(110,110),0,0,360,(0,0,0),5)
cv2.imshow('image',imgg)
cv2.waitKey()

Here, img is the input image.

The center_coordinates are (245,200).

Axes length are (110,110).

The angle of rotation is 0.

The start angle is 0.

The end angle is 360.

The color is black.

The thickness is 5.

How do you draw a circle in Python?

Now to draw a circle using turtle, we will use a predefined function in “turtle”. circle(radius): This function draws a circle of the given radius by taking the “turtle” position as the center. Example: Python3.

How do I draw a circle in Python matplotlib?

MatPlotLib with Python.

Create a new figure or activate an existing figure using figure() method..

Add a subplot arrangement to the current axis..

Create a true circle at a center using Circle class..

Add a patch to the current axis..

Set limits of the x and y axes..

To display the figure, use show() method..

How do I draw a circle in Numpy?

This Python programming example plots a circle using Numpy and Matplotlib Library. In this program, we use numpy to generate data for theta , x and y co-ordinates and pyplot from matplotlib is used to plot data. This example uses formula x = r*cos(θ) and y = r * sin(θ) to generate x and y co-ordinates.

How do you draw a circle in OpenCV?

Circle in OpenCV Python : cv2..

img – It is the image on which the circle has to be drawn..

center – It is the coordinates of the center of the circle..

radius – It is the radius of the circle..

color – It is the color of the circle in RGB..

thickness – It is the thickness of the circle line..