Skip to content

Instantly share code, notes, and snippets.

View andijakl's full-sized avatar

Andreas Jakl andijakl

View GitHub Profile
@andijakl
andijakl / DepthImageVisualizer.cs
Created November 27, 2020 10:16
Acquire the depth map from the AR Foundation Occlusion Manager.
void Update()
{
if (OcclusionManager.TryAcquireEnvironmentDepthCpuImage(out XRCpuImage image))
{
using (image)
{
// Use the texture.
UpdateRawImage(_rawImage, image);
}
}
@andijakl
andijakl / DepthImageVisualizer.cs
Created November 27, 2020 10:05
Properties of the DepthImageVisualizer class for Unity
public class DepthImageVisualizer : MonoBehaviour
{
public ARCameraManager CameraManager
{
get => _cameraManager;
set => _cameraManager = value;
}
[SerializeField]
[Tooltip("The ARCameraManager which will produce camera frame events.")]
@andijakl
andijakl / DepthGradient.shader
Created November 27, 2020 08:52
Part of the AR Foundation DepthGradient shader for Unity
fragment_output frag (v2f i)
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
// Sample the environment depth (in meters).
float envDistance = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord).r;
real lerpFactor = (envDistance - _MinDistance) / (_MaxDistance - _MinDistance);
real hue = lerp(0.70h, -0.15h, saturate(lerpFactor));
if (hue < 0.0h)
@andijakl
andijakl / colored_disparity_map.py
Last active April 20, 2022 07:26
Code to show a colored version of the (non-normalized) disparity map. Full article for context and remaining code: https://www.andreasjakl.com/how-to-apply-stereo-matching-to-generate-depth-maps-part-3
plt.imshow(disparity_SGBM, cmap='plasma')
plt.colorbar()
plt.show()
@andijakl
andijakl / stereo_disparity.py
Last active September 20, 2023 13:39
Calculate the stereo disparity with OpenCV and show a normalized depth map.
# ------------------------------------------------------------
# CALCULATE DISPARITY (DEPTH MAP)
# Adapted from: https://github.com/opencv/opencv/blob/master/samples/python/stereo_match.py
# and: https://docs.opencv.org/master/dd/d53/tutorial_py_depthmap.html
# StereoSGBM Parameter explanations:
# https://docs.opencv.org/4.5.0/d2/d85/classcv_1_1StereoSGBM.html
# Matched block size. It must be an odd number >=1 . Normally, it should be somewhere in the 3..11 range.
block_size = 11
@andijakl
andijakl / draw_stereo_rectified_images.py
Last active April 20, 2022 07:15
Draw the stereo rectified images along with horizontal lines to compare them. Full article for context and remaining code: https://www.andreasjakl.com/understand-and-apply-stereo-rectification-for-depth-maps-part-2/
# Draw the rectified images
fig, axes = plt.subplots(1, 2, figsize=(15, 10))
axes[0].imshow(img1_rectified, cmap="gray")
axes[1].imshow(img2_rectified, cmap="gray")
axes[0].axhline(250)
axes[1].axhline(250)
axes[0].axhline(450)
axes[1].axhline(450)
plt.suptitle("Rectified images")
plt.savefig("rectified_images.png")
@andijakl
andijakl / stereo_rectification_perspective_transformation.py
Last active April 20, 2022 07:15
Perspective transformations applied to the source images. Full article for context and remaining code: https://www.andreasjakl.com/understand-and-apply-stereo-rectification-for-depth-maps-part-2/
# Undistort (rectify) the images and save them
# Adapted from: https://stackoverflow.com/a/62607343
img1_rectified = cv.warpPerspective(img1, H1, (w1, h1))
img2_rectified = cv.warpPerspective(img2, H2, (w2, h2))
cv.imwrite("rectified_1.png", img1_rectified)
cv.imwrite("rectified_2.png", img2_rectified)
@andijakl
andijakl / stereo_rectification.py
Last active April 20, 2022 07:15
Stereo Rectification through OpenCV in the uncalibrated variant. Full article for context and remaining code: https://www.andreasjakl.com/understand-and-apply-stereo-rectification-for-depth-maps-part-2/
# Stereo rectification (uncalibrated variant)
# Adapted from: https://stackoverflow.com/a/62607343
h1, w1 = img1.shape
h2, w2 = img2.shape
_, H1, H2 = cv.stereoRectifyUncalibrated(
np.float32(pts1), np.float32(pts2), fundamental_matrix, imgSize=(w1, h1)
)
@andijakl
andijakl / visualize_epilines.py
Last active April 20, 2022 07:14
Visualize the epilines of both images. Full article for context and remaining code: https://www.andreasjakl.com/understand-and-apply-stereo-rectification-for-depth-maps-part-2/
# Visualize epilines
# Adapted from: https://docs.opencv.org/master/da/de9/tutorial_py_epipolar_geometry.html
def drawlines(img1src, img2src, lines, pts1src, pts2src):
''' img1 - image on which we draw the epilines for the points in img2
lines - corresponding epilines '''
r, c = img1src.shape
img1color = cv.cvtColor(img1src, cv.COLOR_GRAY2BGR)
img2color = cv.cvtColor(img2src, cv.COLOR_GRAY2BGR)
# Edit: use the same random seed so that two images are comparable!
np.random.seed(0)
@andijakl
andijakl / fundamental_matrix.py
Created November 23, 2020 16:42
Calculate the fundamental matrix for stereo rectification
# ------------------------------------------------------------
# STEREO RECTIFICATION
# Calculate the fundamental matrix for the cameras
# https://docs.opencv.org/master/da/de9/tutorial_py_epipolar_geometry.html
pts1 = np.int32(pts1)
pts2 = np.int32(pts2)
fundamental_matrix, inliers = cv.findFundamentalMat(pts1, pts2, cv.FM_RANSAC)
# We select only inlier points