![]() As the name suggests, the np.zeros method fills every element in the array with an initial value of zero. We also allocate space for 3 channels - one each for Red, Green, and Blue. We construct a NumPy array using the np.zeros method with 300 rows and 300 columns, yielding a 300 x 300 pixel image. Initializing our image is handled on Line 7. We will also import cv2, so we can have access to the OpenCV library. You will see this convention utilized in all PyImageSearch tutorials that leverage NumPy (and in fact, you will commonly see this convention in the Python community as well!) Lines 2 and 3 import the packages we will be using.Īs a shortcut, we will create an alias for numpy as np. # (Red, Green, and Blue) with a black backgroundĬanvas = np.zeros((300, 300, 3), dtype="uint8") # initialize our canvas as a 300x300 pixel image with 3 channels Open the basic_drawing.py file in your project directory structure, and let’s get to work. We are now ready to get started! Implementing basic drawing functions with OpenCVīefore we draw on actual images, let’s first learn how to initialize an empty NumPy array/image and draw on it. image_drawing.py: Loads adrian.png from disk and then draws on the image (rather than an empty/blank NumPy array canvas).basic_drawing.py: Initializes an empty NumPy array and utilizes OpenCV to draw lines, circles, and rectangles.We have two Python scripts to review today: Let’s start by reviewing our project directory structure for our OpenCV drawing project: $ tree. Gain access to Jupyter Notebooks for this tutorial and other PyImageSearch guides that are pre-configured to run on Google Colab’s ecosystem right in your web browser! No installation required.Īnd best of all, these Jupyter Notebooks will run on Windows, macOS, and Linux! Project structure Ready to run the code right now on your Windows, macOS, or Linux systems?.Wanting to skip the hassle of fighting with the command line, package managers, and virtual environments?.Learning on your employer’s administratively locked system?.Having problems configuring your development environment? Figure 1: Having trouble configuring your development environment? Want access to pre-configured Jupyter Notebooks running on Google Colab? Be sure to join PyImageSearch Plus - your system will be up and running with this tutorial in a matter of minutes. If you need help configuring your development environment for OpenCV, I highly recommend that you read my pip install OpenCV guide - it will have your system up and running in a matter of minutes. Luckily, OpenCV is pip-installable: $ pip install opencv-contrib-python To follow along with this guide, you need to have the OpenCV library installed on your system. We use them occasionally on the PyImageSearch blog. These OpenCV drawing functions are used less often but are still worth noting. cv2.arrowedLine: Draws an arrow pointing from a starting (x, y)-coordinate to an ending (x, y)-coordinate.cv2.fillPoly: Draws a polygon, but instead of drawing the outline, instead fills in the polygon.cv2.polylines: Draws the outline of a polygon specified by a set of (x, y)-coordinates.cv2.ellipse: Draws an ellipse on an image.However, it’s worth noting that more advanced OpenCV drawing functions exist, including: We will cover these three drawing functions today. cv2.rectangle: Draws a rectangle on an image specified by the top-left corner and bottom-right corner (x, y)-coordinates.cv2.circle: Draws a circle on an image specified by the center (x, y)-coordinate and a supplied radius.cv2.line: Draws a line on image, starting at a specified (x, y)-coordinate and ending at another (x, y)-coordinate.OpenCV has a number of drawing functions you can use to draw various shapes, including polygons of irregular shapes, but the three most common OpenCV drawing functions you will see are: These scripts will help you understand how to perform basic drawing functions with OpenCV.īy the end of this guide, you will understand how to use OpenCV to draw lines, circles, and rectangles. With the review taken care of, we will move on to implement two Python scripts: We will then configure our development environment and review our project directory structure. In the first part of this tutorial, we will briefly review OpenCV’s drawing functions. Looking for the source code to this post? Jump Right To The Downloads Section Drawing with OpenCV ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |