The models of earlier lectures look at a frame and say what is in it. A system that assists a surgeon or guides an instrument needs more. It needs to know where the instrument is in space relative to the anatomy, that it is the same instrument as a second ago, and that the mask does not tremble from frame to frame. This is lecture 9 of the curriculum, after lecture 8 on architectures. Three topics:
- Computer-assisted intervention: vision as the sensing layer.
- 6-DoF pose: where an instrument is in space and how it is turned relative to anatomical landmarks.
- Time: tracking with a Kalman filter and smoothing masks with recurrent layers.
Every image on this page is drawn by code. The project’s models are for research (RESEARCH USE ONLY – NOT FOR CLINICAL USE) and control no device.
Vision as the sensing layer
Computer-assisted intervention (CAI) covers systems that help a surgeon see, decide or act during an operation: navigation, augmented reality over the video, warnings about getting close to a structure, an assisting robot. They share one structure: perception, a scene model, a decision and an action, closed into a loop through the surgical field.
Why vision. A da Vinci robot has kinematics: joint encoders say where the instrument should be. But the absolute error of its end effector’s position from kinematics is large, and calibration is laborious; the SurgPose dataset names this as its motivation. The camera looks where the surgeon looks and sees the instrument where it really is. Hand-held laparoscopic instruments, the kind used in cholecystectomy, have no encoders at all, and the camera is the only sensor.
How much the machine decides by itself is described by six levels of autonomy for surgical robots (Yang et al., Science Robotics 2017):
| Level | Name | What the machine does |
|---|---|---|
| 0 | No autonomy | follows the surgeon’s movements, as a telemanipulator |
| 1 | Robot assistance | constrains or guides the surgeon’s movements, for instance with a virtual wall |
| 2 | Task autonomy | performs a single task the surgeon started by itself, for instance a suture |
| 3 | Conditional autonomy | proposes a plan for a task; the surgeon selects or approves it |
| 4 | High autonomy | makes decisions under the supervision of a qualified doctor |
| 5 | Full autonomy | no surgeon needed |
Already at level 1, vision must deliver not just a mask but the instrument’s position relative to a structure, in every frame and with a known delay. And one more thing: it must report when it is unsure, because a loop that does not know about its error acts on it.
The 6-DoF pose of an instrument
Six degrees of freedom are three translations and three rotations. Together they make the transform from the instrument’s coordinate frame into the camera’s: a rotation matrix R and a translation vector t, often written as one 4 × 4 matrix.
A camera turns a point in space into a pixel. In the pinhole model a point X is first moved into the camera frame, then divided by its depth Zc; f is the focal length in pixels and (cx, cy) the centre of the frame:
The numbers in the second line come from the project’s instrument rendering script: a 70° laparoscope and an 854 × 480 frame, as in CholecSeg8k. The same script records each instrument’s pose in exactly 6 DoF: the tip (x, y, z) in camera coordinates, a unit vector from the tip towards the trocar (two degrees of freedom) and the roll about the shaft’s axis, roll_rad (one more). It places the tip 4 to 14 cm from the lens. So synthetic frames come with an exact pose, and real videos do not.
Try the pose yourself. The sliders set the same six numbers and the jaw opening.
- tip in the 854 × 480 frame, px
- –
- width of the 5 mm shaft at the tip, px
- –
- depth from width with a ±1 px error, mm
- –
The toy shows the two difficulties of a single camera.
Depth. One camera sees the direction to a point, not the distance to it. If the true size is known, the shaft’s diameter for instance, the distance can be read from the apparent size:
The relative error in depth equals the relative error in width. A shaft 20 pixels wide with a one-pixel error gives 5 % in depth; a distant shaft 8 pixels wide, about 12 %. A stereo laparoscope removes the difficulty: SurgPose lifts its 2D points into 3D from the stereo pair.
Roll. The shaft is a cylinder, and with the jaws closed a roll about its axis does not change the image. It can be seen only through asymmetric details: open jaws, the joint, markings on the body.
How pose is estimated in practice:
- Keypoints and PnP. A network finds known points of the instrument in the frame: the jaw tips, the joint, points on the shaft. Then
cv2.solvePnPfits R and t so that the instrument’s 3D model projects onto those points; it needs at least four correspondences. SurgPose labels seven keypoints on each of about 120,000 instruments of six types. The points are painted with ink visible only under ultraviolet light, so the labels do not show in the ordinary frames. - Direct regression. A network outputs R and t directly. This is simple, but rotations need a convenient representation: Euler angles are discontinuous.
- Render and compare. The pose is refined until a render of the model matches the mask or contour in the frame. Here the mask from M1 becomes the input to pose estimation.
Relative to anatomy
The pose relative to the camera changes with every movement of the laparoscope, even when the instrument stands still. The surgeon and the robot care about the pose relative to anatomy: how far the tip is from the cystic duct, at what angle the jaws approach the artery.
The landmark’s position in camera coordinates comes from segmenting the structure (M2) and from depth: a stereo pair or monocular depth estimation. Unlike an instrument, tissue is not a rigid body: it breathes, shifts under traction and changes shape. So an anatomical landmark is a point or a patch of surface in the current frame, not a rigid coordinate frame to be registered once and forgotten.
Tracking over time
Tracking is detection in every frame plus association: an instrument that was in the frame a second ago must keep its number. The classic simple tracker SORT (Bewley et al., 2016) predicts each object’s box with a constant-velocity Kalman filter and matches the predictions to new detections by box overlap.
In surgery tracking has its own features: an instrument leaves the field of view and comes back, exits the body through a trocar, hides in smoke and behind tissue. CholecTrack20 (Nwoye et al.) labels 20 full cholecystectomies at 1 frame per second: over 35,000 frames and 65,000 instruments, with class, number, operating surgeon and phase. Its trajectories are defined three ways: over the whole operation, while the instrument is inside the body, and while it is visible in the frame. The best methods on it stay below 45 % HOTA, a metric that counts both detection and keeping the numbers.
The Kalman filter
A Kalman filter keeps an estimate of a state, for instance the tip’s position and velocity, together with its uncertainty P. In every frame it takes two steps:
F is the motion model (at constant velocity the position adds the velocity), Q is how far the motion may stray from it, H is what part of the state the detector sees, and R is the detector’s noise. The gain K decides whom to trust more. If the prediction is uncertain and the detector precise, K is close to one and the estimate follows the measurement. If the other way round, K is small and the estimate keeps to the prediction.
R comes from the detector’s error on validation. Q is tuned: a large Q follows turns quickly but lets noise through; a small Q is smooth but late on every sharp move.
- error of the detections, px
- –
- average: outside smoke · in smoke, px
- –
- Kalman: outside smoke · in smoke, px
- –
The error here is the root-mean-square distance to the true path, kept apart for frames with detections and frames in smoke. With the default settings the detections are off by 13.6 pixels, an exponential average with α = 0.3 by 16.8, and Kalman by 8.2. It smooths the noise and is not late on turns, because it knows about velocity. An average with a small α is smooth but cuts the corners; with a large α it lets the noise through.
In smoke the picture reverses. The average freezes at the last detection and is off by 33 pixels, while Kalman keeps moving in a straight line as the path bends, and drifts to 85. A constant-velocity model holds only over a short stretch. That is what P is for: it grows with every frame without a detection, and trackers stop following an object by prediction after a few such frames and mark the track as lost.
Continuous video: mask flicker
A model that segments every frame on its own makes different mistakes on neighbouring frames. Pixels at the border of an instrument or a structure switch back and forth, and the mask flickers. On a monitor it distracts; in a CAI loop it is dangerous: the distance from the tip to a structure jumps by millimetres while the instrument stands still.
Flicker is measured by the number of label switches between neighbouring frames that the ground truth does not have. When the camera or the instrument moves, the previous mask is first carried along the optical flow, then compared; this is how Lai et al. (ECCV 2018) compute the temporal consistency error.
The simplest remedy is an exponential average of the probabilities, the first line of the formula below. It costs one map of memory, but with a fixed α it damps both noise and real motion: a fast instrument drags a trail behind it.
A recurrent layer learns when to trust the memory and when to trust the new frame. In a convolutional GRU cell (Ballas et al., 2015), the remaining lines of the formula, xt are the frame’s features, ht the memory, ∗ a convolution and ⊙ an element-wise product:
The last line is the same exponential average, except that α has become a learned gate zt, one per pixel and channel. Where the scene has not changed, the gate is closed and the cell keeps its memory; where the instrument has moved, the gate is open and the cell takes the new frame. ConvLSTM works the same way, with a separate cell state and three gates; it is covered on the video page.
Compare them in a toy. One row of 60 pixels is followed through 160 frames, time running to the right: the instrument’s edge moves slowly, and around frame 100 it leaves quickly and later comes back.
- switches: truth · per frame · smoothed
- –
- IoU with truth: per frame · smoothed
- –
- IoU over the fast stretch: per frame · smoothed
- –
Frame by frame the model switches labels 703 times where the truth switches 121 times. A fixed α = 0.2 leaves 107, fewer than the truth: the average eats part of the real motion too, and over the fast stretch around frame 100 IoU drops to 0.936. The gate opens only where the probability changed more than noise ever changes it: 139 switches, IoU 0.961, and 0.960 over the fast stretch. It is a hand-made copy of what zt learns; a trained cell does the same from features rather than from a single probability.
Three practical notes:
- The past only. During an operation only past frames exist. Bidirectional smoothing that also looks ahead is fine for labelling and reviewing video, not for a live loop. The latency budget is the topic of lecture 10.
- Training on sequences. A recurrent layer is trained on clips of consecutive frames, back-propagating through several steps. In CholecSeg8k the labelled frames come in clips of 80 consecutive frames, enough for short sequences.
- Smooth the object, not the pixels. For a CAI loop a Kalman filter on the object’s quantities, its tip, box or pose, is often more useful than smoothing every pixel.
Where the project is now
osv has no pose estimation, tracking or temporal smoothing yet: M1 segments every frame on its own. The instrument rendering script already records the exact 6-DoF pose for every synthetic frame, and pose and keypoint labels can be derived from it. But RFC 0001 showed that synthetic instruments by themselves transfer poorly to real frames: IoU 0.116 against 0.772 with real labels. Synthetic poses will need the same caution. The streaming server osv.serve, where tracking and smoothing would have to run in real time, is planned.
Checklist
- Decide what the task needs: a mask, a pose relative to the camera, or a pose relative to anatomy.
- With a single camera, remember the ambiguity of depth and of the roll about the shaft; report the depth error, not only the error in pixels.
- In tracking, report a metric that counts keeping the numbers (HOTA), and report episodes of smoke and of leaving the frame separately.
- Take the Kalman filter’s R from the detector’s error on validation, and tune Q on validation, not on test.
- Measure flicker by extra switches or by a flow-aware consistency error, next to IoU.
- During an operation, use past frames only.
Further reading
- Yang et al. Medical robotics – Regulatory, ethical, and legal considerations for increasing levels of autonomy. Science Robotics 2017.
- Wu et al. SurgPose: a Dataset for Articulated Robotic Surgical Tool Pose Estimation and Tracking. 2025.
- Nwoye et al. CholecTrack20: A Multi-Perspective Tracking Dataset for Surgical Tools. 2023.
- Bewley et al. Simple Online and Realtime Tracking. ICIP 2016.
- Kalman. A New Approach to Linear Filtering and Prediction Problems. Journal of Basic Engineering, 1960.
- Ballas et al. Delving Deeper into Convolutional Networks for Learning Video Representations. ICLR 2016.
- Shi et al. Convolutional LSTM Network. NeurIPS 2015.
- Lai et al. Learning Blind Video Temporal Consistency. ECCV 2018.