Xmipp  v3.23.11-Nereus
Classes | Macros | Functions | Variables
python_image.h File Reference
#include "Python.h"
#include "core/xmipp_image_generic.h"
#include <numpy/ndarraytypes.h>
#include <numpy/arrayobject.h>
#include <memory>
Include dependency graph for python_image.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  ImageObject
 

Macros

#define NPY_NO_DEPRECATED_API   NPY_1_7_API_VERSION
 
#define Image_Check(v)   (((v)->ob_type == &ImageType))
 
#define Image_Value(v)   ((*((ImageObject*)(v))->image))
 

Functions

void Image_dealloc (ImageObject *self)
 
PyObject * Image_new (PyTypeObject *type, PyObject *args, PyObject *kwargs)
 
PyObject * Image_repr (PyObject *obj)
 
PyObject * Image_RichCompareBool (PyObject *obj, PyObject *obj2, int opid)
 
PyObject * Image_equal (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_write (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_read (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_readPreview (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_readPreviewSmooth (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_convertPSD (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_readApplyGeo (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_applyGeo (PyObject *obj, PyObject *args, PyObject *kwargs)
 
NPY_TYPES datatype2NpyType (DataType dt)
 
DataType npyType2Datatype (int npy)
 
PyObject * Image_getData (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_projectVolumeDouble (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_setData (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_getPixel (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_setPixel (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_initConstant (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_mirrorY (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_applyTransforMatScipion (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_warpAffine (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_applyCTF (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_initRandom (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_resize (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_scale (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_reslice (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_writeSlices (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_patch (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_getDataType (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_setDataType (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_convert2DataType (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_getDimensions (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_resetOrigin (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_getEulerAngles (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_getMainHeaderValue (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_setMainHeaderValue (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_getHeaderValue (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_setHeaderValue (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_computeStats (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_computePSD (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_adjustAndSubtract (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_correlation (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_correlationAfterAlignment (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_window2D (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_add (PyObject *obj1, PyObject *obj2)
 
PyObject * Image_iadd (PyObject *obj1, PyObject *obj2)
 
PyObject * Image_inplaceAdd (PyObject *self, PyObject *args, PyObject *kwargs)
 
PyObject * Image_subtract (PyObject *obj1, PyObject *obj2)
 
PyObject * Image_isubtract (PyObject *obj1, PyObject *obj2)
 
PyObject * Image_inplaceSubtract (PyObject *self, PyObject *args, PyObject *kwargs)
 
PyObject * Image_multiply (PyObject *obj1, PyObject *obj2)
 
PyObject * Image_imultiply (PyObject *obj1, PyObject *obj2)
 
PyObject * Image_inplaceMultiply (PyObject *self, PyObject *args, PyObject *kwargs)
 
PyObject * Image_true_divide (PyObject *obj1, PyObject *obj2)
 
PyObject * Image_idivide (PyObject *obj1, PyObject *obj2)
 
PyObject * Image_inplaceDivide (PyObject *self, PyObject *args, PyObject *kwargs)
 
PyObject * Image_radialAvgAxis (PyObject *obj, PyObject *args, PyObject *kwargs)
 
PyObject * Image_centerOfMass (PyObject *obj)
 

Variables

PyObject * PyXmippError
 
PyNumberMethods Image_NumberMethods
 
PyMethodDef Image_methods []
 
PyTypeObject ImageType
 

Macro Definition Documentation

◆ Image_Check

#define Image_Check (   v)    (((v)->ob_type == &ImageType))

Definition at line 43 of file python_image.h.

◆ Image_Value

#define Image_Value (   v)    ((*((ImageObject*)(v))->image))

Definition at line 44 of file python_image.h.

◆ NPY_NO_DEPRECATED_API

#define NPY_NO_DEPRECATED_API   NPY_1_7_API_VERSION

Definition at line 30 of file python_image.h.

Function Documentation

◆ datatype2NpyType()

NPY_TYPES datatype2NpyType ( DataType  dt)

Definition at line 520 of file python_image.cpp.

521 {
522  switch (dt)
523  {
524  case DT_Float:
525  return NPY_FLOAT;
526  case DT_Double:
527  return NPY_DOUBLE;
528  case DT_Int:
529  return NPY_INT;
530  case DT_UInt:
531  return NPY_UINT;
532  case DT_Short:
533  return NPY_SHORT;
534  case DT_UShort:
535  return NPY_USHORT;
536  case DT_SChar:
537  return NPY_BYTE;
538  case DT_UChar:
539  return NPY_UBYTE;
540  case DT_Bool:
541  return NPY_BOOL;
542  case DT_CFloat:
543  return NPY_CFLOAT;
544  case DT_CDouble:
545  return NPY_CDOUBLE;
546  case DT_HalfFloat:
547  return NPY_HALF;
548  default:
549  return NPY_NOTYPE;
550  }
551 }

◆ Image_add()

PyObject* Image_add ( PyObject *  obj1,
PyObject *  obj2 
)

Definition at line 1376 of file python_image.cpp.

1377 {
1378  ImageObject * result = (ImageObject*)PyObject_CallFunction((PyObject*)&ImageType, "");
1379  if (result != nullptr)
1380  {
1381  try
1382  {
1383  result->image = std::make_unique<ImageGeneric>(Image_Value(obj1));
1384  Image_Value(result).add(Image_Value(obj2));
1385  }
1386  catch (XmippError &xe)
1387  {
1388  result=nullptr;
1389  PyErr_SetString(PyXmippError, xe.what());
1390  }
1391  }
1392  return (PyObject *)result;
1393 }//operator +
PyTypeObject ImageType
PyObject_HEAD std::unique_ptr< ImageGeneric > image
Definition: python_image.h:50
#define Image_Value(v)
Definition: python_image.h:44
PyObject * PyXmippError
Definition: xmippmodule.cpp:47

◆ Image_adjustAndSubtract()

PyObject* Image_adjustAndSubtract ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 1273 of file python_image.cpp.

1274 {
1275  const auto *self = reinterpret_cast<ImageObject*>(obj);
1276  PyObject *pimg2 = nullptr;
1277  ImageObject * result = (ImageObject*)PyObject_CallFunction((PyObject*)&ImageType, "");
1278  if (self != nullptr)
1279  {
1280  try
1281  {
1282  if (PyArg_ParseTuple(args, "O", &pimg2))
1283  {
1284  ImageObject *img2=(ImageObject *)pimg2;
1285  result->image = std::make_unique<ImageGeneric>(Image_Value(img2));
1286  MULTIDIM_ARRAY_GENERIC(*result->image).rangeAdjust(MULTIDIM_ARRAY_GENERIC(*self->image));
1287  MULTIDIM_ARRAY_GENERIC(*result->image) *=-1;
1288  MULTIDIM_ARRAY_GENERIC(*result->image) += MULTIDIM_ARRAY_GENERIC(*self->image);
1289  }
1290  }
1291  catch (XmippError &xe)
1292  {
1293  PyErr_SetString(PyXmippError, xe.what());
1294  }
1295  }
1296  return (PyObject *)result;
1297 }//function Image_adjustAndSubtract
PyTypeObject ImageType
PyObject_HEAD std::unique_ptr< ImageGeneric > image
Definition: python_image.h:50
#define Image_Value(v)
Definition: python_image.h:44
#define MULTIDIM_ARRAY_GENERIC(v)
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_applyCTF()

PyObject* Image_applyCTF ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 1236 of file xmippmodule.cpp.

1237 {
1238  PyObject *pimg = nullptr;
1239  PyObject *input = nullptr;
1240  double Ts=1.0;
1241  size_t rowId;
1242  auto *pyReplace = Py_False;
1243  bool absPhase = false;
1244 
1245  try
1246  {
1247  PyArg_ParseTuple(args, "OOd|kO", &pimg, &input,&Ts,&rowId,&pyReplace);
1248  if (pimg != nullptr && input != nullptr)
1249  {
1250  if(PyBool_Check(pyReplace))
1251  absPhase = pyReplace == Py_True;
1252 
1253  PyObject *pyStr;
1254  if (PyUnicode_Check(input) || MetaData_Check(input))
1255  {
1256  auto *img = (ImageObject*) pimg;
1257  auto &image = img->image;
1258  image->convert2Datatype(DT_Double);
1259  MultidimArray<double> * mImage=nullptr;
1260  MULTIDIM_ARRAY_GENERIC(*image).getMultidimArrayPointer(mImage);
1261 
1262  // COSS: This is redundant? image->data->getMultidimArrayPointer(mImage);
1263 
1264  CTFDescription ctf;
1265  ctf.enable_CTF=true;
1266  ctf.enable_CTFnoise=false;
1267  if (MetaData_Check(input))
1268  ctf.readFromMetadataRow(MetaData_Value(input), rowId );
1269  else
1270  {
1271  pyStr = PyObject_Str(input);
1272  FileName fnCTF = (char*)PyUnicode_AsUTF8(pyStr);
1273  ctf.read(fnCTF);
1274  }
1275  ctf.produceSideInfo();
1276  ctf.applyCTF(*mImage,Ts,absPhase);
1277  Py_RETURN_NONE;
1278  }
1279  }
1280  }
1281  catch (XmippError &xe)
1282  {
1283  PyErr_SetString(PyXmippError, xe.what());
1284  }
1285  return nullptr;
1286 }
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void read(const FileName &fn, bool disable_if_not_K=true)
Definition: ctf.cpp:1220
bool enable_CTF
Enable CTF part.
Definition: ctf.h:275
void applyCTF(MultidimArray< std::complex< double > > &FFTI, const MultidimArray< double > &I, double Ts, bool absPhase=false)
Apply CTF to an image.
Definition: ctf.cpp:1521
void readFromMetadataRow(const MetaData &MD, size_t id, bool disable_if_not_K=true)
Definition: ctf.cpp:1214
#define MetaData_Check(v)
#define MetaData_Value(v)
#define MULTIDIM_ARRAY_GENERIC(v)
void produceSideInfo()
Produce Side information.
Definition: ctf.cpp:1392
bool enable_CTFnoise
Enable CTFnoise part.
Definition: ctf.h:273

◆ Image_applyGeo()

PyObject* Image_applyGeo ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 1860 of file python_image.cpp.

1861 {
1862  const auto *self = reinterpret_cast<ImageObject*>(obj);
1863 
1864  if (self != nullptr)
1865  {
1866  PyObject *md = nullptr;
1867  PyObject *only_apply_shifts = Py_False;
1868  PyObject *wrap = (xmipp_transformation::WRAP ? Py_True : Py_False);
1869  size_t objectId = BAD_OBJID;
1870  bool boolOnly_apply_shifts = false;
1871  bool boolWrap = xmipp_transformation::WRAP;
1872 
1873  if (PyArg_ParseTuple(args, "Ok|OO", &md, &objectId, &only_apply_shifts, &wrap))
1874  {
1875  try
1876  {
1877  if (PyBool_Check(only_apply_shifts))
1878  boolOnly_apply_shifts = (only_apply_shifts == Py_True);
1879  else
1880  PyErr_SetString(PyExc_TypeError, "ImageGeneric::applyGeo: Expecting boolean value");
1881  if (PyBool_Check(wrap))
1882  boolWrap = (wrap == Py_True);
1883  else
1884  PyErr_SetString(PyExc_TypeError, "ImageGeneric::applyGeo: Expecting boolean value");
1885  ApplyGeoParams params;
1886  params.only_apply_shifts = boolOnly_apply_shifts;
1887  params.wrap = boolWrap;
1888  self->image->applyGeo(MetaData_Value(md), objectId, params);
1889  Py_RETURN_NONE;
1890  }
1891  catch (XmippError &xe)
1892  {
1893  PyErr_SetString(PyXmippError, xe.what());
1894  }
1895  }
1896  }
1897  return nullptr;
1898 }
#define MetaData_Value(v)
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()
#define BAD_OBJID
Definition: metadata_base.h:55

◆ Image_applyTransforMatScipion()

PyObject* Image_applyTransforMatScipion ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Image inplace subtraction, equivalent to -= operator

Definition at line 1672 of file python_image.cpp.

1673 {
1674 
1675  PyObject * list = nullptr;
1676  PyObject * item = nullptr;
1677  const auto *self = reinterpret_cast<ImageObject*>(obj);
1678  ImageBase * img;
1679  PyObject *only_apply_shifts = Py_False;
1680  PyObject *wrap = (xmipp_transformation::WRAP ? Py_True : Py_False);
1681  img = self->image->image;
1682  bool boolOnly_apply_shifts = false;
1683  bool boolWrap = xmipp_transformation::WRAP;
1684 
1685  try
1686  {
1687  PyArg_ParseTuple(args, "O|OO", &list, &only_apply_shifts, &wrap);
1688  if (PyList_Check(list))
1689  {
1690  if (PyBool_Check(only_apply_shifts))
1691  boolOnly_apply_shifts = (only_apply_shifts == Py_True);
1692  else
1693  PyErr_SetString(PyExc_TypeError, "ImageGeneric::applyGeo: Expecting boolean value");
1694  if (PyBool_Check(wrap))
1695  boolWrap = (wrap == Py_True);
1696  else
1697  PyErr_SetString(PyExc_TypeError, "ImageGeneric::applyGeo: Expecting boolean value");
1698 
1699 
1700  size_t size = PyList_Size(list);
1701  Matrix2D<double> A;
1702  A.initIdentity(4);
1703  for (size_t i = 0; i < size; ++i)
1704  {
1705  item = PyList_GetItem(list, i);
1706  MAT_ELEM(A,i/4,i%4 ) = PyFloat_AsDouble(item);
1707  }
1708  double scale;
1709  double shiftX;
1710  double shiftY;
1711  double rot;
1712  double tilt;
1713  double psi;
1714  tilt = 0.0;
1715  rot = 0.0;
1716  bool flip;
1717  transformationMatrix2Parameters2D(A, flip, scale, shiftX, shiftY, psi);//, shiftZ, rot,tilt, psi);
1718  img->setEulerAngles(rot,tilt,psi);
1719  img->setShifts(shiftX,shiftY);
1720  img->setScale(scale);
1721  img->setFlip(flip);
1722  img->selfApplyGeometry(xmipp_transformation::LINEAR, boolWrap, boolOnly_apply_shifts);//wrap, onlyShifts
1723  Py_RETURN_NONE;
1724  }
1725  else
1726  {
1727  PyErr_SetString(PyExc_TypeError, "ImageGeneric::applyTransforMatScipion: Expecting a list");
1728 
1729  }
1730  }
1731  catch (XmippError &xe)
1732  {
1733  PyErr_SetString(PyXmippError, xe.what());
1734  }
1735  return nullptr;
1736 }//operator +=
void setScale(double scale, const size_t n=0)
void transformationMatrix2Parameters2D(const Matrix2D< T > &A, bool &flip, T &scale, T &shiftX, T &shiftY, T &psi)
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
void setShifts(double xoff, double yoff, double zoff=0., const size_t n=0)
void setFlip(bool flip, const size_t n=0)
virtual void selfApplyGeometry(int SplineDegree, bool wrap, bool only_apply_shifts)=0
Image base class.
void setEulerAngles(double rot, double tilt, double psi, const size_t n=0)
double psi(const double x)
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()
void initIdentity()
Definition: matrix2d.h:673

◆ Image_centerOfMass()

PyObject* Image_centerOfMass ( PyObject *  obj)

Definition at line 1975 of file python_image.cpp.

1976 {
1977  auto *self = (ImageObject*) obj;
1978  if (self != nullptr)
1979  {
1980  try
1981  {
1982  Matrix1D< double > center;
1983  self->image->convert2Datatype(DT_Double);
1985  MULTIDIM_ARRAY_GENERIC(*self->image).getMultidimArrayPointer(in);
1986  in->centerOfMass(center);
1987  return Py_BuildValue("fff", XX(center), YY(center), ZZ(center));
1988  }
1989  catch (const XmippError &xe)
1990  {
1991  PyErr_SetString(PyXmippError, xe.what());
1992  }
1993  }
1994  return nullptr;
1995 }
void centerOfMass(Matrix1D< double > &center, void *mask=NULL, size_t n=0)
#define XX(v)
Definition: matrix1d.h:85
int in
#define YY(v)
Definition: matrix1d.h:93
#define MULTIDIM_ARRAY_GENERIC(v)
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()
#define ZZ(v)
Definition: matrix1d.h:101

◆ Image_computePSD()

PyObject* Image_computePSD ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 1235 of file python_image.cpp.

1236 {
1237  const auto *self = reinterpret_cast<ImageObject*>(obj);
1238  if (nullptr == self) return nullptr;
1239  try {
1240  // keep default values consistent with the python
1241  float overlap = 0.4f;
1242  int dimX = 384;
1243  int dimY = 384;
1244  unsigned threads = 1;
1245  ImageObject *result = (ImageObject*)PyObject_CallFunction((PyObject*)&ImageType, "");
1246  if (PyArg_ParseTuple(args, "|fIIb", &overlap, &dimX, &dimY, &threads)
1247  && (nullptr != result)) {
1248  // prepare dims
1249  auto dims = Dimensions(dimX, dimY);
1250  // prepare input image
1251  auto &image = self->image;
1252  image->convert2Datatype(DT_Double);
1254  MULTIDIM_ARRAY_GENERIC(*image).getMultidimArrayPointer(in);
1255  // prepare output image
1256  result->image = std::make_unique<ImageGeneric>(DT_Double);
1257  MultidimArray<double> *out;
1258  MULTIDIM_ARRAY_GENERIC(*result->image).getMultidimArrayPointer(out);
1259  // call the estimation
1260  PSDEstimator<double>::estimatePSD(*in, overlap, dims, *out, threads, false);
1261  } else {
1262  PyErr_SetString(PyXmippError, "Unknown error while allocating data for output or parsing data");
1263  }
1264  return (PyObject *)result;
1265  } catch (XmippError &xe) {
1266  PyErr_SetString(PyXmippError, xe.what());
1267  }
1268  return Py_BuildValue("");
1269 }
PyTypeObject ImageType
PyObject_HEAD std::unique_ptr< ImageGeneric > image
Definition: python_image.h:50
int in
#define MULTIDIM_ARRAY_GENERIC(v)
static void estimatePSD(const MultidimArray< T > &micrograph, float overlap, const Dimensions &tileDim, MultidimArray< T > &psd, unsigned fftThreads, bool normalize)
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_computeStats()

PyObject* Image_computeStats ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 1212 of file python_image.cpp.

1213 {
1214  const auto *self = reinterpret_cast<ImageObject*>(obj);
1215  if (self != nullptr)
1216  {
1217  try
1218  {
1219  double mean=0.0;
1220  double dev=0.0;
1221  double min=0.0;
1222  double max=0.0;
1223  self->image->data->computeStats(mean, dev, min, max);
1224  return Py_BuildValue("ffff", mean, dev, min, max);
1225  }
1226  catch (XmippError &xe)
1227  {
1228  PyErr_SetString(PyXmippError, xe.what());
1229  }
1230  }
1231  return nullptr;
1232 }//function Image_computeStats
void min(Image< double > &op1, const Image< double > &op2)
void max(Image< double > &op1, const Image< double > &op2)
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_convert2DataType()

PyObject* Image_convert2DataType ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 1003 of file python_image.cpp.

1004 {
1005  const auto *self = reinterpret_cast<ImageObject*>(obj);
1006  int datatype;
1007  int castMode=CW_CONVERT;
1008 
1009  if (self != nullptr && PyArg_ParseTuple(args, "i|i", &datatype, &castMode))
1010  {
1011  try
1012  {
1013  self->image->convert2Datatype((DataType)datatype, (CastWriteMode)castMode);
1014  Py_RETURN_NONE;
1015  }
1016  catch (XmippError &xe)
1017  {
1018  PyErr_SetString(PyXmippError, xe.what());
1019  }
1020  }
1021  return nullptr;
1022 }//function Image_setDataType
DataType
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()
CastWriteMode

◆ Image_convertPSD()

PyObject* Image_convertPSD ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 1169 of file xmippmodule.cpp.

1170 {
1171  auto *self = (ImageObject*) obj;
1172 
1173  if (self != nullptr)
1174  {
1175  try
1176  {
1177  auto &image = self->image;
1178  image->convert2Datatype(DT_Double);
1180  MULTIDIM_ARRAY_GENERIC(*image).getMultidimArrayPointer(in);
1181  xmipp2PSD(*in, *in, true);
1182 
1183  Py_RETURN_NONE;
1184  }
1185  catch (XmippError &xe)
1186  {
1187  PyErr_SetString(PyXmippError, xe.what());
1188  }
1189  }
1190  return nullptr;
1191 }//function Image_convertPSD
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void xmipp2PSD(const MultidimArray< T > &input, MultidimArray< T > &output, bool takeLog)
Definition: xmipp_fft.cpp:443
int in
#define MULTIDIM_ARRAY_GENERIC(v)
void(* obj)()

◆ Image_correlation()

PyObject* Image_correlation ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 1301 of file python_image.cpp.

1302 {
1303  const auto *self = reinterpret_cast<ImageObject*>(obj);
1304  if (self != nullptr)
1305  {
1306  try
1307  {
1308  PyObject *pimg2 = nullptr;
1309  if (PyArg_ParseTuple(args, "O", &pimg2))
1310  {
1311  auto &image = self->image;
1312  image->convert2Datatype(DT_Double);
1313  MultidimArray<double> * pImage=nullptr;
1314  MULTIDIM_ARRAY_GENERIC(*image).getMultidimArrayPointer(pImage);
1315 
1316  ImageObject *img2=(ImageObject *)pimg2;
1317  auto &image2 = img2->image;
1318  image2->convert2Datatype(DT_Double);
1319  MultidimArray<double> * pImage2=nullptr;
1320  MULTIDIM_ARRAY_GENERIC(*image2).getMultidimArrayPointer(pImage2);
1321 
1322  double corr=correlationIndex(*pImage,*pImage2);
1323  return Py_BuildValue("f", corr);
1324  }
1325  }
1326  catch (XmippError &xe)
1327  {
1328  PyErr_SetString(PyXmippError, xe.what());
1329  }
1330  }
1331  return nullptr;
1332 }//function Image_correlation
PyObject_HEAD std::unique_ptr< ImageGeneric > image
Definition: python_image.h:50
double correlationIndex(const MultidimArray< T > &x, const MultidimArray< T > &y, const MultidimArray< int > *mask=NULL, MultidimArray< double > *Contributions=NULL)
#define MULTIDIM_ARRAY_GENERIC(v)
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_correlationAfterAlignment()

PyObject* Image_correlationAfterAlignment ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 1336 of file python_image.cpp.

1337 {
1338  const auto *self = reinterpret_cast<ImageObject*>(obj);
1339  if (self != nullptr)
1340  {
1341  try
1342  {
1343  PyObject *pimg2 = nullptr;
1344  if (PyArg_ParseTuple(args, "O", &pimg2))
1345  {
1346  auto &image = self->image;
1347  image->convert2Datatype(DT_Double);
1348  MultidimArray<double> * pImage=nullptr;
1349  MULTIDIM_ARRAY_GENERIC(*image).getMultidimArrayPointer(pImage);
1350 
1351  ImageObject *img2=(ImageObject *)pimg2;
1352  auto &image2 = img2->image;
1353  image2->convert2Datatype(DT_Double);
1354  MultidimArray<double> * pImage2=nullptr;
1355  MULTIDIM_ARRAY_GENERIC(*image2).getMultidimArrayPointer(pImage2);
1356  pImage->setXmippOrigin();
1357  pImage2->setXmippOrigin();
1358 
1359  Matrix2D<double> M;
1360  MultidimArray<double> I2Copy=*pImage2;
1361 
1362  double corr=alignImagesConsideringMirrors(*pImage, I2Copy, M, xmipp_transformation::WRAP);
1363  return Py_BuildValue("f", corr);
1364  }
1365  }
1366  catch (XmippError &xe)
1367  {
1368  PyErr_SetString(PyXmippError, xe.what());
1369  }
1370  }
1371  return nullptr;
1372 }
PyObject_HEAD std::unique_ptr< ImageGeneric > image
Definition: python_image.h:50
#define MULTIDIM_ARRAY_GENERIC(v)
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()
double alignImagesConsideringMirrors(const MultidimArray< double > &Iref, const AlignmentTransforms &IrefTransforms, MultidimArray< double > &I, Matrix2D< double > &M, AlignmentAux &aux, CorrelationAux &aux2, RotationalCorrelationAux &aux3, bool wrap, const MultidimArray< int > *mask)
Definition: filters.cpp:2150

◆ Image_dealloc()

void Image_dealloc ( ImageObject self)

Definition at line 205 of file python_image.cpp.

206 {
207  self->~ImageObject(); // Call the destructor
208  Py_TYPE(self)->tp_free((PyObject*)self);
209 }//function Image_dealloc
#define Py_TYPE(op)
Definition: frm_wrap.cpp:761

◆ Image_equal()

PyObject* Image_equal ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 313 of file python_image.cpp.

314 {
315  const auto *self = reinterpret_cast<ImageObject*>(obj);
316  if (self != nullptr)
317  {
318  double precision = 1.e-3;
319  PyObject *image2 = nullptr;
320  if (PyArg_ParseTuple(args, "O|d", &image2, &precision))
321  {
322  try
323  {
324  if (Image_Value(obj).equal(Image_Value(image2), precision))
325  Py_RETURN_TRUE;
326  else
327  Py_RETURN_FALSE;
328  }
329  catch (XmippError &xe)
330  {
331  PyErr_SetString(PyXmippError, xe.what());
332  }
333  }
334  }
335  return nullptr;
336 }//function Image_equal
#define Image_Value(v)
Definition: python_image.h:44
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_getData()

PyObject* Image_getData ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 604 of file python_image.cpp.

605 {
606  const auto *self = reinterpret_cast<ImageObject*>(obj);
607 
608  if (self != nullptr)
609  {
610  try
611  {
612  ArrayDim adim;
613  ImageGeneric & image = Image_Value(self);
614  DataType dt = image.getDatatype();
615  int nd = image.image->mdaBase->getDim();
616  MULTIDIM_ARRAY_GENERIC(image).getDimensions(adim);
617  npy_intp dims[4];
618  dims[0] = adim.ndim;
619  dims[1] = adim.zdim;
620  dims[2] = adim.ydim;
621  dims[3] = adim.xdim;
622  //Get the pointer to data
623  void *mymem = image().getArrayPointer();
624  NPY_TYPES type = datatype2NpyType(dt);
625  //dims pointer is shifted if ndim or zdim are 1
626  PyArrayObject * arr = (PyArrayObject*) PyArray_SimpleNew(nd, dims+4-nd, type);
627  void * data = PyArray_DATA(arr);
628  memcpy(data, mymem, adim.nzyxdim * gettypesize(dt));
629 
630  return (PyObject*)arr;
631  }
632  catch (XmippError &xe)
633  {
634  PyErr_SetString(PyXmippError, xe.what());
635  }
636  }
637  return nullptr;
638 }//function Image_getData
size_t xdim
#define Image_Value(v)
Definition: python_image.h:44
DataType getDatatype() const
size_t nzyxdim
size_t zdim
viol type
DataType
ImageBase * image
#define MULTIDIM_ARRAY_GENERIC(v)
size_t ndim
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
NPY_TYPES datatype2NpyType(DataType dt)
size_t ydim
void(* obj)()
MultidimArrayBase * mdaBase
size_t gettypesize(DataType type)
Returns memory size of datatype.

◆ Image_getDataType()

PyObject* Image_getDataType ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 959 of file python_image.cpp.

960 {
961  const auto *self = reinterpret_cast<ImageObject*>(obj);
962 
963  if (self != nullptr)
964  {
965  try
966  {
967  ImageGeneric & image = Image_Value(self);
968  DataType dt = image.getDatatype();
969  return Py_BuildValue("i", dt);
970  }
971  catch (XmippError &xe)
972  {
973  PyErr_SetString(PyXmippError, xe.what());
974  }
975  }
976  return nullptr;
977 }//function Image_getDataType
#define Image_Value(v)
Definition: python_image.h:44
DataType getDatatype() const
DataType
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_getDimensions()

PyObject* Image_getDimensions ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 1026 of file python_image.cpp.

1027 {
1028  const auto *self = reinterpret_cast<ImageObject*>(obj);
1029  if (self != nullptr)
1030  {
1031  try
1032  {
1033  size_t xdim;
1034  size_t ydim;
1035  size_t zdim;
1036  size_t ndim;
1037  MULTIDIM_ARRAY_GENERIC(*self->image).getDimensions(xdim, ydim, zdim, ndim);
1038  return Py_BuildValue("iiik", xdim, ydim, zdim, ndim);
1039  }
1040  catch (XmippError &xe)
1041  {
1042  PyErr_SetString(PyXmippError, xe.what());
1043  }
1044  }
1045  return nullptr;
1046 }//function Image_getDimensions
#define MULTIDIM_ARRAY_GENERIC(v)
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_getEulerAngles()

PyObject* Image_getEulerAngles ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 1070 of file python_image.cpp.

1071 {
1072  const auto *self = reinterpret_cast<ImageObject*>(obj);
1073  if (self != nullptr)
1074  {
1075  try
1076  {
1077  double rot;
1078  double tilt;
1079  double psi;
1080  self->image->getEulerAngles(rot, tilt, psi);
1081  return Py_BuildValue("fff", rot, tilt, psi);
1082 
1083  }
1084  catch (XmippError &xe)
1085  {
1086  PyErr_SetString(PyXmippError, xe.what());
1087  }
1088  }
1089  return nullptr;
1090 }//function Image_getEulerAngles
double psi(const double x)
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_getHeaderValue()

PyObject* Image_getHeaderValue ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 1154 of file python_image.cpp.

1155 {
1156  const auto *self = reinterpret_cast<ImageObject*>(obj);
1157  PyObject *pyValue;
1158  int label;
1159 
1160  if (self != nullptr && PyArg_ParseTuple(args, "i", &label))
1161  {
1162  try
1163  {
1164  MDRow &mainHeader = *(self->image->image->MD[0]);
1165  if (mainHeader.containsLabel((MDLabel)label))
1166  {
1167  MDObject * object = mainHeader.getObject((MDLabel) label);
1168  pyValue = getMDObjectValue(object);
1169  return pyValue;
1170  }
1171  else
1172  Py_RETURN_NONE;
1173  }
1174  catch (XmippError &xe)
1175  {
1176  PyErr_SetString(PyXmippError, xe.what());
1177  }
1178  }
1179  return nullptr;
1180 }
PyObject * getMDObjectValue(const MDObject *obj)
virtual bool containsLabel(MDLabel label) const =0
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()
virtual MDObject * getObject(MDLabel label)=0
MDLabel

◆ Image_getMainHeaderValue()

PyObject* Image_getMainHeaderValue ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 1095 of file python_image.cpp.

1096 {
1097  const auto *self = reinterpret_cast<ImageObject*>(obj);
1098  PyObject *pyValue;
1099  int label;
1100 
1101  if (self != nullptr && PyArg_ParseTuple(args, "i", &label))
1102  {
1103  try
1104  {
1105  MDRow &mainHeader = self->image->image->MDMainHeader;
1106  if (mainHeader.containsLabel((MDLabel)label))
1107  {
1108  MDObject * object = mainHeader.getObject((MDLabel) label);
1109  pyValue = getMDObjectValue(object);
1110  return pyValue;
1111  }
1112  else
1113  Py_RETURN_NONE;
1114  }
1115  catch (XmippError &xe)
1116  {
1117  PyErr_SetString(PyXmippError, xe.what());
1118  }
1119  }
1120  return nullptr;
1121 }
PyObject * getMDObjectValue(const MDObject *obj)
virtual bool containsLabel(MDLabel label) const =0
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()
virtual MDObject * getObject(MDLabel label)=0
MDLabel

◆ Image_getPixel()

PyObject* Image_getPixel ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 680 of file python_image.cpp.

681 {
682  const auto *self = reinterpret_cast<ImageObject*>(obj);
683  int i;
684  int j;
685  int k;
686  int n;
687  double value;
688 
689  if (self != nullptr && PyArg_ParseTuple(args, "iiii", &n, &k, &i, &j ))
690  {
691  try
692  {
693  self->image->data->im->resetOrigin();
694  if (n==0 && k==0)
695  value = self->image->getPixel(i, j);
696  else
697  value = self->image->getPixel(n, k , i, j);
698  return PyFloat_FromDouble(value);
699  }
700  catch (XmippError &xe)
701  {
702  PyErr_SetString(PyXmippError, xe.what());
703  }
704  }
705  return nullptr;
706 }//function Image_getPixel
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
#define j
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()
int * n

◆ Image_iadd()

PyObject* Image_iadd ( PyObject *  obj1,
PyObject *  obj2 
)

Image inplace add, equivalent to += operator

Definition at line 1397 of file python_image.cpp.

1398 {
1399  ImageObject * result = nullptr;
1400  try
1401  {
1402  Image_Value(obj1).add(Image_Value(obj2));
1403  if ((result = (ImageObject*)PyObject_CallFunction((PyObject*)&ImageType, "")))
1404  result->image = std::make_unique<ImageGeneric>(Image_Value(obj1));
1405  //return obj1;
1406  }
1407  catch (XmippError &xe)
1408  {
1409  result=nullptr;
1410  PyErr_SetString(PyXmippError, xe.what());
1411  }
1412  return (PyObject *)result;
1413 }//operator +=
PyTypeObject ImageType
PyObject_HEAD std::unique_ptr< ImageGeneric > image
Definition: python_image.h:50
#define Image_Value(v)
Definition: python_image.h:44
PyObject * PyXmippError
Definition: xmippmodule.cpp:47

◆ Image_idivide()

PyObject* Image_idivide ( PyObject *  obj1,
PyObject *  obj2 
)

Image division NOTE (JM): For efficiency reasons, we break the Python convention and return None instead of a new reference of Image just to avoid creating a new Image object.

Definition at line 1618 of file python_image.cpp.

1619 {
1620  std::cerr << "TODO: not working Image_idivide_____________________________" << std::endl;
1621  try
1622  {
1623  ImageObject * result = nullptr;
1624  if ((result = (ImageObject*)PyObject_CallFunction((PyObject*)&ImageType, "")))
1625  result->image = std::make_unique<ImageGeneric>(Image_Value(obj1));
1626  double value = PyFloat_AsDouble(obj2);
1627  Image_Value(result).divide(value);
1628  return (PyObject*) result;
1629  }
1630  catch (XmippError &xe)
1631  {
1632  PyErr_SetString(PyXmippError, xe.what());
1633  }
1634  return nullptr;
1635 }//operator /=
PyTypeObject ImageType
PyObject_HEAD std::unique_ptr< ImageGeneric > image
Definition: python_image.h:50
#define Image_Value(v)
Definition: python_image.h:44
PyObject * PyXmippError
Definition: xmippmodule.cpp:47

◆ Image_imultiply()

PyObject* Image_imultiply ( PyObject *  obj1,
PyObject *  obj2 
)

Definition at line 1539 of file python_image.cpp.

1540 {
1541  try
1542  {
1543  ImageObject * result = nullptr;
1544  if ((result = (ImageObject*)PyObject_CallFunction((PyObject*)&ImageType, "")))
1545  result->image = std::make_unique<ImageGeneric>(Image_Value(obj1));
1546  double value = PyFloat_AsDouble(obj2);
1547  Image_Value(result).multiply(value);
1548  return (PyObject*) result;
1549  }
1550  catch (XmippError &xe)
1551  {
1552  PyErr_SetString(PyXmippError, xe.what());
1553  }
1554  return nullptr;
1555 }//operator *=
PyTypeObject ImageType
PyObject_HEAD std::unique_ptr< ImageGeneric > image
Definition: python_image.h:50
#define Image_Value(v)
Definition: python_image.h:44
PyObject * PyXmippError
Definition: xmippmodule.cpp:47

◆ Image_initConstant()

PyObject* Image_initConstant ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 740 of file python_image.cpp.

741 {
742  const auto *self = reinterpret_cast<ImageObject*>(obj);
743  double value = -1;
744 
745  if (self != nullptr && PyArg_ParseTuple(args, "d", &value))
746  {
747  try
748  {
749  self->image->initConstant(value);
750  Py_RETURN_NONE;
751  }
752  catch (XmippError &xe)
753  {
754  PyErr_SetString(PyXmippError, xe.what());
755  }
756  }
757  return nullptr;
758 }//function Image_initConstant
PyObject_HEAD std::unique_ptr< ImageGeneric > image
Definition: python_image.h:50
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_initRandom()

PyObject* Image_initRandom ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 779 of file python_image.cpp.

780 {
781  const auto *self = reinterpret_cast<ImageObject*>(obj);
782  double op1 = 0;
783  double op2 = 1;
785  int pyMode = -1;
786 
787  if (self != nullptr && PyArg_ParseTuple(args, "|ddi", &op1, &op2, &pyMode))
788  {
789  try
790  {
791  if (pyMode != -1)
792  mode = (RandomMode) pyMode;
793 
794  self->image->initRandom(op1, op2, mode);
795  Py_RETURN_NONE;
796  }
797  catch (XmippError &xe)
798  {
799  PyErr_SetString(PyXmippError, xe.what());
800  }
801  }
802  return nullptr;
803 }//function Image_initRandom
RandomMode
void mode
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_inplaceAdd()

PyObject* Image_inplaceAdd ( PyObject *  self,
PyObject *  args,
PyObject *  kwargs 
)

Image inplace add, equivalent to *= operator but this not return a new instance of image, mainly for efficiency reasons.

Definition at line 1420 of file python_image.cpp.

1421 {
1422  try
1423  {
1424  PyObject *other = nullptr;
1425  if (PyArg_ParseTuple(args, "O", &other) &&
1426  Image_Check(other))
1427  {
1434 
1435  Image_Value(self).add(Image_Value(other));
1436  Py_RETURN_NONE;
1437  }
1438  else
1439  PyErr_SetString(PyXmippError, "Expecting Image as second argument");
1440  }
1441  catch (XmippError &xe)
1442  {
1443  PyErr_SetString(PyXmippError, xe.what());
1444  }
1445  return nullptr;
1446 }// similar to -=
#define Image_Value(v)
Definition: python_image.h:44
#define STARTINGX(v)
#define STARTINGY(v)
#define MULTIDIM_ARRAY_BASE(v)
#define Image_Check(v)
Definition: python_image.h:43
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
#define STARTINGZ(v)

◆ Image_inplaceDivide()

PyObject* Image_inplaceDivide ( PyObject *  self,
PyObject *  args,
PyObject *  kwargs 
)

Image inplace divide, equivalent to /= operator but this not return a new instance of image, mainly for efficiency reasons.

Definition at line 1642 of file python_image.cpp.

1643 {
1644  try
1645  {
1646  PyObject *other = nullptr;
1647  if (PyArg_ParseTuple(args, "O", &other))
1648  {
1649  if (Image_Check(other))
1650  {
1651  Image_Value(self).divide(Image_Value(other));
1652  }
1653  else
1654  {
1655  Image_Value(self).divide(PyFloat_AsDouble(other));
1656  }
1657  Py_RETURN_NONE;
1658  }
1659  else
1660  PyErr_SetString(PyXmippError, "Expecting Number or Image as second argument");
1661  }
1662  catch (XmippError &xe)
1663  {
1664  PyErr_SetString(PyXmippError, xe.what());
1665  }
1666  return nullptr;
1667 
1668 }// similar to /=
#define Image_Value(v)
Definition: python_image.h:44
#define Image_Check(v)
Definition: python_image.h:43
PyObject * PyXmippError
Definition: xmippmodule.cpp:47

◆ Image_inplaceMultiply()

PyObject* Image_inplaceMultiply ( PyObject *  self,
PyObject *  args,
PyObject *  kwargs 
)

Image inplace multiply, equivalent to *= operator but this not return a new instance of image, mainly for efficiency reasons.

Definition at line 1562 of file python_image.cpp.

1563 {
1564  try
1565  {
1566  PyObject *other = nullptr;
1567  if (PyArg_ParseTuple(args, "O", &other))
1568  {
1569  if (Image_Check(other))
1570  {
1571  Image_Value(self).multiply(Image_Value(other));
1572  }
1573  else
1574  {
1575  Image_Value(self).multiply(PyFloat_AsDouble(other));
1576  }
1577  Py_RETURN_NONE;
1578  }
1579  else
1580  PyErr_SetString(PyXmippError, "Expecting Number or Image as second argument");
1581  }
1582  catch (XmippError &xe)
1583  {
1584  PyErr_SetString(PyXmippError, xe.what());
1585  }
1586  return nullptr;
1587 }// similar to *=
#define Image_Value(v)
Definition: python_image.h:44
#define Image_Check(v)
Definition: python_image.h:43
PyObject * PyXmippError
Definition: xmippmodule.cpp:47

◆ Image_inplaceSubtract()

PyObject* Image_inplaceSubtract ( PyObject *  self,
PyObject *  args,
PyObject *  kwargs 
)

Image inplace subtract, equivalent to *= operator but this not return a new instance of image, mainly for efficiency reasons.

Definition at line 1494 of file python_image.cpp.

1495 {
1496  try
1497  {
1498  PyObject *other = nullptr;
1499  if (PyArg_ParseTuple(args, "O", &other) &&
1500  Image_Check(other))
1501  {
1502  Image_Value(self).subtract(Image_Value(other));
1503  Py_RETURN_NONE;
1504  }
1505  else
1506  PyErr_SetString(PyXmippError, "Expecting Image as second argument");
1507  }
1508  catch (XmippError &xe)
1509  {
1510  PyErr_SetString(PyXmippError, xe.what());
1511  }
1512  return nullptr;
1513 }
#define Image_Value(v)
Definition: python_image.h:44
#define Image_Check(v)
Definition: python_image.h:43
PyObject * PyXmippError
Definition: xmippmodule.cpp:47

◆ Image_isubtract()

PyObject* Image_isubtract ( PyObject *  obj1,
PyObject *  obj2 
)

Image inplace subtraction, equivalent to -= operator

Definition at line 1472 of file python_image.cpp.

1473 {
1474  ImageObject * result = nullptr;
1475  try
1476  {
1477  Image_Value(obj1).subtract(Image_Value(obj2));
1478  if ((result = (ImageObject*)PyObject_CallFunction((PyObject*)&ImageType, "")))
1479  result->image = std::make_unique<ImageGeneric>(Image_Value(obj1));
1480  }
1481  catch (XmippError &xe)
1482  {
1483  result = nullptr;
1484  PyErr_SetString(PyXmippError, xe.what());
1485  }
1486  return (PyObject *)result;
1487 }//operator -=
PyTypeObject ImageType
PyObject_HEAD std::unique_ptr< ImageGeneric > image
Definition: python_image.h:50
#define Image_Value(v)
Definition: python_image.h:44
PyObject * PyXmippError
Definition: xmippmodule.cpp:47

◆ Image_mirrorY()

PyObject* Image_mirrorY ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 762 of file python_image.cpp.

763 {
764  const auto *self = reinterpret_cast<ImageObject*>(obj);
765  try
766  {
767  self->image->mirrorY();
768  Py_RETURN_NONE;
769  }
770  catch (XmippError &xe)
771  {
772  PyErr_SetString(PyXmippError, xe.what());
773  }
774  return nullptr;
775 }//function Image_initConstant
PyObject_HEAD std::unique_ptr< ImageGeneric > image
Definition: python_image.h:50
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_multiply()

PyObject* Image_multiply ( PyObject *  obj1,
PyObject *  obj2 
)

Definition at line 1517 of file python_image.cpp.

1518 {
1519  ImageObject * result = (ImageObject*)PyObject_CallFunction((PyObject*)&ImageType, "");
1520  if (result != nullptr)
1521  {
1522  try
1523  {
1524  result->image = std::make_unique<ImageGeneric>(Image_Value(obj1));
1525  double value = PyFloat_AsDouble(obj2);
1526  Image_Value(result).multiply(value);
1527  }
1528  catch (XmippError &xe)
1529  {
1530  result = nullptr;
1531  PyErr_SetString(PyXmippError, xe.what());
1532  }
1533  }
1534  return (PyObject *)result;
1535 }//operator *
PyTypeObject ImageType
PyObject_HEAD std::unique_ptr< ImageGeneric > image
Definition: python_image.h:50
#define Image_Value(v)
Definition: python_image.h:44
PyObject * PyXmippError
Definition: xmippmodule.cpp:47

◆ Image_new()

PyObject* Image_new ( PyTypeObject *  type,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 213 of file python_image.cpp.

214 {
215  ImageObject *self = (ImageObject*)type->tp_alloc(type, 0);
216 
217  if (self != nullptr)
218  {
219  PyObject *input = nullptr;
220 
221  if (PyArg_ParseTuple(args, "|O", &input))
222  {
223  if (input != nullptr)
224  {
225  try
226  {
227  PyObject *pyStr;
228  // If the input object is a tuple, consider it (index, filename)
229  if (PyTuple_Check(input))
230  {
231  // Get the index and filename from the Python tuple object
232  size_t index = PyLong_AsSsize_t(PyTuple_GetItem(input, 0));
233  PyObject* repr = PyObject_Str(PyTuple_GetItem(input, 1));
234  const char * filename = PyUnicode_AsUTF8(repr);
235  // Now read using both of index and filename
236  self->image = std::make_unique<ImageGeneric>();
237  self->image->read(filename, DATA, index);
238 
239  }
240  else if ((pyStr = PyObject_Str(input)) != nullptr)
241  {
242  self->image = std::make_unique<ImageGeneric>(PyUnicode_AsUTF8(pyStr));
243  //todo: add copy constructor
244  }
245  else
246  {
247  PyErr_SetString(PyExc_TypeError,
248  "Image_new: Expected string, FileName or tuple as first argument");
249  return nullptr;
250  }
251  }
252  catch (XmippError &xe)
253  {
254  PyErr_SetString(PyXmippError, xe.what());
255  return nullptr;
256  }
257  }
258  else
259  self->image = std::make_unique<ImageGeneric>();
260  }
261  else
262  return nullptr;
263  }
264  return (PyObject *)self;
265 }//function Image_new
viol index
viol type
PyObject * PyXmippError
Definition: xmippmodule.cpp:47

◆ Image_patch()

PyObject* Image_patch ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 935 of file python_image.cpp.

936 {
937  const auto *self = reinterpret_cast<ImageObject*>(obj);
938  PyObject *patch;
939  int x = 0;
940  int y = 0;
941 
942  if (self != nullptr && PyArg_ParseTuple(args, "Oii", &patch, &x, &y))
943  {
944  try
945  {
947  Py_RETURN_NONE;
948  }
949  catch (XmippError &xe)
950  {
951  PyErr_SetString(PyXmippError, xe.what());
952  }
953  }
954  return nullptr;
955 }//function Image_patch
static double * y
#define Image_Value(v)
Definition: python_image.h:44
doublereal * x
#define MULTIDIM_ARRAY_GENERIC(v)
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_projectVolumeDouble()

PyObject* Image_projectVolumeDouble ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 1290 of file xmippmodule.cpp.

1291 {
1292  PyObject *pvol = nullptr;
1293  ImageObject * result = nullptr;
1294  double rot;
1295  double tilt;
1296  double psi;
1297 
1298  if (PyArg_ParseTuple(args, "Oddd", &pvol, &rot,&tilt,&psi))
1299  {
1300  try
1301  {
1302  // We use the following macro to release the Python Interpreter Lock (GIL)
1303  // while running this C extension code and allows threads to run concurrently.
1304  // See: https://docs.python.org/2.7/c-api/init.html for details.
1305  Py_BEGIN_ALLOW_THREADS
1306  Projection P;
1307  auto *vol = (ImageObject*) pvol;
1308  MultidimArray<double> * mVolume;
1309  vol->image->data->getMultidimArrayPointer(mVolume);
1310  ArrayDim aDim;
1311  mVolume->getDimensions(aDim);
1312  mVolume->setXmippOrigin();
1313  projectVolume(*mVolume, P, aDim.xdim, aDim.ydim,rot, tilt, psi);
1314  result = (ImageObject*)PyObject_CallFunction((PyObject*)&ImageType, "");
1315  Image <double> I;
1316  result->image = std::make_unique<ImageGeneric>();
1317  result->image->setDatatype(DT_Double);
1318  result->image->data->setImage(MULTIDIM_ARRAY(P));
1319  Py_END_ALLOW_THREADS
1320  return (PyObject *)result;
1321  }
1322  catch (XmippError &xe)
1323  {
1324  PyErr_SetString(PyXmippError, xe.what());
1325  }
1326  }
1327  return nullptr;
1328 }//function Image_projectVolumeDouble
PyTypeObject ImageType
size_t xdim
PyObject_HEAD std::unique_ptr< ImageGeneric > image
Definition: python_image.h:50
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
#define MULTIDIM_ARRAY(v)
void projectVolume(FourierProjector &projector, Projection &P, int Ydim, int Xdim, double rot, double tilt, double psi, const MultidimArray< double > *ctf)
double psi(const double x)
size_t ydim
void getDimensions(size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim) const

◆ Image_radialAvgAxis()

PyObject* Image_radialAvgAxis ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 1943 of file python_image.cpp.

1944 {
1945  const auto *self = reinterpret_cast<ImageObject*>(obj);
1946  if (nullptr == self) return nullptr;
1947  try {
1948  char axis = 'z';
1949  ImageObject *result = (ImageObject*)PyObject_CallFunction((PyObject*)&ImageType, "");
1950  if (PyArg_ParseTuple(args, "|c", &axis)
1951  && (nullptr != result)) {
1952  // prepare input image
1953  auto &volume = self->image;
1954  volume->convert2Datatype(DT_Double);
1956  MULTIDIM_ARRAY_GENERIC(*volume).getMultidimArrayPointer(in);
1957  // prepare output image
1958  result->image = std::make_unique<ImageGeneric>(DT_Double);
1959  MultidimArray<double> *out;
1960  MULTIDIM_ARRAY_GENERIC(*result->image).getMultidimArrayPointer(out);
1961  // call the estimation
1962  radialAverageAxis(*in, axis, *out);
1963  } else {
1964  PyErr_SetString(PyXmippError, "Unknown error while allocating data for output or parsing data");
1965  }
1966  return (PyObject *)result;
1967  } catch (XmippError &xe) {
1968  PyErr_SetString(PyXmippError, xe.what());
1969  }
1970  return Py_BuildValue("");
1971 }
PyTypeObject ImageType
PyObject_HEAD std::unique_ptr< ImageGeneric > image
Definition: python_image.h:50
void radialAverageAxis(const MultidimArray< T > &in, char axis, MultidimArray< double > &out)
char axis
int in
#define MULTIDIM_ARRAY_GENERIC(v)
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_read()

PyObject* Image_read ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 388 of file python_image.cpp.

389 {
390  const auto *self = reinterpret_cast<ImageObject*>(obj);
391 
392  if (self != nullptr)
393  {
394  int datamode = DATA;
395  PyObject *input = nullptr;
396  if (PyArg_ParseTuple(args, "O|i", &input, &datamode))
397  {
398 
399  try
400  {
401  PyObject *pyStr;
402  // If the input object is a tuple, consider it (index, filename)
403  if (PyTuple_Check(input))
404  {
405  // Get the index and filename from the Python tuple object
406  size_t index = PyLong_AsSsize_t(PyTuple_GetItem(input, 0));
407  PyObject* repr = PyObject_Str(PyTuple_GetItem(input, 1));
408  const char *filename = PyUnicode_AsUTF8(repr);
409  // Now read using both of index and filename
410  self->image->read(filename,(DataMode)datamode, index);
411  Py_RETURN_NONE;
412  }
413  else if ((pyStr = PyObject_Str(input)) != nullptr)
414  {
415  self->image->read(PyUnicode_AsUTF8(pyStr),(DataMode)datamode);
416  Py_RETURN_NONE;
417  }
418  else
419  {
420  PyErr_SetString(PyExc_TypeError,
421  "Image_write: Expected an string or FileName as first argument");
422  }
423  }
424  catch (XmippError &xe)
425  {
426  PyErr_SetString(PyXmippError, xe.what());
427  }
428  }
429  }
430  return nullptr;
431 }//function Image_read
DataMode
viol index
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_readApplyGeo()

PyObject* Image_readApplyGeo ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 1740 of file python_image.cpp.

1741 {
1742  const auto *self = reinterpret_cast<ImageObject*>(obj);
1743 
1744  if (self != nullptr)
1745  {
1746  PyObject *md = nullptr;
1747  PyObject *only_apply_shifts = Py_False;
1748  PyObject *wrap = (xmipp_transformation::WRAP ? Py_True : Py_False);
1749  size_t objectId = BAD_OBJID;
1750  bool boolOnly_apply_shifts = false;
1751  bool boolWrap = xmipp_transformation::WRAP;
1752  int datamode = DATA;
1753  size_t select_img = ALL_IMAGES;
1754 
1755  if (PyArg_ParseTuple(args, "Ok|OikO", &md, &objectId, &only_apply_shifts, &datamode, &select_img, &wrap))
1756  {
1757  try
1758  {
1759  if (PyBool_Check(only_apply_shifts))
1760  boolOnly_apply_shifts = (only_apply_shifts == Py_True);
1761  else
1762  PyErr_SetString(PyExc_TypeError, "ImageGeneric::readApplyGeo: Expecting boolean value");
1763  if (PyBool_Check(wrap))
1764  boolWrap = (wrap == Py_True);
1765  else
1766  PyErr_SetString(PyExc_TypeError, "ImageGeneric::readApplyGeo: Expecting boolean value");
1767  ApplyGeoParams params;
1768  params.only_apply_shifts = boolOnly_apply_shifts;
1769  params.datamode = (DataMode)datamode;
1770  params.select_img = select_img;
1771  params.wrap = boolWrap;
1772  self->image->readApplyGeo(MetaData_Value(md), objectId, params);
1773  Py_RETURN_NONE;
1774  }
1775  catch (XmippError &xe)
1776  {
1777  PyErr_SetString(PyXmippError, xe.what());
1778  }
1779  }
1780  }
1781  return nullptr;
1782 }
DataMode
#define MetaData_Value(v)
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
#define ALL_IMAGES
void(* obj)()
#define BAD_OBJID
Definition: metadata_base.h:55

◆ Image_readPreview()

PyObject* Image_readPreview ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 446 of file python_image.cpp.

447 {
448  const auto *self = reinterpret_cast<ImageObject*>(obj);
449 
450  if (self != nullptr)
451  {
452  PyObject *input = nullptr;
453  int x = 0;
454  int slice = CENTRAL_SLICE;
455 
456  if (PyArg_ParseTuple(args, "O|ii", &input, &x, &slice))
457  {
458  try
459  {
460  PyObject *pyStr;
461  if ((pyStr = PyObject_Str(input)) != nullptr)
462  {
463  readImagePreview(self->image.get(), PyUnicode_AsUTF8(pyStr), x, slice);
464  Py_RETURN_NONE;
465  }
466  else
467  {
468  PyErr_SetString(PyExc_TypeError,
469  "Image_readPreview: Expected string or FileName as first argument");
470  }
471  }
472  catch (XmippError &xe)
473  {
474  PyErr_SetString(PyXmippError, xe.what());
475  }
476  }
477  else
478  PyErr_SetString(PyXmippError,"Error with input arguments, expected filename and optional size");
479  }
480  return nullptr;
481 }//function Image_readPreview
void readImagePreview(ImageGeneric *ig, FileName fn, size_t xdim, int slice)
doublereal * x
#define CENTRAL_SLICE
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_readPreviewSmooth()

PyObject* Image_readPreviewSmooth ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 485 of file python_image.cpp.

486 {
487  const auto *self = reinterpret_cast<ImageObject*>(obj);
488 
489  if (self != nullptr)
490  {
491  PyObject *input = nullptr;
492  int x;
493  if (PyArg_ParseTuple(args, "Oi", &input, &x))
494  {
495  try
496  {
497 
498  PyObject *pyStr;
499  if ((pyStr = PyObject_Str(input)) != nullptr)
500  {
501  self->image->readPreviewSmooth(PyUnicode_AsUTF8(pyStr), x);
502  Py_RETURN_NONE;
503  }
504  else
505  {
506  PyErr_SetString(PyExc_TypeError,
507  "Image_readPreviewSmooth: Expected string or FileName as first argument");
508  }
509  }
510  catch (XmippError &xe)
511  {
512  PyErr_SetString(PyXmippError, xe.what());
513  }
514  }
515  }
516  return nullptr;
517 }//function Image_readPreviewSmooth
doublereal * x
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_repr()

PyObject* Image_repr ( PyObject *  obj)

Definition at line 269 of file python_image.cpp.

270 {
271  const auto *self = reinterpret_cast<ImageObject*>(obj);
272  String s;
273  self->image->toString(s);
274  return PyUnicode_FromString(s.c_str());
275 }//function Image_repr
PyObject_HEAD std::unique_ptr< ImageGeneric > image
Definition: python_image.h:50
std::string String
Definition: xmipp_strings.h:34
void(* obj)()

◆ Image_resetOrigin()

PyObject* Image_resetOrigin ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 1050 of file python_image.cpp.

1051 {
1052  const auto *self = reinterpret_cast<ImageObject*>(obj);
1053  if (self != nullptr)
1054  {
1055  try
1056  {
1057  self->image->data->im->resetOrigin();
1058  Py_RETURN_NONE;
1059  }
1060  catch (XmippError &xe)
1061  {
1062  PyErr_SetString(PyXmippError, xe.what());
1063  }
1064  }
1065  return nullptr;
1066 }//function Image_getDimensions
PyObject_HEAD std::unique_ptr< ImageGeneric > image
Definition: python_image.h:50
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_resize()

PyObject* Image_resize ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 807 of file python_image.cpp.

808 {
809  const auto *self = reinterpret_cast<ImageObject*>(obj);
810  int xDim = 0;
811  int yDim = 0;
812  int zDim = 1;
813  size_t nDim = 1;
814 
815  if (self != nullptr && PyArg_ParseTuple(args, "ii|in", &xDim, &yDim, &zDim, &nDim))
816  {
817  try
818  {
819  self->image->resize(xDim, yDim, zDim, nDim, false); // TODO: Take care of copy mode if needed
820  Py_RETURN_NONE;
821  }
822  catch (XmippError &xe)
823  {
824  PyErr_SetString(PyXmippError, xe.what());
825  }
826  }
827  return nullptr;
828 }//function Image_resize
#define yDim
Definition: projection.cpp:42
#define xDim
Definition: projection.cpp:41
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_reslice()

PyObject* Image_reslice ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 862 of file python_image.cpp.

863 {
864  const auto *self = reinterpret_cast<ImageObject*>(obj);
865  int axis = VIEW_Z_NEG;
866 
867  if (self != nullptr && PyArg_ParseTuple(args, "i", &axis))
868  {
869  try
870  {
871  self->image->reslice((AxisView) axis);
872  Py_RETURN_NONE;
873  }
874  catch (XmippError &xe)
875  {
876  PyErr_SetString(PyXmippError, xe.what());
877  }
878  }
879  return nullptr;
880 }//function Image_reslice
AxisView
Definition: axis_view.h:32
char axis
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_RichCompareBool()

PyObject* Image_RichCompareBool ( PyObject *  obj,
PyObject *  obj2,
int  opid 
)

Definition at line 279 of file python_image.cpp.

280 {
281  if (obj != nullptr && obj2 != nullptr)
282  {
283  try
284  {
285  if (opid == Py_EQ)
286  {
287  if (Image_Value(obj) == Image_Value(obj2))
288  Py_RETURN_TRUE;
289  else
290  Py_RETURN_FALSE;
291  }
292  else if (opid == Py_NE)
293  {
294  if (Image_Value(obj) == Image_Value(obj2))
295  Py_RETURN_FALSE;
296  else
297  Py_RETURN_TRUE;
298  }
299  else
300  return Py_NotImplemented;
301 
302  }
303  catch (XmippError &xe)
304  {
305  PyErr_SetString(PyXmippError, xe.what());
306  }
307  }
308  return nullptr;
309 }//function Image_compare
#define Py_NotImplemented
Definition: frm_wrap.cpp:860
#define Image_Value(v)
Definition: python_image.h:44
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_scale()

PyObject* Image_scale ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 832 of file python_image.cpp.

833 {
834  const auto *self = reinterpret_cast<ImageObject*>(obj);
835  int xDim = 0;
836  int yDim = 0;
837  int zDim = 1;
838  int forceVolume=0;
839  if (self != nullptr && PyArg_ParseTuple(args, "ii|ii", &xDim, &yDim, &zDim, &forceVolume))
840  {
841  try
842  {
844  I.setXmippOrigin();
845  size_t xdim, ydim, zdim, ndim;
846  I.getDimensions(xdim, ydim, zdim, ndim);
847  if (forceVolume && zdim==1 && ndim>1)
848  I.setDimensions(xdim,ydim,ndim,1);
849  selfScaleToSize(xmipp_transformation::LINEAR, I, xDim, yDim, zDim);
850  Py_RETURN_NONE;
851  }
852  catch (XmippError &xe)
853  {
854  PyErr_SetString(PyXmippError, xe.what());
855  }
856  }
857  return nullptr;
858 }//function Image_scale
#define yDim
Definition: projection.cpp:42
#define xDim
Definition: projection.cpp:41
#define Image_Value(v)
Definition: python_image.h:44
void selfScaleToSize(int SplineDegree, MultidimArrayGeneric &V1, int Xdim, int Ydim, int Zdim)
#define MULTIDIM_ARRAY_GENERIC(v)
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void setDimensions(int Xdim, int Ydim, int Zdim, size_t Ndim)
void(* obj)()
void getDimensions(size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim) const

◆ Image_setData()

PyObject* Image_setData ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 644 of file python_image.cpp.

645 {
646  const auto *self = reinterpret_cast<ImageObject*>(obj);
647  PyArrayObject * arr = nullptr;
648 
649  if (self != nullptr && PyArg_ParseTuple(args, "O", &arr))
650  {
651  try
652  {
653  ImageGeneric & image = Image_Value(self);
654  DataType dt = npyType2Datatype(PyArray_TYPE(arr));
655  int nd = PyArray_NDIM(arr);
656  //Setup of image
657  image.setDatatype(dt);
658  ArrayDim adim;
659  adim.ndim = (nd == 4 ) ? PyArray_DIM(arr, 0) : 1;
660  adim.zdim = (nd > 2 ) ? PyArray_DIM(arr, nd - 3) : 1;
661  adim.ydim = PyArray_DIM(arr, nd - 2);
662  adim.xdim = PyArray_DIM(arr, nd - 1);
663 
664  MULTIDIM_ARRAY_GENERIC(image).resize(adim, false);
665  void *mymem = image().getArrayPointer();
666  void * data = PyArray_DATA(arr);
667  memcpy(mymem, data, adim.nzyxdim * gettypesize(dt));
668  Py_RETURN_NONE;
669  }
670  catch (XmippError &xe)
671  {
672  PyErr_SetString(PyXmippError, xe.what());
673  }
674  }
675  return nullptr;
676 }//function Image_setData
size_t xdim
#define Image_Value(v)
Definition: python_image.h:44
size_t nzyxdim
size_t zdim
DataType
#define MULTIDIM_ARRAY_GENERIC(v)
size_t ndim
void setDatatype(DataType _datatype)
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
size_t ydim
void(* obj)()
DataType npyType2Datatype(int npy)
size_t gettypesize(DataType type)
Returns memory size of datatype.

◆ Image_setDataType()

PyObject* Image_setDataType ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 981 of file python_image.cpp.

982 {
983  const auto *self = reinterpret_cast<ImageObject*>(obj);
984  int datatype;
985 
986  if (self != nullptr && PyArg_ParseTuple(args, "i", &datatype))
987  {
988  try
989  {
990  self->image->setDatatype((DataType)datatype);
991  Py_RETURN_NONE;
992  }
993  catch (XmippError &xe)
994  {
995  PyErr_SetString(PyXmippError, xe.what());
996  }
997  }
998  return nullptr;
999 }//function Image_setDataType
PyObject_HEAD std::unique_ptr< ImageGeneric > image
Definition: python_image.h:50
DataType
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_setHeaderValue()

PyObject* Image_setHeaderValue ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 1183 of file python_image.cpp.

1184 {
1185  const auto *self = reinterpret_cast<ImageObject*>(obj);
1186 
1187  int label;
1188  PyObject *pyValue; //Only used to skip label and value
1189 
1190  if (self != nullptr && PyArg_ParseTuple(args, "iO", &label, &pyValue))
1191  {
1192  try
1193  {
1194  MDRow &mainHeader = *(self->image->image->MD[0]);
1195 
1196  auto object = createMDObject(label, pyValue);
1197  if (!object)
1198  return nullptr;
1199  mainHeader.setValue(*object);
1200  Py_RETURN_TRUE;
1201  }
1202  catch (XmippError &xe)
1203  {
1204  PyErr_SetString(PyXmippError, xe.what());
1205  }
1206  }
1207  return nullptr;
1208 }
std::unique_ptr< MDObject > createMDObject(int label, PyObject *pyValue)
void setValue(MDLabel label, const T &d, bool addLabel=true)
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_setMainHeaderValue()

PyObject* Image_setMainHeaderValue ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 1125 of file python_image.cpp.

1126 {
1127  const auto *self = reinterpret_cast<ImageObject*>(obj);
1128 
1129  int label;
1130  PyObject *pyValue; //Only used to skip label and value
1131 
1132  if (self != nullptr && PyArg_ParseTuple(args, "iO", &label, &pyValue))
1133  {
1134  try
1135  {
1136  MDRow &mainHeader = self->image->image->MDMainHeader;
1137 
1138  auto object = createMDObject(label, pyValue);
1139  if (!object)
1140  return nullptr;
1141  mainHeader.setValue(*object);
1142  Py_RETURN_TRUE;
1143  }
1144  catch (XmippError &xe)
1145  {
1146  PyErr_SetString(PyXmippError, xe.what());
1147  }
1148  }
1149  return nullptr;
1150 }
std::unique_ptr< MDObject > createMDObject(int label, PyObject *pyValue)
void setValue(MDLabel label, const T &d, bool addLabel=true)
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_setPixel()

PyObject* Image_setPixel ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 710 of file python_image.cpp.

711 {
712  const auto *self = reinterpret_cast<ImageObject*>(obj);
713  int i;
714  int j;
715  int k;
716  int n;
717  double value = -1;
718 
719  if (self != nullptr && PyArg_ParseTuple(args, "iiiid", &n, &k, &i, &j, &value))
720  {
721  try
722  {
723  if (n==0 && k==0)
724  self->image->setPixel(i, j, value);
725  else
726  self->image->setPixel(n, k, i, j, value);
727 
728  Py_RETURN_NONE;
729  }
730  catch (XmippError &xe)
731  {
732  PyErr_SetString(PyXmippError, xe.what());
733  }
734  }
735  return nullptr;
736 }//function Image_setPixel
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
#define j
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()
int * n

◆ Image_subtract()

PyObject* Image_subtract ( PyObject *  obj1,
PyObject *  obj2 
)

Definition at line 1451 of file python_image.cpp.

1452 {
1453  ImageObject * result = (ImageObject*)PyObject_CallFunction((PyObject*)&ImageType, "");
1454  if (result != nullptr)
1455  {
1456  try
1457  {
1458  result->image = std::make_unique<ImageGeneric>(Image_Value(obj1));
1459  Image_Value(result).subtract(Image_Value(obj2));
1460  }
1461  catch (XmippError &xe)
1462  {
1463  result = nullptr;
1464  PyErr_SetString(PyXmippError, xe.what());
1465  }
1466  }
1467  return (PyObject *)result;
1468 }//operator -
PyTypeObject ImageType
PyObject_HEAD std::unique_ptr< ImageGeneric > image
Definition: python_image.h:50
#define Image_Value(v)
Definition: python_image.h:44
PyObject * PyXmippError
Definition: xmippmodule.cpp:47

◆ Image_true_divide()

PyObject* Image_true_divide ( PyObject *  obj1,
PyObject *  obj2 
)

Definition at line 1591 of file python_image.cpp.

1592 {
1593  std::cerr << "TODO: not working Image_divide_____________________________" << std::endl;
1594  ImageObject * result = (ImageObject*)PyObject_CallFunction((PyObject*)&ImageType, "");
1595  if (result != nullptr)
1596  {
1597  try
1598  {
1599  result->image = std::make_unique<ImageGeneric>(Image_Value(obj1));
1600  double value = PyFloat_AsDouble(obj2);
1601  Image_Value(result).divide(value);
1602  }
1603  catch (XmippError &xe)
1604  {
1605  result = nullptr;
1606  PyErr_SetString(PyXmippError, xe.what());
1607  }
1608  }
1609  return (PyObject *)result;
1610 }//operator /
PyTypeObject ImageType
PyObject_HEAD std::unique_ptr< ImageGeneric > image
Definition: python_image.h:50
#define Image_Value(v)
Definition: python_image.h:44
PyObject * PyXmippError
Definition: xmippmodule.cpp:47

◆ Image_warpAffine()

PyObject* Image_warpAffine ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Image inplace subtraction, equivalent to -= operator

Definition at line 1786 of file python_image.cpp.

1787 {
1788 
1789  PyObject * list = nullptr;
1790  PyObject * item = nullptr;
1791  PyObject * dsize = nullptr;
1792  PyObject * wrap = Py_True;
1793  PyObject * border_value = nullptr;
1794 
1795  const auto *self = reinterpret_cast<ImageObject*>(obj);
1796  auto &image = self->image;
1797  double doubleBorder_value = 1.0;
1798  size_t Xdim;
1799  size_t Ydim;
1800  size_t Zdim;
1801  bool doWrap = true;
1802  image->getDimensions(Xdim, Ydim, Zdim);
1803  try
1804  {
1805  PyArg_ParseTuple(args, "O|OOO", &list, &dsize, &wrap, &border_value);
1806  if (PyList_Check(list))
1807  {
1808  if (nullptr != dsize)
1809  {
1810  Ydim = PyLong_AsSsize_t(PyTuple_GetItem(dsize, 0));
1811  Xdim = PyLong_AsSsize_t(PyTuple_GetItem(dsize, 1));
1812  }
1813  if (PyBool_Check(wrap))
1814  doWrap = (wrap == Py_True);
1815  else
1816  PyErr_SetString(PyExc_TypeError, "ImageGeneric::warpAffine: Expecting boolean value for wrapping");
1817  if (border_value!=nullptr)
1818  doubleBorder_value = PyFloat_AsDouble(border_value);
1819 
1820  size_t size = PyList_Size(list);
1821  Matrix2D<double> A;
1822  A.initIdentity(3);
1823  for (size_t i = 0; i < size; ++i)
1824  {
1825  item = PyList_GetItem(list, i);
1826  MAT_ELEM(A,i/3,i%3 ) = PyFloat_AsDouble(item);
1827  }
1828 
1829  image->convert2Datatype(DT_Double);
1831  MULTIDIM_ARRAY_GENERIC(*image).getMultidimArrayPointer(in);
1832  in->setXmippOrigin();
1833 
1834  ImageObject *result = (ImageObject*)PyObject_CallFunction((PyObject*)&ImageType, "");
1835  result->image = std::make_unique<ImageGeneric>(DT_Double);
1836  MultidimArray<double> *out;
1837  MULTIDIM_ARRAY_GENERIC(*result->image).getMultidimArrayPointer(out);
1838  out->resize(Ydim,Xdim);
1839  out->initConstant(doubleBorder_value);
1840  out->setXmippOrigin();
1841 
1842  applyGeometry(3, *out, *in, A, false, doWrap, doubleBorder_value);
1843  return (PyObject *)result;
1844  }
1845  else
1846  {
1847  PyErr_SetString(PyExc_TypeError, "ImageGeneric::warpAffine: Expecting a list");
1848 
1849  }
1850  }
1851  catch (XmippError &xe)
1852  {
1853  PyErr_SetString(PyXmippError, xe.what());
1854  }
1855  return nullptr;
1856 }
PyTypeObject ImageType
PyObject_HEAD std::unique_ptr< ImageGeneric > image
Definition: python_image.h:50
void applyGeometry(int SplineDegree, MultidimArray< std::complex< double > > &V2, const MultidimArray< std::complex< double > > &V1, const Matrix2D< double > &A, bool inv, bool wrap, std::complex< double > outside, MultidimArray< double > *BcoeffsPtr)
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
int in
#define MULTIDIM_ARRAY_GENERIC(v)
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()
void initIdentity()
Definition: matrix2d.h:673

◆ Image_window2D()

PyObject* Image_window2D ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 1902 of file python_image.cpp.

1903 {
1904  ImageObject *self = (ImageObject*) obj;
1905 
1906  if (self != nullptr)
1907  {
1908  try {
1909  int x0;
1910  int y0;
1911  int xF;
1912  int yF;
1913  ImageObject *result = (ImageObject*)PyObject_CallFunction((PyObject*)&ImageType, "");
1914 
1915  if (PyArg_ParseTuple(args, "|IIII", &x0, &y0, &xF, &yF)
1916  && (nullptr != result)) {
1917  // prepare input image
1918  const auto& image = self->image;
1919  image->convert2Datatype(DT_Double);
1920  MultidimArray<double> *pImage_in;
1921  MULTIDIM_ARRAY_GENERIC(*image).getMultidimArrayPointer(pImage_in);
1922  // prepare output image
1923  result->image = std::make_unique<ImageGeneric>(DT_Double);
1924  MultidimArray<double> *pImage_out;
1925  MULTIDIM_ARRAY_GENERIC(*result->image).getMultidimArrayPointer(pImage_out);
1926  // call the estimation
1927  window2D(*pImage_in, *pImage_out, (size_t)y0, (size_t)x0, (size_t)yF, (size_t)xF);
1928 
1929  } else {
1930  PyErr_SetString(PyXmippError, "Unknown error while allocating data for output or parsing data");
1931  }
1932  return (PyObject *)result;
1933  } catch (XmippError &xe) {
1934  PyErr_SetString(PyXmippError, xe.what());
1935  }
1936  }
1937  return nullptr;
1938 }
PyTypeObject ImageType
PyObject_HEAD std::unique_ptr< ImageGeneric > image
Definition: python_image.h:50
#define y0
#define x0
void window2D(const MultidimArray< T > &Ibig, MultidimArray< T > &Ismall, size_t y0, size_t x0, size_t yF, size_t xF)
#define xF
#define MULTIDIM_ARRAY_GENERIC(v)
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()
#define yF

◆ Image_write()

PyObject* Image_write ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 340 of file python_image.cpp.

341 {
342  const auto *self = reinterpret_cast<ImageObject*>(obj);
343  if (self != nullptr)
344  {
345  PyObject *input = nullptr;
346  if (PyArg_ParseTuple(args, "O", &input))
347  {
348  try
349  {
350  PyObject *pyStr;
351  // If the input object is a tuple, consider it (index, filename)
352  if (PyTuple_Check(input))
353  {
354  // Get the index and filename from the Python tuple object
355  size_t index = PyLong_AsSsize_t(PyTuple_GetItem(input, 0));
356  PyObject* repr = PyObject_Str(PyTuple_GetItem(input, 1));
357  const char * filename = PyUnicode_AsUTF8(repr);
358  // Now read using both of index and filename
359  bool isStack = (index > 0);
360  WriteMode writeMode = isStack ? WRITE_REPLACE : WRITE_OVERWRITE;
361  self->image->write(filename, index, isStack, writeMode);
362 
363  Py_RETURN_NONE;
364  }
365  if ((pyStr = PyObject_Str(input)) != nullptr)
366  {
367  const char * filename = PyUnicode_AsUTF8(pyStr);
368  self->image->write(filename);
369  Py_RETURN_NONE;
370  }
371  else
372  {
373  PyErr_SetString(PyExc_TypeError,
374  "Image_write: Expected an string or FileName as first argument");
375  }
376  }
377  catch (XmippError &xe)
378  {
379  PyErr_SetString(PyXmippError, xe.what());
380  }
381  }
382  }
383  return nullptr;
384 }//function Image_write
viol index
WriteMode
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
void(* obj)()

◆ Image_writeSlices()

PyObject* Image_writeSlices ( PyObject *  obj,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 884 of file python_image.cpp.

885 {
886  const auto *self = reinterpret_cast<ImageObject*>(obj);
887  if (self != nullptr)
888  {
889  PyObject *oRootname = nullptr;
890  PyObject *oExt = nullptr;
891  PyObject *oAxis = nullptr;
892  if (PyArg_ParseTuple(args, "OOO", &oRootname, &oExt, &oAxis))
893  {
894  try
895  {
896  // Get the index and filename from the Python tuple object
897  const char * rootname = PyUnicode_AsUTF8(PyObject_Str(oRootname));
898  const char * ext = PyUnicode_AsUTF8(PyObject_Str(oExt));
899  const char * axis = PyUnicode_AsUTF8(PyObject_Str(oAxis));
900  // Now read using both of index and filename
901 
902  ImageGeneric Iout;
903  Iout.setDatatype(self->image->getDatatype());
904 
905  size_t xdim;
906  size_t ydim;
907  size_t zdim;
908  size_t ndim;
909  MULTIDIM_ARRAY_GENERIC(*(self->image)).getDimensions(xdim, ydim, zdim, ndim);
910  size_t N=zdim;
911  char caxis=axis[0];
912  if (caxis=='Y')
913  N=ydim;
914  else if (caxis=='X')
915  N=xdim;
916  for (size_t n=0; n<N; n++)
917  {
918  MULTIDIM_ARRAY_GENERIC(*(self->image)).getSlice(n,&MULTIDIM_ARRAY_GENERIC(Iout),caxis);
919  Iout.write(formatString("%s_%04d.%s",rootname,n,ext));
920  }
921  Py_RETURN_NONE;
922  }
923  catch (XmippError &xe)
924  {
925  PyErr_SetString(PyXmippError, xe.what());
926  }
927  }
928  }
929  return nullptr;
930 
931 }//function writeSlices
void write(const FileName &name="", size_t select_img=ALL_IMAGES, bool isStack=false, int mode=WRITE_OVERWRITE, CastWriteMode castMode=CW_CAST, int _swapWrite=0)
char axis
#define MULTIDIM_ARRAY_GENERIC(v)
void setDatatype(DataType _datatype)
PyObject * PyXmippError
Definition: xmippmodule.cpp:47
String formatString(const char *format,...)
void(* obj)()
int * n

◆ npyType2Datatype()

DataType npyType2Datatype ( int  npy)

Definition at line 553 of file python_image.cpp.

554 {
555  switch (npy)
556  {
557  case NPY_FLOAT:
558  return DT_Float;
559  case NPY_DOUBLE:
560  return DT_Double;
561  case NPY_INT:
562  return DT_Int;
563  case NPY_UINT:
564  return DT_UInt;
565  case NPY_SHORT:
566  return DT_Short;
567  case NPY_USHORT:
568  return DT_UShort;
569  case NPY_BYTE:
570  return DT_SChar;
571  case NPY_UBYTE:
572  return DT_UChar;
573  case NPY_BOOL:
574  return DT_Bool;
575  case NPY_CFLOAT:
576  return DT_CFloat;
577  case NPY_CDOUBLE:
578  return DT_CDouble;
579  case NPY_HALF:
580  return DT_HalfFloat;
581  default:
582  return DT_Unknown;
583  }
584 }

Variable Documentation

◆ Image_methods

PyMethodDef Image_methods[]

Definition at line 64 of file python_image.cpp.

◆ Image_NumberMethods

PyNumberMethods Image_NumberMethods

Definition at line 40 of file python_image.cpp.

◆ ImageType

PyTypeObject ImageType

Definition at line 163 of file python_image.cpp.

◆ PyXmippError

PyObject* PyXmippError

Definition at line 47 of file xmippmodule.cpp.