Standard library · AI

ai.opencv

Computer vision with OpenCV: image processing, features, face detection, neural networks and video.

import ai.opencv

This package binds the OpenCV library and gives most of the everyday computer vision work: color conversion, resizing, filters, edge detection and thresholds, contours, ORB and SIFT features, face detection with a cascade classifier, running DNN networks, and reading and writing video and camera frames.

Use it for real computer vision: finding objects, matching features, reading camera frames, running a detection network. For thumbnails, drawing, text on images and format conversion with no outside library, the image package is enough. To run a model file without OpenCV, see onnx.

Things to know

  • Programs link against the salam_opencv_shim library, which in turn needs OpenCV 4. If the shim is missing, the build fails at the link step or the program refuses to start with "cannot open shared object file"; it never gets as far as your code.
  • A Mat stores colour in OpenCV's blue, green, red order, but every function of this package takes and returns Color values as red, green, blue, and FromImage and ToImage convert for you.
  • Every Mat owns native memory that Salam does not track. Free each one, especially inside a loop over video frames.
  • Calls that fail return an empty Mat (or false) and leave the reason in LastError.

The examples need OpenCV, so the site only type-checks them.

Set up OpenCV

The package talks to OpenCV 4 through a small C++ shim, salam_opencv_shim, that lives in std/ai/opencv/native of the Salam repository. On Debian or Ubuntu, install OpenCV and the build tools, build the shim, and let the loader find it:

sudo apt install libopencv-dev cmake g++
cd std/ai/opencv
cmake -S native -B native/build -DCMAKE_BUILD_TYPE=Release
cmake --build native/build --config Release
export LD_LIBRARY_PATH=$PWD/native/build:$LD_LIBRARY_PATH

Every program on this page starts by calling Available, which reports whether OpenCV answers and returns its version through Version. The outputs shown below are what the programs printed with OpenCV 4.10 on Linux.

Find and measure shapes

The classic pipeline for counting objects is: convert to gray, threshold to a black and white mask, then find the contours of the white areas. THRESH_BINARY_INV makes dark shapes on a light background white. Contours gives the number of shapes and, for each one, its area and bounding_rect. Drawing methods such as rectangle and circle change the image in place, and a thickness of -1 fills the shape.

opencv-guide-shapes.salam
import ai.opencv

func main:
    if not opencv.Available():
        println "OpenCV is not available:", opencv.LastError()
        ret 1
    end
    mut board := opencv.NewBlank(400, 300, 3, opencv.White())
    defer board.free()
    board.rectangle(40, 40, 120, 80, opencv.Black(), -1)
    board.circle(280, 90, 50, opencv.Black(), -1)
    board.rectangle(150, 190, 200, 70, opencv.Black(), -1)

    mut gray := board.to_gray()
    mut mask := gray.threshold(128.0, 255.0, opencv.THRESH_BINARY_INV)
    mut found := mask.find_contours()
    defer gray.free()
    defer mask.free()
    defer found.free()
    println "shapes found:", found.count()
    repeat found.count() in i:
        box := found.bounding_rect(i)
        println "  shape at", box.x, box.y, "size", box.width, "x", box.height, "area", found.area(i) as int
        board.rectangle(box.x - 3, box.y - 3, box.width + 6, box.height + 6, opencv.Red(), 2)
    end
    println "saved:", board.save("shapes.png")
end
shapes found: 3
  shape at 150 190 size 200 x 70 area 13731
  shape at 230 40 size 101 x 101 area 7704
  shape at 40 40 size 120 x 80 area 9401
saved: true

The area is measured along the traced outline, so it comes out a little smaller than width times height.

Edge detection

canny finds edges, and works best on a gray image that has been smoothed first with gaussian_blur, so that noise does not turn into tiny false edges. The two thresholds control how strong an edge must be to start a line and to continue one. dilate thickens the result, which helps when the edges go on to a contour search. When photo.jpg is missing, the program draws a test image instead.

opencv-guide-edges.salam
import ai.opencv

func white_pixels(m: opencv.Mat): int:
    hist := m.histogram_gray()
    ret hist.get(255)
end

