Code Monkey home page Code Monkey logo

contour_tracing's Introduction

Contour tracing library

A 2D library to trace contours.
How it works in a live demo: https://stpr.github.io/contour_tracing/

Features

Core features:

  • Trace contours using the Theo Pavlidis' algorithm (connectivity: 4-connected)
  • Trace outlines in clockwise direction
  • Trace holes in counterclockwise direction
  • Input format: a 2D array of bits or an image buffer
  • Output format: a string of SVG Path commands

Manual parameters:

  • User can specify to close or not the paths (with the SVG Path Z command)

A Rust example with an array of bits

  1. Add the following line to your Cargo.toml file in the dependencies section:
contour_tracing = { version = "*", features = ["array"] }
  1. Then use the library:
use contour_tracing::array::bits_to_paths;

fn main() {
    let bits = vec![vec![ 1,0,0 ],
                    vec![ 0,1,0 ],
                    vec![ 0,0,1 ]];

    println!("{}", bits_to_paths(bits, true));
}

A Rust example with an image buffer

  1. Add the following line to your Cargo.toml file in the dependencies section:
contour_tracing = { version = "*", features = ["image"] }
  1. Then use the library:
use image::{GrayImage, Luma};
use contour_tracing::image::single_l8_to_paths;

fn main() {
    let mut image_buffer = GrayImage::new(3, 3);
    let foreground_color: image::Luma<u8> = Luma([1]);

    image_buffer.put_pixel(0, 0, foreground_color);
    image_buffer.put_pixel(1, 1, foreground_color);
    image_buffer.put_pixel(2, 2, foreground_color);

    println!("{}", single_l8_to_paths(&mut image_buffer, foreground_color, true));
}

Both examples should print: M0 0H1V1H0ZM1 1H2V2H1ZM2 2H3V3H2Z

For more Rust examples, have a look at the documentation: Documentation

License

Contour tracing library https://github.com/STPR/contour_tracing

Copyright (c) 2022, STPR - https://github.com/STPR

SPDX-License-Identifier: EUPL-1.2

Contribution

Your contribution is highly appreciated. Do not hesitate to open an issue or a pull request. Note that any contribution submitted for inclusion in the project will be licensed according to the terms given in LICENSE.txt.

contour_tracing's People

Contributors

stpr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

contour_tracing's Issues

Ability to use a image::ImageBuffer directly?

Firstly thanks for this library. It worked great for a project I'm working on.

One feature request. I have a image::ImageBuffer and I'd like to pass it directly to bits_to_paths, instead I have to copy it into a Vec::<Vec<i8>>. For example:

    let mut img = GrayImage::new(width, height); // the image::ImageBuffer
    let background_color = Luma([0]); // Black
    let foreground_color = Luma([1]); // White

    contour_tracing::image_to_paths(img, background_color, true);

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.