All files / web/src/lib/vision markerInpainting.ts

0% Statements 0/289
0% Branches 0/1
0% Functions 0/1
0% Lines 0/289

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
/**
 * Marker Inpainting Utility
 *
 * Removes ArUco markers from images for boundary detector training.
 * The model should learn to detect abacus boundaries by visual characteristics,
 * not by the presence of markers.
 *
 * Uses a simple blur-based inpainting approach:
 * 1. Identify marker regions from corner positions
 * 2. Replace each marker region with blurred surrounding context
 */

import type { QuadCorners, Point } from '@/types/vision'

/**
 * Marker size as a fraction of image width (ArUco markers are typically ~5% of frame)
 */
const DEFAULT_MARKER_SIZE_RATIO = 0.06

/**
 * Blur radius as multiplier of marker size
 */
const BLUR_RADIUS_MULTIPLIER = 0.3

/**
 * Sample radius as multiplier of marker size (for sampling surrounding pixels)
 */
const SAMPLE_RADIUS_MULTIPLIER = 1.2

interface MarkerRegion {
  /** Center X in pixels */
  cx: number
  /** Center Y in pixels */
  cy: number
  /** Half-width in pixels */
  hw: number
  /** Half-height in pixels */
  hh: number
}

/**
 * Calculate the bounding region for a marker corner
 */
function getMarkerRegion(
  corner: Point,
  imageWidth: number,
  imageHeight: number,
  markerSizeRatio: number = DEFAULT_MARKER_SIZE_RATIO
): MarkerRegion {
  // Denormalize if coordinates are in 0-1 range
  const cx = corner.x <= 1 ? corner.x * imageWidth : corner.x
  const cy = corner.y <= 1 ? corner.y * imageHeight : corner.y

  // Calculate marker size based on image dimensions
  const markerSize = Math.min(imageWidth, imageHeight) * markerSizeRatio
  const hw = markerSize / 2
  const hh = markerSize / 2

  return { cx, cy, hw, hh }
}

/**
 * Sample the average color from a ring around a region (excluding the center)
 */
function sampleSurroundingColor(
  imageData: ImageData,
  region: MarkerRegion,
  sampleRadiusMult: number = SAMPLE_RADIUS_MULTIPLIER
): { r: number; g: number; b: number } {
  const { cx, cy, hw, hh } = region
  const sampleHw = hw * sampleRadiusMult
  const sampleHh = hh * sampleRadiusMult

  const data = imageData.data
  const width = imageData.width
  const height = imageData.height

  let r = 0,
    g = 0,
    b = 0,
    count = 0

  // Sample pixels in the outer ring
  const left = Math.max(0, Math.floor(cx - sampleHw))
  const right = Math.min(width - 1, Math.ceil(cx + sampleHw))
  const top = Math.max(0, Math.floor(cy - sampleHh))
  const bottom = Math.min(height - 1, Math.ceil(cy + sampleHh))

  const innerLeft = Math.floor(cx - hw)
  const innerRight = Math.ceil(cx + hw)
  const innerTop = Math.floor(cy - hh)
  const innerBottom = Math.ceil(cy + hh)

  for (let y = top; y <= bottom; y++) {
    for (let x = left; x <= right; x++) {
      // Skip pixels inside the marker region
      if (x >= innerLeft && x <= innerRight && y >= innerTop && y <= innerBottom) {
        continue
      }

      const idx = (y * width + x) * 4
      r += data[idx]
      g += data[idx + 1]
      b += data[idx + 2]
      count++
    }
  }

  if (count === 0) {
    return { r: 128, g: 128, b: 128 } // Default gray
  }

  return {
    r: Math.round(r / count),
    g: Math.round(g / count),
    b: Math.round(b / count),
  }
}

/**
 * Apply a simple blur/fill to a region using surrounding colors
 * with a gradual falloff from edges
 */
