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.
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:
- How to Create a RGB Color Picker for Images using OpenCV Python - 22 April, 2022
- Combine Several Images Vertically with Padding using OpenCV Python - 21 April, 2022
- Combine Several Images Horizontally with Padding using OpenCV Python - 21 April, 2022