/* * File: ImageProcessor.h * Author: lpiccoli * * Created on December 1st, 2013, 14:38 PM */ #ifndef _IMAGEPROCESSOR_H #define _IMAGEPROCESSOR_H #include "NDArray.h" #include "TimeUtil.h" #include "NDPluginXTCAVProperties.h" #include #include #include #include class ImageProcessorTest; typedef struct { int x; int y; unsigned long intensity; } Point; typedef struct Pixel { int x; int y; Pixel(int a, int b) { x = a; y = b; }; }; const int MAX_ROI_W = 1024; const int MAX_ROI_H = 1024; const int MAX_ROI_SLICES = 512; typedef enum { ROI_OK = 0, ROI_FAIL_CROP = 1, ROI_FAIL_SAVE = 2, ROI_FAIL_SLICE = 3, ROI_TOO_SMALL_X = 4, ROI_TOO_SMALL_Y = 5, ROI_FAIL_SEGMENT = 6, ROI_INVALID_BACKGROUND = 7, ROI_STATUS_COUNT } ROIErrorStatus; /** Energy Profile (EP) Status */ typedef enum { EP_OK = 0, EP_INVALID_REFERENCE = 1, EP_NO_REFERENCE = 2, EP_STATUS_COUNT } EnergyStatus; typedef std::vector Waveform; typedef std::vector Waveforms; typedef std::vector WaveformDouble; typedef std::vector WaveformsDouble; /** * * @author L.Piccoli */ class ImageProcessor { public: ImageProcessor(); void setProperties(NDPluginXTCAVProperties *properties); bool process(NDArray *pArray, ImageProcessor *refProcessor = NULL, bool isReference = false, NDArray *backgroundArray = NULL); bool processReference(NDArray *backgroundArray = NULL); bool loadReference(std::string fileName); bool saveReference(NDArray *pArray); bool saveReference(std::string fileName); bool saveNDArray(NDArray &src, std::string fileName); bool saveNDArray(NDArray *src, NDArray &dst); bool loadImageFromFile(std::string fileName, NDArray *pArray); bool loadTiffImage(TIFF *tiff, NDArray *pArray); bool subtractBackgroundROI(bool isReference = false, NDArray *backgroundArray = NULL); void getROISize(int &w, int &h); NDArray *getROIArray(); NDArray *getROIArrayFull(); // NDArray *getReferenceArrayFull(); // NDArray *getReferenceArray(); void setROISlices(unsigned short roiSlices); unsigned short getROISlices() { return _roiSlices; }; void setCharge(double charge); void setStrengthParam(double strength); void setEnableWaveforms(int enable); void setExpectedROISize(unsigned int width, unsigned int height); void getROIPoints(Point &topLeft, Point &bottomRight); Waveform &getROIProfile(); Waveform &getROISliceProfile(unsigned short sliceNumber); WaveformDouble &getROISliceEnergyProfile(unsigned short sliceNumber); WaveformDouble &getROIEnergy(); WaveformDouble &getROIEnergyCenter(); WaveformDouble &getROIEnergyCenterDifference(); WaveformDouble &getROIEnergyRMS(); /* Waveform &getRefROIProfile(); Waveform &getRefROISliceProfile(unsigned short sliceNumber); WaveformDouble &getRefROISliceEnergyProfile(unsigned short sliceNumber); WaveformDouble &getRefROIEnergy(); WaveformDouble &getRefROIEnergyCenter(); WaveformDouble &getRefROIEnergyRMS(); */ ROIErrorStatus getROIStatus(); EnergyStatus getEnergyProfileStatus(); WaveformDouble &getEnergyProfile(); double getBunchEnergy(); friend class ImageProcessorTest; friend class ImageUtils; void lockReference(); void unlockReference(); protected: unsigned long sumColumn(NDArray *pArray, unsigned int columnIndex); unsigned long sumRow(NDArray *pArray, unsigned int rowIndex); unsigned long sumColumn2(NDArray *pArray, unsigned int columnIndex); unsigned long sumRow2(NDArray *pArray, unsigned int rowIndex); bool findROIPoints(NDArray *pArray, unsigned int scanInterval, Point &before, Point &after, bool isColumn, unsigned long *averageIntesityFound, bool isReference = false); bool findCoarseROIPoints(NDArray *pArray, unsigned int scanInterval, Point &before, Point &after, bool isColumn, unsigned long *averageIntesityFound); bool scanMax(NDArray *pArray, unsigned int start, unsigned int scanInterval, unsigned int numScans, Point &before, Point &after, bool isColumn, unsigned long *averageIntesityFound); bool findROI(NDArray *pArray, unsigned int scanIntervalX, unsigned int scanIntervalY, ImageProcessor *refProcessor, bool isReference = false, NDArray *backgroundArray = NULL); bool findROI(NDArray *pArray, ImageProcessor *refProcessor, bool isReference = false, NDArray *backgroundArray = NULL); unsigned long getSquareAverage(NDArray *pArray, unsigned int leftX, unsigned int topY, unsigned int size); void allocROIArray(); bool saveROI(NDArray *pArray, bool isReference = false); bool sliceROI(bool isReference = false); bool calculateEnergy(bool isReference = false); bool calculateSliceEnergyRMS(ImageProcessor *refProcessor = NULL, bool isReference = false); bool eval(NDArray *pArray, int x, int y, unsigned short threshold, bool isReference = false, NDArray *backgroundArray = NULL); bool segment(NDArray *pArray, int x, int y, bool isReference = false, NDArray *backgroundArray = NULL); NDPluginXTCAVProperties *_properties; std::vector _imageRow; std::vector _roiRow; std::vector _roiRowFull; unsigned short _roiSlices; Point _topROI; Point _bottomROI; int _roiW; int _roiH; Point _topRef; Point _bottomRef; int _refW; int _refH; int _enableWaveforms; Point _topReferenceROI; Point _bottomReferenceROI; unsigned long _referenceBackgroundAverageIntensity; // NDArray _refArray; // NDArray _refArrayFull; NDArray _roiArray; NDArray _roiArrayFull; /** Signal when to save image as background */ bool _saveBackground; ROIErrorStatus _roiErrorStatus; // ROIErrorStatus _refErrorStatus; EnergyStatus _energyStatus; /** Sum of intensities along the column slices */ Waveform _roiProjection; // Waveform _refProjection; /** Size of each slice */ Waveform _sliceSize; /** Energy calculated for each slice (horz projection) */ WaveformDouble _energy; // WaveformDouble _refEnergy; /** Vector of vector of vertical sums */ Waveforms _roiVerticalProjection; // Waveforms _refVerticalProjection; /** Energy of each vertical slice */ WaveformsDouble _sliceEnergy; // WaveformsDouble _refSliceEnergy; /** TMIT at DMP BPM */ double _charge; /** Fudge factor - to calculate ROI energies */ double _fudge; /** Peak energy for each slice */ WaveformDouble _peakSliceEnergy; /** Peak energy for each slice - for reference image */ // WaveformDouble _refPeakSliceEnergy; /** Difference between the _peakSliceEnergy and the _refPeakSliceEnergy */ WaveformDouble _diffPeakSliceEnergy; /** Energy power profile (_diffPeakSliceEnergy * TMIT) */ WaveformDouble _energyProfile; /** Bunch energy - sum of the energy power profile */ double _bunchEnergy; /** Slice energy RMS */ WaveformDouble _sliceEnergyRMS; /** Slice energy RMS - for reference image */ // WaveformDouble _refSliceEnergyRMS; char _visitMask[1024][1024]; int _roiX1; int _roiX2; int _roiY1; int _roiY2; // int _refX1; // int _refX2; // int _refY1; // int _refY2; int _seedX; int _seedY; bool _stopSegmentation; TimeAverage _findROITime; TimeAverage _saveROITime; TimeAverage _segmentTime; TimeAverage _processTime; TimeAverage _findPointsTime; std::stack _segmentationStack; /** Mutex for protecting reference image and waveforms */ epicsMutex _refMutex; }; #endif