The primary goal of this project was to develop a deep, hands-on understanding of OpenCV's drawing and annotation functionalities. Rather than focusing on a single complex model, I aimed to master the foundational tools used for visualizing data, creating masks, and annotating images for computer vision tasks such as object detection and segmentation. The project serves as a practical library of examples, from basic shapes to the complex challenge of rendering right-to-left (RTL) text with custom fonts.
I approached this project as a systematic exploration of OpenCV's capabilities. Starting with a blank canvas created using NumPy, I implemented a series of functions to demonstrate key drawing operations.
-
Basic Shapes: I began by drawing fundamental geometric shapes, including lines (
cv2.line), rectangles (cv2.rectangle), circles (cv2.circle), and ellipses (cv2.ellipse). I experimented with parameters like coordinates, color, thickness, and fill options (using negative thickness). -
Complex Polygons: To handle irregular shapes, I utilized
cv2.polylines. This involved defining a series of vertices (points) and instructing OpenCV to connect them, creating both open and closed polygons. This is crucial for tasks like annotating specific regions of interest. -
Text Annotation (Basic): I explored
cv2.putTextto add text labels to images, experimenting with different built-in Hershey fonts, scaling, and positioning. -
Advanced Text Annotation (Persian/RTL): Recognizing the limitations of
cv2.putTextwith non-ASCII and right-to-left languages, I implemented a more robust solution. I integrated the Pillow (PIL) library to handle the complex rendering of Persian text. This involved:- Using
arabic_reshaperto ensure correct character joining. - Applying the
bidialgorithm to manage the right-to-left display order. - Rendering the text onto a Pillow image with a custom
.ttffont. - Converting the Pillow image back into an OpenCV-compatible format to be displayed or overlaid. This demonstrates my ability to solve library-specific challenges by integrating complementary tools.
- Using
- Python
- OpenCV: Core library for all drawing and image manipulation tasks.
- NumPy: Used for creating the base image arrays (canvases).
- Matplotlib: Utilized for displaying images within the Jupyter Notebook environment.
- Pillow (PIL): Integrated for advanced text rendering, specifically to handle custom fonts and RTL languages.
- Arabic-Reshaper & Python-Bidi: Helper libraries used with Pillow for correct Persian text rendering.
- Clone the repository to your local machine.
- Install the required libraries using pip:
pip install opencv-python numpy matplotlib pillow arabic-reshaper python-bidi
- Ensure you have a custom font file (e.g., a
.ttffile for Persian) in the project directory if you wish to replicate the custom text examples. - Open and run the
drawing_annotationg.ipynbnotebook in a Jupyter environment.
Here are some examples of the annotations and drawings created in this project.
| Basic Shapes & Text | Advanced Drawing | Persian Text with Custom Font |
|---|---|---|
![]() |
![]() |
![]() |
This project was an excellent exercise in mastering the fundamentals of image annotation. While drawing a rectangle may seem simple, understanding how to programmatically define regions of interest is a critical skill for preparing datasets and visualizing model outputs.
The most significant challenge was rendering Persian text. It highlighted that even a powerful library like OpenCV has its limitations. Successfully integrating Pillow, arabic-reshaper, and bidi not only solved the problem but also reinforced the importance of a multi-library approach in real-world software development. This experience taught me to look beyond a single tool and combine the strengths of different libraries to achieve a polished, professional result.
This project represents my initial steps into the practical application of computer vision. The foundational knowledge and guidance for this work were derived from the outstanding OpenCV course taught by Alireza Akhavanpour on the Maktabkhooneh platform. His ability to deconstruct complex topics into clear, actionable steps was instrumental in the successful implementation of this project.
Email: imehranasgari@gmail.com.
GitHub: https://github.com/imehranasgari.
This project is licensed under the Apache 2.0 License – see the LICENSE file for details.






