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_shimlibrary, 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
Matstores colour in OpenCV's blue, green, red order, but every function of this package takes and returnsColorvalues as red, green, blue, andFromImageandToImageconvert for you. - Every
Matowns 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 inLastError.
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.
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")
endshapes 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.
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)
endphoto.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.
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")
endbutton 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.
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()
endframes: 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| Name | Type | Description |
|---|---|---|
handle | void* | The internal handle. |
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()
endok
func ok(): boolThe example for this item is shown with the ones above: ai-opencv-Basics.salam
empty
func empty(): boolThe example for this item is shown with the ones above: ai-opencv-Basics.salam
width
func width(): intThe example for this item is shown with the ones above: ai-opencv-Basics.salam
height
func height(): intThe example for this item is shown with the ones above: ai-opencv-Basics.salam
channels
func channels(): intThe example for this item is shown with the ones above: ai-opencv-Basics.salam
size
func size(): Sizeimport 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)
endclone
func clone(): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
free
func free()The example for this item is shown with the ones above: ai-opencv-Basics.salam
save
func save(path: str): boolThe example for this item is shown with the ones above: ai-opencv-Basics.salam
data
func data(): void*The example for this item is shown with the ones above: ai-opencv-Basics.salam
step
func step(): intThe example for this item is shown with the ones above: ai-opencv-Basics.salam
total
func total(): intThe example for this item is shown with the ones above: ai-opencv-Basics.salam
at
func at(x: int, y: int): ColorThe 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)The example for this item is shown with the ones above: ai-opencv-Basics.salam
convert_color
func convert_color(code: int): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
to_gray
func to_gray(): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
to_rgb
func to_rgb(): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
to_hsv
func to_hsv(): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
hsv_to_rgb
func hsv_to_rgb(): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
resize
func resize(width: int, height: int, interpolation: int): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
resize_scale
func resize_scale(fx: f64, fy: f64): MatThe 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): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
rotate90
func rotate90(code: int): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
rotate
func rotate(angle_degrees: f64, scale: f64): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
flip
func flip(mode: int): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
flip_horizontal
func flip_horizontal(): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
flip_vertical
func flip_vertical(): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
blur
func blur(ksize: int): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
gaussian_blur
func gaussian_blur(ksize: int, sigma: f64): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
median_blur
func median_blur(ksize: int): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
canny
func canny(t1: f64, t2: f64): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
threshold
func threshold(thresh: f64, maxval: f64, kind: int): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
threshold_otsu
func threshold_otsu(maxval: f64): MatThe 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): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
histogram_gray
func histogram_gray(): Vector<int>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)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)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)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)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>The example for this item is shown with the ones above: ai-opencv-Basics.salam
detect_orb
func detect_orb(max_features: int): KeypointsThe example for this item is shown with the ones above: ai-opencv-Basics.salam
detect_sift
func detect_sift(max_features: int): KeypointsThe example for this item is shown with the ones above: ai-opencv-Basics.salam
erode
func erode(ksize: int, iterations: int): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
dilate
func dilate(ksize: int, iterations: int): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
morphology
func morphology(op: int, ksize: int): MatThe 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): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
bitwise
func bitwise(other: Mat, op: int): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
bitwise_not
func bitwise_not(): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
abs_diff
func abs_diff(other: Mat): MatThe 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): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
find_template
func find_template(template: Mat): TemplateMatchThe 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)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)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)The example for this item is shown with the ones above: ai-opencv-Basics.salam
find_contours
func find_contours(): ContoursThe 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)The example for this item is shown with the ones above: ai-opencv-Basics.salam
TemplateMatch
struct TemplateMatch| Name | Type | Description |
|---|---|---|
found | bool = false | Whether it was found. |
x | int = 0 | x. |
y | int = 0 | y. |
score | f64 = 0 | The match score. |
The example for this item is shown with the ones above: ai-opencv-Features.salam
Size
struct Size| Name | Type | Description |
|---|---|---|
width | int = 0 | The width. |
height | int = 0 | The height. |
The example for this item is shown with the ones above: ai-opencv-Basics.salam
Point
struct Point| Name | Type | Description |
|---|---|---|
x | int = 0 | x. |
y | int = 0 | y. |
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)
endRect
struct Rect| Name | Type | Description |
|---|---|---|
x | int = 0 | x. |
y | int = 0 | y. |
width | int = 0 | The width. |
height | int = 0 | The height. |
The example for this item is shown with the ones above: ai-opencv-Filters.salam
Color
struct Color| Name | Type | Description |
|---|---|---|
r | int = 0 | Red. |
g | int = 0 | Green. |
b | int = 0 | Blue. |
The example for this item is shown with the ones above: ai-opencv-Basics.salam
Keypoint
struct Keypoint| Name | Type | Description |
|---|---|---|
x | f64 = 0 | x. |
y | f64 = 0 | y. |
size | f64 = 0 | The size. |
angle | f64 = 0 | The angle. |
response | f64 = 0 | The strength. |
The example for this item is shown with the ones above: ai-opencv-Features.salam
Keypoints
struct Keypoints| Name | Type | Description |
|---|---|---|
handle | void* | The internal handle. |
The example for this item is shown with the ones above: ai-opencv-Features.salam
ok
func ok(): boolThe example for this item is shown with the ones above: ai-opencv-Features.salam
count
func count(): intThe example for this item is shown with the ones above: ai-opencv-Features.salam
get
func get(i: int): KeypointThe example for this item is shown with the ones above: ai-opencv-Features.salam
descriptors
func descriptors(): MatThe example for this item is shown with the ones above: ai-opencv-Features.salam
free
func free()The example for this item is shown with the ones above: ai-opencv-Features.salam
Match
struct Match| Name | Type | Description |
|---|---|---|
query_idx | int = 0 | The index in the first set. |
train_idx | int = 0 | The index in the second set. |
distance | f64 = 0 | The distance; lower means more alike. |
The example for this item is shown with the ones above: ai-opencv-Features.salam
Contours
struct Contours| Name | Type | Description |
|---|---|---|
handle | void* | The internal handle. |
The example for this item is shown with the ones above: ai-opencv-Features.salam
ok
func ok(): boolThe example for this item is shown with the ones above: ai-opencv-Features.salam
count
func count(): intThe example for this item is shown with the ones above: ai-opencv-Features.salam
point_count
func point_count(idx: int): intThe example for this item is shown with the ones above: ai-opencv-Features.salam
point
func point(idx: int, pt_idx: int): PointThe example for this item is shown with the ones above: ai-opencv-Features.salam
area
func area(idx: int): f64The example for this item is shown with the ones above: ai-opencv-Features.salam
bounding_rect
func bounding_rect(idx: int): RectThe example for this item is shown with the ones above: ai-opencv-Features.salam
free
func free()The example for this item is shown with the ones above: ai-opencv-Features.salam
DetectedObject
struct DetectedObject| Name | Type | Description |
|---|---|---|
x | int = 0 | x. |
y | int = 0 | y. |
width | int = 0 | The width. |
height | int = 0 | The height. |
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))
endCascadeClassifier
struct CascadeClassifier| Name | Type | Description |
|---|---|---|
handle | void* | The internal handle. |
The example for this item is shown with the ones above: ai-opencv-Detect.salam
ok
func ok(): boolThe 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>The example for this item is shown with the ones above: ai-opencv-Detect.salam
free
func free()The example for this item is shown with the ones above: ai-opencv-Detect.salam
VideoCapture
struct VideoCapture| Name | Type | Description |
|---|---|---|
handle | void* | The internal handle. |
The example for this item is shown with the ones above: ai-opencv-Detect.salam
ok
func ok(): boolThe example for this item is shown with the ones above: ai-opencv-Detect.salam
read
func read(): MatThe example for this item is shown with the ones above: ai-opencv-Detect.salam
fps
func fps(): f64The example for this item is shown with the ones above: ai-opencv-Detect.salam
frame_count
func frame_count(): intThe example for this item is shown with the ones above: ai-opencv-Detect.salam
close
func close()The example for this item is shown with the ones above: ai-opencv-Detect.salam
VideoWriter
struct VideoWriter| Name | Type | Description |
|---|---|---|
handle | void* | The internal handle. |
The example for this item is shown with the ones above: ai-opencv-Detect.salam
ok
func ok(): boolThe example for this item is shown with the ones above: ai-opencv-Detect.salam
write
func write(frame: Mat): boolThe example for this item is shown with the ones above: ai-opencv-Detect.salam
close
func close()The example for this item is shown with the ones above: ai-opencv-Detect.salam
Net
struct Net| Name | Type | Description |
|---|---|---|
handle | void* | The internal handle. |
The example for this item is shown with the ones above: ai-opencv-Detect.salam
ok
func ok(): boolThe 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): boolThe example for this item is shown with the ones above: ai-opencv-Detect.salam
forward
func forward(): MatThe example for this item is shown with the ones above: ai-opencv-Detect.salam
forward_layer
func forward_layer(layer_name: str): MatThe example for this item is shown with the ones above: ai-opencv-Detect.salam
free
func free()The example for this item is shown with the ones above: ai-opencv-Detect.salam
Detection
struct Detection| Name | Type | Description |
|---|---|---|
class_id | int = 0 | The class number. |
confidence | f64 = 0 | The confidence. |
x | int = 0 | x. |
y | int = 0 | y. |
width | int = 0 | The width. |
height | int = 0 | The 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 = 0The example for this item is shown with the ones above: ai-opencv-Basics.salam
COLOR_GRAY2BGR
const COLOR_GRAY2BGR: i32 = 1The example for this item is shown with the ones above: ai-opencv-Basics.salam
COLOR_RGB2GRAY
const COLOR_RGB2GRAY: i32 = 2The example for this item is shown with the ones above: ai-opencv-Basics.salam
COLOR_GRAY2RGB
const COLOR_GRAY2RGB: i32 = 3The example for this item is shown with the ones above: ai-opencv-Basics.salam
COLOR_BGR2RGB
const COLOR_BGR2RGB: i32 = 4The example for this item is shown with the ones above: ai-opencv-Basics.salam
COLOR_RGB2BGR
const COLOR_RGB2BGR: i32 = 5The example for this item is shown with the ones above: ai-opencv-Basics.salam
COLOR_BGR2HSV
const COLOR_BGR2HSV: i32 = 6The example for this item is shown with the ones above: ai-opencv-Basics.salam
COLOR_HSV2BGR
const COLOR_HSV2BGR: i32 = 7The example for this item is shown with the ones above: ai-opencv-Basics.salam
COLOR_RGB2HSV
const COLOR_RGB2HSV: i32 = 8The example for this item is shown with the ones above: ai-opencv-Basics.salam
COLOR_HSV2RGB
const COLOR_HSV2RGB: i32 = 9The example for this item is shown with the ones above: ai-opencv-Basics.salam
THRESH_BINARY
const THRESH_BINARY: i32 = 0The example for this item is shown with the ones above: ai-opencv-Filters.salam
THRESH_BINARY_INV
const THRESH_BINARY_INV: i32 = 1The example for this item is shown with the ones above: ai-opencv-Filters.salam
THRESH_TRUNC
const THRESH_TRUNC: i32 = 2The example for this item is shown with the ones above: ai-opencv-Filters.salam
THRESH_TOZERO
const THRESH_TOZERO: i32 = 3The example for this item is shown with the ones above: ai-opencv-Filters.salam
THRESH_TOZERO_INV
const THRESH_TOZERO_INV: i32 = 4The example for this item is shown with the ones above: ai-opencv-Filters.salam
THRESH_OTSU
const THRESH_OTSU: i32 = 5The example for this item is shown with the ones above: ai-opencv-Filters.salam
ADAPTIVE_MEAN
const ADAPTIVE_MEAN: i32 = 0The example for this item is shown with the ones above: ai-opencv-Filters.salam
ADAPTIVE_GAUSSIAN
const ADAPTIVE_GAUSSIAN: i32 = 1The example for this item is shown with the ones above: ai-opencv-Filters.salam
INTERP_NEAREST
const INTERP_NEAREST: i32 = 0The example for this item is shown with the ones above: ai-opencv-Basics.salam
INTERP_LINEAR
const INTERP_LINEAR: i32 = 1The example for this item is shown with the ones above: ai-opencv-Basics.salam
INTERP_CUBIC
const INTERP_CUBIC: i32 = 2The example for this item is shown with the ones above: ai-opencv-Basics.salam
INTERP_AREA
const INTERP_AREA: i32 = 3The example for this item is shown with the ones above: ai-opencv-Basics.salam
INTERP_LANCZOS4
const INTERP_LANCZOS4: i32 = 4The example for this item is shown with the ones above: ai-opencv-Basics.salam
ROTATE_90_CW
const ROTATE_90_CW: i32 = 0The example for this item is shown with the ones above: ai-opencv-Basics.salam
ROTATE_180
const ROTATE_180: i32 = 1The example for this item is shown with the ones above: ai-opencv-Basics.salam
ROTATE_90_CCW
const ROTATE_90_CCW: i32 = 2The example for this item is shown with the ones above: ai-opencv-Basics.salam
FLIP_VERTICAL
const FLIP_VERTICAL: i32 = 0The example for this item is shown with the ones above: ai-opencv-Basics.salam
FLIP_HORIZONTAL
const FLIP_HORIZONTAL: i32 = 1The example for this item is shown with the ones above: ai-opencv-Basics.salam
FLIP_BOTH
const FLIP_BOTH: i32 = 2The example for this item is shown with the ones above: ai-opencv-Basics.salam
MORPH_OPEN
const MORPH_OPEN: i32 = 0The example for this item is shown with the ones above: ai-opencv-Filters.salam
MORPH_CLOSE
const MORPH_CLOSE: i32 = 1The example for this item is shown with the ones above: ai-opencv-Filters.salam
MORPH_GRADIENT
const MORPH_GRADIENT: i32 = 2The example for this item is shown with the ones above: ai-opencv-Filters.salam
MORPH_TOPHAT
const MORPH_TOPHAT: i32 = 3The example for this item is shown with the ones above: ai-opencv-Filters.salam
MORPH_BLACKHAT
const MORPH_BLACKHAT: i32 = 4The example for this item is shown with the ones above: ai-opencv-Filters.salam
BITWISE_AND
const BITWISE_AND: i32 = 0The example for this item is shown with the ones above: ai-opencv-Filters.salam
BITWISE_OR
const BITWISE_OR: i32 = 1The example for this item is shown with the ones above: ai-opencv-Filters.salam
BITWISE_XOR
const BITWISE_XOR: i32 = 2The example for this item is shown with the ones above: ai-opencv-Filters.salam
NORM_HAMMING
const NORM_HAMMING: i32 = 0The example for this item is shown with the ones above: ai-opencv-Features.salam
NORM_L2
const NORM_L2: i32 = 1The example for this item is shown with the ones above: ai-opencv-Features.salam
Functions
RGB
func RGB(r: int, g: int, b: int): ColorThe example for this item is shown with the ones above: ai-opencv-Basics.salam
Black
func Black(): ColorThe example for this item is shown with the ones above: ai-opencv-Basics.salam
White
func White(): ColorThe example for this item is shown with the ones above: ai-opencv-Basics.salam
Red
func Red(): ColorThe example for this item is shown with the ones above: ai-opencv-Basics.salam
Green
func Green(): ColorThe example for this item is shown with the ones above: ai-opencv-Basics.salam
Blue
func Blue(): ColorThe example for this item is shown with the ones above: ai-opencv-Basics.salam
Read
func Read(path: str): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
Write
func Write(path: str, m: Mat): boolThe 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): MatThe example for this item is shown with the ones above: ai-opencv-Basics.salam
Zeros
func Zeros(width: int, height: int, channels: int): MatThe 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 example for this item is shown with the ones above: ai-opencv-Features.salam
FromImage
func FromImage(img: image.Image): MatThe example for this item is shown with the ones above: ai-opencv-Detect.salam
ToImage
func ToImage(m: Mat): image.ImageThe example for this item is shown with the ones above: ai-opencv-Detect.salam
Imshow
func Imshow(window: str, m: Mat)The example for this item is shown with the ones above: ai-opencv-Detect.salam
WaitKey
func WaitKey(delay_ms: int): intThe example for this item is shown with the ones above: ai-opencv-Detect.salam
DestroyWindow
func DestroyWindow(window: str)The example for this item is shown with the ones above: ai-opencv-Detect.salam
DestroyAllWindows
func DestroyAllWindows()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): boolThe example for this item is shown with the ones above: ai-opencv-Detect.salam
GetTrackbarPos
func GetTrackbarPos(name: str, window: str): intThe example for this item is shown with the ones above: ai-opencv-Detect.salam
SetTrackbarPos
func SetTrackbarPos(name: str, window: str, pos: int)The example for this item is shown with the ones above: ai-opencv-Detect.salam
LoadCascade
func LoadCascade(path: str): CascadeClassifierThe example for this item is shown with the ones above: ai-opencv-Detect.salam
OpenVideo
func OpenVideo(path: str): VideoCaptureThe example for this item is shown with the ones above: ai-opencv-Detect.salam
OpenCamera
func OpenCamera(index: int): VideoCaptureThe 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): VideoWriterThe example for this item is shown with the ones above: ai-opencv-Detect.salam
LoadNet
func LoadNet(model_path: str, config_path: str): NetThe example for this item is shown with the ones above: ai-opencv-Detect.salam
LoadNetSingleFile
func LoadNetSingleFile(path: str): NetThe 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>The example for this item is shown with the ones above: ai-opencv-Detect.salam
LastError
func LastError(): strThe example for this item is shown with the ones above: ai-opencv-Basics.salam
Available
func Available(): boolThe example for this item is shown with the ones above: ai-opencv-Basics.salam
Version
func Version(): strThe example for this item is shown with the ones above: ai-opencv-Basics.salam