Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Homography ipython is added

mona77a4a611ea3cbe
diff --git a/path.bash.inc b/path.bash.inc
index 0fd650d0c..ab3f16e83 100644
--- a/path.bash.inc
+++ b/path.bash.inc
@@ -12,4 +12,4 @@ if [ ! -e $sdk_dir/.dev_env/bin/conda ]; then
$sdk_dir/setup_dev_env.sh
fi
source $sdk_dir/.dev_env/bin/activate $sdk_dir/.dev_env
-export PYTHONPATH=$PYTHONPATH:$sdk_root
+export PYTHONPATH=$PYTHONPATH:$sdk_dir
diff --git a/setup_dev_env.sh b/setup_dev_env.sh
index fb0b1b0d8..111b86bcf 100755
--- a/setup_dev_env.sh
+++ b/setup_dev_env.sh
@@ -19,11 +19,12 @@ fi
$sdk_dir/.dev_env/bin/conda install -y \
ipython \
+ jupyter \
+ matplotlib \
numpy \
pytorch \
torchvision \
opencv \
-c pytorch
-
-$sdk_dir/.dev_env/bin/conda clean -ya
+$sdk_dir/.dev_env/bin/conda clean -ya
diff --git a/torchgeometry/utils.py b/torchgeometry/utils.py
index d856545e6..f7f9bed4c 100644
--- a/torchgeometry/utils.py
+++ b/torchgeometry/utils.py
@@ -1,8 +1,12 @@
+import cv2
+import numpy as np
import torch
+import torchgeometry as tgm
__all__ = [
"tensor_to_image",
"image_to_tensor",
+ "draw_rectangle"
]
@@ -25,3 +29,32 @@ def tensor_to_image(tensor):
if len(tensor.shape) == 2:
tensor = torch.unsqueeze(tensor, dim=0)
return tensor.permute(1, 2, 0).contiguous().cpu().detach().numpy()
+
+def draw_rectangle(image, dst_homo_src):
+ height, width = image.shape[:2]
+ pts_src = torch.FloatTensor([[
+ [-1, -1], # top-left
+ [1, -1], # bottom-left
+ [1, 1], # bottom-right
+ [-1, 1], # top-right
+ ]]).to(dst_homo_src.device)
+ # transform points
+ pts_dst = tgm.transform_points(tgm.inverse(dst_homo_src), pts_src)
+ def compute_factor(size):
+ return 1.0 * size / 2
+
+ def convert_coordinates_to_pixel(coordinates, factor):
+ return factor * (coordinates + 1.0)
+ # compute convertion factor
+ x_factor = compute_factor(width - 1)
+ y_factor = compute_factor(height - 1)
+ pts_dst = pts_dst.cpu().squeeze().detach().numpy()
+ pts_dst[..., 0] = convert_coordinates_to_pixel(
+ pts_dst[..., 0], x_factor)
+ pts_dst[..., 1] = convert_coordinates_to_pixel(
+ pts_dst[..., 1], y_factor)
+ # do the actual drawing
+ for i in range(4):
+ pt_i, pt_ii = tuple(pts_dst[i % 4]), tuple(pts_dst[(i + 1) % 4])
+ image = cv2.line(image, pt_i, pt_ii, (255, 0, 0), 3)
+ return image