38 #include "Core/include/DCoreEvents.h"
39 #include "Base/include/private/DMeasures.hpp"
40 #include "Base/include/private/DImageArith.hpp"
41 #include "IO/include/private/DImageIO.hxx"
42 #include "Gui/include/DGuiInstance.h"
45 #include "NSTypes/RGB/include/DRGB.h"
49 #include "NSTypes/Bit/include/DBit.h"
60 Image<T>::Image(
size_t w,
size_t h,
size_t d) : BaseImage(
"Image")
67 Image<T>::Image(
const Image<T> &rhs,
bool cloneData) : BaseImage(rhs)
77 Image<T>::Image(
const ResImage<T> &rhs,
bool cloneData) : BaseImage(rhs)
88 Image<T>::Image(
const Image<T2> &rhs,
bool cloneData) : BaseImage(rhs)
98 Image<T>::Image(BaseImage *_im,
bool stealIdentity) : BaseImage(
"Image")
105 Image *im = castBaseImage(_im, T());
109 if (!stealIdentity) {
116 template <
class T>
void Image<T>::drain(Image<T> *im,
bool deleteSrc)
121 this->width = im->width;
122 this->height = im->height;
123 this->depth = im->depth;
125 this->sliceCount = im->sliceCount;
126 this->lineCount = im->lineCount;
127 this->pixelCount = im->pixelCount;
129 this->pixels = im->pixels;
130 this->slices = im->slices;
131 this->lines = im->lines;
133 this->allocated = im->allocated;
134 this->allocatedSize = im->allocatedSize;
136 this->name = im->name;
137 this->viewer = im->viewer;
139 im->allocated =
false;
147 bool isAlloc = rhs.isAllocated();
148 this->setSize(rhs, isAlloc);
150 copyLine<T>(rhs.getPixels(), getPixelCount(), this->pixels);
158 bool isAlloc = rhs.isAllocated();
159 this->setSize(rhs, isAlloc);
165 template <
class T> Image<T>::Image(
const char *fileName) : BaseImage(
"Image")
167 triggerEvents =
true;
169 read(fileName, *
this);
172 template <
class T> Image<T>::~Image()
181 template <
class T>
void Image<T>::init()
187 this->dataTypeSize =
sizeof(pixelType);
194 template <
class T> RES_T Image<T>::load(
const char *fileName)
196 return read(fileName, *
this);
199 template <
class T> RES_T Image<T>::save(
const char *fileName)
201 return write(*
this, fileName);
204 template <
class T>
void Image<T>::modified()
206 if (!this->updatesEnabled)
209 BaseImageEvent event(
this);
210 onModified.trigger(&event);
213 template <
class T>
void Image<T>::setName(
const char *_name)
215 parentClass::setName(_name);
218 viewer->setName(_name);
221 template <
class T>
void Image<T>::createViewer()
226 viewer = Gui::getInstance()->createDefaultViewer<T>(*this);
229 template <
class T> ImageViewer<T> *Image<T>::getViewer()
235 template <
class T>
bool Image<T>::isVisible()
237 return (viewer && viewer->isVisible());
240 template <
class T>
void Image<T>::hide()
246 template <
class T>
void Image<T>::show(
const char *_name,
bool labelImage)
251 if (!this->allocated) {
252 ERR_MSG(
"Image isn't allocated !");
272 template <
class T>
void Image<T>::showLabel(
const char *_name)
277 template <
class T>
void Image<T>::showNormal(
const char *_name)
288 RES_T Image<T>::setSize(
size_t w,
size_t h,
size_t d,
bool doAllocate)
290 if (w == this->width && h == this->height && d == this->depth)
300 this->sliceCount = d;
301 this->lineCount = d * h;
302 this->pixelCount = this->lineCount * w;
305 ASSERT((this->allocate() == RES_OK));
308 viewer->setImage(*
this);
315 template <
class T> RES_T Image<T>::allocate()
318 return RES_ERR_BAD_ALLOCATION;
320 this->pixels = createAlignedBuffer<T>(pixelCount);
323 ASSERT((this->pixels != NULL),
"Can't allocate image",
324 RES_ERR_BAD_ALLOCATION);
326 this->allocated =
true;
327 this->allocatedSize = this->pixelCount *
sizeof(T);
334 template <
class T> RES_T Image<T>::restruct(
void)
337 delete[] this->slices;
339 delete[] this->lines;
341 this->lines =
new lineType[lineCount];
342 this->slices =
new sliceType[sliceCount];
344 sliceType cur_line = this->lines;
345 volType cur_slice = this->slices;
347 size_t pixelsPerSlice = this->width * this->height;
349 for (
size_t k = 0; k < depth; k++, cur_slice++) {
350 *cur_slice = cur_line;
352 for (
size_t j = 0; j < height; j++, cur_line++)
353 *cur_line = pixels + k * pixelsPerSlice + j * width;
359 template <
class T> RES_T Image<T>::deallocate()
361 if (!this->allocated)
365 delete[] this->slices;
367 delete[] this->lines;
369 deleteAlignedBuffer<T>(pixels);
375 this->allocated =
false;
376 this->allocatedSize = 0;
382 template <
class T>
void Image<T>::toArray(T outArray[])
384 for (
size_t i = 0; i < pixelCount; i++)
385 outArray[i] = pixels[i];
388 template <
class T>
void Image<T>::fromArray(
const T inArray[])
390 for (
size_t i = 0; i < pixelCount; i++)
391 pixels[i] = inArray[i];
395 template <
class T>
void Image<T>::toCharArray(
signed char outArray[])
397 for (
size_t i = 0; i < pixelCount; i++)
398 outArray[i] = pixels[i];
401 template <
class T>
void Image<T>::fromCharArray(
const signed char inArray[])
403 for (
size_t i = 0; i < pixelCount; i++)
404 pixels[i] = inArray[i];
408 template <
class T>
void Image<T>::toIntArray(
int outArray[])
410 for (
size_t i = 0; i < pixelCount; i++)
411 outArray[i] = pixels[i];
414 template <
class T>
void Image<T>::fromIntArray(
const int inArray[])
416 for (
size_t i = 0; i < pixelCount; i++)
417 pixels[i] = inArray[i];
422 template <
class T> vector<int> Image<T>::toIntVector()
425 for (
size_t i = 0; i < pixelCount; i++)
426 vec.push_back(pixels[i]);
430 template <
class T>
void Image<T>::fromIntVector(vector<int> inVector)
432 ASSERT((inVector.size() == pixelCount),
433 "Vector length doesn't match image size.", );
434 for (
size_t i = 0; i < min(pixelCount, inVector.size()); i++)
435 pixels[i] = inVector[i];
439 template <
class T>
string Image<T>::toString()
442 for (
size_t i = 0; i < pixelCount; i++)
443 buf.push_back(pixels[i]);
447 template <
class T>
void Image<T>::fromString(
string pixVals)
449 ASSERT((pixVals.size() == pixelCount),
450 "String length doesn't match image size.", );
451 for (
size_t i = 0; i < pixelCount; i++)
452 pixels[i] = pixVals[i];
457 void Image<T>::printSelf(ostream &os,
bool displayPixVals,
bool hexaGrid,
461 cout <<
"Image::printSelf: " <<
this << endl;
464 os << indent <<
"Image name: " << name << endl;
467 os << indent <<
"3D image" << endl;
469 os << indent <<
"2D image" << endl;
472 os << indent <<
"Data type: " << getDataTypeAsString<T>(dum) << endl;
475 os << indent <<
"Size: " << width <<
"x" << height <<
"x" << depth << endl;
477 os << indent <<
"Size: " << width <<
"x" << height << endl;
480 os << indent <<
"Allocated (" << displayBytes(allocatedSize) <<
")" << endl;
482 os << indent <<
"Not allocated" << endl;
484 if (displayPixVals) {
485 std::stringstream tStr;
486 tStr << (long) ImDtTypes<T>::max();
487 size_t tSsize = tStr.str().size();
489 tSsize = size_t(tSsize * 1.5);
491 os << indent <<
"Pixel values:" << endl;
494 for (k = 0; k < depth; k++) {
495 for (j = 0; j < height; j++) {
496 if (hexaGrid && j % 2)
497 os << setw(tSsize / 2 + 1) <<
" ";
498 for (i = 0; i < width; i++)
499 os << setw(tSsize + 1) << ImDtTypes<T>::toString(getPixel(i, j, k))
511 template <
class T> SharedImage<T> Image<T>::getSlice(
size_t sliceNum)
const
513 if (sliceNum >= this->depth) {
514 ERR_MSG(
"sliceNum > image depth");
515 return SharedImage<T>();
517 return SharedImage<T>(*this->getSlices()[sliceNum], this->width,
523 template <
class T>
void operator<<(ostream &os,
const Image<T> &im)
528 template <
class T> Image<T> &Image<T>::operator<<(
const char *s)
534 template <
class T> Image<T> &Image<T>::operator>>(
const char *s)
540 template <
class T> Image<T> &Image<T>::operator<<(
const Image<T> &rhs)
546 template <
class T> Image<T> &Image<T>::operator<<(
const T &value)
552 template <
class T> ResImage<T> Image<T>::operator~()
const
554 ResImage<T> im(*
this);
559 template <
class T> ResImage<T> Image<T>::operator-()
const
561 ResImage<T> im(*
this);
566 template <
class T> ResImage<T> Image<T>::operator+(
const Image<T> &rhs)
568 ResImage<T> im(*
this);
573 template <
class T> ResImage<T> Image<T>::operator+(
const T &value)
575 ResImage<T> im(*
this);
576 add(*
this, value, im);
580 template <
class T> Image<T> &Image<T>::operator+=(
const Image<T> &rhs)
582 add(*
this, rhs, *
this);
586 template <
class T> Image<T> &Image<T>::operator+=(
const T &value)
588 add(*
this, value, *
this);
592 template <
class T> ResImage<T> Image<T>::operator-(
const Image<T> &rhs)
594 ResImage<T> im(*
this);
599 template <
class T> ResImage<T> Image<T>::operator-(
const T &value)
601 ResImage<T> im(*
this);
602 sub(*
this, value, im);
606 template <
class T> Image<T> &Image<T>::operator-=(
const Image<T> &rhs)
608 sub(*
this, rhs, *
this);
612 template <
class T> Image<T> &Image<T>::operator-=(
const T &value)
614 sub(*
this, value, *
this);
618 template <
class T> ResImage<T> Image<T>::operator*(
const Image<T> &rhs)
620 ResImage<T> im(*
this);
625 template <
class T> ResImage<T> Image<T>::operator*(
const T &value)
627 ResImage<T> im(*
this);
628 mul(*
this, value, im);
632 template <
class T> Image<T> &Image<T>::operator*=(
const Image<T> &rhs)
634 mul(*
this, rhs, *
this);
638 template <
class T> Image<T> &Image<T>::operator*=(
const T &value)
640 mul(*
this, value, *
this);
644 template <
class T> ResImage<T> Image<T>::operator/(
const Image<T> &rhs)
646 ResImage<T> im(*
this);
651 template <
class T> ResImage<T> Image<T>::operator/(
const T &value)
653 ResImage<T> im(*
this);
654 div(*
this, value, im);
658 template <
class T> Image<T> &Image<T>::operator/=(
const Image<T> &rhs)
660 div(*
this, rhs, *
this);
664 template <
class T> Image<T> &Image<T>::operator/=(
const T &value)
666 div(*
this, value, *
this);
672 ResImage<T> im(*
this);
677 template <
class T> ResImage<T> Image<T>::operator!=(
const Image<T> &rhs)
679 ResImage<T> im(*
this);
680 diff(*
this, rhs, im);
684 template <
class T> ResImage<T> Image<T>::operator<(
const Image<T> &rhs)
686 ResImage<T> im(*
this);
691 template <
class T> ResImage<T> Image<T>::operator<(
const T &value)
693 ResImage<T> im(*
this);
694 low(*
this, value, im);
698 template <
class T> ResImage<T> Image<T>::operator<=(
const Image<T> &rhs)
700 ResImage<T> im(*
this);
705 template <
class T> ResImage<T> Image<T>::operator<=(
const T &value)
707 ResImage<T> im(*
this);
712 template <
class T> ResImage<T> Image<T>::operator>(
const Image<T> &rhs)
714 ResImage<T> im(*
this);
719 template <
class T> ResImage<T> Image<T>::operator>(
const T &value)
721 ResImage<T> im(*
this);
722 grt(*
this, value, im);
726 template <
class T> ResImage<T> Image<T>::operator>=(
const Image<T> &rhs)
728 ResImage<T> im(*
this);
733 template <
class T> ResImage<T> Image<T>::operator>=(
const T &value)
735 ResImage<T> im(*
this);
740 template <
class T> ResImage<T> Image<T>::operator|(
const Image<T> &rhs)
742 ResImage<T> im(*
this);
747 template <
class T> ResImage<T> Image<T>::operator|(
const T &value)
749 ResImage<T> im(*
this);
750 sup(*
this, value, im);
754 template <
class T> Image<T> &Image<T>::operator|=(
const Image<T> &rhs)
756 sup(*
this, rhs, *
this);
760 template <
class T> Image<T> &Image<T>::operator|=(
const T &value)
762 sup(*
this, value, *
this);
766 template <
class T> ResImage<T> Image<T>::operator&(
const Image<T> &rhs)
768 ResImage<T> im(*
this);
773 template <
class T> ResImage<T> Image<T>::operator&(
const T &value)
775 ResImage<T> im(*
this);
776 inf(*
this, value, im);
780 template <
class T> Image<T> &Image<T>::operator&=(
const Image<T> &rhs)
782 inf(*
this, rhs, *
this);
786 template <
class T> Image<T> &Image<T>::operator&=(
const T &value)
788 inf(*
this, value, *
this);
792 template <
class T> Image<T>::operator bool()
794 return vol(*
this) == ImDtTypes<T>::max() * pixelCount;
797 template <
class T> Image<T> &Image<T>::operator<<(
const lineType &tab)
799 for (
size_t i = 0; i < pixelCount; i++)
805 template <
class T> Image<T> &Image<T>::operator<<(vector<T> &vect)
807 typename vector<T>::iterator it = vect.begin();
808 typename vector<T>::iterator it_end = vect.end();
810 for (
size_t i = 0; i < pixelCount; i++, it++) {
819 template <
class T> Image<T> &Image<T>::operator>>(vector<T> &vect)
822 for (
size_t i = 0; i < pixelCount; i++) {
823 vect.push_back(pixels[i]);
828 typedef Image<UINT8> Image_UINT8;
829 typedef Image<UINT16> Image_UINT16;
830 typedef Image<UINT32> Image_UINT32;
831 typedef Image<bool> Image_bool;
835 #if defined SWIGPYTHON && defined USE_NUMPY && defined(SWIG_WRAP_CORE)
836 #include "Core/include/DNumpy.h"
843 int dim = this->getDimension();
845 d[0] = this->getDepth();
846 d[1] = this->getHeight();
847 d[2] = this->getWidth();
849 d[0] = this->getHeight();
850 d[1] = this->getWidth();
852 PyObject *array = PyArray_SimpleNewFromData(dim, d, getNumpyType(*
this),
858 npy_intp t2[] = {1, 0};
859 npy_intp t3[] = {2, 1, 0};
860 PyArray_Dims trans_dims;
865 trans_dims.len = dim;
867 PyObject *res = PyArray_Transpose((PyArrayObject *) array, &trans_dims);
875 PyArrayObject *arr = NULL;
876 PyArray_Descr *descr = NULL;
878 if (PyArray_GetArrayParamsFromObject(obj, NULL, 1, &descr, NULL, NULL, &arr,
880 ERR_MSG(
"Input must be a NumPy array");
883 descr = PyArray_DESCR(arr);
884 if (descr && descr->type_num != getNumpyType(*
this)) {
885 ERR_MSG(
"Wrong input NumPy array data type");
888 int ndim = PyArray_NDIM(arr);
890 ERR_MSG(
"Numpy array has more than three axes");
893 npy_intp *dims = PyArray_DIMS(arr);
894 for (
int i = 0; i < 3; i++) {
899 setSize(dims[0], dims[1], dims[2]);
901 T *data = (T*)PyArray_DATA(arr);
904 for (
size_t i=0;i<pixelCount;i++)
910 for (npy_intp i = 0; i < dims[0]; i++) {
911 for (npy_intp j = 0; j < dims[1]; j++) {
912 for (npy_intp k = 0; k < dims[2]; k++) {
913 T *v = (T *) PyArray_GETPTR3(arr, i, j, k);
914 setPixel(i, j, k, *v);
920 for (ind[0] = 0; ind[0] < dims[0]; ind[0]++) {
921 for (ind[1] = 0; ind[1] < dims[1]; ind[1]++) {
922 for (ind[2] = 0; ind[2] < dims[2]; ind[2]++) {
923 T *v = (T *) PyArray_GetPtr(arr, ind);
924 setPixel(ind[0], ind[1], ind[2], *v);
Image()
Default constructor.
void fromNumArray(PyObject *array)
fromNumArray() -
PyObject * getNumArray(bool c_contigous=false)
getNumArray() -
RES_T mul(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
mul() - Multiply two images
Definition: DImageArith.hpp:749
RES_T low(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
low() - Lower operator
Definition: DImageArith.hpp:604
RES_T sub(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
sub() - Subtraction between two images
Definition: DImageArith.hpp:160
RES_T grtOrEqu(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
grtOrEqu() - Greater or equal operator
Definition: DImageArith.hpp:560
RES_T div(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
div() - Division between two images
Definition: DImageArith.hpp:690
RES_T lowOrEqu(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
lowOrEqu() - Lower or equal operator
Definition: DImageArith.hpp:648
RES_T diff(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
diff() - Difference between two images.
Definition: DImageArith.hpp:448
RES_T inv(const Image< T > &imIn, Image< T > &imOut)
inv() - Invert an image.
Definition: DImageArith.hpp:70
RES_T sup(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
sup() - Sup of two images
Definition: DImageArith.hpp:254
RES_T inf(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
inf() - Inf of two images
Definition: DImageArith.hpp:305
RES_T add(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
add() - Addition (with saturation check)
Definition: DImageArith.hpp:89
RES_T equ(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
equ() - Equality operator (pixel by pixel)
Definition: DImageArith.hpp:366
RES_T grt(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
grt() - Greater operator
Definition: DImageArith.hpp:516
RES_T fill(Image< T > &imOut, const T &value)
fill() - Fill an image with a given value.
Definition: DImageArith.hpp:1456
bool operator==(const vector< Diedge< NodeT, WeightT >> &e1, const vector< Diedge< NodeT, WeightT >> &e2)
Check if two vectors of edges are equal.
Definition: DDigraph.hpp:166
RES_T write(const Image< T > &image, const char *filename)
Write image into file.
Definition: DImageIO.hxx:184
RES_T read(const char *filename, Image< T > &image)
Read image file.
Definition: DImageIO.hxx:107
double vol(const Image< T > &imIn)
vol() - Volume of an image
Definition: DMeasures.hpp:129