Image

This class defines the image base structure providing methods for: * reading from file (fits, root, png/gif/…) * writing to file (fits, root, …) * filtering map * computing bkg & noise map * computing significance map * searching blobs/sources * TBD

Defined in src/img/Image.h

class Image : public TNamed

Public Functions

Image()

Class constructor: initialize structures.

virtual ~Image()

Class destructor: free allocated memory.

inline void SetName(std::string name)

Set image name.

inline std::string GetName()

Get image name.

inline long int GetNPixels()

Get npixels.

inline const std::vector<float> &GetPixels() const

Return pixel list.

inline long int GetPixelDataSize()

Get pixel vector size.

inline bool HasPixelData()

Has pixel data.

int SetSize(long int Nx, long int Ny, float xlow = 0, float ylow = 0)

Set image size (NB: this cleares pixel vector and allocated space!!!)

inline void SetXmin(float val)

Set image Xmin.

inline void SetYmin(float val)

Set image Ymin.

inline void GetSize(long int &Nx, long int &Ny)

Get size.

inline long int GetNx()

Get size x.

inline long int GetNy()

Get size y.

inline float GetXmin()

Get xmin.

inline float GetXmax()

Get xmax.

inline float GetYmin()

Get ymin.

inline float GetYmax()

Get ymax.

inline void GetRange(float &xmin, float &xmax, float &ymin, float &ymax)

Get range.

inline float GetX(long int binx)

Get x value corresponding to pixel bin x (NB: if no x offset is given it should return the same bin)

inline float GetY(long int biny)

Get y value corresponding to pixel bin y (NB: if no y offset is given it should return the same bin)

inline long int GetBin(long int binx, long int biny)

Get global bin corresponding to x & y bins.

inline long int GetBinX(long int gbin)

Get bin x corresponding to global bin.

inline long int GetBinY(long int gbin)

Get bin y corresponding to global bin.

inline long int HasSameBinning(Image *img, bool checkRange = false)

Check if this and a given image have the same binnings (and also range if required)

inline float GetPixelValue(long int gbin)

Get pixel value.

inline float GetPixelValue(long int ix, long int iy)

Get pixel value.

inline int SetPixelValue(long int gbin, double w)

Set pixel value.

inline int SetPixelValue(long int ix, long int iy, double w)

Set pixel value.

inline float GetBinContent(long int ix, long int iy)

Get bin content (equivalent to GetPixelValue)

inline float GetBinContent(long int gbin)

Get bin content (equivalent to GetPixelValue)

inline int SetBinContent(long int ix, long int iy, double w)

Set bin content (equivalent to SetPixelValue)

inline int SetBinContent(long int gbin, double w)

Set bin content (equivalent to SetPixelValue)

inline Image *GetCloned(std::string name, bool copyMetaData = true, bool resetStats = true)

Clone image (return a new image)

inline void Reset()

Reset image (zero all pixels and clear stats, size & meta-data preserved)

inline float GetMinimum()

Get pixel minimum.

inline float GetMaximum()

Get pixel maximum.

inline bool HasBin(long int gbin)

Check if given bin id is within image range.

inline bool HasBin(long int binIdX, long int binIdY)

Check if given bin with image coordinates (ix,iy) is within image range.

inline bool HasBin(double x, double y)

Check if given bin with physical coordinates (x,y) is within image range.

inline bool HasBinX(long int ix)

Check if given bin with image coordinate ix is within image range.

inline bool HasBinX(double x)

Check if given physical coordinate x is within image range.

inline bool HasBinY(long int iy)

Check if given bin with image coordinate iy is within image range.

inline bool HasBinY(double y)

Check if given physical coordinate y is within image range.

inline long int FindBin(double x, double y)

Find bin corresponding to physical coordinates (x,y)

inline bool IsBinContentInRange(long int binIdX, long int binIdY, double minThr, double maxThr)

Check if bin content is within given value range.

inline bool IsEdgeBin(long int binIdX, long int binIdY)

Check if bin is at edge.

inline bool IsEdgeBin(long int gbin)

Check if bin is at edge.

int CheckFillPixel(long int gbin, double w)

Check filled pixel.

