Skip to content

Commit

Permalink
feat: image transform with uv index
Browse files Browse the repository at this point in the history
  • Loading branch information
isherman authored and strasdat committed Dec 8, 2023
1 parent 47312c0 commit 038a0c3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cpp/sophus/image/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ class Image : public ImageView<TPixel> {
return MutImage<TPixel>::makeFromTransform(view, unary_op);
}

/// Creates new Image given view and unary uv-transform function.
///
/// image(u, v) = unary_op(view(u, v));
template <class TOtherPixel, class TUnaryOperationUv>
static auto makeFromTransformUv(
ImageView<TOtherPixel> view, TUnaryOperationUv const& unary_op) -> Image {
return MutImage<TPixel>::makeFromTransformUv(view, unary_op);
}

/// Creates new Image given two views and binary transform function.
///
/// image(u, v) = binary_op(lhs(u, v), rhs(u, v));
Expand Down
12 changes: 12 additions & 0 deletions cpp/sophus/image/mut_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ class MutImage : public MutImageView<TPixel> {
return mut_image;
}

/// Creates new MutImage given view and unary uv-transform function.
///
/// mut_image(u, v) = unary_op(view(u, v), u, v);
template <class TOtherPixel, class TUnaryOperationUv>
static auto makeFromTransformUv(
ImageView<TOtherPixel> view, TUnaryOperationUv const& unary_op)
-> MutImage {
MutImage mut_image(view.imageSize());
mut_image.transformUvFrom(view, unary_op);
return mut_image;
}

/// Creates new MutImage given two views and binary transform function.
///
/// mut_image(u, v) = binary_op(lhs(u, v), rhs(u, v));
Expand Down
18 changes: 18 additions & 0 deletions cpp/sophus/image/mut_image_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,21 @@ TEST(MutImage, makeFromTransform) {
}
}
}

TEST(MutImage, makeFromTransformUv) {
ImageLayout layout = ImageLayout::makeFromSizeAndPitch<float>(
{2, 3}, 2 * sizeof(float) + sizeof(float));
MutImage<float> one_image(layout);
one_image.fill(1.f);

MutImage3F32 pattern = MutImage3F32::makeFromTransformUv(
one_image, [](float value, int u, int v) {
return Pixel3F32(value, 0.5f * u, 2.f * v);
});

for (int v = 0; v < 3; ++v) {
for (int u = 0; u < 2; ++u) {
SOPHUS_ASSERT_EQ(pattern(u, v), Pixel3F32(1.0, 0.5f * u, 2.f * v));
}
}
}
25 changes: 25 additions & 0 deletions cpp/sophus/image/mut_image_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,31 @@ class MutImageView : public ImageView<TPixel> {
}
}

/// Transforms view using unary operation and pixel index.
///
/// It assigns result to this.
///
/// Preconditions:
/// - this must not be empty.
/// - this->imageSize() == view.imageSize()
template <class TOtherPixel, class TUnaryOperationUv>
void transformUvFrom(
ImageView<TOtherPixel> view, TUnaryOperationUv const& unary_op) const {
SOPHUS_ASSERT(!this->isEmpty());
SOPHUS_ASSERT_EQ(view.imageSize(), this->imageSize());

for (int v = 0; v < this->layout_.height(); ++v) {
TPixel* mut_p = this->rowPtrMut(v);
TOtherPixel const* p = view.rowPtr(v);
TOtherPixel const* end_of_row = p + view.layout().width();
int u = 0;

for (; p != end_of_row; ++p, ++mut_p, ++u) {
*mut_p = unary_op(*p, u, v);
}
}
}

/// Transforms two views using binary operation and assigns result to this.
///
/// Preconditions:
Expand Down

0 comments on commit 038a0c3

Please sign in to comment.