function fillRegionWithGradient(
  imageData: ImageData,
  region: MarkerRegion,
  surroundingColor: { r: number; g: number; b: number }
): void {
  const { cx, cy, hw, hh } = region
  const data = imageData.data
  const width = imageData.width
  const height = imageData.height

  const left = Math.max(0, Math.floor(cx - hw))
  const right = Math.min(width - 1, Math.ceil(cx + hw))
  const top = Math.max(0, Math.floor(cy - hh))
  const bottom = Math.min(height - 1, Math.ceil(cy + hh))

  for (let y = top; y <= bottom; y++) {
    for (let x = left; x <= right; x++) {
      const idx = (y * width + x) * 4

      // Calculate distance from center as fraction (0 at center, 1 at edge)
      const dx = (x - cx) / hw
      const dy = (y - cy) / hh
      const dist = Math.sqrt(dx * dx + dy * dy)

      // Only fill inside an ellipse
      if (dist <= 1) {
        // Blend factor: more blending near center, less near edges
        const blend = Math.cos((dist * Math.PI) / 2) // 1 at center, 0 at edge

        // Get original pixel
        const origR = data[idx]
        const origG = data[idx + 1]
        const origB = data[idx + 2]

        // Blend with surrounding color
        data[idx] = Math.round(origR * (1 - blend) + surroundingColor.r * blend)
        data[idx + 1] = Math.round(origG * (1 - blend) + surroundingColor.g * blend)
        data[idx + 2] = Math.round(origB * (1 - blend) + surroundingColor.b * blend)
        // Alpha unchanged
      }
    }
  }
}

/**
 * Remove markers from an image by inpainting the marker regions
 *
 * @param canvas - Canvas element with the source image
 * @param corners - Normalized corner positions (0-1 range)
 * @param markerSizeRatio - Size of markers as fraction of image width
 * @returns New canvas with markers removed
 */
export function inpaintMarkers(
  canvas: HTMLCanvasElement,
  corners: QuadCorners,
  markerSizeRatio: number = DEFAULT_MARKER_SIZE_RATIO
): HTMLCanvasElement {
  const ctx = canvas.getContext('2d')
  if (!ctx) {
    throw new Error('Failed to get canvas context')
  }

  const width = canvas.width
  const height = canvas.height

  // Create output canvas
  const outputCanvas = document.createElement('canvas')
  outputCanvas.width = width
  outputCanvas.height = height
  const outputCtx = outputCanvas.getContext('2d')
  if (!outputCtx) {
    throw new Error('Failed to get output canvas context')
  }

  // Copy source to output
  outputCtx.drawImage(canvas, 0, 0)

  // Get image data for manipulation
  const imageData = outputCtx.getImageData(0, 0, width, height)

  // Process each corner
  const cornerList: Point[] = [
    corners.topLeft,
    corners.topRight,
    corners.bottomLeft,
    corners.bottomRight,
  ]

  for (const corner of cornerList) {
    const region = getMarkerRegion(corner, width, height, markerSizeRatio)
    const surroundingColor = sampleSurroundingColor(imageData, region)
    fillRegionWithGradient(imageData, region, surroundingColor)
  }

  // Write back
  outputCtx.putImageData(imageData, 0, 0)

  return outputCanvas
}

/**
 * Remove markers from an ImageData object
 *
 * @param imageData - Source image data
 * @param corners - Normalized corner positions (0-1 range)
 * @param markerSizeRatio - Size of markers as fraction of image width
 * @returns New ImageData with markers removed
 */
export function inpaintMarkersImageData(
  imageData: ImageData,
  corners: QuadCorners,
  markerSizeRatio: number = DEFAULT_MARKER_SIZE_RATIO
): ImageData {
  const width = imageData.width
  const height = imageData.height

  // Clone the image data
  const outputData = new ImageData(new Uint8ClampedArray(imageData.data), width, height)

  // Process each corner
  const cornerList: Point[] = [
    corners.topLeft,
    corners.topRight,
    corners.bottomLeft,
    corners.bottomRight,
  ]

  for (const corner of cornerList) {
    const region = getMarkerRegion(corner, width, height, markerSizeRatio)
    const surroundingColor = sampleSurroundingColor(outputData, region)
    fillRegionWithGradient(outputData, region, surroundingColor)
  }

  return outputData
}

/**
 * Convert image data to base64 PNG with markers inpainted
 *
 * @param imageData - Source image data
 * @param corners - Normalized corner positions (0-1 range)
 * @param markerSizeRatio - Size of markers as fraction of image width
 * @returns Base64 encoded PNG string (without data URL prefix)
 */
export function inpaintToBase64Png(
  imageData: ImageData,
  corners: QuadCorners,
  markerSizeRatio: number = DEFAULT_MARKER_SIZE_RATIO
): string {
  const inpainted = inpaintMarkersImageData(imageData, corners, markerSizeRatio)

  // Create canvas to convert to PNG
  const canvas = document.createElement('canvas')
  canvas.width = inpainted.width
  canvas.height = inpainted.height
  const ctx = canvas.getContext('2d')
  if (!ctx) {
    throw new Error('Failed to get canvas context')
  }

  ctx.putImageData(inpainted, 0, 0)

  // Convert to base64
  const dataUrl = canvas.toDataURL('image/png')
  return dataUrl.replace(/^data:image\/png;base64,/, '')
}