int FillPixel(long int ix, long int iy, double w, bool useNegativePixInStats = true)

Fill pixels (to be used to compute stats at fill time).

NB: use only in single-thread otherwise computed moments are not correct (+ race conditions)!!!

int FillFromMat(cv::Mat&, bool useNegativePixInStats = true)

Fill pixel (multithreaded version).

This updated the moments per thread. To get the cumulative moments use the StatsUtils Fill image from OpenCV mat object

int FillFromTMatrix(TMatrixD&, bool useNegativePixInStats = true)

Fill image from ROOT TMatrixD object.

int FillFromTH2(const TH2&, bool useNegativePixInStats = true)

Fill image from ROOT TH2 object.

int ReadFITS(std::string filename, int hdu_id = 1, int ix_min = -1, int ix_max = -1, int iy_min = -1, int iy_max = -1)

Read image from FITS file.

int WriteFITS(std::string outfilename)

Write image to FITS file.

Image *GetTile(long int ix_min, long int ix_max, long int iy_min, long int iy_max, std::string imgname = "")

Get image subregion or tile.

int GetTilePixels(std::vector<float> &pixels, long int ix_min, long int ix_max, long int iy_min, long int iy_max, bool useRange = false, double minThr = -std::numeric_limits<double>::infinity(), double maxThr = std::numeric_limits<double>::infinity(), std::vector<float> maskedValues = {}, bool requireFinitePixValues = false)

Get tile pixels.

It excludes NAN/inf and masked pixels (=0 by default) and optionally also pixels outside range

Image *GetSourceCutout(Source *source, int boxThickness = 1)

Get image cutout around a source.

int ReadFile(std::string filename, bool invert = false)

Read image from an image file.

inline void CopyMetaData(ImgMetaData *data)

Set image metadata information.

inline ImgMetaData *GetMetaData()

Get image metadata information.

inline bool HasMetaData()

Has metadata information.

int ComputeMoments(bool useRange = false, double minThr = -std::numeric_limits<double>::infinity(), double maxThr = std::numeric_limits<double>::infinity(), std::vector<float> maskedValues = {0})

Update moments (multithreaded version).

It excludes NAN/inf and masked (e.g. =0 by default) pixels and optionally also pixels outside given range

inline ImgStats *GetPixelStats()

Get stats information.

inline bool HasStats()

Check if stats has been computed.

int ComputeStats(bool computeRobustStats, bool forceRecomputing = false, bool useRange = false, double minThr = -std::numeric_limits<double>::infinity(), double maxThr = std::numeric_limits<double>::infinity(), bool useParallelVersion = false, std::vector<float> maskedValues = {0})

Compute stats information.

It excludes NAN/inf and masked (e.g. =0 by default) pixels and optionally also pixels outside given range

inline void PrintStats()

Print stats information.

inline void LogStats(std::string level = "INFO")

Print stats information.

inline void SetMoments(Caesar::StatMoments<double> moments)

Set stat moments.

inline Caesar::StatMoments<double> GetMoments()

Get stat moments.

int GetTileMeanStats(float &mean, float &stddev, long int &npix, long int ix_min, long int ix_max, long int iy_min, long int iy_max, bool useRange = false, double minThr = -std::numeric_limits<double>::infinity(), double maxThr = std::numeric_limits<double>::infinity(), std::vector<float> maskedValues = {})

Get tile mean stats.

int GetTileMedianStats(float &median, float &mad, long int &npix, long int ix_min, long int ix_max, long int iy_min, long int iy_max, bool useRange = false, double minThr = -std::numeric_limits<double>::infinity(), double maxThr = std::numeric_limits<double>::infinity(), std::vector<float> maskedValues = {})

Get tile median stats.

int GetTileClippedStats(Caesar::ClippedStats<float> &clipped_stats, long int &npix, double clipsigma, long int ix_min, long int ix_max, long int iy_min, long int iy_max, bool useRange = false, double minThr = -std::numeric_limits<double>::infinity(), double maxThr = std::numeric_limits<double>::infinity(), std::vector<float> maskedValues = {})

Get tile median stats.

