Skip to content

imehranasgari/OpenCV_drawing_annotationg_Text

Repository files navigation

OpenCV Drawing and Annotation Toolkit

Problem Statement and Goal of Project

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.

Solution Approach

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.

  1. 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).

  2. 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.

  3. Text Annotation (Basic): I explored cv2.putText to add text labels to images, experimenting with different built-in Hershey fonts, scaling, and positioning.

  4. Advanced Text Annotation (Persian/RTL): Recognizing the limitations of cv2.putText with 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_reshaper to ensure correct character joining.
    • Applying the bidi algorithm to manage the right-to-left display order.
    • Rendering the text onto a Pillow image with a custom .ttf font.
    • 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.

Technologies & Libraries

  • 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.

Installation & Execution Guide

  1. Clone the repository to your local machine.
  2. Install the required libraries using pip:
    pip install opencv-python numpy matplotlib pillow arabic-reshaper python-bidi
  3. Ensure you have a custom font file (e.g., a .ttf file for Persian) in the project directory if you wish to replicate the custom text examples.
  4. Open and run the drawing_annotationg.ipynb notebook in a Jupyter environment.

Screenshots / Sample Out

Here are some examples of the annotations and drawings created in this project.

Basic Shapes & Text Advanced Drawing Persian Text with Custom Font
Basic shapes and English text A drawing of a house using various shapes Persian text rendered correctly with a custom font

Additional Learnings / Reflections

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.

alt text alt text alt text alt text

🙏 Acknowledgments

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.

👤 Author

Mehran Asgari


📄 License

This project is licensed under the Apache 2.0 License – see the LICENSE file for details.

About

Visualizing and annotating data is a critical first step in many computer vision workflows. With that in mind, I created this educational project to demonstrate core drawing capabilities in OpenCV using Python and NumPy.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors