Skip to content

Latest commit

 

History

History
250 lines (201 loc) · 17 KB

SEGMENTATION.md

File metadata and controls

250 lines (201 loc) · 17 KB

Objects drawn by SAM (experimental)

The two object targets of the PIPETTE group, on a raster, can use a segmentation model: SAM 2.1 tiny, locally on the CPU.

  • Clicked object draws the object under the click.
  • Similar objects draws the clicked object, then every object like it in the visible map.

The objects are plain polygons. The filter applied to the other layers needs no model, and a saved filter replays without one. Without SAM, both targets keep finding objects by their colour, as before.

FilterMate ships neither the model nor its runtime, so its package stays small and publishable on plugins.qgis.org. Both are installed once, from the panel, after your confirmation.

Set up

  1. Select a raster and choose Clicked object or Similar objects in the PIPETTE group. A message offers to install SAM: onnxruntime (about 100 MB) and the model (62 MB). Nothing is installed before you agree, and nothing is installed at the start of QGIS. The question comes once a session; an Install SAM… button stays in the group.
  2. Once you confirm, pip installs onnxruntime in the background, as a QGIS task, into your QGIS profile (filtermate/python/py<version>). The Python of QGIS is not modified, and pip keeps the numpy version QGIS has, so that the onnxruntime it picks loads with it. An install cut short (QGIS closed meanwhile) is noticed by its missing files, and the button offers it again. onnxruntime is declared by the python_dependencies key of metadata.txt.
  3. The model then downloads from a pinned revision of onnx-community/sam2.1-hiera-tiny-ONNX into filtermate/models/sam2.1-hiera-tiny-q. Each file is checked against its SHA-256 before it replaces anything. The download can be canceled and uses the HTTP proxy set in the QGIS options.
  4. If pip fails (no network, no pip), the reason is in the FilterMate log and the group shows the command to run by hand in a terminal (the OSGeo4W Shell on Windows): the same install, into the same folder. If the model files turn out to be damaged, a Repair the model… button replaces the bad ones.

Nothing else leaves your computer: the model runs on your CPU.

Use

Turn the object detector or the similar objects tool of the rail on, or choose the target in the PIPETTE group. The Engine list of the group switches between SAM and the colour engine.

Gesture Effect
Move the mouse Outline of the object under it, once the area is prepared
Click A new object, added at once
Shift+click The last object, extended
Right click The last object, trimmed
Drag a box A new object inside the box
G (or Tab when QGIS lets it through) The last object at the next granularity: a part, the object, a group
Escape Rejects the proposals; otherwise removes the last object, or the last search with its clicked object
Clear Forgets every object drawn by SAM
To layer Adds the objects to the project as a polygon layer (memory layer, area in m² on the project ellipsoid)

The first click of an area prepares it: about 3 s under QGIS for Windows, then about 0.1 s per gesture.

SAM proposes three outlines for each click and FilterMate keeps the one it rates best. On 20 buildings that was the best of the three 11 times: G shows the others (median overlap 0.71 with the chosen one, 0.76 with the best of the three).

Similar objects first draws the clicked object. The patches of a similar colour in the visible map, at most 120 of them, are then proposed to SAM, followed by the 10 places of the visible map whose encoder features look most like the clicked object's; SAM redraws each one in the background, on the window prepared for the click when the candidate lies well inside it (no second encoding, as the search was measured). An object is kept when its area, and its shape with same shape, are within the ratios of the group compared with the clicked one, and when it looks like it: the encoder features of SAM under the two objects must point the same way (cosine at least 0.68).

The objects found are proposals, outlined in dashes, outside the selection: click one to keep it, right click to reject it, Enter (or Keep) keeps them all, Escape (or Reject) rejects them all. From the third proposal kept or rejected on, the proposals left that look more like the rejected ones than like the kept ones (and the clicked object) are taken away, and the status says how many.

The objects are polygons in the CRS of the raster, stored with the filter like any other raster selection. Processing (the SEGMENTS parameter of Filter layers of any type by the pixels of a raster, a polygon layer), the REST API and the MCP tools (segments in raster_selection) accept them too: see docs/AUTOMATION.md. To layer turns them into a memory layer of the project, numbered, with their area in m²: the layer to give Processing, to export or to edit. It is not saved with the project: Make Permanent in QGIS keeps it.

From a vector layer (Processing)

Draw objects on a raster with SAM from points or polygons (filtermate:segment_from_features) draws one object per feature of a guide layer, with no click: a point is a click on the object, a polygon gives its bounding box and a point inside it. The output is a polygon layer in the CRS of the raster, with the attributes of the feature, sam_score (the score SAM gives its mask) and, for a polygon, sam_iou (overlap of the footprint with the drawn object, from 0 to 1). It runs in the background and changes no layer of the project; it can be a step of a model, for example before Filter layers of any type by the pixels of a raster with its SEGMENTS.

