22 #include <opencv2/core.hpp> 34 int vsize(
const std::vector<T>& v)
36 return static_cast<int>(v.size());
42 virtual bool Query(
const std::string& name, std::string& value)
const = 0;
63 template <DataType DT>
66 template <DataType DT>
90 for (
int i = 0; i < dsize - 1; ++i) {
103 return std::accumulate(std::begin(
dims_), std::end(
dims_),
size_t(
elemsize_), std::multiplies<size_t>());
285 const std::vector<float>& spacings = std::vector<float>(), Device dev = Device::CPU) :
297 hal_{ HardwareAbstractionLayer::Factory(dev) },
301 throw std::runtime_error(
"Number of dimensions must match number of channels.");
327 hal_->Copy(img, *
this);
382 assert(
this != &rhs);
409 if (
dev_ == Device::NONE || dev == Device::NONE) {
410 throw std::runtime_error(
ECVL_ERROR_MSG "Source or dest device is NONE");
413 if (
dev_ == Device::CPU) {
414 auto dst_hal_ = HardwareAbstractionLayer::Factory(dev);
415 dst_hal_->FromCpu(*
this);
417 else if (dev == Device::CPU) {
421 throw std::runtime_error(
ECVL_ERROR_MSG "Source or dest device must be CPU");
441 const std::vector<float>& spacings = std::vector<float>(), Device dev = Device::CPU);
468 if (c != std::string::npos) {
472 if (c != std::string::npos) {
476 if (c != std::string::npos) {
486 if (x != std::string::npos) {
496 if (y != std::string::npos) {
503 uint8_t*
Ptr(
const std::vector<int>& coords)
505 assert(coords.size() ==
strides_.size());
506 return std::inner_product(std::begin(coords), std::end(coords), std::begin(
strides_),
data_);
509 const uint8_t*
Ptr(
const std::vector<int>& coords)
const 511 assert(coords.size() ==
strides_.size());
512 return std::inner_product(std::begin(coords), std::end(coords), std::begin(
strides_),
data_);
523 void Add(
const T& rhs,
bool saturate =
true)
530 void Sub(
const T& rhs,
bool saturate =
true)
537 void Mul(
const T& rhs,
bool saturate =
true)
544 void Div(
const T& rhs,
bool saturate =
true)
553 hal_->SetTo(*
this, value);
559 hal_->ConvertTo(*
this, *
this, dtype, saturate);
581 template <
typename ViewType>
582 static void CropViewInternal(ViewType& view,
const std::vector<int>& start,
const std::vector<int>& size)
584 std::vector<int> new_dims;
585 int ssize =
vsize(size);
586 for (
int i = 0; i < ssize; ++i) {
587 if (start[i] < 0 || start[i] >= view.dims_[i])
588 throw std::runtime_error(
"Start of crop outside image limits");
589 new_dims.push_back(view.dims_[i] - start[i]);
590 if (size[i] > new_dims[i]) {
591 throw std::runtime_error(
"Crop outside image limits");
594 new_dims[i] = size[i];
599 auto cpos = view.channels_.find(
'c');
600 if (cpos != std::string::npos) {
602 if (new_dims[cpos] != view.dims_[cpos]) {
603 if (new_dims[cpos] == 1) {
607 view.channels_[cpos] =
'o';
613 view.data_ = view.Ptr(start);
615 if (view.contiguous_) {
616 for (
int i = 0; i < view.dims_.size() - 1; ++i) {
617 if (new_dims[i] != view.dims_[i]) {
618 view.contiguous_ =
false;
622 if (view.contiguous_) {
623 view.datasize_ = std::accumulate(std::begin(new_dims), std::end(new_dims),
size_t(view.elemsize_), std::multiplies<size_t>());
629 view.dims_ = std::move(new_dims);
633 template <DataType DT>
634 class View :
public Image
644 throw std::runtime_error(
"View type is different from Image type");
656 hal_ = HardwareAbstractionLayer::Factory(img.
dev_,
true);
660 View(
Image& img,
const std::vector<int>& start,
const std::vector<int>& size) :
View(img)
662 CropViewInternal(*
this, start, size);
667 return *reinterpret_cast<basetype*>(
Ptr(coords));
670 void Create(std::vector<int> dims, std::string channels,
ColorType colortype, uint8_t* ptr,
671 const std::vector<float>& spacings = std::vector<float>(), Device dev = Device::CPU)
675 dims_ = std::move(dims);
684 hal_ = HardwareAbstractionLayer::Factory(dev,
true);
693 template <DataType DT>
694 class ConstView :
public Image
714 hal_ = HardwareAbstractionLayer::Factory(img.
dev_,
true);
720 CropViewInternal(*
this, start, size);
725 return *reinterpret_cast<const basetype*>(
Ptr(coords));
732 template <DataType DT>
753 hal_ = HardwareAbstractionLayer::Factory(img.
dev_,
true);
759 return *reinterpret_cast<basetype*>(
Ptr(coords));
766 template <DataType DT>
787 hal_ = HardwareAbstractionLayer::Factory(img.
dev_,
true);
793 return *reinterpret_cast<const basetype*>(
Ptr(coords));
800 template <DataType DT>
809 throw std::runtime_error(
"ContiguousView2D can be built only from \"xyc\" images");
811 throw std::runtime_error(
"ContiguousView2D can be built only from images with contiguous data");
823 hal_ = HardwareAbstractionLayer::Factory(img.
dev_,
true);
840 template <DataType DT>
849 throw std::runtime_error(
"ContiguousView2D can be built only from \"xyc\" images");
851 throw std::runtime_error(
"ContiguousView2D can be built only from images with contiguous data");
863 hal_ = HardwareAbstractionLayer::Factory(img.
dev_,
true);
893 void RearrangeChannels(
const Image& src, Image& dst,
const std::string& channels);
953 void CopyImage(
const Image& src, Image& dst,
DataType new_type,
const std::string& channels);
979 void ConvertTo(
const Image& src, Image& dst,
DataType dtype,
bool saturate =
true);
987 #endif // !ECVL_IMAGE_H_
ContiguousIterator< basetype > End()
ConstContiguousIterator< T > ContiguousEnd() const
Contiguous const End Iterator.
ContiguousIterator< basetype > Begin()
Image & operator/=(const Image &rhs)
ConstContiguousView(const Image &img)
typename TypeInfo< DT >::basetype basetype
int Width() const
Returns the width of Image.
uint8_t * data_
Pointer to Image data.
void Neg()
In-place negation.
void Create(std::vector< int > dims, std::string channels, ColorType colortype, uint8_t *ptr, const std::vector< float > &spacings=std::vector< float >(), Device dev=Device::CPU)
ConstView(const Image &img, const std::vector< int > &start, const std::vector< int > &size)
basetype & operator()(const std::vector< int > &coords)
HardwareAbstractionLayer * hal_
Pointer to the HardwareAbstractionLayer employed by the Image.
basetype & operator()(int x, int y, int c)
std::string channels_
String which describes how Image planes are organized.
size_t datasize_
Size of Image data in bytes.
Image & operator *=(const Image &rhs)
const basetype & operator()(const std::vector< int > &coords)
friend Image operator+(Image lhs, const Image &rhs)
ContiguousViewXYC(Image &img)
int vsize(const std::vector< T > &v)
void SetDefaultStrides()
Sets default strides for contiguous memory layouts.
typename TypeInfo< DT >::basetype basetype
const basetype & operator()(const std::vector< int > &coords)
DataType
DataType is an enum class which defines data types allowed for images.
DataType elemtype_
Type of Image pixels, must be one of the values available in DataType.
ColorType
Enum class representing the ECVL supported color spaces.
basetype & operator()(const std::vector< int > &coords)
ColorType colortype_
Image ColorType.
ConstContiguousIterator< T > ContiguousBegin() const
Contiguous const Begin Iterator.
Image & operator=(const Image &rhs)
ContiguousView(Image &img)
Iterator< basetype > End()
size_t GetDefaultDatasize()
Gets the default datasize for contiguous images.
int Height() const
Returns the height of Image.
ContiguousIterator< basetype > Begin()
std::vector< int > strides_
Vector of Image strides.
Image(const std::vector< int > &dims, DataType elemtype, std::string channels, ColorType colortype, const std::vector< float > &spacings=std::vector< float >(), Device dev=Device::CPU)
Initializing constructor.
uint8_t * Ptr(const std::vector< int > &coords)
Returns a non-const pointer to data at given coordinates.
Image & operator+=(const Image &rhs)
ConstContiguousViewXYC(const Image &img)
Image & operator=(Image &&rhs)
uint8_t elemsize_
Size (in bytes) of Image pixels.
ContiguousIterator< T > ContiguousBegin()
Contiguous non-const Begin Iterator.
ConstContiguousIterator< basetype > End()
Image(const Image &img)
Copy constructor.
ConstContiguousIterator< basetype > End()
void ConvertTo(DataType dtype, bool saturate=true)
Convert Image to another DataType.
View(Image &img, const std::vector< int > &start, const std::vector< int > &size)
void SetTo(T value)
Set Image value to rhs.
bool contiguous_
Whether the image is stored contiguously or not in memory.
void ShallowCopyImage(const Image &src, Image &dst)
Performs a shallow copy of the source Image into the destination.
typename TypeInfo< DT >::basetype basetype
ConstView(const Image &img)
friend void swap(Image &lhs, Image &rhs)
ConstContiguousIterator< basetype > Begin()
void Create(const std::vector< int > &dims, DataType elemtype, std::string channels, ColorType colortype, const std::vector< float > &spacings=std::vector< float >(), Device dev=Device::CPU)
Allocates new contiguous data if needed.
bool IsOwner() const
To check whether the Image is owner of the data.
void Div(const T &rhs, bool saturate=true)
In-place division.
friend Image operator *(Image lhs, const Image &rhs)
void RearrangeChannels(const Image &src, Image &dst, const std::string &channels)
Changes the order of the Image dimensions.
ConstContiguousIterator< basetype > Begin()
Iterator< T > Begin()
Generic non-const Begin Iterator.
ContiguousIterator< T > ContiguousEnd()
Contiguous non-const End Iterator.
void Mul(const T &rhs, bool saturate=true)
In-place multiplication.
friend class HardwareAbstractionLayer
ContiguousIterator< basetype > End()
typename TypeInfo< DT >::basetype basetype
friend Image operator/(Image lhs, const Image &rhs)
const basetype & operator()(int x, int y, int c) const
Image()
Default constructor.
const uint8_t * Ptr(const std::vector< int > &coords) const
Returns a const pointer to data at given coordinates.
Device dev_
Identifier for the device on which the image data is.
Image(Image &&img)
Move constructor.
bool IsEmpty() const
To check whether the Image contains data or not, regardless of the owning status.
void ConvertTo(const Image &src, Image &dst, DataType dtype, bool saturate=true)
Convert Image to another DataType.
typename TypeInfo< DT >::basetype basetype
void CopyImage(const Image &src, Image &dst, DataType new_type=DataType::none)
Copies the source Image into the destination Image.
int Channels() const
Returns the number of channels.
ConstIterator< basetype > End()
uint8_t DataTypeSize(DataType dt)
Provides the size in bytes of a given DataType.
Iterator< basetype > Begin()
void SetDefaultDatasize()
Sets the default datasize for contiguous images.
typename TypeInfo< DT >::basetype basetype
ConstIterator< T > Begin() const
Generic const Begin Iterator.
MetaData * meta_
Pointer to Image MetaData.
ConstIterator< T > End() const
Generic const End Iterator.
void Sub(const T &rhs, bool saturate=true)
In-place subtraction.
void Add(const T &rhs, bool saturate=true)
In-place addition.
ConstIterator< basetype > Begin()
Iterator< T > End()
Generic non-const End Iterator.
Image & operator-=(const Image &rhs)
std::vector< float > spacings_
Space between pixels/voxels.