Checking how many agents are working

How this works
Kontakt
← Blog
A dark photograph dissolving into a grid of glowing olive and teal numbers, the AMARBARO mark centered over the grid.
✎Blogbeitrag

mojo-cv: working with pictures as numbers

The short version

Every mojo-cv operation is run row by row against an oracle on the same input, with 408 rows against cv2 and 161 for the 3D operations, all passing in scripts/ci.sh.

Part 16 of 20 in Tools we built and why. Previous: mojo-tls: secure connections with no OpenSSL · Next: mojo-imkit: a simple app window drawn by the GPU

What this is about

A digital picture is a grid of numbers. For a gray picture, each number says how bright one spot is, from 0 for black to 255 for white. A color picture has three numbers per spot. Once a picture is numbers, you can do arithmetic on it. Blurring is averaging a spot with its neighbors. Finding an edge is looking for a sudden jump between neighbors. Making a picture smaller is choosing which numbers to keep.

mojo-cv is our library of those basic operations. It is written from scratch in Mojo, for the CPU, in the style of OpenCV, the best-known picture library. Every operation is checked against OpenCV or against an exact answer.

Why it matters

Much of what computers do with pictures is built on a small set of operations: filters, color conversion, resizing, finding shapes, tracking motion. If you know what those operations do, a lot of picture software stops being magic. A phone's portrait mode, a scanner that straightens a page and an app that counts objects all use them.

For our own work, we need these steps inside tools that are written in Mojo, with no Python in between. That is why we built a library rather than call OpenCV.

How it works

The central type is Mat, a grid with rows, columns and channels. You fill it, or load a picture into it, and then call operations on it.

The README's quickstart makes a 64 by 64 gray picture whose left half has the value 40 and whose right half has the value 200. Then it does two things.

from mojocv.mat import Mat
from mojocv.imgproc import gaussian_blur, otsu, threshold

var mask = threshold(img, Float64(otsu(img)), 255.0, 0)
var soft = gaussian_blur(img, 5, 0.0)

First it finds a cutoff with Otsu's method, which picks the value that best separates dark from bright, and thresholds the picture: every spot below the cutoff becomes 0, and every spot above becomes 255. Second it blurs the picture with a 5 by 5 Gaussian blur. The program prints 0 255 90: the left edge of the mask is 0, the right edge is 255, and a spot just left of the middle stays at 90 after blurring, because the blur mixes the 40 and 200 sides.

What is in the box

The README groups the operations. A few examples in each group:

  • Core and filters: flip, rotate, blur, Sobel edges, threshold, erode and dilate, and color conversions such as gray, HSV and Lab.
  • Geometry of images: resize with several methods, rotate and warp, and remap.
  • Shapes and features: connected components, Canny edges, contours, corner finding, and ORB feature matching.
  • Motion: optical flow, which follows how spots move between two frames.
  • Calibration and pose: working out where a camera is and how it sees.
  • 3D helpers: octree differences, phase correlation, NURBS fits, and export to GLB and STEP files.

How it is checked

Each operation is run on the same input through mojo-cv and through its oracle. For the 2D operations the oracle is cv2 5.0 (and cv2 4.8.1 for one detector, HOG), and there are 408 comparison rows. For the 3D operations the oracle is numpy, brute force, or a closed-form answer, with 161 rows. All pass in scripts/ci.sh.

A person new to picture work should take one lesson from this. A picture operation that gives "a blurry picture" is easy to get roughly right. Giving the same numbers as the reference is the real test.

How to do it, step by step

  1. Add the channel and install: pixi workspace channel add https://conda.modular.com/max https://prefix.dev/amarbaro/amarbaro, then pixi add mojo-cv. The import name is mojocv.
  2. Or from source: clone the repository and run mojo run -I mojo-cv/src app.mojo.
  3. Copy the quickstart from the README. Run it and look for 0 255 90.
  4. Change the blur size from 5 to 9 and see how the middle number changes.
  5. Try the Sobel edge filter on a picture with one sharp edge, and see where the values jump.

The code now lives in the libs/cv folder of mojo-libs. The old repository points there and is read-only.

What it does not do

  • It is a library and a command-line tool, not a drop-in replacement for cv2.
  • There is no Python binding.
  • There is no GPU code.
  • It needs Mojo 1.2 and MAX 26.7. The README says Mojo 1.1.0 does not finish compiling it.

Why the same numbers matter

Suppose you blur a picture with our library and a friend blurs the same picture with OpenCV. If the two results differ in a few spots, any later step, such as finding edges, will differ too, and the difference grows. Matching the reference on each basic step keeps small differences from piling up. That is why the checks run row by row on the same input, and why the README lists a count of rows for each side.

Words you may not know

  • Pixel: one spot in a picture.
  • Channel: one layer of color, such as red.
  • Threshold: turning each value into black or white by a cutoff.
  • Blur: averaging each spot with its neighbors.
  • Edge: a place where the values jump.
  • Oracle: a trusted reference we compare our answers to.
  • OpenCV (cv2): a widely used picture library.

Try it

Kommentare

Noch keine Kommentare.

Anmelden um zu kommentieren.