template struct sophus::ImageView¶
Overview¶
A view of an (immutable) image, which does not own the data. More…
#include <image_view.h> template <class TPixel> struct ImageView { // typedefs using PixelType = TPixel ; // construction ImageView(); ImageView(ImageShape shape, TPixel const* ptr); ImageView(sophus::ImageSize image_size, TPixel const* ptr); // methods bool isEmpty() const; bool isContiguous() const; sophus::ImageSize const& imageSize() const; ImageShape const& shape() const; int width() const; int height() const; size_t pitchBytes() const; bool colInBounds(int u) const; bool rowInBounds(int v) const; TPixel const* rowPtr(int v) const; TPixel const& checked(int u, int v) const; TPixel const& checked(Eigen::Vector2i uv) const; TPixel const& unchecked(int u, int v) const; TPixel const& unchecked(Eigen::Vector2i uv) const; TPixel const* ptr() const; ImageView subview(Eigen::Vector2i uv, sophus::ImageSize size) const; template <class TFunc> void visit(TFunc const& user_function) const; template <class TReduceOp, class TVal> TVal reduce( TReduceOp const& reduce_op, TVal val = TVal{} ) const; template <class TShortCircuitReduceOp, class TVal> TVal shortCircuitReduce( TShortCircuitReduceOp const& short_circuit_reduce_op, TVal val = TVal{} ) const; bool operator==(ImageView const& rhs) const; bool operator!=(ImageView const& rhs) const; bool hasSameData(ImageView const& rhs) const; protected: // fields ImageShape shape_ = {}; TPixel const* ptr_ = nullptr; // methods void setViewToEmpty(); }; // direct descendants template <class TPixel> class MutImageView;
Detailed Documentation¶
A view of an (immutable) image, which does not own the data.
The API of ImageView allows for read-only access. There is an escape hook for write access, see MutImageView::unsafeConstCast below.
ImageViews are nullable. In that case this->isEmpty()
is true.
Details on equality comparison, the state of the object, and const-correctness.
ImageView is a “shallow-compare type” similar to std::span<Pixel const> and std::unique_ptr<Pixel const>. In particular, we define that the state of an ImageView instance consists of the shape of the image shape_
(see ImageShape) and the pointer address to the first pixel ptr_
. No public member method can change the pointer nor the shape, hence they are all marked const.
Construction¶
ImageView()
Default constructor creates an empty image.
ImageView(ImageShape shape, TPixel const* ptr)
Creates view from shape and pointer to first pixel.
ImageView(sophus::ImageSize image_size, TPixel const* ptr)
Creates view from image size and pointer to first pixel. The image is assumed to be contiguous and the pitch is set accordingly.
Methods¶
bool isEmpty() const
Returns true if view is empty.
bool isContiguous() const
Returns true if view is contiguous.
sophus::ImageSize const& imageSize() const
Returns ImageSize. It is {0,0} if view is empty.
ImageShape const& shape() const
Returns ImageShape. It is {{0,0}, 0} is view is empty.
bool colInBounds(int u) const
Returns true if u is in [0, width).
bool rowInBounds(int v) const
Returns true if v is in [0, height).
TPixel const* rowPtr(int v) const
Returns v-th row pointer.
Precondition: v must be in [0, height).
TPixel const& checked(int u, int v) const
Returns pixel u, v.
Precondition: u must be in [0, width) and v must be in [0, height).
Note:
panics if u.v is invalid,
This is a costly function to call - e.g. when iterating over the whole image. Use the following instead:
for (int v=0; v<view.shape(). height(); ++v) { TPixel const* row = img.rowPtr(v); for (int u=0; u<view.shape(). width(); ++u) { PixetT p = row[u]; } }
TPixel const& unchecked(int u, int v) const
Returns pixel u, v.
Precondition: u must be in [0, width) and v must be in [0, height). Silent UB on failure.
TPixel const* ptr() const
Returns pointer to first pixel.
ImageView subview(Eigen::Vector2i uv, sophus::ImageSize size) const
Returns subview.
template <class TFunc> void visit(TFunc const& user_function) const
Performs reduction / fold on image view.
template <class TReduceOp, class TVal> TVal reduce( TReduceOp const& reduce_op, TVal val = TVal{} ) const
Performs reduction / fold on image view.
template <class TShortCircuitReduceOp, class TVal> TVal shortCircuitReduce( TShortCircuitReduceOp const& short_circuit_reduce_op, TVal val = TVal{} ) const
Performs reduction / fold on image view with short circuit condition.
bool operator==(ImageView const& rhs) const
The equality operator is deleted to avoid confusion. Since ImageView is a “shallow-copy” type, a consistently defined equality would check for equality of its (shallow) state:
this->shape_ == rhs.shape() && this->ptr_ == rhs.ptr_
`
However, some users might expect that equality would check for pixel values equality and return true for identical copies of data blocks.
Here we follow std::span which also does not offer equality comparions.
bool operator!=(ImageView const& rhs) const
The in-equality operator is deleted to avoid confusion.
bool hasSameData(ImageView const& rhs) const
Returns true both views have the same size and contain the same data.
void setViewToEmpty()
Resets view such that it is empty.