A multipoint gives all its points to one object. A multipolygon is prompted by its largest part. An invalid footprint is made valid before sam_iou is computed. A footprint under a pixel is prompted by its point. Source fields named sam_score or sam_iou (any case) are replaced. A window that cannot be drawn, such as a tile of a service, is reported and loses its objects only; the run fails only when no window could be drawn.

Nearby objects of one size share a window of 1024 pixels, encoded once. The cell is the native one, doubled as often as needed for a box to fill at most two thirds of the window. A point sees at least 150 m of ground.

Measured 2026-11-10 on 40 buildings of the BD TOPO (50 to 2000 m²) on the 25 cm Cerdagne orthophoto, median overlap with the footprint (scripts/sam_feature_bench.py, rule set before the run):

Prompt Median IoU
Box of the footprint and a point inside it (kept) 0.81
Box only 0.80
Box grown by 10 % and the point 0.78
Point only (a point layer) 0.73

Neither other prompt met the rule (+0.02, at most 4 of 40 buildings losing more than 0.05). The 40 buildings took 8 windows of about 1.8 s each under QGIS for Windows. The BD TOPO footprint is not the roof seen from above: a low sam_iou points at a building to look at, it does not prove a change.

processing.run("filtermate:segment_from_features", {
    "RASTER": ortho, "FEATURES": footprints, "OUTPUT": "memory:"})

With the agent

The AGENT section, the REST API and the MCP servers reach these objects too (docs/AUTOMATION.md):

  • Objects drawn in the dock. map_context lists the rasters that have some (drawn_objects: count, area, extent), and the project summary sent with each request says so. "The buildings under what I drew" becomes a cross_filter from that raster with raster_selection: {"dock_objects": true}: the polygons are read in QGIS, they never go to the model.
  • Drawn from a guide layer. segment_objects runs Draw objects on a raster with SAM from points or polygons on a raster and a guide layer of the project, the selected features of the guide by default, at most 200 in one call, and adds the objects as a layer with sam_score and sam_iou, written to a temporary GeoPackage (Make Permanent keeps it). "Which footprints no longer match the orthophoto?" is that call, then select_features on "sam_iou" < 0.5.
  • Like an example. similar_objects takes examples (one selected feature of a layer, or every object drawn in the dock), has SAM draw them, then looks window by window over the area searched (the extent given, or the visible map) for the places whose encoder features look like one of them, draws them, and keeps those of 0.5 to 2 times their area and shape whose features look like the nearest of them (cosine at least 0.8). No colour is read, so it needs no values of the raster. That is the feature peaks only setting measured below: on buildings of the 25 cm Cerdagne orthophoto, about half of what it finds is right and it finds about a fifth of what is there. The layer is to be checked, not counted; the agent is told so. Until 2026-11-13 the dock gave its last object only: on the pools of the same orthophoto (27 to 90 m², two to six cells of the coarsest feature map against five to twenty-five for the buildings of the calibration), a small pool drawn last left examined at 0 to 2 over the whole kilometre and nothing was found, while a mid-sized one found three of the other six pools. With every drawn object as an example (best cosine of them, at the same 0.7 and 0.8), four of the seven pools are found in turn, held out one by one, for about one bright roof kept beside them; lowering the thresholds to 0.6 and 0.75 found no more pools and kept three times as many roofs, so the thresholds stayed.

SAM holds QGIS while it draws (about 2 s for each window of 1024 pixels): the run stops between two windows past 20 s and says how far it went, and other changes from the API are refused meanwhile. Only the objects that select in the dock are listed: with the colour engine, the objects SAM drew wait in the panel and are not offered. Undo of the turn removes the layer, Redo opens its file again. The agent never installs SAM: it tells the user to do it from the PIPETTE group.

What it is good at

Buildings and other well delimited objects. Measured with this model on a 25 cm orthophoto, 2026-11-09, median overlap with the reference outlines (IoU, 1 is a perfect match):

Gesture 100 buildings 18 parcels Colour engine, 3 clicks
1 click 0.65 0.21
3 corrective clicks 0.76 0.27 buildings 0.52, parcels 0.26
Box 0.82 0.44

Parcels did not reach the improvement we required: a cadastral limit is often invisible on the image. Trees were not validated either: hedges tend to merge. Use the colour engine or a vector layer for them.

Similar objects: a start, not a finished tool. Measured on the 25 cm orthophoto of the test project (Cerdagne), 2026-11-10, with scripts/sam_similar_bench.py under QGIS 4.2.3 for Windows. Ten reference buildings of 80 to 400 m² were each clicked in a 150 m view, with the default ratios (0.5 to 2, same shape). A found object counts when it overlaps a BD TOPO building with an IoU of at least 0.5. Medians:

SAM Colour engine
Precision (found objects that are buildings) 0.36 0.00
Recall (buildings of the same size found) 0.46 0.00
Time per search 5.2 s 2.4 s, 24 s on two views
Overlap of the clicked object 0.78

These were the numbers before the appearance filter. On ten other buildings of the same orthophoto (scripts/sam_similar_features.py), SAM did less well: median precision 0.24, recall 0.31.

