opencv

OpenCV computer vision library bindings for Kit

Files

FileDescription
.editorconfigEditor formatting configuration
.gitignoreGit ignore rules for build artifacts and dependencies
.tool-versionsasdf tool versions (Zig, Kit)
LICENSEMIT license file
README.mdThis file
c/kit_opencv.cppkit_opencv.cpp
c/kit_opencv.hC header for FFI wrapper
examples/basic.kitBasic usage example
examples/phase1.kitExample: phase1
kit.tomlPackage manifest with metadata and dependencies
src/opencv.kitKit OpenCV - Computer vision library bindings
tests/opencv.test.kitTests for opencv

Dependencies

No Kit package dependencies.

Installation

kit add gitlab.com/kit-lang/packages/kit-opencv.git

Usage

import Kit.OpenCV as OpenCV

main = fn =>
  println ("OpenCV version: " ++ OpenCV.version)

  # Create a blank image and draw on it
  match OpenCV.create 400 300 3
    | Ok img ->
      OpenCV.draw-rectangle img 50 50 200 150 255 0 0 2
      OpenCV.draw-circle img 300 150 60 0 0 255 -1
      OpenCV.draw-text img "Hello!" 80 30 0.8 255 255 255

      match OpenCV.save img "/tmp/output.png"
        | Ok _ -> println "Saved!"
        | Err e -> println e

      # Edge detection pipeline
      match OpenCV.to-grayscale img
        | Ok gray ->
          match OpenCV.canny gray 50.0 150.0
            | Ok edges ->
              OpenCV.save edges "/tmp/edges.png"
              OpenCV.free edges
            | Err e -> println e
          OpenCV.free gray
        | Err e -> println e

      OpenCV.free img
    | Err e -> println e

main

Development

Running Examples

Run examples with the interpreter:

kit run examples/basic.kit

Compile examples to a native binary:

kit build examples/basic.kit && ./basic

Running Tests

Run the test suite:

kit test

Run the test suite with coverage:

kit test --coverage

Running kit dev

Run the standard development workflow (format, check, test):

kit dev

This will:

  1. Format and check source files in src/
  2. Run tests in tests/ with coverage

Generating Documentation

Generate API documentation from doc comments:

kit doc

