# Structured light in Python

**URL:** <https://forum.opencv.org/t/structured-light-in-python/10842>\
**Category:** Python\
**Tags:** structured\_light\
**Created:** [November 6, 2022, 5:33pm UTC](https://forum.opencv.org/t/structured-light-in-python/10842 "2022-11-06T17:33:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fpl](https://avatars.discourse-cdn.com/v4/letter/f/76d3ee/32.png) [@fpl](https://forum.opencv.org/u/fpl)\
**Post date:** [November 6, 2022, 5:33pm UTC](https://forum.opencv.org/t/structured-light-in-python/10842/1 "2022-11-06T17:33:23Z")

</div>

Hello,  
i am trying to replicate this [tutorial](https://docs.opencv.org/4.x/dc/da9/tutorial_decode_graycode_pattern.html) in Python.  
It seems to work correctly but the disparity map i am getting contains only zeros.  
am I missing something?  
i am using 4.6.0 contrib from pip  
The code:

```auto
import cv2 as cv
import numpy as np
from pathlib import Path
import glob
import matplotlib.pyplot as plt

image_path=Path('data')
proj_width=1280
proj_height=800

cam_width=1920
cam_height=1280

black_threshold=0
white_threshold=0
cam1_imagelist = list(sorted(image_path.glob(pattern='pattern_cam1*.jpg')))
cam2_imagelist = list(sorted(image_path.glob(pattern='pattern_cam2*.jpg')))

#import camera calbiration
print("import camera calbiration...")
fs=cv.FileStorage()
fs.open("data/calibrationParameters.yml",cv.FileStorage_READ)
R=fs.getNode('R').mat()
T=fs.getNode('T').mat()
cam1_intrinsics=fs.getNode('cam1_intrinsics').mat()
cam2_intrinsics=fs.getNode('cam2_intrinsics').mat()
cam1_distorsion=fs.getNode('cam1_distorsion').mat()
cam2_distorsion=fs.getNode('cam2_distorsion').mat()
cam1_size=fs.getNode('cam1_size').at(0).real(),fs.getNode('cam1_size').at(1).real()
cam2_size=fs.getNode('cam2_size').at(0).real(),fs.getNode('cam2_size').at(1).real()
stereo_error=fs.getNode('stereo_error').real()

print("done")

graycode=cv.structured_light.GrayCodePattern.create(proj_width, proj_height)
number_of_pattern_images=graycode.getNumberOfPatternImages()
graycode.setBlackThreshold(black_threshold)
graycode.setWhiteThreshold(white_threshold)

retval, patternImages =graycode.generate()

color=cv.imread(str(cam1_imagelist[number_of_pattern_images]))

#for i,pattern in enumerate(patternImages):
    #cv.imwrite(str(i)+"pat.png",pattern)
    
image_size=color[:,:,0]

print("rectifying images...")

R1, R2, P1, P2, Q, validPixROI1, validPixROI2 = cv.stereoRectify(cam1_intrinsics, cam1_distorsion, cam2_intrinsics, cam2_distorsion, (cam_width,cam_height), R, T)

map1x, map1y = cv.initUndistortRectifyMap(cam1_intrinsics, cam1_distorsion, R1, P1, (cam_width,cam_height), cv.CV_32FC1)
map2x, map2y = cv.initUndistortRectifyMap(cam2_intrinsics, cam2_distorsion, R2, P2, (cam_width,cam_height), cv.CV_32FC1) 

captured_pattern=np.empty((2,number_of_pattern_images+2,cam_height,cam_width))

for i,image in enumerate(cam1_imagelist):
    captured_pattern[0,i]=cv.imread(str(image),cv.IMREAD_GRAYSCALE)
    #plt.imshow(captured_pattern[0,i],cmap='gray')
    #plt.show()
    captured_pattern[0,i]=cv.remap(captured_pattern[0,i], map2x, map2y, cv.INTER_NEAREST, borderMode=cv.BORDER_CONSTANT) #borderValue=(0, 0, 0, 0)
    #plt.imshow(captured_pattern[0,i],cmap='gray')
    #plt.show()

    
for i,image in enumerate(cam2_imagelist):
    captured_pattern[1,i]=cv.imread(str(image),cv.IMREAD_GRAYSCALE)
    captured_pattern[1,i]=cv.remap(captured_pattern[1,i], map1x, map1y, cv.INTER_NEAREST, borderMode=cv.BORDER_CONSTANT)
    

     
black_images=np.empty((2,cam_height,cam_width))
white_images=np.empty((2,cam_height,cam_width))

black_images[0]=captured_pattern[0,number_of_pattern_images+1]
black_images[1]=captured_pattern[1,number_of_pattern_images+1]
white_images[0]=captured_pattern[0,number_of_pattern_images]
white_images[1]=captured_pattern[1,number_of_pattern_images]

      
captured_pattern=captured_pattern[:,:-2,:,:]

print("done")

print("decoding and reprojecting...")

#decoding
decoded,disparity_map=graycode.decode(captured_pattern,blackImages=black_images,whiteImages=white_images,flags=cv.structured_light.DECODE_3D_UNDERWORLD)

```

it should work using this [dataset](https://github.com/opencv/opencv_extra/tree/4.x/testdata/cv/structured_light/data), but you need to rename the single digit images: “pattern\_cam1\_im2” to “pattern\_cam1\_im02”, because they will be alphabetically sorted and i was to lazy to modify the code.

---

<div class="post-metadata">

**Author:** ![Sharan\_Rabinson\_M](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sharan_rabinson_m/32/11049_2.png) [@Sharan\_Rabinson\_M](https://forum.opencv.org/u/Sharan_Rabinson_M)\
**Post date:** [September 3, 2024, 1:13pm UTC](https://forum.opencv.org/t/structured-light-in-python/10842/2 "2024-09-03T13:13:41Z")

</div>

i tried replicating the same. my disparity map contains only zeros.  
Here is the code:

```auto
#!/usr/bin/env python
# coding: utf-8

# In[69]:

import cv2 as cv
import numpy as np
from pathlib import Path
import glob
import matplotlib.pyplot as plt

image_path=Path('data')
proj_width=1280
proj_height=800

cam_width=1920
cam_height=1280

black_threshold=0
white_threshold=0

cam1_imagelist=[]
cam2_imagelist=[]
for i in range(1,45):
        filename = f"pattern_cam1_im{i}.jpg"
        filepath = os.path.join(r"C:\Users\SARAN\Desktop\test\1", filename)
        cam1_imagelist.append(filepath)
        
for i in range(1,45):
        filename = f"pattern_cam2_im{i}.jpg"
        filepath = os.path.join(r"C:\Users\SARAN\Desktop\test\2", filename)
        cam2_imagelist.append(filepath)

# In[70]:

#import camera calbiration
print("import camera calbiration...")
fs=cv.FileStorage()
fs.open("calibrationParameters.yml",cv.FileStorage_READ)
R=fs.getNode('R').mat()
T=fs.getNode('T').mat()
cam1_intrinsics=fs.getNode('cam1_intrinsics').mat()
cam2_intrinsics=fs.getNode('cam2_intrinsics').mat()
cam1_distorsion=fs.getNode('cam1_distorsion').mat()
cam2_distorsion=fs.getNode('cam2_distorsion').mat()
cam1_size=fs.getNode('cam1_size').at(0).real(),fs.getNode('cam1_size').at(1).real()
cam2_size=fs.getNode('cam2_size').at(0).real(),fs.getNode('cam2_size').at(1).real()
stereo_error=fs.getNode('stereo_error').real()

print("done")

# In[71]:

graycode=cv.structured_light.GrayCodePattern.create(proj_width, proj_height)
number_of_pattern_images=graycode.getNumberOfPatternImages()
graycode.setBlackThreshold(black_threshold)
graycode.setWhiteThreshold(white_threshold)

retval, patternImages =graycode.generate()

# In[72]:

color=cv.imread(str(cam1_imagelist[number_of_pattern_images]))

    
image_size=color[:,:,0]

print("rectifying images...")

R1, R2, P1, P2, Q, validPixROI1, validPixROI2 = cv.stereoRectify(cam1_intrinsics, cam1_distorsion, cam2_intrinsics, cam2_distorsion, (cam_width,cam_height), R, T)

# In[73]:

map1x, map1y = cv.initUndistortRectifyMap(cam1_intrinsics, cam1_distorsion, R1, P1, (cam_width,cam_height), cv.CV_32FC1)
map2x, map2y = cv.initUndistortRectifyMap(cam2_intrinsics, cam2_distorsion, R2, P2, (cam_width,cam_height), cv.CV_32FC1) 

captured_pattern=np.empty((2,number_of_pattern_images+2,cam_height,cam_width))

for i,image in enumerate(cam1_imagelist):
    captured_pattern[0,i]=cv.imread(str(image),cv.IMREAD_GRAYSCALE)
    #plt.imshow(captured_pattern[0,i],cmap='gray')
    #plt.show()
    captured_pattern[0,i]=cv.remap(captured_pattern[0,i], map2x, map2y, cv.INTER_NEAREST, borderMode=cv.BORDER_CONSTANT) #borderValue=(0, 0, 0, 0)
    #plt.imshow(captured_pattern[0,i],cmap='gray')
    #plt.show()

    
for i,image in enumerate(cam2_imagelist):
    captured_pattern[1,i]=cv.imread(str(image),cv.IMREAD_GRAYSCALE)
    captured_pattern[1,i]=cv.remap(captured_pattern[1,i], map1x, map1y, cv.INTER_NEAREST, borderMode=cv.BORDER_CONSTANT)
    

     
black_images=np.empty((2,cam_height,cam_width))
white_images=np.empty((2,cam_height,cam_width))

black_images[0]=captured_pattern[0,number_of_pattern_images+1]
black_images[1]=captured_pattern[1,number_of_pattern_images+1]
white_images[0]=captured_pattern[0,number_of_pattern_images]
white_images[1]=captured_pattern[1,number_of_pattern_images]

      
captured_pattern=captured_pattern[:,:-2,:,:]

print("done")

# In[74]:

print("decoding and reprojecting...")
#decoding
decoded,disparity_map=graycode.decode(captured_pattern,blackImages=black_images,whiteImages=white_images,flags=cv.structured_light.DECODE_3D_UNDERWORLD)

# In[75]:

print(disparity_map)

```

Am I missing something?  
I have also loaded the images in the correct sequence.
