#computer-vision #polygon #image #graphics #vertices #early #computervision

rustvision

Simple library for interacting with images, vertices, and polygons (and some other stuff related to computer vision)

2 releases

0.1.1 Dec 12, 2022
0.1.0 Dec 6, 2022

#579 in Images

Download history 12/week @ 2024-02-22 16/week @ 2024-02-29 64/week @ 2024-03-07

88 downloads per month

LGPL-3.0

7MB
795 lines

Rustvision

Simple (and very early) project for interacting with images, vertices, and polygons (and some other stuff related to computer vision).

Note: This project is in a very early state and the documentation is far from perfect. Feel free to open PRs with improvements!

Installation

Simply add this crate to your Cargo.toml (either via cargo add or manually).

Usage

The usage of this library is very straight forward. First you need to create a new image.

use rustvision::image::Image;

fn main() {
    let mut img = Image::new(400, 400);
}

Rectangle

To work with a simple rectangle, you can import and then create one:

use rustvision::{image::Image, shapes::Rectangle, rgb, vec2};

// ...
    let rect = Rectangle::new(vec2![50.0, 40.0], 100, 70, rgb!(255, 0, 0));
// ...

This will create a new instance of a rectangle at the coordinates x-y-coordinates (50.0, 40.0) with a width of 100, a height of 70 and the color red.

You will notice the usage of two macros here: vec2![] and rgb!(). vec2![] is a utility macro to create a 2D vector with the specified coordinates, whereas rgb!() creates a new RGB color for you. For more information on these have a look at the code (or later documentation).

If you want to draw this created rectangle to the image, you can do so by calling img.draw:

// ...
    img.draw(&rect);
// ...

This will draw the rectangle to the provided image. To finally save the image to your disk, you can use another utility macro:

use rustvision::{image::Image, shapes::Rectangle, rgb, vec2, save_pnm_p6};

// ...
    save_pnm_p6!("path_to_image.ppm", img);
// ...

The image will be saved in the P6 (binary PPM) representation. If you prefer the ASCII representation (P3), this library provides another utility macro as well.

Polygons

Similar to rectangles, you can create more complex polygons:

use rustvision::{image::Image, shapes::Polygon, vec2};

// ...
    let mut polygon = Polygon::from_points(vec![
        vec2![20.0, 250.0],
        vec2![50.0, 350.0],
        vec2![80.0, 280.0],
        vec2![110.0, 350.0],
        vec2![140.0, 250.0],
        vec2![110.0, 300.0],
        vec2![80.0, 250.0],
        vec2![50.0, 300.0],
    ]);
// ...

Notice that this "constructor" does not take a color. By default, this polygon will be black. If you want to specify a color, you can do so with the respective method:

use rustvision::{image::Image, shapes::Polygon, rgb, vec2};

// ...
    polygon.set_color(rgb!(0, 255, 0));
// ...

This will color the polygon green. Painting this polygon now would result in only the outline being drawn. To fill it, you need to manually set the flag:

// ...
    polygon.set_filled(true);
// ...

Similar to a rectangle, you can draw this polygon to an image:

// ...
    img.draw(&polygon);
// ...

Reading Files

You can read files and convert them to the internal image representation by using the load_image macro:

use rustvision::{load_image, save_pnm_p3};

fn main() {
    let image = load_image!("assets/mona_lisa_small.ppm");
}

Note: Currently, this library only support images in PPM6 (i.e., binary PPM) representation. Trying to read any other file will lead to a panic!

No runtime deps