func main:
    if not opencv.Available():
        println "OpenCV is not available:", opencv.LastError()
        ret 1
    end
    mut photo := opencv.Read("photo.jpg")
    if not photo.ok() or photo.empty():
        println "photo.jpg not found, using a drawn test image"
        photo = opencv.NewBlank(320, 240, 3, opencv.RGB(200, 200, 200))
        photo.circle(160, 120, 70, opencv.RGB(40, 40, 40), -1)
    end
    defer photo.free()
    println "size:", photo.width(), "x", photo.height(), "channels:", photo.channels()

    mut gray := photo.to_gray()
    mut smooth := gray.gaussian_blur(5, 1.4)
    mut edges := smooth.canny(50.0, 150.0)
    mut thick := edges.dilate(3, 1)
    defer gray.free()
    defer smooth.free()
    defer edges.free()
    defer thick.free()
    println "edge pixels:", white_pixels(edges), "after dilate:", white_pixels(thick)
    println "saved:", opencv.Write("edges.png", thick)
end
photo.jpg not found, using a drawn test image
size: 320 x 240 channels: 3
edge pixels: 480 after dilate: 1600
saved: true

Every method that returns a new Mat allocates; free each result, as the defer lines do, or a long running program, such as one that processes camera frames, will grow without limit.

Find a picture inside another

find_template slides a small image over a large one and reports the best position and a score between 0 and 1. That is how a test robot finds a button on a screenshot, or a program finds a logo in a scan. Check found and the score before you trust the position.

opencv-guide-template.salam
import ai.opencv

func main:
    if not opencv.Available():
        println "OpenCV is not available:", opencv.LastError()
        ret 1
    end
    mut screen := opencv.NewBlank(640, 480, 3, opencv.RGB(230, 230, 230))
    defer screen.free()
    screen.rectangle(420, 300, 90, 40, opencv.RGB(30, 120, 220), -1)
    screen.put_text("OK", 445, 330, 0.9, opencv.White(), 2)
    screen.rectangle(100, 100, 200, 60, opencv.RGB(120, 120, 120), -1)

    mut button := screen.crop(420, 300, 90, 40)
    defer button.free()
    hit := screen.find_template(button)
    if hit.found:
        println "button at", hit.x, hit.y, "score above 0.99:", hit.score > 0.99
        screen.rectangle(hit.x, hit.y, 90, 40, opencv.Red(), 2)
    else:
        println "button not on screen"
    end
    println "saved:", screen.save("found.png")
end
button at 420 300 score above 0.99: true
saved: true

Write and read video

NewVideoWriter takes a file name, a four letter codec, the frames per second and the frame size; every frame you write must have that size. OpenVideo reads a file and OpenCamera(0) the first camera; read returns an empty Mat when there are no more frames. Which codecs work depends on how OpenCV was built; MJPG in an .avi file is available almost everywhere.

opencv-guide-video.salam
import ai.opencv

func main:
    if not opencv.Available():
        println "OpenCV is not available:", opencv.LastError()
        ret 1
    end
    mut writer := opencv.NewVideoWriter("ball.avi", "MJPG", 10.0, 160, 120)
    if not writer.ok():
        println "this OpenCV build cannot write MJPG video"
        ret 1
    end
    repeat 20 in i:
        mut frame := opencv.NewBlank(160, 120, 3, opencv.Black())
        frame.circle(10 + i * 7, 60, 8, opencv.Green(), -1)
        writer.write(frame)
        frame.free()
    end
    writer.close()

    mut video := opencv.OpenVideo("ball.avi")
    if not video.ok():
        println "could not open ball.avi"
        ret 1
    end
    println "frames:", video.frame_count(), "fps:", video.fps()
    mut read := 0
    mut last := opencv.Mat {}
    until true:
        frame := video.read()
        if frame.empty(): break end
        read++
        last.free()
        last = frame
    end
    println "read", read, "frames, last frame", last.width(), "x", last.height()
    last.free()
    video.close()
end
frames: 20 fps: 10
read 20 frames, last frame 160 x 120

To show frames in a window while you work, use Imshow and WaitKey; they need a desktop session. ToImage and FromImage move pictures between this package and image, for example to draw Persian text with a TrueType font.

Types

Mat

struct Mat
An OpenCV image or matrix with BGR channels.
NameTypeDescription
handlevoid*The internal handle.
ai-opencv-Basics.salam
import ai.opencv

