Camera and computer vision
Reading a live webcam, working its pixels, and simple thresholding and color tracking.
What’s realistic in Umfeld: the built-in
Captureclass uses SDL3’s camera subsystem, and provides full access to pixels — so brightness thresholding and color tracking are a few loops away. Umfeld does not bundle OpenCV, blob detection, or face detection. If you need those, the patterns below (working directly onpixels[]) are the supported path; reach for an external C++ vision library only when you truly need it.
Using a webcam
Capture::list() enumerates devices. Open a camera with one of the typed constructors, call
start(), and then poll available() in draw(). When a frame is ready, read() copies it into
both the capture’s pixels[] array and its texture. Guard against drawing before the first frame
has initialized the pixel buffer.
#include "Umfeld.h"
#include "Capture.h"
using namespace umfeld;
Capture* cam = nullptr;
std::vector<std::string> devices;
void setup() {
devices = Capture::list();
for (size_t i = 0; i < devices.size(); ++i) {
console("[", i, "] ", devices[i]);
}
// An empty constructor selects the default camera. Passing a device name
// selects that camera instead.
cam = devices.empty() ? new Capture() : new Capture(devices[0]);
cam->start();
}
void draw() {
background(0);
if (cam != nullptr && cam->available()) {
cam->read();
}
const bool has_frame = cam != nullptr && cam->pixels != nullptr &&
cam->width > 0 && cam->height > 0;
if (has_frame) {
image(cam, 0, 0, width, height);
}
}
To request a size, frame rate, and device, pass typed values rather than encoded strings:
cam = new Capture(1280, 720, devices[0], 30.0f);
The equivalent reinitialization call is cam->init(1280, 720, devices[0], 30.0f). The camera may
negotiate a different format, so use cam->width and cam->height after the first frame. For
exact format selection, choose a CameraCapability returned by Capture::getCapabilities() and
pass it to the constructor or init().
Camera permissions
The device is opened — and any OS permission prompt is triggered — by the constructor or
init(), not by start().
- macOS: the executable must contain an
NSCameraUsageDescription. Copy theInfo.plistand the macOStarget_link_options(... -sectcreate __TEXT __info_plist ...)block from the Advanced/capture example. If access was denied, re-enable it in System Settings → Privacy & Security → Camera. - Windows: enable Camera access and Let desktop apps access your camera under Settings → Privacy & security → Camera.
- Linux: grant camera access to the app’s sandbox or desktop portal. For a native executable,
make sure the user can access
/dev/video*(commonly through thevideogroup or an equivalent udev rule).
Camera support must also be enabled at build time. Place
option(ENABLE_CAPTURE "Enable camera capture" ON) before adding Umfeld in CMakeLists.txt.
The SDL3-backed Capture has no FFmpeg dependency and remains available when
DISABLE_VIDEO=ON. See the upstream
Capture reference
for callbacks, capability enumeration, backend status, and the interim CaptureFFmpeg fallback.
Reading the camera’s pixels
A successful read() makes the new frame available in pixels[]. Pull channels out of each
pixel with red(), green(), and blue():
if (cam != nullptr && cam->available() && cam->read()) {
const int x = 10;
const int y = 10;
color_t c = cam->pixels[y * static_cast<int>(cam->width) + x];
float brightness = (red(c) + green(c) + blue(c)) / 3.0f;
}
Thresholding video
Turn the frame into pure black and white at a brightness cut-off — the classic first step of any vision pipeline. Draw into the window’s own pixel buffer. This version maps the negotiated camera size to the window, so it remains safe when the camera does not provide the requested resolution.
void draw() {
if (cam != nullptr && cam->available()) {
cam->read();
}
const bool has_frame = cam != nullptr && cam->pixels != nullptr &&
cam->width > 0 && cam->height > 0;
if (!has_frame) return;
loadPixels();
const int camera_width = static_cast<int>(cam->width);
const int camera_height = static_cast<int>(cam->height);
const float threshold = map(mouseX, 0, width, 0, 255);
for (int y = 0; y < height; ++y) {
const int camera_y = y * camera_height / height;
for (int x = 0; x < width; ++x) {
const int camera_x = x * camera_width / width;
color_t c = cam->pixels[camera_y * camera_width + camera_x];
float b = (red(c) + green(c) + blue(c)) / 3.0f;
pixels[y * width + x] = (b > threshold) ? color(255) : color(0);
}
}
updatePixels();
}
Color tracking
Find the average position of every pixel near a target color — enough to follow a brightly colored object without any library:
void draw() {
background(0);
if (cam != nullptr && cam->available()) {
cam->read();
}
const bool has_frame = cam != nullptr && cam->pixels != nullptr &&
cam->width > 0 && cam->height > 0;
if (!has_frame) return;
image(cam, 0, 0, width, height);
const int camera_width = static_cast<int>(cam->width);
const int camera_height = static_cast<int>(cam->height);
const color_t target = color(255, 0, 0); // tracking red
double sumX = 0, sumY = 0;
uint64_t count = 0;
for (int y = 0; y < camera_height; ++y) {
for (int x = 0; x < camera_width; ++x) {
color_t c = cam->pixels[y * camera_width + x];
float dr = red(c) - red(target);
float dg = green(c) - green(target);
float db = blue(c) - blue(target);
if (dr * dr + dg * dg + db * db < 60 * 60) { // within tolerance
sumX += x;
sumY += y;
++count;
}
}
}
if (count > 0) {
float cx = static_cast<float>(sumX / count * width / camera_width);
float cy = static_cast<float>(sumY / count * height / camera_height);
noFill();
stroke(0, 255, 0);
strokeWeight(4);
circle(cx, cy, 40);
}
}
The same idea — scanning pixels[] and collecting matching points — is the basis of a simple
blob tracker. Build up the bounding box of the matching pixels instead of just their average to
draw a box around the object.