Note: Kit sources with doc comments (##) will generate HTML documents in docs/*.html

Cleaning Build Artifacts

Remove generated files, caches, and build artifacts:

kit task clean

Note: Defined in kit.toml.

Local Installation

To install this package locally for development:

kit install

This installs the package to ~/.kit/packages/@kit/opencv/, making it available for import as Kit.OpenCV in other projects.

License

This package is released under the MIT License - see LICENSE for details.

OpenCV is released under the Apache 2.0 License.

Exported Functions & Types

load

Load an image from a file.

Parameters:

Returns:

String -> Result Ptr String

save

Save an image to a file. Format is determined by file extension.

Parameters:

Returns:

Ptr -> String -> Result Unit String

free

Free an image and release its resources.

Parameters:

    Ptr -> Unit

width

Get the width of an image in pixels.

Parameters:

Returns:

Ptr -> Int

height

Get the height of an image in pixels.

Parameters:

Returns:

Ptr -> Int

channels

Get the number of channels in an image.

Parameters:

Returns:

Ptr -> Int

create

Create a blank image filled with black.

Parameters:

Returns:

Int -> Int -> Int -> Result Ptr String

clone

Deep copy an image.

Parameters:

Returns:

Ptr -> Result Ptr String

to-grayscale

Convert an image to grayscale.

Parameters:

Returns:

Ptr -> Result Ptr String

to-rgb

Convert an image from BGR to RGB color order.

Parameters:

Returns:

Ptr -> Result Ptr String

resize

Resize an image to the given dimensions.

Parameters:

Returns:

Ptr -> Int -> Int -> Result Ptr String

blur

Apply Gaussian blur to an image.

Parameters:

Returns:

Ptr -> Int -> Result Ptr String

threshold

Apply binary threshold to an image. Auto-converts to grayscale if needed.

Parameters:

Returns:

Ptr -> Float -> Float -> Result Ptr String

canny

Detect edges using the Canny algorithm. Auto-converts to grayscale if needed.

Parameters:

Returns:

Ptr -> Float -> Float -> Result Ptr String

sobel

Compute Sobel derivatives. Auto-converts to grayscale if needed.

Parameters:

Returns:

Ptr -> Int -> Int -> Result Ptr String

erode

Apply morphological erosion to an image.

Parameters:

Returns:

Ptr -> Int -> Result Ptr String

dilate

Apply morphological dilation to an image.

Parameters:

Returns:

Ptr -> Int -> Result Ptr String

draw-line

Draw a line on an image (mutates in place).

Parameters:

    Ptr -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Unit

draw-rectangle

Draw a rectangle on an image (mutates in place).

Parameters:

    Ptr -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Unit

draw-circle

Draw a circle on an image (mutates in place).

Parameters:

    Ptr -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Unit

draw-text

Draw text on an image (mutates in place).

Parameters:

    Ptr -> String -> Int -> Int -> Float -> Int -> Int -> Int -> Unit

named-window

Create a named display window.

Parameters:

    String -> Unit

show

Show an image in a named window.

Parameters:

    String -> Ptr -> Unit

wait-key

Wait for a keypress. Returns the key code.

Parameters:

Returns:

Int -> Int

destroy-all-windows

Close all display windows.

Unit

version

Get the OpenCV version string.

Returns:

String

get-pixel

Get a single pixel channel value.

Parameters:

Returns:

Ptr -> Int -> Int -> Int -> Float

set-pixel

Set a single pixel channel value (mutates in place).

Parameters:

    Ptr -> Int -> Int -> Int -> Float -> Unit

get-pixel-bgr

Get BGR pixel values as a record.

Parameters:

Returns:

Ptr -> Int -> Int -> {b: Float, g: Float, r: Float}

set-pixel-bgr

Set BGR pixel values (mutates in place).

Parameters:

    Ptr -> Int -> Int -> Int -> Int -> Int -> Unit

copy

Deep copy an image (alias for clone).

Parameters:

Returns:

Ptr -> Result Ptr String

fill

Fill an image with a BGR color (mutates in place).

Parameters:

    Ptr -> Int -> Int -> Int -> Unit

set-zero

Clear an image to black (mutates in place).

Parameters:

    Ptr -> Unit

convert-scale

Scale and shift pixel values. Output = src * scale + shift.

Parameters:

Returns:

Ptr -> Float -> Float -> Result Ptr String

flip

Flip an image along an axis.

Parameters:

Returns:

Ptr -> Int -> Result Ptr String

transpose

Transpose an image (swap rows and columns).

Parameters:

Returns:

Ptr -> Result Ptr String

bitwise-and

Per-element bitwise AND of two images.

Parameters:

Returns:

Ptr -> Ptr -> Result Ptr String

bitwise-or

Per-element bitwise OR of two images.

Parameters:

Returns:

Ptr -> Ptr -> Result Ptr String

bitwise-xor

Per-element bitwise XOR of two images.

Parameters:

Returns:

Ptr -> Ptr -> Result Ptr String

bitwise-not

Per-element bitwise NOT (invert) of an image.

Parameters:

Returns:

Ptr -> Result Ptr String

add

Per-element addition of two images (saturating).

Parameters:

Returns:

Ptr -> Ptr -> Result Ptr String

subtract

Per-element subtraction of two images (saturating).

Parameters:

Returns:

Ptr -> Ptr -> Result Ptr String

add-weighted

Alpha-blend two images. Output = src1 * alpha + src2 * beta + gamma.

Parameters:

Returns:

Ptr -> Float -> Ptr -> Float -> Float -> Result Ptr String

abs-diff

Per-element absolute difference of two images.

Parameters:

Returns:

Ptr -> Ptr -> Result Ptr String