func main:
    if not opencv.Available():
        println "OpenCV is not available:", opencv.LastError()
        ret
    end
    println opencv.Version()
    mut photo := opencv.Read("photo_en.jpg")
    if not photo.ok() or photo.empty():
        photo = opencv.NewBlank(320, 240, 3, opencv.White())
    end
    dims := photo.size()
    println photo.width(), photo.height(), photo.channels(), dims.width, dims.height, photo.step(), photo.total(), photo.data() != null
    color := photo.at(10, 10)
    println color.r, color.g, color.b
    photo.set_pixel(0, 0, opencv.RGB(255, 128, 0))
    black := opencv.Zeros(10, 10, 1)
    println black.channels(), opencv.Black().r, opencv.Red().r, opencv.Green().g, opencv.Blue().b

    gray := photo.to_gray()
    _ := photo.to_rgb()
    hsv := photo.to_hsv()
    _ := hsv.hsv_to_rgb()
    _ := photo.convert_color(opencv.COLOR_BGR2GRAY)
    _ := gray.convert_color(opencv.COLOR_GRAY2BGR)
    println opencv.COLOR_RGB2GRAY, opencv.COLOR_GRAY2RGB, opencv.COLOR_BGR2RGB, opencv.COLOR_RGB2BGR, opencv.COLOR_BGR2HSV, opencv.COLOR_HSV2BGR, opencv.COLOR_RGB2HSV, opencv.COLOR_HSV2RGB

    small := photo.resize(160, 120, opencv.INTERP_AREA)
    _ := photo.resize_scale(2.0, 2.0)
    _ := photo.crop(10, 10, 100, 80)
    _ := photo.rotate90(opencv.ROTATE_90_CW)
    _ := photo.rotate(45.0, 1.0)
    _ := photo.flip(opencv.FLIP_BOTH)
    _ := photo.flip_horizontal()
    _ := photo.flip_vertical()
    println opencv.INTERP_NEAREST, opencv.INTERP_LINEAR, opencv.INTERP_CUBIC, opencv.INTERP_LANCZOS4, opencv.ROTATE_180, opencv.ROTATE_90_CCW, opencv.FLIP_VERTICAL, opencv.FLIP_HORIZONTAL
    copied := small.clone()
    println copied.save("small_en.png"), opencv.Write("gray_en.png", gray)
    copied.free()
    photo.free()
end

ok

func ok(): bool
Whether it is valid.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

empty

func empty(): bool
Whether it is empty.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

width

func width(): int
The width.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

height

func height(): int
The height.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

channels

func channels(): int
The number of channels.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

size

func size(): Size
The size.
ai-opencv-Features.salam
import ai.opencv

func main:
    mut scene := opencv.Read("scene_en.jpg")
    mut target := opencv.Read("object_en.jpg")
    mut contours := scene.to_gray().threshold_otsu(255.0).find_contours()
    println contours.ok(), contours.count()
    repeat contours.count() in i:
        box := contours.bounding_rect(i)
        println contours.area(i), contours.point_count(i), contours.point(i, 0).x, box.x, box.y, box.width, box.height
        scene.draw_contour(contours, i, opencv.Green(), 2)
    end
    contours.free()

    corners := scene.to_gray().good_features(50, 0.01, 10.0)
    println corners.len()
    mut keys1 := scene.detect_orb(500)
    mut keys2 := target.detect_orb(500)
    mut sift := scene.detect_sift(500)
    println keys1.ok(), keys1.count(), sift.count()
    if keys1.count() > 0:
        first := keys1.get(0)
        println first.x, first.y, first.size, first.angle, first.response
    end
    matches := opencv.MatchDescriptors(keys1.descriptors(), keys2.descriptors(), 50, opencv.NORM_HAMMING)
    each m in matches:
        println m.query_idx, m.train_idx, m.distance
    end
    println opencv.NORM_L2
    keys1.free()
    keys2.free()
    sift.free()

    found := scene.find_template(target)
    println found.found, found.x, found.y, found.score
    mut quad := Vector {} as Vector<opencv.Point>
    quad.push(opencv.Point { x = 0, y = 0 })
    quad.push(opencv.Point { x = 400, y = 10 })
    quad.push(opencv.Point { x = 410, y = 580 })
    quad.push(opencv.Point { x = 5, y = 590 })
    flat := scene.warp_perspective(quad, 420, 594)
    _ := opencv.Write("document_en.png", flat)
end

clone

func clone(): Mat
A copy.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

free

func free()
Frees it.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

save

func save(path: str): bool
Saves it to a file.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

data

func data(): void*
A pointer to the pixels.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

step

func step(): int
The bytes in each row.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

total

func total(): int
The number of pixels.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

at

func at(x: int, y: int): Color
The color of a pixel.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

set_pixel

func set_pixel(x: int, y: int, c: Color)
Sets the color of a pixel.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

convert_color

func convert_color(code: int): Mat
Converts the colors with one of the conversion constants.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

to_gray

func to_gray(): Mat
Gray.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

to_rgb

