Structured light in Python

Hello,
i am trying to replicate this tutorial 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:

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, 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.

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

#!/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.

1 Like