Appearance filter, adopted (2026-11-10, rule set before the run: a threshold chosen on the first ten buildings, judged on the ten others, adopted if the median precision gains at least 0.10 for a recall loss of at most 0.05). The cosine of the encoder features of SAM separates the classes (median 0.80 for the buildings found, 0.69 for the rest). With the threshold of 0.68 chosen on the first ten (scripts/sam_similar_appearance.py):

Median First ten, before First ten, after Ten others, before Ten others, after
Precision 0.36 0.49 0.24 0.35
Recall 0.46 0.43 0.31 0.26

About half of the objects proposed are buildings: keep or reject them before filtering with them.

Candidates from the features, and sorting by keep / reject, adopted (2026-11-11, scripts/sam_similar_candidates.py, decided by scripts/sam_similar_candidates_analysis.py by rules written before the run). Same 20 buildings and sets; every candidate was drawn on the window of the click, the current search measured the same way. The colour patches gave about 40 candidates per search; the cosine between the features of the clicked object and each cell of the coarsest feature map peaks at about 90 places of a 150 m view.

Median, ten others Precision Recall Prompts
Colour patches (as before) 0.37 0.20 34
Colour patches, then the 10 best feature peaks (adopted) 0.36 0.32 41
Feature peaks only (cosine at least 0.7, 40 at most, appearance at least 0.8) 0.55 0.20 14

The union was adopted: recall +0.12 for precision -0.01 (the rule asked +0.10 and at most -0.05). The feature peaks alone are more precise for a third of the prompts, but they did not raise the recall the rule asked for; they are a lead for a search that needs no colour, on a raster that is only drawn.

Sorting, measured on the proposals of the current search: once the first three are kept or rejected (by whether they are a building), the proposals left closer to the rejected ones than to the kept ones and the clicked object are taken away. Median precision of the proposals left 0.21 to 0.33 on the ten others (0.43 to 0.63 on the first ten), none of the buildings they held lost.

Sorting by a model that looks, not adopted (2026-11-11, scripts/sam_vision_judge.py, rule set before any call). The proposals of the search above were shown to Claude Haiku 5.5 as contact sheets made by the contact_sheet tool of the MCP server (tile E the clicked object, then the proposals), with the question "which are the same kind of object as the example?". It kept almost all of them: on the ten others, precision 0.36 for all the proposals, 0.34 for those it kept (the rule asked +0.10). What SAM gets wrong here is mostly the outline, a part of a roof or two roofs in one, not the kind of object: a judge of the kind cannot see it.

The mask nearest the size of the clicked object, not adopted (2026-11-11, scripts/sam_similar_granularity.py, rule set before the run). SAM proposes three masks for a point, roughly a part, the object and a group; drawing each candidate with the one whose area is nearest the clicked object's, instead of the one SAM scores best, should have fixed the outlines the judge above could not see. On the ten others the rule passed (recall 0.32 to 0.52, precision 0.36 to 0.33), but on the first ten the precision fell from 0.56 to 0.38, so a confirmation on 14 buildings never used (40 to 80 and 400 to 2000 m²) was set before running it: precision 0.50 to 0.21 for a recall of 0.13 to 0.25. Over the 20 first searches it added 90 proposals, 78 of them not buildings. The search keeps the mask SAM scores best.

Sorting the proposals by the LiDAR, not adopted (2026-11-11, scripts/sam_similar_lidar.py and scripts/sam_similar_lidar_analysis.py, explored on the first ten buildings only). Where a point cloud covers the orthophoto, a proposal could have been kept when the footprint of the LiDAR class under the clicked object covers it. On the test project every clicked roof lies on class 6, and so do the proposals: a median share of 0.96 of the right ones on class 6, 0.99 of the wrong ones, a precision of 0.47 to 0.49 at best. As for the judge that looks, the proposals are wrong by their outline, not by their kind. Where a point cloud covers the area, its own similar objects (CLASSES) do better on buildings: see the guide.

Tried and not adopted. A score and colour filter (precision 0.24 to 0.31 on the ten others, short of the 0.10 required, scripts/sam_similar_analysis.py), and a second, framed pass of SAM after the click (median overlap 0.711 to 0.713 on 20 buildings, short of the 0.02 required).

Colour engine. Its similar objects took up to 34 s on this orthophoto: the patches of the whole raster were simplified to a vertex budget they did not need. They are now only measured, and the budget applies to the objects kept: 5.5 s at most, median precision 0.06 and recall 0.11 instead of 0 and 0, as the patches and the clicked object are now measured alike.

Model

SAM 2.1 hiera tiny (Meta, Apache 2.0), quantized ONNX export of onnx-community/sam2.1-hiera-tiny-ONNX, run by onnxruntime with the CPU execution provider, 4 threads at most (more was slower).

Code: adapters/qgis/coverage_layers/sam_model.py (files, checksums, download, provider), adapters/qgis/coverage_layers/segmentation.py (windows, session, encoding, similar objects and download tasks), ui/controllers/segmentation_controller.py and ui/tools/segmentation_tool.py (the gestures on the map), core/ports/segmentation_port.py (what FilterMate asks of a model).