func to_rgb(): Mat
RGB.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

to_hsv

func to_hsv(): Mat
HSV; for picking out a color.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

hsv_to_rgb

func hsv_to_rgb(): Mat
HSV to RGB.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

resize

func resize(width: int, height: int, interpolation: int): Mat
Resizes with an interpolation method.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

resize_scale

func resize_scale(fx: f64, fy: f64): Mat
Resizes by a factor.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

crop

func crop(x: int, y: int, w: int, h: int): Mat
Cuts out a rectangle.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

rotate90

func rotate90(code: int): Mat
Rotates by a multiple of ninety degrees.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

rotate

func rotate(angle_degrees: f64, scale: f64): Mat
Rotates by an angle and a scale.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

flip

func flip(mode: int): Mat
Flips.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

flip_horizontal

func flip_horizontal(): Mat
Flips horizontally.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

flip_vertical

func flip_vertical(): Mat
Flips vertically.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

blur

func blur(ksize: int): Mat
Mean blur.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

gaussian_blur

func gaussian_blur(ksize: int, sigma: f64): Mat
Gaussian blur.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

median_blur

func median_blur(ksize: int): Mat
Median blur; for salt and pepper noise.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

canny

func canny(t1: f64, t2: f64): Mat
Canny edge detection with two thresholds.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

threshold

func threshold(thresh: f64, maxval: f64, kind: int): Mat
Applies a threshold.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

threshold_otsu

func threshold_otsu(maxval: f64): Mat
Otsu's automatic threshold.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

adaptive_threshold

func adaptive_threshold(maxval: f64, method: int, kind: int, block_size: int, c: f64): Mat
Adaptive threshold; for uneven light.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

histogram_gray

func histogram_gray(): Vector<int>
The 256 bin histogram of a gray image.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

line

func line(x1: int, y1: int, x2: int, y2: int, color: Color, thickness: int)
A line.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

rectangle

func rectangle(x: int, y: int, w: int, h: int, color: Color, thickness: int)
A rectangle; a negative thickness fills it.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

circle

func circle(cx: int, cy: int, radius: int, color: Color, thickness: int)
A circle.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

put_text

func put_text(text: str, x: int, y: int, font_scale: f64, color: Color, thickness: int)
Latin text.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

good_features

func good_features(max_corners: int, quality: f64, min_dist: f64): Vector<Point>
Good corners to track.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

detect_orb

func detect_orb(max_features: int): Keypoints
ORB keypoints and descriptors.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

detect_sift

func detect_sift(max_features: int): Keypoints
SIFT keypoints.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

erode

func erode(ksize: int, iterations: int): Mat
Erosion.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

dilate

func dilate(ksize: int, iterations: int): Mat
Dilation.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

morphology

func morphology(op: int, ksize: int): Mat
A morphological operation.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

blend

func blend(other: Mat, alpha: f64, beta: f64, gamma: f64): Mat
A weighted blend of two images.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

bitwise

func bitwise(other: Mat, op: int): Mat
A bitwise operation with another image.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

bitwise_not

func bitwise_not(): Mat
Bitwise NOT.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

abs_diff

func abs_diff(other: Mat): Mat
The absolute difference; for motion detection.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

warp_perspective

func warp_perspective(src: Vector<Point>, out_width: int, out_height: int): Mat
Turns four corners into a flat rectangle; for scanning documents.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

find_template

func find_template(template: Mat): TemplateMatch
Where a template sits in the image.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

ellipse

func ellipse(cx: int, cy: int, axis_width: int, axis_height: int, angle_degrees: f64, color: Color, thickness: int)
An ellipse.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

polylines

func polylines(pts: Vector<Point>, closed: bool, color: Color, thickness: int)
A polyline.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

fill_poly

func fill_poly(pts: Vector<Point>, color: Color)
A filled polygon.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

find_contours

func find_contours(): Contours
The contours of the shapes in a binary image.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

draw_contour

func draw_contour(cs: Contours, idx: int, color: Color, thickness: int)
Draws one contour.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

TemplateMatch

struct TemplateMatch
The result of a template search.
NameTypeDescription
foundbool = falseWhether it was found.
xint = 0x.
yint = 0y.
scoref64 = 0The match score.

The example for this item is shown with the ones above: ai-opencv-Features.salam

Size

struct Size
A size.
NameTypeDescription
widthint = 0The width.
heightint = 0The height.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

Point

struct Point
A point.
NameTypeDescription
xint = 0x.
yint = 0y.
ai-opencv-Filters.salam
import ai.opencv