int GetTileBiWeightStats(float &bwLocation, float &bwScale, long int &npix, double C, long int ix_min, long int ix_max, long int iy_min, long int iy_max, bool useRange = false, double minThr = -std::numeric_limits<double>::infinity(), double maxThr = std::numeric_limits<double>::infinity(), std::vector<float> maskedValues = {})

Get tile biweight stats.

ImgBkgData *ComputeBkg(int estimator = eMedianBkg, bool computeLocalBkg = true, int boxSizeX = 100, int boxSizeY = 100, double gridStepSizeX = 10, double gridStepSizeY = 10, bool use2ndPass = true, bool skipOutliers = false, double seedThr = 5, double mergeThr = 2.6, int minPixels = 10, bool useRange = false, double minThr = -std::numeric_limits<double>::infinity(), double maxThr = std::numeric_limits<double>::infinity())

Compute image bkg.

ImgBkgData *ComputeBkg(ImgBkgPars pars)

Compute image bkg (version with bkg parameter class argument)

Image *GetSignificanceMap(ImgBkgData *bkgData, bool useLocalBkg = false)

Compute significance map.

TH1D *GetPixelHisto(int nbins = 100, bool normalize = false)

Compute pixel histo.

double FindOtsuThreshold(int nbins = 100)

Find Otsu threshold.

double FindOtsuThreshold(TH1D *pixelHisto)

Find Otsu threshold.

double FindValleyThreshold(int nbins = 100, bool smooth = true)

Find valley threshold.

double FindMedianThreshold(double thrFactor)

Find median global threshold.

double FindOptimalGlobalThreshold(double thrFactor, int nbins = 100, bool smooth = true)

Find optimal global threshold.

Image *GetBinarizedImage(double threshold, double fgValue = 1, bool isLowerThreshold = false)

Get binarized image.

int ApplyThreshold(double thr_min, double thr_max = 1.e+99, double maskedValue = 0)

Apply threshold to image and replace pixel outside thr range with masked value.

double FindCumulativeSumThr(double threshold, bool useRange = false, double minThr = -std::numeric_limits<double>::infinity(), double maxThr = std::numeric_limits<double>::infinity())

Find image threshold at which the cumulative pixel sum (scaled to image sum) is lower than desired threshold.

int FindCompactSource(std::vector<Source*>&, double thr, int minPixels = 10)

Find compact sources by thresholding.

int FindCompactSource(std::vector<Source*> &sources, Image *floodImg = 0, ImgBkgData *bkgData = 0, double seedThr = 5, double mergeThr = 2.6, int minPixels = 10, bool findNestedSources = false, Image *blobMask = 0, double minNestedMotherDist = 2, double maxMatchingPixFraction = 0.5, long int nPixThrToSearchNested = 0)

Find compact sources.

int FindNestedSource(std::vector<Source*> &sources, Image *blobMask, ImgBkgData *bkgData = 0, int minPixels = 5, double minNestedMotherDist = 2, double maxMatchingPixFraction = 0.5, long int nPixThrToSearchNested = 0)

Find nested sources.

int FindBlendedSources(std::vector<Source*> &deblendedSources, std::vector<ImgPeak> &deblendedPeaks, double sigmaMin, double sigmaMax, double sigmaStep, int minBlobSize = 5, double thrFactor = 0, int kernelFactor = 1)

Find blended sources.

int FindExtendedSource_CV(std::vector<Source*>&, Image *initSegmImg = 0, ImgBkgData *bkgData = 0, int minPixels = 10, bool findNegativeExcess = false, double dt = 0.1, double h = 1, double lambda1 = 1.0, double lambda2 = 2.0, double mu = 0.5, double nu = 0, double p = 1, int niters = 1000, double tol = 1.e-2, int niters_inner = 1, int niters_reinit = 10)

Find extended sources with ChanVese method.

int AddSimPointSources(long int nGenSources, float Smin = 1.e-4, float Smax = 1., float Sslope = 1.6, int marginX = 0, int marginY = 0, std::string genFileName = "gensources.dat")

Find extended sources with Hierarchical Clustering method.

Image *GetMask(Image *mask, bool isBinary = false)

Get masked image.

