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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | /** * Marker Masking Utility * * Masks ArUco markers in images to prevent the model from "cheating" * by learning to detect markers instead of the actual abacus frame. * * Used during training data visualization and preprocessing. */ import { loadAruco, initArucoDetector, MARKER_IDS, type MarkerCorners } from './arucoDetection' // Re-export the ArUco marker interface for use by consumers export type { MarkerCorners } interface ArucoMarker { id: number corners: Array<{ x: number; y: number }> } interface ArucoDetector { detect: (imageData: ImageData) => ArucoMarker[] } /** * Result of marker masking operation */ export interface MarkerMaskingResult { /** The masked image as a data URL */ maskedImageUrl: string /** Number of markers that were masked */ markersMasked: number /** Detected marker positions (for visualization) */ markerPositions: MarkerCorners[] /** Original image dimensions */ width: number height: number } /** * Fill type for masked regions */ export type MaskFillType = 'noise' | 'gray' | 'localMean' /** * Extract corner positions from js-aruco2 marker */ function extractMarkerCorners(marker: ArucoMarker): MarkerCorners { const corners = marker.corners const topLeft = { x: corners[0].x, y: corners[0].y } const topRight = { x: corners[1].x, y: corners[1].y } const bottomRight = { x: corners[2].x, y: corners[2].y } const bottomLeft = { x: corners[3].x, y: corners[3].y } const center = { x: (topLeft.x + topRight.x + bottomRight.x + bottomLeft.x) / 4, y: (topLeft.y + topRight.y + bottomRight.y + bottomLeft.y) / 4, } return { topLeft, topRight, bottomRight, bottomLeft, center } } /** * Get bounding box of a marker with optional padding */ function getMarkerBounds( marker: MarkerCorners, padding: number = 0 ): { minX: number minY: number maxX: number maxY: number } { const xs = [marker.topLeft.x, marker.topRight.x, marker.bottomRight.x, marker.bottomLeft.x] const ys = [marker.topLeft.y, marker.topRight.y, marker.bottomRight.y, marker.bottomLeft.y] return { minX: Math.min(...xs) - padding, minY: Math.min(...ys) - padding, maxX: Math.max(...xs) + padding, maxY: Math.max(...ys) + padding, } } /** * Fill a polygon region with noise */ function fillPolygonWithNoise( ctx: CanvasRenderingContext2D, imageData: ImageData, marker: MarkerCorners, padding: number = 5 ): void { const bounds = getMarkerBounds(marker, padding) const { minX, minY, maxX, maxY } = bounds // Clamp to image bounds const startX = Math.max(0, Math.floor(minX)) const startY = Math.max(0, Math.floor(minY)) const endX = Math.min(imageData.width, Math.ceil(maxX)) const endY = Math.min(imageData.height, Math.ceil(maxY)) // Create polygon path for hit testing const path = new Path2D() path.moveTo(marker.topLeft.x, marker.topLeft.y) path.lineTo(marker.topRight.x, marker.topRight.y) path.lineTo(marker.bottomRight.x, marker.bottomRight.y) path.lineTo(marker.bottomLeft.x, marker.bottomLeft.y) path.closePath() // Expand the path slightly for padding const expandedPath = new Path2D() const cx = marker.center.x const cy = marker.center.y const scale = 1 + padding / 50 // Approximate scaling expandedPath.moveTo(cx + (marker.topLeft.x - cx) * scale, cy + (marker.topLeft.y - cy) * scale) expandedPath.lineTo(cx + (marker.topRight.x - cx) * scale, cy + (marker.topRight.y - cy) * scale) expandedPath.lineTo( cx + (marker.bottomRight.x - cx) * scale, cy + (marker.bottomRight.y - cy) * scale ) expandedPath.lineTo( cx + (marker.bottomLeft.x - cx) * scale, cy + (marker.bottomLeft.y - cy) * scale ) expandedPath.closePath() // Fill pixels within the expanded polygon with noise for (let y = startY; y < endY; y++) { for (let x = startX; x < endX; x++) { if (ctx.isPointInPath(expandedPath, x, y)) { const idx = (y * imageData.width + x) * 4 // Random noise with some base gray const noise = Math.random() * 80 + 88 // 88-168 range (grayish noise) imageData.data[idx] = noise imageData.data[idx + 1] = noise imageData.data[idx + 2] = noise // Keep alpha at 255 } } } } /** * Fill a polygon region with solid gray */ function fillPolygonWithGray( ctx: CanvasRenderingContext2D, imageData: ImageData, marker: MarkerCorners, padding: number = 5 ): void { const bounds = getMarkerBounds(marker, padding) const { minX, minY, maxX, maxY } = bounds const startX = Math.max(0, Math.floor(minX)) const startY = Math.max(0, Math.floor(minY)) const endX = Math.min(imageData.width, Math.ceil(maxX)) const endY = Math.min(imageData.height, Math.ceil(maxY)) const cx = marker.center.x const cy = marker.center.y const scale = 1 + padding / 50 const expandedPath = new Path2D() expandedPath.moveTo(cx + (marker.topLeft.x - cx) * scale, cy + (marker.topLeft.y - cy) * scale) expandedPath.lineTo(cx + (marker.topRight.x - cx) * scale, cy + (marker.topRight.y - cy) * scale) expandedPath.lineTo( cx + (marker.bottomRight.x - cx) * scale, cy + (marker.bottomRight.y - cy) * scale ) expandedPath.lineTo( cx + (marker.bottomLeft.x - cx) * scale, cy + (marker.bottomLeft.y - cy) * scale ) expandedPath.closePath() const gray = 128 for (let y = startY; y < endY; y++) { for (let x = startX; x < endX; x++) { if (ctx.isPointInPath(expandedPath, x, y)) { const idx = (y * imageData.width + x) * 4 imageData.data[idx] = gray imageData.data[idx + 1] = gray imageData.data[idx + 2] = gray } } } } /** * Fill a polygon region with local mean color (from surrounding pixels) */ function fillPolygonWithLocalMean( ctx: CanvasRenderingContext2D, imageData: ImageData, marker: MarkerCorners, padding: number = 5 ): void { const bounds = getMarkerBounds(marker, padding) const { minX, minY, maxX, maxY } = bounds const startX = Math.max(0, Math.floor(minX)) const startY = Math.max(0, Math.floor(minY)) const endX = Math.min(imageData.width, Math.ceil(maxX)) const endY = Math.min(imageData.height, Math.ceil(maxY)) // Calculate mean color from border pixels let sumR = 0, sumG = 0, sumB = 0, count = 0 // Sample from border region const borderWidth = 10 for (let y = startY; y < endY; y++) { for (let x = startX; x < endX; x++) { const isInBorder = x < startX + borderWidth || x >= endX - borderWidth || y < startY + borderWidth || y >= endY - borderWidth if (isInBorder) { const idx = (y * imageData.width + x) * 4 sumR += imageData.data[idx] sumG += imageData.data[idx + 1] sumB += imageData.data[idx + 2] count++ } } } const meanR = count > 0 ? sumR / count : 128 const meanG = count > 0 ? sumG / count : 128 const meanB = count > 0 ? sumB / count : 128 const cx = marker.center.x const cy = marker.center.y const scale = 1 + padding / 50 const expandedPath = new Path2D() expandedPath.moveTo(cx + (marker.topLeft.x - cx) * scale, cy + (marker.topLeft.y - cy) * scale) expandedPath.lineTo(cx + (marker.topRight.x - cx) * scale, cy + (marker.topRight.y - cy) * scale) expandedPath.lineTo( cx + (marker.bottomRight.x - cx) * scale, cy + (marker.bottomRight.y - cy) * scale ) expandedPath.lineTo( cx + (marker.bottomLeft.x - cx) * scale, cy + (marker.bottomLeft.y - cy) * scale ) expandedPath.closePath() for (let y = startY; y < endY; y++) { for (let x = startX; x < endX; x++) { if (ctx.isPointInPath(expandedPath, x, y)) { const idx = (y * imageData.width + x) * 4 // Add slight noise to local mean to prevent solid color pattern const noiseAmount = 15 imageData.data[idx] = Math.min( 255, Math.max(0, meanR + (Math.random() - 0.5) * noiseAmount) ) imageData.data[idx + 1] = Math.min( 255, Math.max(0, meanG + (Math.random() - 0.5) * noiseAmount) ) imageData.data[idx + 2] = Math.min( 255, Math.max(0, meanB + (Math.random() - 0.5) * noiseAmount) ) } } } } /** * Load an image from a URL into a canvas */ async function loadImageToCanvas(imageSrc: string): Promise<{ canvas: HTMLCanvasElement ctx: CanvasRenderingContext2D }> { return new Promise((resolve, reject) => { const img = new Image() img.crossOrigin = 'anonymous' img.onload = () => { const canvas = document.createElement('canvas') canvas.width = img.naturalWidth canvas.height = img.naturalHeight const ctx = canvas.getContext('2d', { willReadFrequently: true }) if (!ctx) { reject(new Error('Failed to get canvas context')) return } ctx.drawImage(img, 0, 0) resolve({ canvas, ctx }) } img.onerror = () => { reject(new Error(`Failed to load image: ${imageSrc}`)) } img.src = imageSrc }) } /** * Detect and mask ArUco markers in an image * * @param imageSrc - URL of the image to process * @param fillType - How to fill masked regions: 'noise', 'gray', or 'localMean' * @param markerPadding - Pixels of padding around each marker to mask * @returns Promise resolving to masking result with data URL */ export async function maskMarkersInImage( imageSrc: string, fillType: MaskFillType = 'noise', markerPadding: number = 10 ): Promise<MarkerMaskingResult> { // Ensure ArUco library is loaded await loadAruco() initArucoDetector() // Load image const { canvas, ctx } = await loadImageToCanvas(imageSrc) // Get image data for marker detection const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height) // Detect markers const AR = (window as { AR?: { Detector: new (options?: object) => ArucoDetector } }).AR if (!AR) { throw new Error('ArUco library not loaded') } const detector = new AR.Detector({ dictionaryName: 'ARUCO' }) const markers = detector.detect(imageData) // Filter to our corner markers (IDs 0-3) const cornerMarkers = markers.filter( (m) => m.id === MARKER_IDS.TOP_LEFT || m.id === MARKER_IDS.TOP_RIGHT || m.id === MARKER_IDS.BOTTOM_RIGHT || m.id === MARKER_IDS.BOTTOM_LEFT ) const markerPositions: MarkerCorners[] = [] // Mask each detected marker for (const marker of cornerMarkers) { const markerCorners = extractMarkerCorners(marker) markerPositions.push(markerCorners) switch (fillType) { case 'noise': fillPolygonWithNoise(ctx, imageData, markerCorners, markerPadding) break case 'gray': fillPolygonWithGray(ctx, imageData, markerCorners, markerPadding) break case 'localMean': fillPolygonWithLocalMean(ctx, imageData, markerCorners, markerPadding) break } } // Put modified image data back to canvas ctx.putImageData(imageData, 0, 0) // Convert to data URL const maskedImageUrl = canvas.toDataURL('image/png') return { maskedImageUrl, markersMasked: cornerMarkers.length, markerPositions, width: canvas.width, height: canvas.height, } } /** * Check if ArUco masking is available */ export function isMarkerMaskingAvailable(): boolean { return typeof window !== 'undefined' } |