func main:
    mut photo := opencv.Read("photo_en.jpg")
    gray := photo.to_gray()
    _ := photo.blur(5)
    _ := photo.gaussian_blur(5, 1.5)
    _ := photo.median_blur(5)
    edges := gray.canny(50.0, 150.0)
    _ := gray.threshold(128.0, 255.0, opencv.THRESH_BINARY)
    _ := gray.threshold_otsu(255.0)
    _ := gray.adaptive_threshold(255.0, opencv.ADAPTIVE_GAUSSIAN, opencv.THRESH_BINARY, 11, 2.0)
    println opencv.THRESH_BINARY_INV, opencv.THRESH_TRUNC, opencv.THRESH_TOZERO, opencv.THRESH_TOZERO_INV, opencv.THRESH_OTSU, opencv.ADAPTIVE_MEAN
    println gray.histogram_gray().len()
    _ := edges.erode(3, 1)
    _ := edges.dilate(3, 2)
    _ := edges.morphology(opencv.MORPH_CLOSE, 5)
    println opencv.MORPH_OPEN, opencv.MORPH_GRADIENT, opencv.MORPH_TOPHAT, opencv.MORPH_BLACKHAT
    white := opencv.NewBlank(photo.width(), photo.height(), 3, opencv.White())
    _ := photo.blend(white, 0.7, 0.3, 0.0)
    _ := photo.bitwise(white, opencv.BITWISE_AND)
    _ := photo.bitwise(white, opencv.BITWISE_OR)
    _ := photo.bitwise(white, opencv.BITWISE_XOR)
    _ := photo.bitwise_not()
    _ := photo.abs_diff(white)
    photo.line(0, 0, 100, 100, opencv.Red(), 2)
    photo.rectangle(10, 10, 50, 30, opencv.Green(), 1)
    photo.circle(80, 80, 20, opencv.Blue(), -1)
    photo.ellipse(100, 50, 40, 20, 30.0, opencv.Black(), 2)
    photo.put_text("Salam", 10, 200, 1.0, opencv.White(), 2)
    mut points := Vector {} as Vector<opencv.Point>
    points.push(opencv.Point { x = 0, y = 0 })
    points.push(opencv.Point { x = 50, y = 0 })
    points.push(opencv.Point { x = 25, y = 40 })
    photo.polylines(points, true, opencv.Red(), 2)
    photo.fill_poly(points, opencv.Green())
    _ := opencv.Write("filtered_en.png", photo)
end

Rect

struct Rect
A rectangle.
NameTypeDescription
xint = 0x.
yint = 0y.
widthint = 0The width.
heightint = 0The height.

The example for this item is shown with the ones above: ai-opencv-Filters.salam

Color

struct Color
A color.
NameTypeDescription
rint = 0Red.
gint = 0Green.
bint = 0Blue.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

Keypoint

struct Keypoint
One keypoint.
NameTypeDescription
xf64 = 0x.
yf64 = 0y.
sizef64 = 0The size.
anglef64 = 0The angle.
responsef64 = 0The strength.

The example for this item is shown with the ones above: ai-opencv-Features.salam

Keypoints

struct Keypoints
Keypoints and descriptors.
NameTypeDescription
handlevoid*The internal handle.

The example for this item is shown with the ones above: ai-opencv-Features.salam

ok

func ok(): bool
Whether it is valid.

The example for this item is shown with the ones above: ai-opencv-Features.salam

count

func count(): int
The count.

The example for this item is shown with the ones above: ai-opencv-Features.salam

get

func get(i: int): Keypoint
Keypoint i.

The example for this item is shown with the ones above: ai-opencv-Features.salam

descriptors

func descriptors(): Mat
The descriptors.

The example for this item is shown with the ones above: ai-opencv-Features.salam

free

func free()
Frees it.

The example for this item is shown with the ones above: ai-opencv-Features.salam

Match

struct Match
One match.
NameTypeDescription
query_idxint = 0The index in the first set.
train_idxint = 0The index in the second set.
distancef64 = 0The distance; lower means more alike.

The example for this item is shown with the ones above: ai-opencv-Features.salam

Contours

struct Contours
Contours.
NameTypeDescription
handlevoid*The internal handle.

The example for this item is shown with the ones above: ai-opencv-Features.salam

ok

func ok(): bool
Whether it is valid.

The example for this item is shown with the ones above: ai-opencv-Features.salam

count

func count(): int
The number of contours.

The example for this item is shown with the ones above: ai-opencv-Features.salam

point_count