Image *GetSourceMask(std::vector<Source*> const &sources, bool isBinary = false, bool invert = false, bool searchSourceCoords = false)

Get source masked image.

int MaskSources(std::vector<Source*> const &sources, float maskValue = 0.)

Mask sources.

int GetBkgInfoAroundSource(BkgSampleData &bkgSampleData, Source *source, int boxThickness = 20, int bkgEstimator = eMedianBkg, Image *mask = 0, bool useParallelVersion = false, std::vector<float> maskedValues = {})

Measure bkg in a box around given source bounding box.

Box thickness= bkgbox_max-sourcebox_max

Source *GetMorphTransformedSource(Source *source, int morphTransform = eMORPH_DILATION, int kernSize = 3, Image *mask = 0)

Returns a morph trasnformed source (e.g.

enlarged or shrinked, obtained by dilating/eroding given source with a square kernel, eventually accounting masked pixels)

Image *GetSourceFitModelImage(const std::vector<Source*> &sources, bool useNested = true, double nsigmaTrunc = 5)

Create image with bin contents equal to fitted sources.

int FillFromSourceFitModel(Source *source, bool useNested = true, double nsigmaTrunc = 5)

Fill image bins from source fit model.

int SubtractFittedSources(const std::vector<Source*> &sources, bool subtractNested = true, bool recomputeStats = true, double nsigmaTrunc = 5)

Subtract fitted sources from image.

int SubtractFittedSource(Source *source, bool subtractNested = true, bool recomputeStats = true, double nsigmaTrunc = 5)

Subtract fitted source from image.

int FindPeaks(std::vector<Caesar::ImgPeak> &peakPoints, std::vector<int> kernelSizes = {3, 5, 7}, int peakShiftTolerance = 1, bool skipBorders = true, int multiplicityThr = -1)

Find image peaks.

TGraph *ComputePeakGraph(std::vector<int> kernelSizes = {3, 5, 7}, int peakShiftTolerance = 1, bool skipBorders = true, int multiplicityThr = -1)

Find graph with image peaks.

Image *GetSourceResidual(std::vector<Source*> const &sources, int KernSize = 5, int dilateModel = eDilateWithBkg, int dilateSourceType = -1, bool skipToNested = false, ImgBkgData *bkgData = 0, bool useLocalBkg = false, bool randomize = false, double zThr = 5, double zBrightThr = 20, int psSubtractionMethod = ePS_DILATION, Image *mask = 0, int bkgBoxThickness = 20)

Returns a residual image obtained by dilating given sources with a random background.

int Scale(double c)

Scale image by a factor ‘c’.

int Add(Image *img, double c = 1, bool computeStats = false)

Add an image to this (this= this + img*c)

Image *GetNormalizedImage(std::string normScale = "LINEAR", int normmin = 1, int normmax = 256, bool skipEmptyBins = true)

Get normalized image.

Image *GetLaplacianImage(bool invert = false)

Get image laplacian.

Image *GetGuidedFilterImage(int radius = 12, double eps = 0.04)

Get guided filter image.

Image *GetSmoothedImage(int size_x = 3, int size_y = 3, double sigma_x = 1, double sigma_y = 1)

Smooth image.

Image *GetSaliencyMap(int reso = 20, double regFactor = 1, int minRegionSize = 10, double knnFactor = 1, bool useRobust = false, double expFalloffPar = 100, double distanceRegPar = 1)

Get single-reso saliency map.

Image *GetMultiResoSaliencyMap(int resoMin = 20, int resoMax = 60, int resoStep = 10, double beta = 1, int minRegionSize = 10, double knnFactor = 0.2, bool useRobustPars = false, double expFalloffPar = 100, double distanceRegPar = 1, double salientMultiplicityThrFactor = 0.7, bool addBkgMap = true, bool addNoiseMap = true, ImgBkgData *bkgData = 0, double saliencyThrFactor = 2, double imgThrFactor = 1, bool useOptimalThr = false)

Get multi-reso saliency map.

std::vector<Image*> GetWaveletDecomposition(int nScales)

Get image wavelength decomposition.

Image *GetGradientImage(bool invert = false)

Get image gradient.

