Skip to main content

A Python tool for extracting and visualizing contours in videos.

Project description

ContourVision: Video Contour Extraction Tool ContourVision is a Python library designed for extracting and visualizing contours from video files. It allows users to process videos and obtain an output where object outlines are highlighted.

Features Converts video frames to grayscale.

Applies Otsu's method for automatic thresholding.

Finds external contours of objects in frames.

Outputs video with:

White contours on a black background.

Black contours on a white background.

Simple and easy-to-use class interface.

(Future) Draw contours directly on the original color frames.

Installation You can install ContourVision using pip.

From PyPI (Once Published) pip install contourvision

From Source (for development) Clone the repository:

git clone https://github.com/viliusbankauskas/contourvision.git

cd contourvision

Install in editable mode:

pip install -e .

Dependencies Python 3.8+

OpenCV (opencv-python >= 4.0)

NumPy (numpy >= 1.19)

These dependencies will be automatically installed if you install ContourVision via pip (as they are listed in setup.py).

Usage Example Create a Python script (e.g., process_my_video.py):

from contourvision import VideoContourExtractor import os

Define paths for your input video and desired output

input_video_file = "path/to/your/video.mp4" # REQUIRED: Change this to your video file
output_dir = "processed_videos" # A directory to store output videos
os.makedirs(output_dir, exist_ok=True) # Create the output directory if it doesn't exist

output_video_wb = os.path.join(output_dir, "result_white_on_black.avi")
output_video_bw = os.path.join(output_dir, "result_black_on_white.avi")

if not os.path.exists(input_video_file):
    print(f"Error: Input video '{input_video_file}' not found.")
else:
    try:
        # Initialize the extractor for white contours on a black background
        print(f"Processing for white on black: {input_video_file} -> {output_video_wb}")
        extractor_wb = VideoContourExtractor(contour_style='white_on_black')
        extractor_wb.process_video(input_video_file, output_video_wb)
        print(f"White on black processing finished. Output: {output_video_wb}")

        # Initialize the extractor for black contours on a white background
        print(f"Processing for black on white: {input_video_file} -> {output_video_bw}")
        extractor_bw = VideoContourExtractor(contour_style='black_on_white')
        extractor_bw.process_video(input_video_file, output_video_bw)
        print(f"Black on white processing finished. Output: {output_video_bw}")

        print(f"\nAll processing done. Check outputs in '{output_dir}'.")

    except Exception as e:
        print(f"An error occurred during processing: {e}")
        import traceback
        traceback.print_exc()

Remember to replace "path/to/your/video.mp4" with the actual path to your video file.

How to Run the Included Example The project includes an example script to demonstrate its usage.

Locate a test video: Ensure you have a video file (e.g., .mp4, .avi) for testing. You can place it in the examples/input_videos/ directory for convenience, or note its absolute path.

Configure the example script:

Open examples/run_contour_detection.py.

Find the line input_path = "examples/input_videos/test.mp4"

Change this string to the correct path of your test video file.

Navigate to the project root: Open your terminal or command prompt and navigate to the main directory of the ContourVision project (the one containing setup.py and the examples folder).

Run the script:

python examples/run_contour_detection.py

Output videos will be saved in the examples/output_videos/ directory by default.

Future Enhancements Draw on Original Frame: Implement the on_original contour style to draw contours directly on the original color video frames.

More Thresholding Methods: Add support for other thresholding techniques beyond Otsu's method.

Contour Customization: Allow users to configure contour thickness, color (for on_original), and type (e.g., cv2.RETR_LIST, cv2.RETR_TREE).

Command-Line Interface (CLI): Develop a CLI using argparse or click for easier use without writing Python scripts.

Performance Optimization: Investigate potential performance improvements for processing long videos.

Error Handling: Enhance error handling and provide more informative messages.

Unit Tests: Write comprehensive unit tests for all functionalities.

Documentation: Expand documentation, possibly using tools like Sphinx or MkDocs for a dedicated documentation site.

Output Formats: Allow specifying different output video codecs and containers.

License This project is licensed under the MIT License. Please see the LICENSE file in the repository for the full license text.

Contributing Contributions are welcome!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

contourvision-0.1.7.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

contourvision-0.1.7-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file contourvision-0.1.7.tar.gz.

File metadata

  • Download URL: contourvision-0.1.7.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for contourvision-0.1.7.tar.gz
Algorithm Hash digest
SHA256 9865be2edc286e932cabee68ce49adafd88548c12fdc393cb942c3a595ec4259
MD5 4a32eb8bbc2def8e3ea6e5d9e199e9ee
BLAKE2b-256 fc8c00578cfc3bd96e18e4d7116df600b9d2d8a54639b554fd88ceab3ddec473

See more details on using hashes here.

File details

Details for the file contourvision-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: contourvision-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for contourvision-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 e9c171632209beb2591692df0b8396692e3f1d87b23bec9cc4a167738896c65d
MD5 3e72975f8867e6b60eb8b10b66fd57bb
BLAKE2b-256 225827bda9bc073f504371fea06c813d1f2c69a4aa379340042cda28f54a9f34

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page