func point_count(idx: int): int
The number of points in a contour.

The example for this item is shown with the ones above: ai-opencv-Features.salam

point

func point(idx: int, pt_idx: int): Point
One point.

The example for this item is shown with the ones above: ai-opencv-Features.salam

area

func area(idx: int): f64
The area.

The example for this item is shown with the ones above: ai-opencv-Features.salam

bounding_rect

func bounding_rect(idx: int): Rect
The bounding rectangle.

The example for this item is shown with the ones above: ai-opencv-Features.salam

free

func free()
Frees it.

The example for this item is shown with the ones above: ai-opencv-Features.salam

DetectedObject

struct DetectedObject
A detected object.
NameTypeDescription
xint = 0x.
yint = 0y.
widthint = 0The width.
heightint = 0The height.
ai-opencv-Detect.salam
import ai.opencv
import ai.image

func main:
    mut cascade := opencv.LoadCascade("haarcascade_frontalface_default.xml")
    mut photo := opencv.Read("people_en.jpg")
    if cascade.ok():
        each face in cascade.detect(photo.to_gray(), 1.1, 5, 100):
            photo.rectangle(face.x, face.y, face.width, face.height, opencv.Green(), 2)
        end
    end
    cascade.free()

    mut net := opencv.LoadNet("MobileNetSSD_en.caffemodel", "MobileNetSSD_en.prototxt")
    mut single := opencv.LoadNetSingleFile("model_en.onnx")
    if net.ok():
        _ := net.set_input(photo, 0.007843, opencv.RGB(127, 127, 127), false, opencv.Size { width = 300, height = 300 })
        output := net.forward()
        _ := net.forward_layer("detection_out")
        each obj in opencv.ParseSSDDetections(output, photo.width(), photo.height(), 0.5):
            println obj.class_id, obj.confidence, obj.x, obj.y, obj.width, obj.height
        end
    end
    net.free()
    single.free()

    mut video := opencv.OpenVideo("clip_en.mp4")
    mut camera := opencv.OpenCamera(0)
    mut writer := opencv.NewVideoWriter("out_en.mp4", "mp4v", video.fps(), 640, 480)
    println video.ok(), video.frame_count(), camera.ok(), writer.ok()
    opencv.CreateTrackbar("threshold", "preview", 100, 255)
    until video.ok():
        frame := video.read()
        if frame.empty():
            break
        end
        level := opencv.GetTrackbarPos("threshold", "preview")
        edges := frame.to_gray().canny(level as f64, 200.0)
        _ := writer.write(frame)
        opencv.Imshow("preview", edges)
        if opencv.WaitKey(1) == 27:
            break
        end
    end
    opencv.SetTrackbarPos("threshold", "preview", 50)
    opencv.DestroyWindow("preview")
    opencv.DestroyAllWindows()
    video.close()
    camera.close()
    writer.close()

    picture := opencv.ToImage(photo)
    println picture.width
    _ := opencv.FromImage(image.New(8, 8))
end

CascadeClassifier

struct CascadeClassifier
A Haar cascade classifier; for face detection.
NameTypeDescription
handlevoid*The internal handle.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

ok

func ok(): bool
Whether it loaded.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

detect

func detect(m: Mat, scale_factor: f64, min_neighbors: int, max_results: int): Vector<DetectedObject>
Finds objects with a scale factor and a minimum neighbor count.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

free

func free()
Frees it.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

VideoCapture

struct VideoCapture
Reads video or a camera.
NameTypeDescription
handlevoid*The internal handle.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

ok

func ok(): bool
Whether it is open.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

read

func read(): Mat
The next frame; empty at the end.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

fps

func fps(): f64
Frames per second.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

frame_count

func frame_count(): int
The number of frames.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

close

func close()
Closes it.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

VideoWriter

struct VideoWriter
Writes video.
NameTypeDescription
handlevoid*The internal handle.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

ok

func ok(): bool
Whether it is open.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

write

func write(frame: Mat): bool
Writes a frame.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

close

func close()
Closes it.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

Net

struct Net
A DNN neural network.
NameTypeDescription
handlevoid*The internal handle.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

ok

func ok(): bool
Whether it loaded.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

set_input

func set_input(image: Mat, scale: f64, mean: Color, swap_rb: bool, size: Size): bool
Prepares the input with a scale, a mean, a channel swap and a size.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

forward

func forward(): Mat
Runs the network.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

forward_layer

func forward_layer(layer_name: str): Mat
The output of one layer.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

free

func free()
Frees it.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

Detection

