Skip to content

Instantly share code, notes, and snippets.

Avatar

Andreas Jakl andijakl

View GitHub Profile
@andijakl
andijakl / DepthImageVisualizer.cs
Created Nov 27, 2020
Adapt the RawImage size in Unity to the depth map
View DepthImageVisualizer.cs
// Get the aspect ratio for the current texture.
var textureAspectRatio = (float)texture.width / texture.height;
// Determine the raw image rectSize preserving the texture aspect ratio, matching the screen orientation,
// and keeping a minimum dimension size.
const float minDimension = 480.0f;
var maxDimension = Mathf.Round(minDimension * textureAspectRatio);
var rectSize = new Vector2(maxDimension, minDimension);
//var rectSize = new Vector2(minDimension, maxDimension); //Portrait
rawImage.rectTransform.sizeDelta = rectSize;
@andijakl
andijakl / DepthImageVisualizer.cs
Created Nov 27, 2020
Convert the XRCpuImage to a Texture2D for AR Foundation.
View DepthImageVisualizer.cs
// Source: https://github.com/Unity-Technologies/arfoundation-samples/blob/6296272a416925b56ce85470e0c7bef5c913ec0c/Assets/Scripts/CpuImageSample.cs
private static void UpdateRawImage(RawImage rawImage, XRCpuImage cpuImage)
{
// Get the texture associated with the UI.RawImage that we wish to display on screen.
var texture = rawImage.texture as Texture2D;
// If the texture hasn't yet been created, or if its dimensions have changed, (re)create the texture.
// Note: Although texture dimensions do not normally change frame-to-frame, they can change in response to
// a change in the camera resolution (for camera images) or changes to the quality of the human depth
// and human stencil buffers.
@andijakl
andijakl / DepthImageVisualizer.cs
Created Nov 27, 2020
Acquire the depth map from the AR Foundation Occlusion Manager.
View DepthImageVisualizer.cs
void Update()
{
if (OcclusionManager.TryAcquireEnvironmentDepthCpuImage(out XRCpuImage image))
{
using (image)
{
// Use the texture.
UpdateRawImage(_rawImage, image);
}
}
@andijakl
andijakl / DepthImageVisualizer.cs
Created Nov 27, 2020
Properties of the DepthImageVisualizer class for Unity
View DepthImageVisualizer.cs
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 Nov 27, 2020
Part of the AR Foundation DepthGradient shader for Unity
View DepthGradient.shader
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
Created Nov 24, 2020
Code to show a colored version of the (non-normalized) disparity map.
View colored_disparity_map.py
plt.imshow(disparity_SGBM, cmap='plasma')
plt.colorbar()
plt.show()
@andijakl
andijakl / stereo_disparity.py
Last active Nov 24, 2020
Calculate the stereo disparity with OpenCV and show a normalized depth map.
View stereo_disparity.py
# ------------------------------------------------------------
# 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 Nov 24, 2020
Draw the stereo rectified images along with horizontal lines to compare them
View draw_stereo_rectified_images.py
# 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 Nov 24, 2020
Perspective transformations applied to the source images.
View stereo_rectification_perspective_transformation.py
# 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 Nov 23, 2020
Stereo Rectification through OpenCV in the uncalibrated variant
View stereo_rectification.py
# 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)
)
You can’t perform that action at this time.