Image *GetKirschImage()

Get kirsch image.

Image *GetLoGImage(bool invert = false)

Get laplacian of gaussian image.

Image *GetNormLoGImage(int size = 3, double scale = 1, bool invert = false)

Get scale-normalized laplacian of gaussian image.

Image *GetBeamConvolvedImage(double bmaj, double bmin, double bpa, int nsigmas = 5, double scale = 1)

Get image convolved with an elliptical gaussian beam.

Image *GetMorphDilatedImage(int kernSize = 3, int niters = 1, bool skipZeroPixels = true)

Get dilated filtered image.

Image *GetMorphErodedImage(int kernSize = 3, int niters = 1, bool skipZeroPixels = true)

Get eroded filtered image.

Image *GetMorphTopHatImage(int kernSize = 3, int niters = 1, bool skipZeroPixels = true)

Get tophat filtered image.

Image *GetMorphClosingImage(int kernSize = 3, int niters = 1, bool skipZeroPixels = true)

Get morph closing filtered image.

Image *GetMorphOpeningImage(int kernSize = 3, int niters = 1, bool skipZeroPixels = true)

Get morph opening filtered image.

Image *GetMorphGradientImage(int kernSize = 3, int niters = 1, bool skipZeroPixels = true)

Get morph gradient filtered image.

Image *GetMorphRecoImage(double baseline, int kernSize = 3, double tol = 1.e-6)

Get morph reco filtered image.

Image *GetHDomeImage(double baseline, int kernSize = 3)

Get H-dome image.

TH2D *GetHisto2D(std::string histoname = "")

Export to ROOT TH2.

TH2D *GetWCSHisto2D(std::string histoname = "", WCS *wcs = 0, bool useImageCoord = true)

Export to ROOT TH2 converted to WCS (TO BE DEPRECATED)

Export to ROOT TH2 converted to WCS

cv::Mat GetOpenCVMat(std::string encoding = "64")

Convert image to OpenCV mat float image.

TMatrixD *GetMatrix()

Get ROOT matrix from image.

std::string GetPixelNumpyArrayStr()

Get numpy pixel array string.

int Draw(int palette = Caesar::eRAINBOW, bool drawFull = false, bool useCurrentCanvas = false, std::string units = "")

Draw image (plain image, no WCS, no sources)

int Draw(std::vector<Source*> const &sources, int palette = Caesar::eRAINBOW, bool drawFull = false, bool useCurrentCanvas = false, std::string units = "")

Draw image and sources (no WCS axis)

int Plot(std::vector<Source*> const&, bool useCurrentCanvas = true, bool drawFull = false, int paletteStyle = Caesar::eRAINBOW, bool drawColorPalette = true, bool putWCAxis = false, int coordSystem = -1, std::string units = "", bool save = false, std::string outFileName = "plot.png")

Draw image.

class ImgPeak : public TObject

Public Functions

inline ImgPeak()

Constructor.

inline ImgPeak(double _x, double _y, double _S, long int _ix, long int _iy)

Parametric Constructor.

inline virtual ~ImgPeak()

Destructor.

class ImgBkgPars : public TObject

Public Functions

inline ImgBkgPars()

Constructor.

inline virtual ~ImgBkgPars()

Destructor.

inline void SetLocalBkg(bool choice)

Turn on/off local bkg.

inline void SetDataRange(double minVal = -std::numeric_limits<double>::infinity(), double maxVal = std::numeric_limits<double>::infinity())

Set data range thresholds (min/max)

class ImgRange : public TObject

Public Functions

inline ImgRange()

Constructor.

inline ImgRange(long int _nx, long int _ny, float _smin, float _smax, float _xlow, float _ylow)

Parametric constructor.

inline ImgRange(long int _nx, long int _ny, float _smin, float _smax)

Parametric constructor.

inline ImgRange(float _smin, float _smax, float _xlow, float _xup, float _ylow, float _yup)

Parametric constructor.

inline virtual ~ImgRange()

Destructor.

inline void GetRange(float &xmin, float &xmax, float &ymin, float &ymax)

Get range.

inline void GetSRange(float &_smin, float &_smax)

Get signal range.