This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Update() | |
{ | |
if (OcclusionManager.TryAcquireEnvironmentDepthCpuImage(out XRCpuImage image)) | |
{ | |
using (image) | |
{ | |
// Use the texture. | |
UpdateRawImage(_rawImage, image); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DepthImageVisualizer : MonoBehaviour | |
{ | |
public ARCameraManager CameraManager | |
{ | |
get => _cameraManager; | |
set => _cameraManager = value; | |
} | |
[SerializeField] | |
[Tooltip("The ARCameraManager which will produce camera frame events.")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plt.imshow(disparity_SGBM, cmap='plasma') | |
plt.colorbar() | |
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ------------------------------------------------------------ | |
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ------------------------------------------------------------ | |
# 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 |