v9.1.1
Menu

Gray Code Pattern Decoding

mediumFREE3D ScanningScanningCV: 3D Reconstruction

Decode Gray code binary patterns to get projector column index.

Structured light scanning projects binary patterns onto objects. Gray code is preferred over standard binary because adjacent columns differ by only one bit, making it robust to slight misalignments.

For N patterns, decode to column index: col=∑i=0N−1bi⋅2N−1−icol = \sum_{i=0}^{N-1} b_i \cdot 2^{N-1-i}

This treats the pattern bits as a binary number with the first pattern as the most significant bit. The resulting value indexes into the projector's column space.

Example

Input
decode_gray([1, 0, 1, 0])
Output
10
Reasoning
  • Input: patterns holds one bit per projected pattern seen at one camera pixel: 11 = lit, 00 = dark. Here N=4N = 4 patterns, so the bits are b0=1, b1=0, b2=1, b3=0b_0 = 1,\ b_1 = 0,\ b_2 = 1,\ b_3 = 0.
  • Weights: pattern ii carries weight 2N−1−i2^{N-1-i}. With N=4N = 4 the weights are 23=82^3 = 8, 22=42^2 = 4, 21=22^1 = 2, 20=12^0 = 1. The first pattern is the most significant: it splits the columns in half.
  • Contributions: only lit patterns add their weight: 1⋅8+0⋅4+1⋅2+0⋅1=8+21 \cdot 8 + 0 \cdot 4 + 1 \cdot 2 + 0 \cdot 1 = 8 + 2.
  • Output: col=10col = 10, the integer index of the projector column that lit this pixel. Four patterns can address 24=162^4 = 16 columns, numbered 00 to 1515, so all four dark gives 00 and all four lit gives 8+4+2+1=158 + 4 + 2 + 1 = 15.
  • Notice: the bits are read as ordinary binary, first bit most significant. There is no separate Gray-to-binary conversion, so flipping only the last bit moves the column by exactly 11.

Constraints

  • patterns: list of binary values [b0, b1, ..., bN-1]
  • Return decoded column index (integer)

Learn this in

More from CV: 3D Reconstruction

solution.py

Test results

Sign in to run your code against the test cases.