struct Detection
One object found with DNN.
NameTypeDescription
class_idint = 0The class number.
confidencef64 = 0The confidence.
xint = 0x.
yint = 0y.
widthint = 0The width.
heightint = 0The height.

The example for this item is shown with the ones above: ai-opencv-Features.salam

Constants and variables

COLOR_BGR2GRAY

const COLOR_BGR2GRAY: i32 = 0
Color conversion: BGR to gray.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

COLOR_GRAY2BGR

const COLOR_GRAY2BGR: i32 = 1
Color conversion: gray to BGR.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

COLOR_RGB2GRAY

const COLOR_RGB2GRAY: i32 = 2
Color conversion: RGB to gray.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

COLOR_GRAY2RGB

const COLOR_GRAY2RGB: i32 = 3
Color conversion: gray to RGB.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

COLOR_BGR2RGB

const COLOR_BGR2RGB: i32 = 4
Color conversion: BGR to RGB.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

COLOR_RGB2BGR

const COLOR_RGB2BGR: i32 = 5
Color conversion: RGB to BGR.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

COLOR_BGR2HSV

const COLOR_BGR2HSV: i32 = 6
Color conversion: BGR to HSV.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

COLOR_HSV2BGR

const COLOR_HSV2BGR: i32 = 7
Color conversion: HSV to BGR.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

COLOR_RGB2HSV

const COLOR_RGB2HSV: i32 = 8
Color conversion: RGB to HSV.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

COLOR_HSV2RGB

const COLOR_HSV2RGB: i32 = 9
Color conversion: HSV to RGB.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

THRESH_BINARY

const THRESH_BINARY: i32 = 0
Threshold: the maximum above the threshold, zero elsewhere.

The example for this item is shown with the ones above: ai-opencv-Filters.salam

THRESH_BINARY_INV

const THRESH_BINARY_INV: i32 = 1
Threshold: inverted binary.

The example for this item is shown with the ones above: ai-opencv-Filters.salam

THRESH_TRUNC

const THRESH_TRUNC: i32 = 2
Threshold: values above the threshold become the threshold.

The example for this item is shown with the ones above: ai-opencv-Filters.salam

THRESH_TOZERO

const THRESH_TOZERO: i32 = 3
Threshold: values below the threshold become zero.

The example for this item is shown with the ones above: ai-opencv-Filters.salam

THRESH_TOZERO_INV

const THRESH_TOZERO_INV: i32 = 4
Threshold: values above the threshold become zero.

The example for this item is shown with the ones above: ai-opencv-Filters.salam

THRESH_OTSU

const THRESH_OTSU: i32 = 5
Threshold: Otsu's automatic threshold.

The example for this item is shown with the ones above: ai-opencv-Filters.salam

ADAPTIVE_MEAN

const ADAPTIVE_MEAN: i32 = 0
Adaptive threshold with the neighborhood mean.

The example for this item is shown with the ones above: ai-opencv-Filters.salam

ADAPTIVE_GAUSSIAN

const ADAPTIVE_GAUSSIAN: i32 = 1
Adaptive threshold with a Gaussian weighted mean.

The example for this item is shown with the ones above: ai-opencv-Filters.salam

INTERP_NEAREST

const INTERP_NEAREST: i32 = 0
Interpolation: nearest neighbor.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

INTERP_LINEAR

const INTERP_LINEAR: i32 = 1
Interpolation: bilinear.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

INTERP_CUBIC

const INTERP_CUBIC: i32 = 2
Interpolation: bicubic.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

INTERP_AREA

const INTERP_AREA: i32 = 3
Interpolation: area based, the best for shrinking.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

INTERP_LANCZOS4

const INTERP_LANCZOS4: i32 = 4
Interpolation: Lanczos.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

ROTATE_90_CW

const ROTATE_90_CW: i32 = 0
Rotate ninety degrees clockwise.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

ROTATE_180

const ROTATE_180: i32 = 1
Rotate one hundred and eighty degrees.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

ROTATE_90_CCW

const ROTATE_90_CCW: i32 = 2
Rotate ninety degrees counterclockwise.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

FLIP_VERTICAL

const FLIP_VERTICAL: i32 = 0
Vertical flip.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

FLIP_HORIZONTAL

const FLIP_HORIZONTAL: i32 = 1
Horizontal flip.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

FLIP_BOTH

const FLIP_BOTH: i32 = 2
Flip in both directions.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

MORPH_OPEN

const MORPH_OPEN: i32 = 0
Opening: erosion then dilation; removes small specks.

The example for this item is shown with the ones above: ai-opencv-Filters.salam

MORPH_CLOSE

const MORPH_CLOSE: i32 = 1
Closing: dilation then erosion; fills small holes.

The example for this item is shown with the ones above: ai-opencv-Filters.salam

MORPH_GRADIENT

const MORPH_GRADIENT: i32 = 2
Gradient: the outline of the shapes.

The example for this item is shown with the ones above: ai-opencv-Filters.salam

MORPH_TOPHAT

const MORPH_TOPHAT: i32 = 3
Top hat.

The example for this item is shown with the ones above: ai-opencv-Filters.salam

MORPH_BLACKHAT

const MORPH_BLACKHAT: i32 = 4
Black hat.

The example for this item is shown with the ones above: ai-opencv-Filters.salam

BITWISE_AND

const BITWISE_AND: i32 = 0
Bitwise AND.

The example for this item is shown with the ones above: ai-opencv-Filters.salam

BITWISE_OR

const BITWISE_OR: i32 = 1
Bitwise OR.

The example for this item is shown with the ones above: ai-opencv-Filters.salam

BITWISE_XOR

const BITWISE_XOR: i32 = 2
Bitwise exclusive or.

The example for this item is shown with the ones above: ai-opencv-Filters.salam

NORM_HAMMING

const NORM_HAMMING: i32 = 0
Hamming distance; for ORB descriptors.

The example for this item is shown with the ones above: ai-opencv-Features.salam

NORM_L2

const NORM_L2: i32 = 1
Euclidean distance; for SIFT.

The example for this item is shown with the ones above: ai-opencv-Features.salam

Functions

RGB

func RGB(r: int, g: int, b: int): Color
A color from red, green and blue.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

Read

func Read(path: str): Mat
Reads an image from a file.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

Write

func Write(path: str, m: Mat): bool
Writes an image to a file.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

NewBlank

func NewBlank(width: int, height: int, channels: int, fill: Color): Mat
A canvas filled with a color.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

Zeros

func Zeros(width: int, height: int, channels: int): Mat
A black canvas.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

MatchDescriptors

func MatchDescriptors(desc1: Mat, desc2: Mat, max_matches: int, norm: int): Vector<Match>
The best matches between two sets of descriptors.

The example for this item is shown with the ones above: ai-opencv-Features.salam

FromImage

func FromImage(img: image.Image): Mat
From an image of the image package.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

ToImage

func ToImage(m: Mat): image.Image
To an image of the image package.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

Imshow

func Imshow(window: str, m: Mat)
Shows an image in a window.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

WaitKey

func WaitKey(delay_ms: int): int
Waits up to delay milliseconds for a key; the key code or -1.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

DestroyWindow

func DestroyWindow(window: str)
Closes a window.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

DestroyAllWindows

func DestroyAllWindows()
Closes all windows.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

CreateTrackbar

func CreateTrackbar(name: str, window: str, initial: int, max: int): bool
A slider in a window.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

GetTrackbarPos

func GetTrackbarPos(name: str, window: str): int
The position of a slider.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

SetTrackbarPos

func SetTrackbarPos(name: str, window: str, pos: int)
Sets the position.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

LoadCascade

func LoadCascade(path: str): CascadeClassifier
Loads a classifier from an XML file.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

OpenVideo

func OpenVideo(path: str): VideoCapture
Opens a video file.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

OpenCamera

func OpenCamera(index: int): VideoCapture
A camera by number.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

NewVideoWriter

func NewVideoWriter(path: str, fourcc: str, fps: f64, width: int, height: int): VideoWriter
A writer with a four letter codec, frames per second and a size.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

LoadNet

func LoadNet(model_path: str, config_path: str): Net
A model and its configuration, such as Caffe.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

LoadNetSingleFile

func LoadNetSingleFile(path: str): Net
A single file model, such as ONNX.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

ParseSSDDetections

func ParseSSDDetections(output: Mat, img_width: int, img_height: int, confidence_threshold: f64): Vector<Detection>
Turns SSD output into the objects whose confidence is above the threshold.

The example for this item is shown with the ones above: ai-opencv-Detect.salam

LastError

func LastError(): str
The message of the last error.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

Available

func Available(): bool
True when OpenCV reports a version. A program whose shim library is missing does not start at all, so once your code runs this is true.

The example for this item is shown with the ones above: ai-opencv-Basics.salam

Version

func Version(): str
The OpenCV version.

The example for this item is shown with the ones above: ai-opencv-Basics.salam